Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing of options to Katex renderer #19

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Use it with JavaScript.
tm = require('markdown-it-texmath'),
md = require('markdown-it')().use(tm, { engine: require('katex'),
delimiters:'dollars',
macros:{"\\RR": "\\mathbb{R}"}
katexOptions: { macros: {"\\RR": "\\mathbb{R}"} }
});

md.render('Euler\'s identity \(e^{i\pi}+1=0\) is a beautiful formula in $\\RR 2$.')
Expand All @@ -81,7 +81,7 @@ md.render('Euler\'s identity \(e^{i\pi}+1=0\) is a beautiful formula in $\\RR 2$
const tm = texmath.use(katex);
const md = markdownit().use(tm, { engine: katex,
delimiters:'dollars',
macros:{"\\RR": "\\mathbb{R}"}
katexOptions: { macros: {"\\RR": "\\mathbb{R}"} }
});
document.getElementById('out').innerHTML =
md.render('Euler\'s identity $e^{i\pi}+1=0$ is a beautiful formula in //RR 2.');
Expand Down
14 changes: 8 additions & 6 deletions texmath.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
'use strict';

function texmath(md, options) {
let delimiters = options && options.delimiters || 'dollars',
macros = options && options.macros;
let delimiters = options && options.delimiters || 'dollars';
let katexOptions = options && options.katexOptions || { throwOnError: false };
katexOptions.macros = options && options.macros || katexOptions.macros; // ensure backwards compatibility

if (!texmath.katex) { // else ... depricated `use` method was used ...
if (options && typeof options.engine === 'object') {
Expand All @@ -21,13 +22,13 @@ function texmath(md, options) {
if (delimiters in texmath.rules) {
for (let rule of texmath.rules[delimiters].inline) {
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,false,macros));
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,false,katexOptions));
}

for (let rule of texmath.rules[delimiters].block) {
md.block.ruler.before('fence', rule.name, texmath.block(rule));
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,tokens[idx].info) // equation number .. ?
.replace(/\$1/,texmath.render(tokens[idx].content,true,macros));
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
}
}
}
Expand Down Expand Up @@ -85,10 +86,11 @@ texmath.block = (rule) =>
return !!res;
}

texmath.render = function(tex,displayMode,macros) {
texmath.render = function(tex,displayMode,options) {
options.displayMode = displayMode;
let res;
try {
res = texmath.katex.renderToString(tex,{throwOnError:false,displayMode,macros});
res = texmath.katex.renderToString(tex, options);
}
catch(err) {
res = tex+": "+err.message.replace("<","&lt;");
Expand Down