// schema.mjs — the editor's ProseMirror schema (spec §6.3). It holds only // what Mapa's Markdown subset can say: each Mapa block is one textblock or // one rule, and every node's toDOM is the markup the server renders for // it (internal/mapa/markup/render.go, inline.go, chips.go). Gate L4 // (parity.test.mjs) holds the two together byte for byte. // // Attributes that keep a block's Markdown exact (indents, bullets, fences, // quote prefixes, sources) are never drawn. origin is the side table's key // (§6.4), never serialised. marker is a block's ^id, drawn as its id. // // parseDOM is for HTML pasted from outside Mapa (§6.6): it maps what it // can onto the subset, and everything else becomes text. Mapa's own // clipboard never goes through it (clipboard.mjs). import { Schema } from 'prosemirror-model' import { wikiChip, embedChip } from './chips.mjs' import { imageSpec } from './imagedom.mjs' import { safeURL, isDate, isImageID, ImagePrefix } from './markup.mjs' // markerAttrs is a block's id: its ^id, as an anchor (base §3.2). function markerAttrs (node) { return node.attrs.marker ? { id: `b-${node.attrs.marker}` } : {} } const textblock = { origin: { default: null }, marker: { default: null } } const nodes = { doc: { content: 'block+' }, paragraph: { group: 'block flow', content: 'inline*', attrs: { ...textblock }, parseDOM: [{ tag: 'p' }], toDOM: node => ['p', markerAttrs(node), 0] }, // heading: level 1 to 6, drawn h2 to h6, because the page title is // the H1. source is the line as typed (# count and spacing). heading: { group: 'block flow', content: 'inline*', defining: true, attrs: { ...textblock, level: { default: 1 }, source: { default: null } }, // h1 is a level 1 heading, and h2 to h6 are read as Mapa draws them, // one level down, so a page's own HTML comes back as it was typed. parseDOM: [1, 2, 3, 4, 5, 6].map(level => ({ tag: `h${level}`, attrs: { level: Math.max(level - 1, 1) } })), toDOM: node => [`h${Math.min(node.attrs.level + 1, 6)}`, markerAttrs(node), 0] }, // A run of items of one kind at one depth is one list; a change of // kind starts another. An item's content is its one paragraph, then // the lists nested under it: more than one when the kind changes there. bullet_list: { group: 'block flow', content: 'list_item+', parseDOM: [{ tag: 'ul' }], toDOM: () => ['ul', 0] }, ordered_list: { group: 'block flow', content: 'list_item+', parseDOM: [{ tag: 'ol' }], toDOM: () => ['ol', 0] }, // list_item keeps its line's syntax: indent exactly as typed, the // bullet (- * +) or number and delimiter (. or )), and a task box. list_item: { content: 'paragraph (bullet_list | ordered_list)*', defining: true, attrs: { marker: { default: null }, indent: { default: '' }, bullet: { default: '-' }, number: { default: null }, delim: { default: null }, task: { default: false }, checked: { default: null } }, parseDOM: [{ tag: 'li', getAttrs: dom => { const box = dom.getAttribute('mapa-task') if (box === 'open') return { task: true, checked: ' ' } if (box === 'done') return { task: true, checked: 'x' } return {} } }], toDOM: node => { const attrs = markerAttrs(node) if (node.attrs.task) { attrs['mapa-task'] = node.attrs.checked === ' ' ? 'open' : 'done' } return ['li', attrs, 0] } }, // blockquote: one Mapa quote block. prefix is each line's quote mark // as typed (">" or "> ", after any spaces). blockquote: { group: 'block flow', content: 'paragraph', defining: true, attrs: { marker: { default: null }, prefix: { default: null } }, parseDOM: [{ tag: 'blockquote' }], toDOM: node => ['blockquote', markerAttrs(node), 0] }, // code_block: fence is the opening fence as typed, its spaces and its // run of backticks or tildes; info is the rest of that line. closed is // whether Go's parser found a closing line. close is the last line when // the text does not hold it (markup.codeText drops a last line that // looks like a fence, closing or not), or null. A new block is closed. code_block: { group: 'block flow', content: 'text*', marks: '', code: true, defining: true, attrs: { origin: { default: null }, fence: { default: '```' }, info: { default: '' }, closed: { default: true }, close: { default: '```' } }, parseDOM: [{ tag: 'pre', preserveWhitespace: 'full' }], toDOM: () => ['pre', ['code', 0]] }, // A column layout (columns and icons spec §1.7): two to four columns, // each holding any block but another layout. fence is the colon count // of the fences as typed; openOrigin and closeOrigin are the side // table's keys for the fence lines, never serialised or drawn. columns: { group: 'block', content: 'column{2,4}', defining: true, attrs: { fence: { default: 4 }, openOrigin: { default: null }, closeOrigin: { default: null } }, parseDOM: [{ tag: 'div[mapa-columns]' }], toDOM: node => ['div', { 'mapa-columns': String(node.childCount) }, 0] }, column: { content: 'flow+', isolating: true, defining: true, attrs: { fence: { default: 3 }, openOrigin: { default: null }, closeOrigin: { default: null } }, parseDOM: [{ tag: 'div[mapa-column]' }], toDOM: () => ['div', { 'mapa-column': '' }, 0] }, horizontal_rule: { group: 'block flow', attrs: { origin: { default: null }, source: { default: '---' } }, parseDOM: [{ tag: 'hr' }], toDOM: () => ['hr'] }, text: { group: 'inline' }, // hard_break is a newline in a paragraph. indent is the next line's // leading whitespace, kept in the source and not shown. hard_break: { inline: true, group: 'inline', selectable: false, attrs: { indent: { default: '' } }, parseDOM: [{ tag: 'br' }], toDOM: () => ['br'] } } // Mark order is nesting order: em comes before strong, so em is always // drawn outside strong (§5, §6.3). const marks = { link: { attrs: { href: {}, bare: { default: false } }, inclusive: false, excludes: 'em strong code', parseDOM: [{ tag: 'a[href]', getAttrs: dom => safeURL(dom.getAttribute('href')) ? { href: dom.getAttribute('href') } : false }], toDOM: mark => ['a', { href: mark.attrs.href, rel: 'noopener noreferrer' }, 0] }, em: { parseDOM: [{ tag: 'em' }, { tag: 'i' }, { style: 'font-style=italic' }], toDOM: () => ['em', 0] }, strong: { parseDOM: [{ tag: 'strong' }, { // Google Docs wraps a whole paste in . tag: 'b', getAttrs: dom => dom.style?.fontWeight !== 'normal' && null }, { style: 'font-weight', getAttrs: value => /^(bold(er)?|[6-9]\d{2,})$/u.test(value) && null }], toDOM: () => ['strong', 0] }, code: { excludes: '_', parseDOM: [{ tag: 'code' }], toDOM: () => ['code', 0] } } // chipText is a chip's words, without its glyph or its note. function chipText (dom) { let text = '' for (const child of dom.childNodes) { if (child.nodeType === 3) text += child.nodeValue } return text.trim() } // atoms are Mapa's inline atoms. Their chips need the viewer's answer for // each link, so the schema is made with a resolver: resolve(target, // anchor) gives the server's resolution (the editor's data island, or // POST /links/resolve for a link typed after mount). function atoms (resolve, resolveImage) { const atom = { inline: true, group: 'inline', atom: true, selectable: true } return { wikilink: { ...atom, attrs: { target: {}, anchor: { default: '' }, shown: { default: '' }, source: { default: null }, origin: { default: null } }, // A chip from Mapa's HTML comes back as a link to the words shown. parseDOM: [{ tag: 'a[mapa-chip]', priority: 60, getAttrs: dom => { const day = /^\/d\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/u .exec(dom.getAttribute('href') ?? '') if (day) return false const target = chipText(dom) return target === '' ? false : { target } } }], toDOM: node => wikiChip(node.attrs, resolve(node.attrs.target, node.attrs.anchor)) }, datelink: { ...atom, attrs: { date: {}, anchor: { default: '' }, shown: { default: '' }, source: { default: null }, origin: { default: null } }, parseDOM: [{ tag: 'a[mapa-chip]', priority: 61, getAttrs: dom => { const day = /^\/d\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/u .exec(dom.getAttribute('href') ?? '') return day && isDate(day[1]) ? { date: day[1] } : false } }], toDOM: node => wikiChip({ ...node.attrs, target: node.attrs.date }, resolve(node.attrs.date, node.attrs.anchor)) }, embed: { ...atom, attrs: { ref: {}, source: { default: null }, origin: { default: null } }, parseDOM: [{ tag: 'span[mapa-chip="embed"]', priority: 60, getAttrs: dom => chipText(dom) === '' ? false : { ref: chipText(dom) } }], toDOM: node => embedChip(node.attrs.ref) }, // image is ![alt](/i/id) (images spec §2). Only Mapa's own address // is an image; an from outside is turned into a link before // it gets here (clipboard.mjs). Its size and state are the // resolver's, never attributes. image: { ...atom, draggable: true, attrs: { id: {}, alt: { default: '' }, source: { default: null }, origin: { default: null } }, parseDOM: [{ tag: 'img[src]', getAttrs: dom => { const src = dom.getAttribute('src') ?? '' const id = src.startsWith(ImagePrefix) ? src.slice(ImagePrefix.length) : '' return isImageID(id) ? { id, alt: cleanAlt(dom.getAttribute('alt') ?? '') } : false } }], toDOM: node => imageSpec(node.attrs, resolveImage(node.attrs.id)) } } } // cleanAlt is alt text as Mapa keeps it (images §2.1): no [, ] or line // break, and at most 1,000 bytes of UTF-8, cut at a character. export function cleanAlt (alt) { let out = '' let bytes = 0 for (const char of alt.replace(/[[\]\r\n]/gu, '')) { const point = char.codePointAt(0) const width = point < 0x80 ? 1 : point < 0x800 ? 2 : point < 0x10000 ? 3 : 4 if (bytes + width > 1000) break bytes += width out += char } return out } const unavailable = () => null const pending = target => ({ href: `/new?title=${encodeURIComponent(target)}`, pending: true }) // createSchema is the schema for one page load's resolver. Without one, // every link draws as not written yet. export function createSchema (resolve = pending, resolveImage = unavailable) { return new Schema({ nodes: { ...nodes, ...atoms(resolve, resolveImage) }, marks }) }