From e81fb8948ab6fc9f1fe740a9da64516de9751f77 Mon Sep 17 00:00:00 2001 From: Kirill Goncharov Date: Tue, 28 Apr 2020 22:57:14 +0300 Subject: [PATCH] Allow passing of options to Katex renderer --- readme.md | 4 ++-- texmath.js | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 7deeb1a..ea46951 100644 --- a/readme.md +++ b/readme.md @@ -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$.') @@ -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.'); diff --git a/texmath.js b/texmath.js index 09ec1c5..6b715d5 100644 --- a/texmath.js +++ b/texmath.js @@ -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') { @@ -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)); } } } @@ -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("<","<");