| index a94c7c6..6c979e7 100644 |
| --- a/editor/fromMarkdown.mjs |
| +++ b/editor/fromMarkdown.mjs |
| @@ -13,7 +13,7 @@ import { Mark } from 'prosemirror-model' |
| import { |
| parse, plain, spans, words, task, itemSyntax, isDate, isFenceLine, |
| fenceOf, closesFence, |
| - Paragraph, Heading, Item, Code, Quote, Rule, |
| + Paragraph, Heading, Item, Code, Quote, Rule, Columns, Column, ColumnEnd, |
| InCode, WikiLink, WebLink, Embed, Image |
| } from './markup.mjs' |
| |
| @@ -23,23 +23,63 @@ export function fromMarkdown (schema, 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 = 0; index < blocks.length;) { |
| + for (let index = from; index < to;) { |
| if (blocks[index].kind === Item) { |
| - const [lists, next] = listsFrom(schema, blocks, index, origin) |
| + 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++ |
| } |
| - const last = content[content.length - 1] |
| - if (options.trailing !== false && |
| - (!last || last.type !== schema.nodes.paragraph)) { |
| - content.push(schema.nodes.paragraph.create()) |
| + 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 |
| } |
| - return schema.nodes.doc.create(null, content) |
| + 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) { |
| @@ -88,11 +128,11 @@ function quotePrefixes (source) { |
| // 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) { |
| +function listsFrom (schema, blocks, start, origin, to = blocks.length) { |
| const top = [] |
| const open = [] |
| let index = start |
| - for (; index < blocks.length && blocks[index].kind === Item; index++) { |
| + 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) |