| 1 | // inputrules.mjs — Markdown typed at the start of a block makes that |
| 2 | // block (spec §6.6): "# ", "- ", "* ", "1. ", "1) ", "[ ] " in an item. |
| 3 | // The settle plugin's reparse would read most of them anyway, but only |
| 4 | // once the line says something: "- " alone is an item with no text, and |
| 5 | // writing it back would trim the space you just typed. These turn the |
| 6 | // block over as the space is typed, so the caret is where you expect. |
| 7 | // "---", ">" and "```" need no rule: the line already says a rule, a |
| 8 | // quote or a fence, and the reparse makes it one. iOS's smart dashes |
| 9 | // ("—-", "–-") are taken as a rule too (§6.11). Marks typed as **x**, |
| 10 | // *x* and `x` are the reparse's (D11); markRule keeps the next letter |
| 11 | // out of the mark just closed. |
| 12 | |
| 13 | import { InputRule, inputRules, textblockTypeInputRule } from 'prosemirror-inputrules' |
| 14 | import { findWrapping } from 'prosemirror-transform' |
| 15 | import { Plugin, TextSelection } from 'prosemirror-state' |
| 16 | import { fidelityKey } from './fidelity.mjs' |
| 17 | |
| 18 | function inItem ($pos) { |
| 19 | for (let depth = $pos.depth; depth > 0; depth--) { |
| 20 | if ($pos.node(depth).type.name === 'list_item') return depth |
| 21 | } |
| 22 | return 0 |
| 23 | } |
| 24 | |
| 25 | // outsideList keeps a rule to a block outside a list: in an item, "- " |
| 26 | // is text (Tab makes a nested item). |
| 27 | function outsideList (rule) { |
| 28 | const handler = rule.handler |
| 29 | rule.handler = (state, match, start, end) => |
| 30 | inItem(state.doc.resolve(start)) ? null : handler(state, match, start, end) |
| 31 | return rule |
| 32 | } |
| 33 | |
| 34 | // listRule wraps the paragraph in a list whose item keeps the bullet or |
| 35 | // number typed. Lists of one kind side by side are joined by the settle |
| 36 | // plugin, as the Markdown reads them. |
| 37 | function listRule (regexp, listType, attrsOf) { |
| 38 | return new InputRule(regexp, (state, match, start, end) => { |
| 39 | const $start = state.doc.resolve(start) |
| 40 | if (inItem($start) || $start.parent.type.name !== 'paragraph') return null |
| 41 | const tr = state.tr.delete(start, end) |
| 42 | const range = tr.doc.resolve(start).blockRange() |
| 43 | const wrapping = range && findWrapping(range, listType) |
| 44 | if (!wrapping) return null |
| 45 | tr.wrap(range, wrapping) |
| 46 | const at = range.start + 1 |
| 47 | tr.setNodeMarkup(at, null, { ...tr.doc.nodeAt(at).attrs, ...attrsOf(match) }) |
| 48 | return tr |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | function bulletRule (schema) { |
| 53 | return listRule(/^\s{0,3}([-*+])\s$/u, schema.nodes.bullet_list, |
| 54 | match => ({ bullet: match[1] })) |
| 55 | } |
| 56 | |
| 57 | function orderedRule (schema) { |
| 58 | return listRule(/^\s{0,3}(\d{1,9})([.)])\s$/u, schema.nodes.ordered_list, |
| 59 | match => ({ number: match[1], delim: match[2] })) |
| 60 | } |
| 61 | |
| 62 | function headingRule (schema) { |
| 63 | return outsideList(textblockTypeInputRule(/^(#{1,6})\s$/u, schema.nodes.heading, |
| 64 | match => ({ level: match[1].length, source: null }))) |
| 65 | } |
| 66 | |
| 67 | // taskRule: "[ ] " or "[x] " at the start of an item makes it a task. |
| 68 | function taskRule (schema) { |
| 69 | return new InputRule(/^\[([ xX])\]\s$/u, (state, match, start, end) => { |
| 70 | const $start = state.doc.resolve(start) |
| 71 | const depth = inItem($start) |
| 72 | if (!depth || $start.parentOffset !== 0) return null |
| 73 | const item = $start.node(depth) |
| 74 | return state.tr.delete(start, end).setNodeMarkup($start.before(depth), null, |
| 75 | { ...item.attrs, task: true, checked: match[1] }) |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | // ruleRule: a line of three dashes, stars or underscores is a divider, |
| 80 | // and so is iOS's "—-" or "–-" (smart dashes, §6.11). The caret goes to |
| 81 | // the paragraph after it. |
| 82 | function ruleRule (schema) { |
| 83 | return new InputRule(/^\s{0,3}(?:---|\*\*\*|___|[—–]-)$/u, (state, match, start) => { |
| 84 | const $start = state.doc.resolve(start) |
| 85 | if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null |
| 86 | const from = $start.before(1) |
| 87 | const to = $start.after(1) |
| 88 | const tr = state.tr.replaceWith(from, to, [ |
| 89 | schema.nodes.horizontal_rule.create(), schema.nodes.paragraph.create()]) |
| 90 | return tr.setSelection(TextSelection.create(tr.doc, from + 2)) |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | // quoteRule: ">" first in a paragraph makes it a quote. |
| 95 | function quoteRule (schema) { |
| 96 | return new InputRule(/^\s{0,3}>$/u, (state, match, start, end) => { |
| 97 | const $start = state.doc.resolve(start) |
| 98 | if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null |
| 99 | const tr = state.tr.delete(start, end) |
| 100 | const range = tr.doc.resolve(start).blockRange() |
| 101 | return range ? tr.wrap(range, [{ type: schema.nodes.blockquote, attrs: { prefix: null } }]) : null |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | // fenceRule: three backticks or tildes first in a paragraph make it a |
| 106 | // code block, closed. |
| 107 | function fenceRule (schema) { |
| 108 | return new InputRule(/^(\s{0,3}(?:```|~~~))$/u, (state, match, start, end) => { |
| 109 | const $start = state.doc.resolve(start) |
| 110 | if ($start.parent.type.name !== 'paragraph' || $start.depth !== 1) return null |
| 111 | const fence = match[1] |
| 112 | return state.tr.delete(start, end).setBlockType(start, start, schema.nodes.code_block, |
| 113 | { fence, info: '', closed: true, close: fence.trimStart() }) |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | // lead: a space typed first in an empty quote is not text (">" made the |
| 118 | // quote, and its space belongs to the prefix). |
| 119 | function leadRule () { |
| 120 | return new InputRule(/^\s$/u, (state, match, start, end) => { |
| 121 | const $start = state.doc.resolve(start) |
| 122 | if ($start.parent.content.size !== 0 || $start.depth < 2 || |
| 123 | $start.node($start.depth - 1).type.name !== 'blockquote') return null |
| 124 | return state.tr |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | // markRule keeps the next letter out of a mark the reparse just made: |
| 129 | // after "**pilot**" you type plain text again. |
| 130 | function markRule () { |
| 131 | return new Plugin({ |
| 132 | appendTransaction (trs, oldState, state) { |
| 133 | if (!trs.some(tr => tr.getMeta(fidelityKey)?.settled) || !state.selection.empty) return null |
| 134 | const before = oldState.selection.$head.marks() |
| 135 | const now = state.selection.$head.marks() |
| 136 | if (before.length === now.length && before.every(mark => mark.isInSet(now))) return null |
| 137 | return state.tr.setStoredMarks(before) |
| 138 | } |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | export function rules (schema) { |
| 143 | return [ |
| 144 | inputRules({ |
| 145 | rules: [headingRule(schema), bulletRule(schema), orderedRule(schema), |
| 146 | taskRule(schema), ruleRule(schema), quoteRule(schema), fenceRule(schema), |
| 147 | leadRule()] |
| 148 | }), |
| 149 | markRule() |
| 150 | ] |
| 151 | } |
| 152 | |