|
| 1 | +/** |
| 2 | + * @typedef {import('mdast').FootnoteReference} FootnoteReference |
| 3 | + * @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition |
| 4 | + * @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension |
| 5 | + * @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle |
| 6 | + * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension |
| 7 | + * @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle |
| 8 | + * @typedef {import('mdast-util-to-markdown').Map} Map |
| 9 | + */ |
| 10 | + |
| 11 | +import {normalizeIdentifier} from 'micromark-util-normalize-identifier' |
| 12 | +import {association} from 'mdast-util-to-markdown/lib/util/association.js' |
| 13 | +import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js' |
| 14 | +import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js' |
| 15 | +import {safe} from 'mdast-util-to-markdown/lib/util/safe.js' |
| 16 | +import {visit, EXIT} from 'unist-util-visit' |
| 17 | + |
| 18 | +let warningColonInFootnote = false |
| 19 | +let warningListInFootnote = false |
| 20 | + |
| 21 | +/** |
| 22 | + * @returns {FromMarkdownExtension} |
| 23 | + */ |
| 24 | +export function gfmFootnoteFromMarkdown() { |
| 25 | + return { |
| 26 | + enter: { |
| 27 | + gfmFootnoteDefinition: enterFootnoteDefinition, |
| 28 | + gfmFootnoteDefinitionLabelString: enterFootnoteDefinitionLabelString, |
| 29 | + gfmFootnoteCall: enterFootnoteCall, |
| 30 | + gfmFootnoteCallString: enterFootnoteCallString |
| 31 | + }, |
| 32 | + exit: { |
| 33 | + gfmFootnoteDefinition: exitFootnoteDefinition, |
| 34 | + gfmFootnoteDefinitionLabelString: exitFootnoteDefinitionLabelString, |
| 35 | + gfmFootnoteCall: exitFootnoteCall, |
| 36 | + gfmFootnoteCallString: exitFootnoteCallString |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** @type {FromMarkdownHandle} */ |
| 41 | + function enterFootnoteDefinition(token) { |
| 42 | + this.enter( |
| 43 | + {type: 'footnoteDefinition', identifier: '', label: '', children: []}, |
| 44 | + token |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + /** @type {FromMarkdownHandle} */ |
| 49 | + function enterFootnoteDefinitionLabelString() { |
| 50 | + this.buffer() |
| 51 | + } |
| 52 | + |
| 53 | + /** @type {FromMarkdownHandle} */ |
| 54 | + function exitFootnoteDefinitionLabelString(token) { |
| 55 | + const label = this.resume() |
| 56 | + const node = /** @type {FootnoteDefinition} */ ( |
| 57 | + this.stack[this.stack.length - 1] |
| 58 | + ) |
| 59 | + node.label = label |
| 60 | + node.identifier = normalizeIdentifier( |
| 61 | + this.sliceSerialize(token) |
| 62 | + ).toLowerCase() |
| 63 | + } |
| 64 | + |
| 65 | + /** @type {FromMarkdownHandle} */ |
| 66 | + function exitFootnoteDefinition(token) { |
| 67 | + this.exit(token) |
| 68 | + } |
| 69 | + |
| 70 | + /** @type {FromMarkdownHandle} */ |
| 71 | + function enterFootnoteCall(token) { |
| 72 | + this.enter({type: 'footnoteReference', identifier: '', label: ''}, token) |
| 73 | + } |
| 74 | + |
| 75 | + /** @type {FromMarkdownHandle} */ |
| 76 | + function enterFootnoteCallString() { |
| 77 | + this.buffer() |
| 78 | + } |
| 79 | + |
| 80 | + /** @type {FromMarkdownHandle} */ |
| 81 | + function exitFootnoteCallString(token) { |
| 82 | + const label = this.resume() |
| 83 | + const node = /** @type {FootnoteDefinition} */ ( |
| 84 | + this.stack[this.stack.length - 1] |
| 85 | + ) |
| 86 | + node.label = label |
| 87 | + node.identifier = normalizeIdentifier( |
| 88 | + this.sliceSerialize(token) |
| 89 | + ).toLowerCase() |
| 90 | + } |
| 91 | + |
| 92 | + /** @type {FromMarkdownHandle} */ |
| 93 | + function exitFootnoteCall(token) { |
| 94 | + this.exit(token) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @returns {ToMarkdownExtension} |
| 100 | + */ |
| 101 | +export function gfmFootnoteToMarkdown() { |
| 102 | + footnoteReference.peek = footnoteReferencePeek |
| 103 | + |
| 104 | + return { |
| 105 | + // This is on by default already. |
| 106 | + unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}], |
| 107 | + handlers: {footnoteDefinition, footnoteReference} |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @type {ToMarkdownHandle} |
| 112 | + * @param {FootnoteReference} node |
| 113 | + */ |
| 114 | + function footnoteReference(node, _, context) { |
| 115 | + const exit = context.enter('footnoteReference') |
| 116 | + const subexit = context.enter('reference') |
| 117 | + const reference = safe(context, association(node), { |
| 118 | + before: '^', |
| 119 | + after: ']' |
| 120 | + }) |
| 121 | + subexit() |
| 122 | + exit() |
| 123 | + return '[^' + reference + ']' |
| 124 | + } |
| 125 | + |
| 126 | + /** @type {ToMarkdownHandle} */ |
| 127 | + function footnoteReferencePeek() { |
| 128 | + return '[' |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @type {ToMarkdownHandle} |
| 133 | + * @param {FootnoteDefinition} node |
| 134 | + */ |
| 135 | + function footnoteDefinition(node, _, context) { |
| 136 | + const exit = context.enter('footnoteDefinition') |
| 137 | + const subexit = context.enter('label') |
| 138 | + const id = safe(context, association(node), {before: '^', after: ']'}) |
| 139 | + const label = '[^' + id + ']:' |
| 140 | + subexit() |
| 141 | + const value = indentLines(containerFlow(node, context), map) |
| 142 | + exit() |
| 143 | + |
| 144 | + if (!warningColonInFootnote && id.includes(':')) { |
| 145 | + console.warn( |
| 146 | + '[mdast-util-gfm-footnote] Warning: Found a colon in footnote identifier `' + |
| 147 | + id + |
| 148 | + '`. GitHub currently crahes on colons in footnotes (see <https://github.com/github/cmark-gfm/issues/241> for more info)' |
| 149 | + ) |
| 150 | + warningColonInFootnote = true |
| 151 | + } |
| 152 | + |
| 153 | + if (!warningListInFootnote) { |
| 154 | + visit(node, 'list', () => { |
| 155 | + console.warn( |
| 156 | + '[mdast-util-gfm-footnote] Warning: Found a list in a footnote definition. GitHub currently crahes on lists in footnotes (see <https://github.com/github/cmark-gfm/issues/241> for more info)' |
| 157 | + ) |
| 158 | + warningListInFootnote = true |
| 159 | + return EXIT |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + return value |
| 164 | + |
| 165 | + /** @type {Map} */ |
| 166 | + function map(line, index, blank) { |
| 167 | + if (index) { |
| 168 | + return (blank ? '' : ' ') + line |
| 169 | + } |
| 170 | + |
| 171 | + return (blank ? label : label + ' ') + line |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments