forked from yzhang-gh/markdown-it-katex
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (27 loc) · 942 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const katex = require('katex');
const inline = require('./lib/inline');
const block = require('./lib/block');
function markdownItKatexPlugin(md, options) {
options = options || {};
const render = (latex, displayMode = false) => {
try {
return katex.renderToString(latex, { ...options, displayMode });
} catch (error) {
if (options.throwOnError) {
console.log(error);
}
return latex;
}
};
md.inline.ruler.after('escape', 'math_inline', (state, silent) =>
inline(state, silent, options.skipDelimitersCheck)
);
md.block.ruler.after('blockquote', 'math_block', block, {
alt: ['paragraph', 'reference', 'blockquote', 'list'],
});
md.renderer.rules.math_inline = (tokens, idx) => render(tokens[idx].content);
md.renderer.rules.math_block = (tokens, idx) =>
'<p>' + render(tokens[idx].content, true) + '</p>\n';
}
module.exports = markdownItKatexPlugin;