diff --git a/packages/ui-default/backendlib/markdown-it-katex.js b/packages/ui-default/backendlib/markdown-it-katex.js index facbb97c6..20a2cc8fc 100644 --- a/packages/ui-default/backendlib/markdown-it-katex.js +++ b/packages/ui-default/backendlib/markdown-it-katex.js @@ -111,32 +111,24 @@ function block(state, start, end, silent) { module.exports = function plugin(md) { const options = { throwOnError: false, strict: 'ignore' }; - const katexInline = function (latex) { - options.displayMode = false; + const render = (latex: string, displayMode = false) => { + options.displayMode = displayMode; try { latex = latex.replace(/\\def{\\([a-zA-Z0-9]+)}/g, '\\def\\$1'); return katex.renderToString(latex, options); } catch (error) { if (options.throwOnError) logger.error(error); - return `
${escapeHtml(latex)}
`; + return `${escapeHtml(latex)}
`; } }; const inlineRenderer = function (tokens, idx) { - return katexInline(tokens[idx].content); - }; - const katexBlock = function (latex) { - options.displayMode = true; - try { - latex = latex.replace(/\\def{\\([a-zA-Z0-9]+)}/g, '\\def\\$1'); - return `${katex.renderToString(latex, options)}
`; - } catch (error) { - if (options.throwOnError) logger.error(error); - return `${escapeHtml(latex)}
`; - } + if (tokens[idx].content.length > 50) return `$${tokens[idx].content}$`; + return render(tokens[idx].content); }; const blockRenderer = function (tokens, idx) { - return `${katexBlock(tokens[idx].content)}\n`; + if (tokens[idx].content.length > 50) return `$$${tokens[idx].content}$$`; + return `${render(tokens[idx].content, true)}\n`; }; md.inline.ruler.after('escape', 'math_inline', inline); md.block.ruler.after('blockquote', 'math_block', block, {