// inputrules.mjs — Markdown typed at the start of a block makes that // block (spec §6.6): "# ", "- ", "* ", "1. ", "1) ", "[ ] " in an item. // The settle plugin's reparse would read most of them anyway, but only // once the line says something: "- " alone is an item with no text, and // writing it back would trim the space you just typed. These turn the // block over as the space is typed, so the caret is where you expect. // "---", ">" and "```" need no rule: the line already says a rule, a // quote or a fence, and the reparse makes it one. iOS's smart dashes // ("—-", "–-") are taken as a rule too (§6.11). Marks typed as **x**, // *x* and `x` are the reparse's (D11); markRule keeps the next letter // out of the mark just closed. import { InputRule, inputRules, textblockTypeInputRule } from 'prosemirror-inputrules' import { findWrapping } from 'prosemirror-transform' import { Plugin, TextSelection } from 'prosemirror-state' import { fidelityKey } from './fidelity.mjs' function inItem ($pos) { for (let depth = $pos.depth; depth > 0; depth--) { if ($pos.node(depth).type.name === 'list_item') return depth } return 0 } // outsideList keeps a rule to a block outside a list: in an item, "- " // is text (Tab makes a nested item). function outsideList (rule) { const handler = rule.handler rule.handler = (state, match, start, end) => inItem(state.doc.resolve(start)) ? null : handler(state, match, start, end) return rule } // listRule wraps the paragraph in a list whose item keeps the bullet or // number typed. Lists of one kind side by side are joined by the settle // plugin, as the Markdown reads them. function listRule (regexp, listType, attrsOf) { return new InputRule(regexp, (state, match, start, end) => { const $start = state.doc.resolve(start) if (inItem($start) || $start.parent.type.name !== 'paragraph') return null const tr = state.tr.delete(start, end) const range = tr.doc.resolve(start).blockRange() const wrapping = range && findWrapping(range, listType) if (!wrapping) return null tr.wrap(range, wrapping) const at = range.start + 1 tr.setNodeMarkup(at, null, { ...tr.doc.nodeAt(at).attrs, ...attrsOf(match) }) return tr }) } function bulletRule (schema) { return listRule(/^\s{0,3}([-*+])\s$/u, schema.nodes.bullet_list, match => ({ bullet: match[1] })) } function orderedRule (schema) { return listRule(/^\s{0,3}(\d{1,9})([.)])\s$/u, schema.nodes.ordered_list, match => ({ number: match[1], delim: match[2] })) } function headingRule (schema) { return outsideList(textblockTypeInputRule(/^(#{1,6})\s$/u, schema.nodes.heading, match => ({ level: match[1].length, source: null }))) } // taskRule: "[ ] " or "[x] " at the start of an item makes it a task. function taskRule (schema) { return new InputRule(/^\[([ xX])\]\s$/u, (state, match, start, end) => { const $start = state.doc.resolve(start) const depth = inItem($start) if (!depth || $start.parentOffset !== 0) return null const item = $start.node(depth) return state.tr.delete(start, end).setNodeMarkup($start.before(depth), null, { ...item.attrs, task: true, checked: match[1] }) }) } // ruleRule: a line of three dashes, stars or underscores is a divider, // and so is iOS's "—-" or "–-" (smart dashes, §6.11). The caret goes to // the paragraph after it. function ruleRule (schema) { return new InputRule(/^\s{0,3}(?:---|\*\*\*|___|[—–]-)$/u, (state, match, start) => { const $start = state.doc.resolve(start) if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null const from = $start.before(1) const to = $start.after(1) const tr = state.tr.replaceWith(from, to, [ schema.nodes.horizontal_rule.create(), schema.nodes.paragraph.create()]) return tr.setSelection(TextSelection.create(tr.doc, from + 2)) }) } // quoteRule: ">" first in a paragraph makes it a quote. function quoteRule (schema) { return new InputRule(/^\s{0,3}>$/u, (state, match, start, end) => { const $start = state.doc.resolve(start) if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null const tr = state.tr.delete(start, end) const range = tr.doc.resolve(start).blockRange() return range ? tr.wrap(range, [{ type: schema.nodes.blockquote, attrs: { prefix: null } }]) : null }) } // fenceRule: three backticks or tildes first in a paragraph make it a // code block, closed. function fenceRule (schema) { return new InputRule(/^(\s{0,3}(?:```|~~~))$/u, (state, match, start, end) => { const $start = state.doc.resolve(start) if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null const fence = match[1] return state.tr.delete(start, end).setBlockType(start, start, schema.nodes.code_block, { fence, info: '', closed: true, close: fence.trimStart() }) }) } // lead: a space typed first in an empty quote is not text (">" made the // quote, and its space belongs to the prefix). function leadRule () { return new InputRule(/^\s$/u, (state, match, start, end) => { const $start = state.doc.resolve(start) if ($start.parent.content.size !== 0 || $start.depth < 2 || $start.node($start.depth - 1).type.name !== 'blockquote') return null return state.tr }) } // markRule keeps the next letter out of a mark the reparse just made: // after "**pilot**" you type plain text again. function markRule () { return new Plugin({ appendTransaction (trs, oldState, state) { if (!trs.some(tr => tr.getMeta(fidelityKey)?.settled) || !state.selection.empty) return null const before = oldState.selection.$head.marks() const now = state.selection.$head.marks() if (before.length === now.length && before.every(mark => mark.isInSet(now))) return null return state.tr.setStoredMarks(before) } }) } export function rules (schema) { return [ inputRules({ rules: [headingRule(schema), bulletRule(schema), orderedRule(schema), taskRule(schema), ruleRule(schema), quoteRule(schema), fenceRule(schema), leadRule()] }), markRule() ] }