mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: optimize markdown render
This commit is contained in:
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, {
|
||||||
|
|||||||
Reference in new issue
Block a user