// ══════════════════════════════════════════════════════════════════════════════ // 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 ? '

' : ''; 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 };