ui: optimize markdown render

This commit is contained in:
undefined committed 2024-03-07 03:57:21 +08:00
1 parent a56b1650be
commit f4a66118be
1 file changed
+7 -15
@@ -111,32 +111,24 @@ function block(state, start, end, silent) {
module.exports = function plugin(md) { module.exports = function plugin(md) {
const options = { throwOnError: false, strict: 'ignore' }; const options = { throwOnError: false, strict: 'ignore' };
const katexInline = function (latex) { const render = (latex: string, displayMode = false) => {
options.displayMode = false; options.displayMode = displayMode;
try { try {
latex = latex.replace(/\\def{\\([a-zA-Z0-9]+)}/g, '\\def\\$1'); latex = latex.replace(/\\def{\\([a-zA-Z0-9]+)}/g, '\\def\\$1');
return katex.renderToString(latex, options); return katex.renderToString(latex, options);
} catch (error) { } catch (error) {
if (options.throwOnError) logger.error(error); if (options.throwOnError) logger.error(error);
return `<p class='katex-error' title='${escapeHtml(error.toString())}'>${escapeHtml(latex)}</p>`; return `<p class='${displayMode ? 'katex-block ' : ''}katex-error' title='${escapeHtml(error.toString())}'>${escapeHtml(latex)}</p>`;
} }
}; };
const inlineRenderer = function (tokens, idx) { const inlineRenderer = function (tokens, idx) {
return katexInline(tokens[idx].content); if (tokens[idx].content.length > 50) return `$${tokens[idx].content}$`;
}; return render(tokens[idx].content);
const katexBlock = function (latex) {
options.displayMode = true;
try {
latex = latex.replace(/\\def{\\([a-zA-Z0-9]+)}/g, '\\def\\$1');
return `<p>${katex.renderToString(latex, options)}</p>`;
} catch (error) {
if (options.throwOnError) logger.error(error);
return `<p class='katex-block katex-error' title='${escapeHtml(error.toString())}'>${escapeHtml(latex)}</p > `;
}
}; };
const blockRenderer = function (tokens, idx) { 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.inline.ruler.after('escape', 'math_inline', inline);
md.block.ruler.after('blockquote', 'math_block', block, { md.block.ruler.after('blockquote', 'math_block', block, {