// fromMarkdown.mjs — a ProseMirror document from Mapa Markdown, built from // markup.mjs's blocks and spans (spec §6.3). Each Mapa block becomes one // textblock (or a rule); list items nest exactly as render.go's // renderList nests them; and the document always ends in a paragraph, // the place the editor keeps to type in (§6.2). // // Origins and the side table (§6.4) are fidelity.mjs's: options.origin // gives the origin for the block at an index of blocks, and it is set on // that block's textblock (or rule) as it is made. options.trailing false // leaves off the trailing paragraph, for a pasted slice. import { Mark } from 'prosemirror-model' import { parse, plain, spans, words, task, itemSyntax, isDate, isFenceLine, fenceOf, closesFence, Paragraph, Heading, Item, Code, Quote, Rule, Columns, Column, ColumnEnd, InCode, WikiLink, WebLink, Embed, Image } from './markup.mjs' export function fromMarkdown (schema, body, options) { return fromBlocks(schema, parse(body), options) } export function fromBlocks (schema, blocks, options = {}) { const origin = options.origin ?? (() => null) const content = contentFrom(schema, blocks, 0, blocks.length, origin) const last = content[content.length - 1] if (options.trailing !== false && (!last || last.type !== schema.nodes.paragraph)) { content.push(schema.nodes.paragraph.create()) } return schema.nodes.doc.create(null, content) } // contentFrom is the nodes for blocks[from:to]: lists nested as // renderList nests them, and a column layout as its columns (columns // and icons spec §1.7). function contentFrom (schema, blocks, from, to, origin) { const content = [] for (let index = from; index < to;) { if (blocks[index].kind === Item) { const [lists, next] = listsFrom(schema, blocks, index, origin, to) content.push(...lists) index = next continue } if (blocks[index].kind === Columns) { const [layout, next] = layoutFrom(schema, blocks, index, origin) content.push(layout) index = next continue } content.push(blockNode(schema, blocks[index], origin(index))) index++ } return content } const colons = source => /^[ \t]*(:+)/u.exec(source)[1].length // layoutFrom is the layout opening at start, and the index after it. // An empty column holds the editor's empty paragraph, as Go draws it. function layoutFrom (schema, blocks, start, origin) { const columns = [] let index = start + 1 while (index < blocks.length && blocks[index].kind === Column) { let end = index + 1 while (end < blocks.length && blocks[end].kind !== ColumnEnd) end++ const inside = contentFrom(schema, blocks, index + 1, end, origin) columns.push(schema.nodes.column.create({ fence: colons(blocks[index].source), openOrigin: origin(index), closeOrigin: origin(end) }, inside.length ? inside : [schema.nodes.paragraph.create()])) index = end + 1 } const layout = schema.nodes.columns.create({ fence: colons(blocks[start].source), openOrigin: origin(start), closeOrigin: origin(index) }, columns) return [layout, index + 1] } function blockNode (schema, block, origin) { const marker = block.marker || null const text = plain(block) switch (block.kind) { case Heading: return schema.nodes.heading.create( { origin, marker, level: block.level, source: block.source }, inlineNodes(schema, text)) case Code: return codeNode(schema, block, text, origin) case Rule: return schema.nodes.horizontal_rule.create({ origin, source: block.source }) case Quote: return schema.nodes.blockquote.create( { marker, prefix: quotePrefixes(block.source) }, schema.nodes.paragraph.create({ origin }, inlineNodes(schema, text))) } return schema.nodes.paragraph.create({ origin, marker }, inlineNodes(schema, text)) } // codeNode keeps every byte of a fence that the text does not: the // opening line (fence and info) and, when markup.codeText left it out, // the last line (close). function codeNode (schema, block, text, origin) { const lines = block.source.split('\n') // dotAll: the info string may hold U+2028 or U+2029, which Go reads as // text and . alone would not match (review M5). const fence = /^( {0,3}(?:`{3,}|~{3,}))(.*)$/su.exec(lines[0]) const last = lines[lines.length - 1] return schema.nodes.code_block.create({ origin, fence: fence[1], info: fence[2], closed: lines.length > 1 && closesFence(last, fenceOf(lines[0])), close: lines.length > 1 && isFenceLine(last) ? last : null }, text === '' ? null : schema.text(text)) } function quotePrefixes (source) { return source.split('\n').map(line => /^ *> ?/u.exec(line)?.[0] ?? '') } // listsFrom is renderList's nesting: depth is min(level, lists open), a // level skip nests one deep, and an item of the other kind starts a new // list at its depth. function listsFrom (schema, blocks, start, origin, to = blocks.length) { const top = [] const open = [] let index = start for (; index < to && blocks[index].kind === Item; index++) { const block = blocks[index] const depth = Math.min(block.level, open.length) open.length = Math.min(open.length, depth + 1) const kind = block.ordered ? 'ordered_list' : 'bullet_list' if (open.length === depth + 1 && open[depth].kind !== kind) { open.length = depth } if (open.length !== depth + 1) { const list = { kind, items: [] } if (depth === 0) top.push(list) else open[depth - 1].items.at(-1).lists.push(list) open.push(list) } open[depth].items.push({ block, lists: [], origin: origin(index) }) } return [top.map(list => listNode(schema, list)), index] } function listNode (schema, list) { return schema.nodes[list.kind].create(null, list.items.map(item => itemNode(schema, item))) } function itemNode (schema, { block, lists, origin }) { const [text, box] = task(plain(block)) const attrs = { marker: block.marker || null, ...itemSyntax(block), task: box !== null, checked: box } const paragraph = schema.nodes.paragraph.create({ origin }, inlineNodes(schema, text)) return schema.nodes.list_item.create(attrs, [paragraph, ...lists.map(list => listNode(schema, list))]) } // inlineNodes is a block's inline text as nodes: text with marks, hard // breaks, and atoms. export function inlineNodes (schema, text) { const out = [] for (const span of spans(text)) { switch (span.kind) { case InCode: out.push(schema.text(span.text, [schema.marks.code.create()])) break case WebLink: out.push(schema.text(span.text, [schema.marks.link.create({ href: span.url, bare: !span.source.startsWith('[') })])) break case WikiLink: out.push(linkNode(schema, span)) break case Embed: out.push(schema.nodes.embed.create({ ref: span.text.slice(3, -2), source: span.source })) break case Image: out.push(schema.nodes.image.create({ id: span.target, alt: span.text, source: span.source })) break default: out.push(...wordNodes(schema, span.text)) } } return out } function linkNode (schema, span) { const attrs = { anchor: span.anchor, shown: span.text, source: span.source } if (isDate(span.target)) { return schema.nodes.datelink.create({ ...attrs, date: span.target }) } return schema.nodes.wikilink.create({ ...attrs, target: span.target }) } function wordNodes (schema, text) { return words(text).map(run => { const marks = Mark.setFrom([ ...(run.em ? [schema.marks.em.create()] : []), ...(run.strong ? [schema.marks.strong.create()] : []) ]) if (run.kind === 'break') { return schema.nodes.hard_break.create({ indent: run.indent }, null, marks) } return schema.text(run.text, marks) }) }