| 1 | // schema.mjs — the editor's ProseMirror schema (spec §6.3). It holds only |
| 2 | // what Mapa's Markdown subset can say: each Mapa block is one textblock or |
| 3 | // one rule, and every node's toDOM is the markup the server renders for |
| 4 | // it (internal/mapa/markup/render.go, inline.go, chips.go). Gate L4 |
| 5 | // (parity.test.mjs) holds the two together byte for byte. |
| 6 | // |
| 7 | // Attributes that keep a block's Markdown exact (indents, bullets, fences, |
| 8 | // quote prefixes, sources) are never drawn. origin is the side table's key |
| 9 | // (§6.4), never serialised. marker is a block's ^id, drawn as its id. |
| 10 | // |
| 11 | // parseDOM is for HTML pasted from outside Mapa (§6.6): it maps what it |
| 12 | // can onto the subset, and everything else becomes text. Mapa's own |
| 13 | // clipboard never goes through it (clipboard.mjs). |
| 14 | |
| 15 | import { Schema } from 'prosemirror-model' |
| 16 | import { wikiChip, embedChip } from './chips.mjs' |
| 17 | import { imageSpec } from './imagedom.mjs' |
| 18 | import { safeURL, isDate, isImageID, ImagePrefix } from './markup.mjs' |
| 19 | |
| 20 | // markerAttrs is a block's id: its ^id, as an anchor (base §3.2). |
| 21 | function markerAttrs (node) { |
| 22 | return node.attrs.marker ? { id: `b-${node.attrs.marker}` } : {} |
| 23 | } |
| 24 | |
| 25 | const textblock = { origin: { default: null }, marker: { default: null } } |
| 26 | |
| 27 | const nodes = { |
| 28 | doc: { content: 'block+' }, |
| 29 | |
| 30 | paragraph: { |
| 31 | group: 'block flow', |
| 32 | content: 'inline*', |
| 33 | attrs: { ...textblock }, |
| 34 | parseDOM: [{ tag: 'p' }], |
| 35 | toDOM: node => ['p', markerAttrs(node), 0] |
| 36 | }, |
| 37 | |
| 38 | // heading: level 1 to 6, drawn h2 to h6, because the page title is |
| 39 | // the H1. source is the line as typed (# count and spacing). |
| 40 | heading: { |
| 41 | group: 'block flow', |
| 42 | content: 'inline*', |
| 43 | defining: true, |
| 44 | attrs: { ...textblock, level: { default: 1 }, source: { default: null } }, |
| 45 | // h1 is a level 1 heading, and h2 to h6 are read as Mapa draws them, |
| 46 | // one level down, so a page's own HTML comes back as it was typed. |
| 47 | parseDOM: [1, 2, 3, 4, 5, 6].map(level => ({ |
| 48 | tag: `h${level}`, attrs: { level: Math.max(level - 1, 1) } |
| 49 | })), |
| 50 | toDOM: node => [`h${Math.min(node.attrs.level + 1, 6)}`, |
| 51 | markerAttrs(node), 0] |
| 52 | }, |
| 53 | |
| 54 | // A run of items of one kind at one depth is one list; a change of |
| 55 | // kind starts another. An item's content is its one paragraph, then |
| 56 | // the lists nested under it: more than one when the kind changes there. |
| 57 | bullet_list: { |
| 58 | group: 'block flow', |
| 59 | content: 'list_item+', |
| 60 | parseDOM: [{ tag: 'ul' }], |
| 61 | toDOM: () => ['ul', 0] |
| 62 | }, |
| 63 | |
| 64 | ordered_list: { |
| 65 | group: 'block flow', |
| 66 | content: 'list_item+', |
| 67 | parseDOM: [{ tag: 'ol' }], |
| 68 | toDOM: () => ['ol', 0] |
| 69 | }, |
| 70 | |
| 71 | // list_item keeps its line's syntax: indent exactly as typed, the |
| 72 | // bullet (- * +) or number and delimiter (. or )), and a task box. |
| 73 | list_item: { |
| 74 | content: 'paragraph (bullet_list | ordered_list)*', |
| 75 | defining: true, |
| 76 | attrs: { |
| 77 | marker: { default: null }, |
| 78 | indent: { default: '' }, |
| 79 | bullet: { default: '-' }, |
| 80 | number: { default: null }, |
| 81 | delim: { default: null }, |
| 82 | task: { default: false }, |
| 83 | checked: { default: null } |
| 84 | }, |
| 85 | parseDOM: [{ |
| 86 | tag: 'li', |
| 87 | getAttrs: dom => { |
| 88 | const box = dom.getAttribute('mapa-task') |
| 89 | if (box === 'open') return { task: true, checked: ' ' } |
| 90 | if (box === 'done') return { task: true, checked: 'x' } |
| 91 | return {} |
| 92 | } |
| 93 | }], |
| 94 | toDOM: node => { |
| 95 | const attrs = markerAttrs(node) |
| 96 | if (node.attrs.task) { |
| 97 | attrs['mapa-task'] = node.attrs.checked === ' ' ? 'open' : 'done' |
| 98 | } |
| 99 | return ['li', attrs, 0] |
| 100 | } |
| 101 | }, |
| 102 | |
| 103 | // blockquote: one Mapa quote block. prefix is each line's quote mark |
| 104 | // as typed (">" or "> ", after any spaces). |
| 105 | blockquote: { |
| 106 | group: 'block flow', |
| 107 | content: 'paragraph', |
| 108 | defining: true, |
| 109 | attrs: { marker: { default: null }, prefix: { default: null } }, |
| 110 | parseDOM: [{ tag: 'blockquote' }], |
| 111 | toDOM: node => ['blockquote', markerAttrs(node), 0] |
| 112 | }, |
| 113 | |
| 114 | // code_block: fence is the opening fence as typed, its spaces and its |
| 115 | // run of backticks or tildes; info is the rest of that line. closed is |
| 116 | // whether Go's parser found a closing line. close is the last line when |
| 117 | // the text does not hold it (markup.codeText drops a last line that |
| 118 | // looks like a fence, closing or not), or null. A new block is closed. |
| 119 | code_block: { |
| 120 | group: 'block flow', |
| 121 | content: 'text*', |
| 122 | marks: '', |
| 123 | code: true, |
| 124 | defining: true, |
| 125 | attrs: { |
| 126 | origin: { default: null }, |
| 127 | fence: { default: '```' }, |
| 128 | info: { default: '' }, |
| 129 | closed: { default: true }, |
| 130 | close: { default: '```' } |
| 131 | }, |
| 132 | parseDOM: [{ tag: 'pre', preserveWhitespace: 'full' }], |
| 133 | toDOM: () => ['pre', ['code', 0]] |
| 134 | }, |
| 135 | |
| 136 | // A column layout (columns and icons spec §1.7): two to four columns, |
| 137 | // each holding any block but another layout. fence is the colon count |
| 138 | // of the fences as typed; openOrigin and closeOrigin are the side |
| 139 | // table's keys for the fence lines, never serialised or drawn. |
| 140 | columns: { |
| 141 | group: 'block', |
| 142 | content: 'column{2,4}', |
| 143 | defining: true, |
| 144 | attrs: { |
| 145 | fence: { default: 4 }, |
| 146 | openOrigin: { default: null }, |
| 147 | closeOrigin: { default: null } |
| 148 | }, |
| 149 | parseDOM: [{ tag: 'div[mapa-columns]' }], |
| 150 | toDOM: node => ['div', { 'mapa-columns': String(node.childCount) }, 0] |
| 151 | }, |
| 152 | |
| 153 | column: { |
| 154 | content: 'flow+', |
| 155 | isolating: true, |
| 156 | defining: true, |
| 157 | attrs: { |
| 158 | fence: { default: 3 }, |
| 159 | openOrigin: { default: null }, |
| 160 | closeOrigin: { default: null } |
| 161 | }, |
| 162 | parseDOM: [{ tag: 'div[mapa-column]' }], |
| 163 | toDOM: () => ['div', { 'mapa-column': '' }, 0] |
| 164 | }, |
| 165 | |
| 166 | horizontal_rule: { |
| 167 | group: 'block flow', |
| 168 | attrs: { origin: { default: null }, source: { default: '---' } }, |
| 169 | parseDOM: [{ tag: 'hr' }], |
| 170 | toDOM: () => ['hr'] |
| 171 | }, |
| 172 | |
| 173 | text: { group: 'inline' }, |
| 174 | |
| 175 | // hard_break is a newline in a paragraph. indent is the next line's |
| 176 | // leading whitespace, kept in the source and not shown. |
| 177 | hard_break: { |
| 178 | inline: true, |
| 179 | group: 'inline', |
| 180 | selectable: false, |
| 181 | attrs: { indent: { default: '' } }, |
| 182 | parseDOM: [{ tag: 'br' }], |
| 183 | toDOM: () => ['br'] |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Mark order is nesting order: em comes before strong, so em is always |
| 188 | // drawn outside strong (§5, §6.3). |
| 189 | const marks = { |
| 190 | link: { |
| 191 | attrs: { href: {}, bare: { default: false } }, |
| 192 | inclusive: false, |
| 193 | excludes: 'em strong code', |
| 194 | parseDOM: [{ |
| 195 | tag: 'a[href]', |
| 196 | getAttrs: dom => safeURL(dom.getAttribute('href')) ? { href: dom.getAttribute('href') } : false |
| 197 | }], |
| 198 | toDOM: mark => ['a', { href: mark.attrs.href, rel: 'noopener noreferrer' }, 0] |
| 199 | }, |
| 200 | em: { |
| 201 | parseDOM: [{ tag: 'em' }, { tag: 'i' }, { style: 'font-style=italic' }], |
| 202 | toDOM: () => ['em', 0] |
| 203 | }, |
| 204 | strong: { |
| 205 | parseDOM: [{ tag: 'strong' }, { |
| 206 | // Google Docs wraps a whole paste in <b style="font-weight:normal">. |
| 207 | tag: 'b', getAttrs: dom => dom.style?.fontWeight !== 'normal' && null |
| 208 | }, { |
| 209 | style: 'font-weight', |
| 210 | getAttrs: value => /^(bold(er)?|[6-9]\d{2,})$/u.test(value) && null |
| 211 | }], |
| 212 | toDOM: () => ['strong', 0] |
| 213 | }, |
| 214 | code: { excludes: '_', parseDOM: [{ tag: 'code' }], toDOM: () => ['code', 0] } |
| 215 | } |
| 216 | |
| 217 | // chipText is a chip's words, without its glyph or its note. |
| 218 | function chipText (dom) { |
| 219 | let text = '' |
| 220 | for (const child of dom.childNodes) { |
| 221 | if (child.nodeType === 3) text += child.nodeValue |
| 222 | } |
| 223 | return text.trim() |
| 224 | } |
| 225 | |
| 226 | // atoms are Mapa's inline atoms. Their chips need the viewer's answer for |
| 227 | // each link, so the schema is made with a resolver: resolve(target, |
| 228 | // anchor) gives the server's resolution (the editor's data island, or |
| 229 | // POST /links/resolve for a link typed after mount). |
| 230 | function atoms (resolve, resolveImage) { |
| 231 | const atom = { inline: true, group: 'inline', atom: true, selectable: true } |
| 232 | return { |
| 233 | wikilink: { |
| 234 | ...atom, |
| 235 | attrs: { |
| 236 | target: {}, anchor: { default: '' }, shown: { default: '' }, |
| 237 | source: { default: null }, origin: { default: null } |
| 238 | }, |
| 239 | // A chip from Mapa's HTML comes back as a link to the words shown. |
| 240 | parseDOM: [{ |
| 241 | tag: 'a[mapa-chip]', |
| 242 | priority: 60, |
| 243 | getAttrs: dom => { |
| 244 | const day = /^\/d\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/u |
| 245 | .exec(dom.getAttribute('href') ?? '') |
| 246 | if (day) return false |
| 247 | const target = chipText(dom) |
| 248 | return target === '' ? false : { target } |
| 249 | } |
| 250 | }], |
| 251 | toDOM: node => wikiChip(node.attrs, |
| 252 | resolve(node.attrs.target, node.attrs.anchor)) |
| 253 | }, |
| 254 | datelink: { |
| 255 | ...atom, |
| 256 | attrs: { |
| 257 | date: {}, anchor: { default: '' }, shown: { default: '' }, |
| 258 | source: { default: null }, origin: { default: null } |
| 259 | }, |
| 260 | parseDOM: [{ |
| 261 | tag: 'a[mapa-chip]', |
| 262 | priority: 61, |
| 263 | getAttrs: dom => { |
| 264 | const day = /^\/d\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/u |
| 265 | .exec(dom.getAttribute('href') ?? '') |
| 266 | return day && isDate(day[1]) ? { date: day[1] } : false |
| 267 | } |
| 268 | }], |
| 269 | toDOM: node => wikiChip({ ...node.attrs, target: node.attrs.date }, |
| 270 | resolve(node.attrs.date, node.attrs.anchor)) |
| 271 | }, |
| 272 | embed: { |
| 273 | ...atom, |
| 274 | attrs: { ref: {}, source: { default: null }, origin: { default: null } }, |
| 275 | parseDOM: [{ |
| 276 | tag: 'span[mapa-chip="embed"]', |
| 277 | priority: 60, |
| 278 | getAttrs: dom => chipText(dom) === '' ? false : { ref: chipText(dom) } |
| 279 | }], |
| 280 | toDOM: node => embedChip(node.attrs.ref) |
| 281 | }, |
| 282 | // image is  (images spec §2). Only Mapa's own address |
| 283 | // is an image; an <img> from outside is turned into a link before |
| 284 | // it gets here (clipboard.mjs). Its size and state are the |
| 285 | // resolver's, never attributes. |
| 286 | image: { |
| 287 | ...atom, |
| 288 | draggable: true, |
| 289 | attrs: { |
| 290 | id: {}, alt: { default: '' }, source: { default: null }, |
| 291 | origin: { default: null } |
| 292 | }, |
| 293 | parseDOM: [{ |
| 294 | tag: 'img[src]', |
| 295 | getAttrs: dom => { |
| 296 | const src = dom.getAttribute('src') ?? '' |
| 297 | const id = src.startsWith(ImagePrefix) ? src.slice(ImagePrefix.length) : '' |
| 298 | return isImageID(id) ? { id, alt: cleanAlt(dom.getAttribute('alt') ?? '') } : false |
| 299 | } |
| 300 | }], |
| 301 | toDOM: node => imageSpec(node.attrs, resolveImage(node.attrs.id)) |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // cleanAlt is alt text as Mapa keeps it (images §2.1): no [, ] or line |
| 307 | // break, and at most 1,000 bytes of UTF-8, cut at a character. |
| 308 | export function cleanAlt (alt) { |
| 309 | let out = '' |
| 310 | let bytes = 0 |
| 311 | for (const char of alt.replace(/[[\]\r\n]/gu, '')) { |
| 312 | const point = char.codePointAt(0) |
| 313 | const width = point < 0x80 ? 1 : point < 0x800 ? 2 : point < 0x10000 ? 3 : 4 |
| 314 | if (bytes + width > 1000) break |
| 315 | bytes += width |
| 316 | out += char |
| 317 | } |
| 318 | return out |
| 319 | } |
| 320 | |
| 321 | const unavailable = () => null |
| 322 | |
| 323 | const pending = target => ({ |
| 324 | href: `/new?title=${encodeURIComponent(target)}`, pending: true |
| 325 | }) |
| 326 | |
| 327 | // createSchema is the schema for one page load's resolver. Without one, |
| 328 | // every link draws as not written yet. |
| 329 | export function createSchema (resolve = pending, resolveImage = unavailable) { |
| 330 | return new Schema({ nodes: { ...nodes, ...atoms(resolve, resolveImage) }, marks }) |
| 331 | } |
| 332 | |