|
| 1 | +// BoxNote JSON → Markdown converter |
| 2 | + |
| 3 | +interface BoxNode { |
| 4 | + type: string; |
| 5 | + attrs?: Record<string, unknown>; |
| 6 | + content?: BoxNode[]; |
| 7 | + text?: string; |
| 8 | + marks?: Array<{ type: string; attrs?: Record<string, unknown> }>; |
| 9 | +} |
| 10 | + |
| 11 | +function renderMarks(text: string, marks?: BoxNode['marks']): string { |
| 12 | + if (!marks) return text; |
| 13 | + let r = text; |
| 14 | + for (const m of marks) { |
| 15 | + if (m.type === 'strong') r = `**${r}**`; |
| 16 | + else if (m.type === 'strikethrough') r = `~~${r}~~`; |
| 17 | + else if (m.type === 'code') r = `\`${r}\``; |
| 18 | + else if (m.type === 'link') r = `[${r}](${m.attrs?.href ?? ''})`; |
| 19 | + } |
| 20 | + return r; |
| 21 | +} |
| 22 | + |
| 23 | +function renderInline(nodes?: BoxNode[]): string { |
| 24 | + if (!nodes) return ''; |
| 25 | + return nodes |
| 26 | + .map((n) => { |
| 27 | + if (n.type === 'text') return renderMarks(n.text ?? '', n.marks); |
| 28 | + if (n.type === 'hard_break') return '\n'; |
| 29 | + if (n.type === 'image') |
| 30 | + return ``; |
| 31 | + return ''; |
| 32 | + }) |
| 33 | + .join(''); |
| 34 | +} |
| 35 | + |
| 36 | +function renderTable(node: BoxNode): string { |
| 37 | + const rows = (node.content ?? []).filter((r) => r.type === 'table_row'); |
| 38 | + if (rows.length === 0) return ''; |
| 39 | + const matrix = rows.map((row) => |
| 40 | + (row.content ?? []).map((cell) => { |
| 41 | + const text = (cell.content ?? []) |
| 42 | + .map((c) => renderInline(c.content)) |
| 43 | + .join(' ') |
| 44 | + .replace(/\n/g, ' ') |
| 45 | + .trim(); |
| 46 | + return text; |
| 47 | + }) |
| 48 | + ); |
| 49 | + const cols = Math.max(...matrix.map((r) => r.length)); |
| 50 | + const pad = (arr: string[]) => { |
| 51 | + while (arr.length < cols) arr.push(''); |
| 52 | + return arr; |
| 53 | + }; |
| 54 | + const lines: string[] = []; |
| 55 | + lines.push(`| ${pad(matrix[0]).join(' | ')} |`); |
| 56 | + lines.push(`| ${Array(cols).fill('---').join(' | ')} |`); |
| 57 | + for (let i = 1; i < matrix.length; i++) { |
| 58 | + lines.push(`| ${pad(matrix[i]).join(' | ')} |`); |
| 59 | + } |
| 60 | + return lines.join('\n') + '\n'; |
| 61 | +} |
| 62 | + |
| 63 | +function renderBlock(node: BoxNode, indent = 0): string { |
| 64 | + const pfx = ' '.repeat(indent); |
| 65 | + switch (node.type) { |
| 66 | + case 'doc': |
| 67 | + return (node.content ?? []).map((c) => renderBlock(c)).join('\n'); |
| 68 | + case 'heading': { |
| 69 | + const lvl = (node.attrs?.level as number) ?? 1; |
| 70 | + const t = renderInline(node.content); |
| 71 | + return t ? `${'#'.repeat(lvl)} ${t}\n` : ''; |
| 72 | + } |
| 73 | + case 'paragraph': { |
| 74 | + const t = renderInline(node.content); |
| 75 | + return `${pfx}${t}\n`; |
| 76 | + } |
| 77 | + case 'bullet_list': |
| 78 | + case 'ordered_list': |
| 79 | + return (node.content ?? []).map((c) => renderBlock(c, indent)).join(''); |
| 80 | + case 'list_item': { |
| 81 | + const parts: string[] = []; |
| 82 | + for (const child of node.content ?? []) { |
| 83 | + if (child.type === 'paragraph') |
| 84 | + parts.push(`${pfx}- ${renderInline(child.content).trim()}`); |
| 85 | + else if ( |
| 86 | + child.type === 'bullet_list' || |
| 87 | + child.type === 'ordered_list' |
| 88 | + ) |
| 89 | + parts.push(renderBlock(child, indent + 1)); |
| 90 | + else parts.push(renderBlock(child, indent + 1)); |
| 91 | + } |
| 92 | + return parts.join('\n') + '\n'; |
| 93 | + } |
| 94 | + case 'table': |
| 95 | + return renderTable(node) + '\n'; |
| 96 | + case 'code_block': { |
| 97 | + const lang = (node.attrs?.language as string) ?? ''; |
| 98 | + return `\`\`\`${lang}\n${renderInline(node.content)}\n\`\`\`\n`; |
| 99 | + } |
| 100 | + case 'blockquote': { |
| 101 | + const inner = (node.content ?? []).map((c) => renderBlock(c)).join(''); |
| 102 | + return ( |
| 103 | + inner |
| 104 | + .split('\n') |
| 105 | + .map((l) => (l ? `> ${l}` : '>')) |
| 106 | + .join('\n') + '\n' |
| 107 | + ); |
| 108 | + } |
| 109 | + case 'horizontal_rule': |
| 110 | + return '---\n'; |
| 111 | + default: |
| 112 | + return (node.content ?? []).map((c) => renderBlock(c, indent)).join(''); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export function convertBoxNoteToMarkdown(json: string): string { |
| 117 | + const parsed = JSON.parse(json) as { doc?: BoxNode } & BoxNode; |
| 118 | + const doc = parsed.doc ?? parsed; |
| 119 | + const raw = renderBlock(doc); |
| 120 | + // collapse 3+ consecutive blank lines into 2 |
| 121 | + return raw.replace(/\n{3,}/g, '\n\n').trim() + '\n'; |
| 122 | +} |
0 commit comments