File size: 11,940 Bytes
f462b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// ══════════════════════════════════════════════════════════════════════════════
// Chahua Markdown Presenter - Layout Schema & Tree Builder
// Converts parsed markdown slides into structured presentation layout blocks
// ══════════════════════════════════════════════════════════════════════════════
// Company: Chahua Development Co., Ltd.
// Version: 1.0.0
// License: MIT
// ══════════════════════════════════════════════════════════════════════════════

const LayoutNodeKind = {
    SLIDE: 'slide',
    SECTION: 'section',
    HEADING: 'heading',
    TEXT: 'text',
    LIST: 'list',
    CODE: 'code',
    TABLE: 'table',
    CALLOUT: 'callout',
    IMAGE: 'image',
    DIVIDER: 'divider',
    HTML: 'html',
    LINK: 'link'
};

const ListKind = {
    UNORDERED: 'unordered',
    ORDERED: 'ordered',
    TASK: 'task'
};

let layoutIdCounter = 0;
function nextLayoutId(prefix) {
    layoutIdCounter += 1;
    return `${prefix}-${layoutIdCounter}`;
}

const HEADING_LINE_WEIGHTS = {
    1: 3.2,
    2: 2.6,
    3: 2,
    4: 1.6,
    5: 1.4,
    6: 1.2
};

function estimateTextLines(text) {
    if (!text) return 1;
    const words = text.trim().split(/\s+/).length;
    return Math.max(1, Math.ceil(words / 12));
}

function estimateListLines(items) {
    if (!items || items.length === 0) return 0;
    return items.reduce((sum, item) => sum + Math.max(1.1, estimateTextLines(item.text) * 0.85), 0);
}

function estimateBlockLines(block) {
    switch (block.kind) {
        case LayoutNodeKind.HEADING:
            return HEADING_LINE_WEIGHTS[block.level] || 2.4;
        case LayoutNodeKind.TEXT:
            return estimateTextLines(block.text);
        case LayoutNodeKind.LIST:
            return estimateListLines(block.items) + 0.5;
        case LayoutNodeKind.CODE: {
            const lines = block.content.split('\n').length;
            return Math.min(12, lines + 1.5);
        }
        case LayoutNodeKind.TABLE: {
            const rows = block.rows ? block.rows.length : 0;
            return 2.5 + rows * 0.8;
        }
        case LayoutNodeKind.CALLOUT:
            return Math.max(2, estimateTextLines(block.text) * 1.2);
        case LayoutNodeKind.IMAGE:
            return 4.5;
        case LayoutNodeKind.DIVIDER:
            return 0.5;
        case LayoutNodeKind.HTML:
            return 3;
        case LayoutNodeKind.LINK:
            return Math.max(1, estimateTextLines(block.text));
        default:
            return 1;
    }
}

function createHeadingBlock(level, text, html = null) {
    const normalizedLevel = Math.min(Math.max(level || 1, 1), 6);
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.HEADING,
        level: normalizedLevel,
        text: text || '',
        html: html || null
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createSectionFromHeading(headingItem, fallbackTitle, fallbackLevel = 2) {
    const level = headingItem ? parseInt(headingItem.type.replace('h', ''), 10) || fallbackLevel : fallbackLevel;
    const title = headingItem ? headingItem.content : fallbackTitle;

    const section = {
        id: nextLayoutId('section'),
        kind: LayoutNodeKind.SECTION,
        level,
        title: title || fallbackTitle || '',
        heading: headingItem || null,
        blocks: [],
        metrics: {
            estimatedLines: 0
        }
    };

    const headingText = title || fallbackTitle || '';
    if (headingText) {
        const headingBlock = createHeadingBlock(level, headingText, headingItem?.html || null);
        section.blocks.push(headingBlock);
        section.metrics.estimatedLines += headingBlock.metrics.estimatedLines;
    }

    return section;
}

function createTextBlock(text, html = null) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.TEXT,
        text,
        html: html || null
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createListBlock(listType) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.LIST,
        listType,
        items: []
    };
    block.metrics = { estimatedLines: 0 };
    return block;
}

function createCodeBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.CODE,
        language: item.language || 'text',
        content: item.content
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createTableBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.TABLE,
        rows: item.rows || []
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createCalloutBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.CALLOUT,
        text: item.content,
        level: item.level || 1,
        html: item.html || null
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createImageBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.IMAGE,
        src: item.src,
        alt: item.alt || '',
        title: item.title || ''
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createDividerBlock() {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.DIVIDER
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createHtmlBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.HTML,
        html: item.html || item.content
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function createLinkBlock(item) {
    const block = {
        id: nextLayoutId('block'),
        kind: LayoutNodeKind.LINK,
        text: item.text || item.content || '',
        href: item.url || item.href || '',
        title: item.title || '',
        html: item.html || null
    };
    block.metrics = { estimatedLines: estimateBlockLines(block) };
    return block;
}

function appendBlockToSection(section, block) {
    section.blocks.push(block);
    section.metrics.estimatedLines += block.metrics.estimatedLines;
}

function appendListItem(block, item) {
    block.items.push({
        id: nextLayoutId('listitem'),
        text: item.content,
        marker: item.marker || null,
        number: item.number || null,
        checked: !!item.checked,
        html: item.html || null
    });
    block.metrics.estimatedLines = estimateBlockLines(block);
}

function resolveListType(itemType) {
    if (itemType === 'numbered-item') return ListKind.ORDERED;
    if (itemType === 'task-list') return ListKind.TASK;
    return ListKind.UNORDERED;
}

function addItemToSection(section, item) {
    switch (item.type) {
        case 'text': {
            const lastBlock = section.blocks[section.blocks.length - 1];
            if (lastBlock && lastBlock.kind === LayoutNodeKind.TEXT) {
                const previousLines = lastBlock.metrics.estimatedLines;
                lastBlock.text = `${lastBlock.text}\n\n${item.content}`;
                if (item.html) {
                    const joiner = lastBlock.html ? '<br><br>' : '';
                    lastBlock.html = lastBlock.html ? `${lastBlock.html}${joiner}${item.html}` : item.html;
                }
                lastBlock.metrics.estimatedLines = estimateBlockLines(lastBlock);
                section.metrics.estimatedLines += (lastBlock.metrics.estimatedLines - previousLines);
            } else {
                appendBlockToSection(section, createTextBlock(item.content, item.html || null));
            }
            break;
        }
        case 'list-item':
        case 'numbered-item':
        case 'task-list': {
            const listType = resolveListType(item.type);
            let listBlock = section.blocks[section.blocks.length - 1];
            if (!listBlock || listBlock.kind !== LayoutNodeKind.LIST || listBlock.listType !== listType) {
                listBlock = createListBlock(listType);
                appendBlockToSection(section, listBlock);
            }
            const previousLines = listBlock.metrics.estimatedLines;
            appendListItem(listBlock, item);
            section.metrics.estimatedLines += (listBlock.metrics.estimatedLines - previousLines);
            break;
        }
        case 'code':
            appendBlockToSection(section, createCodeBlock(item));
            break;
        case 'table':
            appendBlockToSection(section, createTableBlock(item));
            break;
        case 'blockquote':
            appendBlockToSection(section, createCalloutBlock(item));
            break;
        case 'image':
            appendBlockToSection(section, createImageBlock(item));
            break;
        case 'hr':
            appendBlockToSection(section, createDividerBlock());
            break;
        case 'html':
            appendBlockToSection(section, createHtmlBlock(item));
            break;
        case 'link':
            appendBlockToSection(section, createLinkBlock(item));
            break;
        default:
            appendBlockToSection(section, createTextBlock(item.content || '', item.html || null));
            break;
    }
}

export function buildLayoutTreeForSlide(slide) {
    const root = {
        id: nextLayoutId('slide'),
        kind: LayoutNodeKind.SLIDE,
        title: slide.title || '',
        subtitle: slide.subtitle || '',
        sections: [],
        metrics: {
            estimatedLines: 0
        }
    };

    let currentSection = null;

    const ensureSection = (headingItem = null) => {
        if (headingItem) {
            currentSection = createSectionFromHeading(headingItem, slide.title);
            root.sections.push(currentSection);
        } else if (!currentSection) {
            currentSection = createSectionFromHeading(null, slide.title || '');
            root.sections.push(currentSection);
        }
        return currentSection;
    };

    const contentItems = Array.isArray(slide.content) ? slide.content : [];
    for (const item of contentItems) {
        if (item.type && /^h[1-6]$/.test(item.type)) {
            ensureSection(item);
            continue;
        }

        ensureSection();
        addItemToSection(currentSection, item);
    }

    if (root.sections.length === 0) {
        currentSection = createSectionFromHeading(null, slide.title || 'Overview');
        root.sections.push(currentSection);
        if (contentItems.length === 0 && slide.subtitle) {
            appendBlockToSection(currentSection, createTextBlock(slide.subtitle));
        }
    }

    root.metrics.estimatedLines = root.sections.reduce((sum, section) => {
        return sum + (section.metrics?.estimatedLines || 0);
    }, 0);

    return root;
}

export { 
    LayoutNodeKind, 
    ListKind, 
    estimateBlockLines 
};