// Lexical text format bitflags. const IS_BOLD = 1; const IS_ITALIC = 1 << 1; const IS_STRIKETHROUGH = 1 << 2; const IS_CODE = 1 << 4; function isNodeArray(value) { return Array.isArray(value); } function wrapText(text, format) { if (!text) { return text; } // Don't wrap pure whitespace — would produce "** **". if (!text.trim()) { return text; } let out = text; if (format & IS_CODE) { out = `\`${out}\``; } if (format & IS_BOLD) { out = `**${out}**`; } if (format & IS_ITALIC) { out = `_${out}_`; } if (format & IS_STRIKETHROUGH) { out = `~~${out}~~`; } return out; } /** Render inline children (text, links, line breaks) to a single string. */ function inline(nodes) { if (!isNodeArray(nodes)) { return ''; } let out = ''; for (const node of nodes){ if (!node || typeof node !== 'object') { continue; } const type = node.type; if (type === 'text') { const text = typeof node.text === 'string' ? node.text : ''; const format = typeof node.format === 'number' ? node.format : 0; out += wrapText(text, format); } else if (type === 'linebreak') { out += '\n'; } else if (type === 'link') { const fields = node.fields ?? {}; const url = typeof fields.url === 'string' ? fields.url : typeof fields.doc?.value === 'string' ? fields.doc.value : ''; const label = inline(node.children); out += url ? `[${label}](${url})` : label; } else if (node.children) { out += inline(node.children); } else if (typeof node.text === 'string') { out += node.text; } } return out; } /** Render a list (ordered/unordered), handling nesting via indentation. */ function renderList(node, depth) { const ordered = node.listType === 'number'; const items = isNodeArray(node.children) ? node.children : []; const indent = ' '.repeat(depth); const lines = []; let i = 1; for (const item of items){ if (!item || typeof item !== 'object') { continue; } const marker = ordered ? `${i}.` : '-'; // A listitem may contain inline content and/or a nested list. const kids = isNodeArray(item.children) ? item.children : []; const nested = kids.filter((k)=>k && typeof k === 'object' && k.type === 'list'); const inlineKids = kids.filter((k)=>!(k && typeof k === 'object' && k.type === 'list')); const text = inline(inlineKids); if (text.trim()) { lines.push(`${indent}${marker} ${text}`); i++; } for (const n of nested){ lines.push(renderList(n, depth + 1)); } } return lines.join('\n'); } /** Render a block-level node to Markdown. */ function block(node) { const type = node.type; switch(type){ case 'heading': { const level = typeof node.tag === 'string' ? Number(node.tag.replace('h', '')) || 2 : 2; return `${'#'.repeat(Math.min(level, 6))} ${inline(node.children)}`; } case 'horizontalrule': return '---'; case 'list': return renderList(node, 0); case 'paragraph': { const text = inline(node.children); return text.trim() ? text : ''; } case 'quote': return inline(node.children).split('\n').map((l)=>`> ${l}`).join('\n'); case 'upload': { const value = node.value ?? {}; const url = typeof value.url === 'string' ? value.url : ''; const alt = typeof value.alt === 'string' ? value.alt : ''; return url ? `![${alt}](${url})` : ''; } default: // Unknown block-level node: emit its inline content if any. return node.children ? inline(node.children) : ''; } } export function lexicalToMarkdown(data) { const root = data?.root; if (!root || !isNodeArray(root.children)) { return ''; } const blocks = []; for (const node of root.children){ if (!node || typeof node !== 'object') { continue; } const md = block(node); if (md.trim()) { blocks.push(md); } } // Blank line between block-level elements — standard Markdown separation. return blocks.join('\n\n').trim(); } //# sourceMappingURL=lexicalToMarkdown.js.map