|
| 1 | +import MarkdownIt from 'markdown-it'; |
| 2 | +import StateInline from 'markdown-it/lib/rules_inline/state_inline'; |
| 3 | +import StateBlock from 'markdown-it/lib/rules_block/state_block'; |
| 4 | +import { TeX } from 'mathjax-full/js/input/tex'; |
| 5 | +import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages'; |
| 6 | +import { SVG } from 'mathjax-full/js/output/svg'; |
| 7 | +import Token from 'markdown-it/lib/token'; |
| 8 | +import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor'; |
| 9 | +import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html'; |
| 10 | +import { AssistiveMmlHandler } from 'mathjax-full/js/a11y/assistive-mml'; |
| 11 | +import { mathjax } from 'mathjax-full/js/mathjax'; |
| 12 | +import juice from 'juice'; |
| 13 | +import { SafeAny } from './utils'; |
| 14 | +import { MathJaxOutputType } from './plugin-settings'; |
| 15 | + |
| 16 | + |
| 17 | +interface MarkdownItMathJax3PluginOptions { |
| 18 | + outputType: MathJaxOutputType; |
| 19 | +} |
| 20 | + |
| 21 | +interface ConvertOptions { |
| 22 | + display: boolean |
| 23 | +} |
| 24 | + |
| 25 | +export default function MarkdownItMathJax3Plugin(md: MarkdownIt, options: MarkdownItMathJax3PluginOptions): void { |
| 26 | + // set MathJax as the renderer for markdown-it-simplemath |
| 27 | + md.inline.ruler.after('escape', 'math_inline', mathInline); |
| 28 | + md.block.ruler.after('blockquote', 'math_block', mathBlock, { |
| 29 | + alt: ['paragraph', 'reference', 'blockquote', 'list'], |
| 30 | + }); |
| 31 | + md.renderer.rules.math_inline = (tokens: Token[], idx: number) => { |
| 32 | + return renderMath(tokens[idx].content, { |
| 33 | + display: false |
| 34 | + }, options); |
| 35 | + }; |
| 36 | + md.renderer.rules.math_block = (tokens: Token[], idx: number) => { |
| 37 | + return renderMath(tokens[idx].content, { |
| 38 | + display: true |
| 39 | + }, options); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function renderMath(content: string, convertOptions: ConvertOptions, options: MarkdownItMathJax3PluginOptions): string { |
| 44 | + if (options.outputType === MathJaxOutputType.SVG) { |
| 45 | + const documentOptions = { |
| 46 | + InputJax: new TeX({ packages: AllPackages }), |
| 47 | + OutputJax: new SVG({ fontCache: 'none' }) |
| 48 | + }; |
| 49 | + const adaptor = liteAdaptor(); |
| 50 | + const handler = RegisterHTMLHandler(adaptor); |
| 51 | + AssistiveMmlHandler(handler); |
| 52 | + const mathDocument = mathjax.document(content, documentOptions); |
| 53 | + const html = adaptor.outerHTML(mathDocument.convert(content, convertOptions)); |
| 54 | + const stylesheet = adaptor.outerHTML(documentOptions.OutputJax.styleSheet(mathDocument) as SafeAny); |
| 55 | + return juice(html + stylesheet); |
| 56 | + } else { |
| 57 | + if (convertOptions.display) { |
| 58 | + return `$$\n${content}$$\n`; |
| 59 | + } else { |
| 60 | + return `$${content}$`; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Test if potential opening or closing delimiter |
| 66 | +// Assumes that there is a '$' at state.src[pos] |
| 67 | +function isValidDelimiter(state: StateInline, pos: number) { |
| 68 | + const max = state.posMax; |
| 69 | + let canOpen = true; |
| 70 | + let canClose = true; |
| 71 | + |
| 72 | + const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1; |
| 73 | + const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1; |
| 74 | + |
| 75 | + // Check non-whitespace conditions for opening and closing, and |
| 76 | + // check that closing delimiter isn't followed by a number |
| 77 | + if (prevChar === 0x20 /* ' ' */ |
| 78 | + || prevChar === 0x09 /* \t */ |
| 79 | + || (nextChar >= 0x30 /* '0' */ && nextChar <= 0x39) /* '9' */ |
| 80 | + ) { |
| 81 | + canClose = false; |
| 82 | + } |
| 83 | + if (nextChar === 0x20 /* ' ' */ || nextChar === 0x09 /* \t */) { |
| 84 | + canOpen = false; |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + canOpen, |
| 89 | + canClose |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +function mathInline(state: StateInline, silent: boolean) { |
| 94 | + if (state.src[state.pos] !== '$') { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + let res = isValidDelimiter(state, state.pos); |
| 99 | + if (!res.canOpen) { |
| 100 | + if (!silent) { |
| 101 | + state.pending += '$'; |
| 102 | + } |
| 103 | + state.pos += 1; |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + // First check for and bypass all properly escaped delimiters |
| 108 | + // This loop will assume that the first leading backtick can not |
| 109 | + // be the first character in state.src, which is known since |
| 110 | + // we have found an opening delimiter already. |
| 111 | + const start = state.pos + 1; |
| 112 | + let match = start; |
| 113 | + while ((match = state.src.indexOf('$', match)) !== -1) { |
| 114 | + // Found potential $, look for escapes, pos will point to |
| 115 | + // first non escape when complete |
| 116 | + let pos = match - 1; |
| 117 | + while (state.src[pos] === '\\') { |
| 118 | + pos -= 1; |
| 119 | + } |
| 120 | + |
| 121 | + // Even number of escapes, potential closing delimiter found |
| 122 | + if ((match - pos) % 2 == 1) { |
| 123 | + break; |
| 124 | + } |
| 125 | + match += 1; |
| 126 | + } |
| 127 | + |
| 128 | + // No closing delimter found. Consume $ and continue. |
| 129 | + if (match === -1) { |
| 130 | + if (!silent) { |
| 131 | + state.pending += '$'; |
| 132 | + } |
| 133 | + state.pos = start; |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + // Check if we have empty content, ie: $$. Do not parse. |
| 138 | + if (match - start === 0) { |
| 139 | + if (!silent) { |
| 140 | + state.pending += '$$'; |
| 141 | + } |
| 142 | + state.pos = start + 1; |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + // Check for valid closing delimiter |
| 147 | + res = isValidDelimiter(state, match); |
| 148 | + if (!res.canClose) { |
| 149 | + if (!silent) { |
| 150 | + state.pending += '$'; |
| 151 | + } |
| 152 | + state.pos = start; |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + if (!silent) { |
| 157 | + const token = state.push('math_inline', 'math', 0); |
| 158 | + token.markup = '$'; |
| 159 | + token.content = state.src.slice(start, match); |
| 160 | + } |
| 161 | + |
| 162 | + state.pos = match + 1; |
| 163 | + return true; |
| 164 | +} |
| 165 | + |
| 166 | +function mathBlock(state: StateBlock, start: number, end: number, silent: boolean) { |
| 167 | + let next: number; |
| 168 | + let lastPos: number; |
| 169 | + let found = false; |
| 170 | + let pos = state.bMarks[start] + state.tShift[start]; |
| 171 | + let max = state.eMarks[start]; |
| 172 | + let lastLine = ''; |
| 173 | + |
| 174 | + if (pos + 2 > max) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + if (state.src.slice(pos, pos + 2) !== '$$') { |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + pos += 2; |
| 182 | + let firstLine = state.src.slice(pos, max); |
| 183 | + |
| 184 | + if (silent) { |
| 185 | + return true; |
| 186 | + } |
| 187 | + if (firstLine.trim().slice(-2) === '$$') { |
| 188 | + // Single line expression |
| 189 | + firstLine = firstLine.trim().slice(0, -2); |
| 190 | + found = true; |
| 191 | + } |
| 192 | + |
| 193 | + for (next = start; !found; ) { |
| 194 | + next++; |
| 195 | + |
| 196 | + if (next >= end) { |
| 197 | + break; |
| 198 | + } |
| 199 | + |
| 200 | + pos = state.bMarks[next] + state.tShift[next]; |
| 201 | + max = state.eMarks[next]; |
| 202 | + |
| 203 | + if (pos < max && state.tShift[next] < state.blkIndent) { |
| 204 | + // non-empty line with negative indent should stop the list: |
| 205 | + break; |
| 206 | + } |
| 207 | + |
| 208 | + if (state.src.slice(pos, max).trim().slice(-2) === '$$') { |
| 209 | + lastPos = state.src.slice(0, max).lastIndexOf('$$'); |
| 210 | + lastLine = state.src.slice(pos, lastPos); |
| 211 | + found = true; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + state.line = next + 1; |
| 216 | + |
| 217 | + const token = state.push('math_block', 'math', 0); |
| 218 | + token.block = true; |
| 219 | + token.content = |
| 220 | + (firstLine && firstLine.trim() ? firstLine + '\n' : '') + |
| 221 | + state.getLines(start + 1, next, state.tShift[start], true) + |
| 222 | + (lastLine && lastLine.trim() ? lastLine : ''); |
| 223 | + token.map = [start, state.line]; |
| 224 | + token.markup = '$$'; |
| 225 | + return true; |
| 226 | +} |
0 commit comments