|
| 1 | +// Ported from https://github.com/waylonflinn/markdown-it-katex |
| 2 | + |
| 3 | +/* Process inline math */ |
| 4 | +/* |
| 5 | +Like markdown-it-simplemath, this is a stripped down, simplified version of: |
| 6 | +https://github.com/runarberg/markdown-it-math |
| 7 | +
|
| 8 | +It differs in that it takes (a subset of) LaTeX as input and relies on KaTeX |
| 9 | +for rendering output. |
| 10 | +*/ |
| 11 | + |
| 12 | +import katex, { KatexOptions } from 'katex' |
| 13 | + |
| 14 | +// Test if potential opening or closing delimieter |
| 15 | +// Assumes that there is a "$" at state.src[pos] |
| 16 | +function isValidDelim(state: any, pos: number) { |
| 17 | + const max = state.posMax |
| 18 | + let can_open = true |
| 19 | + let can_close = true |
| 20 | + |
| 21 | + const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1 |
| 22 | + const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1 |
| 23 | + |
| 24 | + // Check non-whitespace conditions for opening and closing, and |
| 25 | + // check that closing delimeter isn't followed by a number |
| 26 | + if (prevChar === 0x20/* " " */ || prevChar === 0x09 |
| 27 | + ||/* \t */ (nextChar >= 0x30/* "0" */ && nextChar <= 0x39/* "9" */)) |
| 28 | + can_close = false |
| 29 | + |
| 30 | + if (nextChar === 0x20/* " " */ || nextChar === 0x09/* \t */) |
| 31 | + can_open = false |
| 32 | + |
| 33 | + return { |
| 34 | + can_open, |
| 35 | + can_close, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function math_inline(state: any, silent: boolean) { |
| 40 | + let match, token, res, pos |
| 41 | + |
| 42 | + if (state.src[state.pos] !== '$') return false |
| 43 | + |
| 44 | + res = isValidDelim(state, state.pos) |
| 45 | + if (!res.can_open) { |
| 46 | + if (!silent) state.pending += '$' |
| 47 | + state.pos += 1 |
| 48 | + return true |
| 49 | + } |
| 50 | + |
| 51 | + // First check for and bypass all properly escaped delimieters |
| 52 | + // This loop will assume that the first leading backtick can not |
| 53 | + // be the first character in state.src, which is known since |
| 54 | + // we have found an opening delimieter already. |
| 55 | + const start = state.pos + 1 |
| 56 | + match = start |
| 57 | + // eslint-disable-next-line no-cond-assign |
| 58 | + while ((match = state.src.indexOf('$', match)) !== -1) { |
| 59 | + // Found potential $, look for escapes, pos will point to |
| 60 | + // first non escape when complete |
| 61 | + pos = match - 1 |
| 62 | + while (state.src[pos] === '\\') pos -= 1 |
| 63 | + |
| 64 | + // Even number of escapes, potential closing delimiter found |
| 65 | + if (((match - pos) % 2) === 1) break |
| 66 | + match += 1 |
| 67 | + } |
| 68 | + |
| 69 | + // No closing delimter found. Consume $ and continue. |
| 70 | + if (match === -1) { |
| 71 | + if (!silent) state.pending += '$' |
| 72 | + state.pos = start |
| 73 | + return true |
| 74 | + } |
| 75 | + |
| 76 | + // Check if we have empty content, ie: $$. Do not parse. |
| 77 | + if (match - start === 0) { |
| 78 | + if (!silent) state.pending += '$$' |
| 79 | + state.pos = start + 1 |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + // Check for valid closing delimiter |
| 84 | + res = isValidDelim(state, match) |
| 85 | + if (!res.can_close) { |
| 86 | + if (!silent) state.pending += '$' |
| 87 | + state.pos = start |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + if (!silent) { |
| 92 | + token = state.push('math_inline', 'math', 0) |
| 93 | + token.markup = '$' |
| 94 | + token.content = state.src.slice(start, match) |
| 95 | + } |
| 96 | + |
| 97 | + state.pos = match + 1 |
| 98 | + return true |
| 99 | +} |
| 100 | + |
| 101 | +function math_block(state: any, start: number, end: number, silent: boolean) { |
| 102 | + let firstLine; let lastLine; let next; let lastPos |
| 103 | + let found = false |
| 104 | + let pos = state.bMarks[start] + state.tShift[start] |
| 105 | + let max = state.eMarks[start] |
| 106 | + |
| 107 | + if (pos + 2 > max) return false |
| 108 | + if (state.src.slice(pos, pos + 2) !== '$$') return false |
| 109 | + |
| 110 | + pos += 2 |
| 111 | + firstLine = state.src.slice(pos, max) |
| 112 | + |
| 113 | + if (silent) return true |
| 114 | + if (firstLine.trim().slice(-2) === '$$') { |
| 115 | + // Single line expression |
| 116 | + firstLine = firstLine.trim().slice(0, -2) |
| 117 | + found = true |
| 118 | + } |
| 119 | + |
| 120 | + for (next = start; !found;) { |
| 121 | + next++ |
| 122 | + |
| 123 | + if (next >= end) break |
| 124 | + |
| 125 | + pos = state.bMarks[next] + state.tShift[next] |
| 126 | + max = state.eMarks[next] |
| 127 | + |
| 128 | + if (pos < max && state.tShift[next] < state.blkIndent) { |
| 129 | + // non-empty line with negative indent should stop the list: |
| 130 | + break |
| 131 | + } |
| 132 | + |
| 133 | + if (state.src.slice(pos, max).trim().slice(-2) === '$$') { |
| 134 | + lastPos = state.src.slice(0, max).lastIndexOf('$$') |
| 135 | + lastLine = state.src.slice(pos, lastPos) |
| 136 | + found = true |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + state.line = next + 1 |
| 141 | + |
| 142 | + const token = state.push('math_block', 'math', 0) |
| 143 | + token.block = true |
| 144 | + token.content = (firstLine && firstLine.trim() ? `${firstLine}\n` : '') |
| 145 | + + state.getLines(start + 1, next, state.tShift[start], true) |
| 146 | + + (lastLine && lastLine.trim() ? lastLine : '') |
| 147 | + token.map = [start, state.line] |
| 148 | + token.markup = '$$' |
| 149 | + return true |
| 150 | +} |
| 151 | + |
| 152 | +export default function math_plugin(md: any, options: KatexOptions) { |
| 153 | + // Default options |
| 154 | + |
| 155 | + options = options || {} |
| 156 | + |
| 157 | + // set KaTeX as the renderer for markdown-it-simplemath |
| 158 | + const katexInline = function(latex: string) { |
| 159 | + options.displayMode = false |
| 160 | + try { |
| 161 | + return katex.renderToString(latex, options) |
| 162 | + } |
| 163 | + catch (error) { |
| 164 | + if (options.throwOnError) |
| 165 | + // eslint-disable-next-line no-console |
| 166 | + console.warn(error) |
| 167 | + return latex |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + const inlineRenderer = function(tokens: any, idx: number) { |
| 172 | + return katexInline(tokens[idx].content) |
| 173 | + } |
| 174 | + |
| 175 | + const katexBlock = function(latex: string) { |
| 176 | + options.displayMode = true |
| 177 | + try { |
| 178 | + return `<p>${katex.renderToString(latex, options)}</p>` |
| 179 | + } |
| 180 | + catch (error) { |
| 181 | + if (options.throwOnError) |
| 182 | + // eslint-disable-next-line no-console |
| 183 | + console.warn(error) |
| 184 | + return latex |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + const blockRenderer = function(tokens: any, idx: number) { |
| 189 | + return `${katexBlock(tokens[idx].content)}\n` |
| 190 | + } |
| 191 | + |
| 192 | + md.inline.ruler.after('escape', 'math_inline', math_inline) |
| 193 | + md.block.ruler.after('blockquote', 'math_block', math_block, { |
| 194 | + alt: ['paragraph', 'reference', 'blockquote', 'list'], |
| 195 | + }) |
| 196 | + md.renderer.rules.math_inline = inlineRenderer |
| 197 | + md.renderer.rules.math_block = blockRenderer |
| 198 | +} |
0 commit comments