From b5240b5010d15ef136c8d1b5aefe4971cccc0deb Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 26 Jan 2024 02:32:20 +0800 Subject: [PATCH] ui: katex: show render errors --- packages/ui-default/backendlib/markdown-it-katex.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/ui-default/backendlib/markdown-it-katex.js b/packages/ui-default/backendlib/markdown-it-katex.js index 5e1ebff26..facbb97c6 100644 --- a/packages/ui-default/backendlib/markdown-it-katex.js +++ b/packages/ui-default/backendlib/markdown-it-katex.js @@ -61,6 +61,15 @@ function inline(state, silent) { return true; } +function escapeHtml(unsafe) { + return unsafe + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + function block(state, start, end, silent) { let lastLine; let lastPos; @@ -109,7 +118,7 @@ module.exports = function plugin(md) { return katex.renderToString(latex, options); } catch (error) { if (options.throwOnError) logger.error(error); - return latex; + return `
${escapeHtml(latex)}
`; } }; const inlineRenderer = function (tokens, idx) { @@ -122,7 +131,7 @@ module.exports = function plugin(md) { return `${katex.renderToString(latex, options)}
`; } catch (error) { if (options.throwOnError) logger.error(error); - return latex; + return `${escapeHtml(latex)}
`; } };