mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-07-07 22:28:01 +02:00
fix(#263): render math expression correctly
This commit is contained in:
Vendored
+52
-3
File diff suppressed because one or more lines are too long
+41
-16
@@ -117,25 +117,36 @@ marked.use(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
function renderMD(md, baseUrl) {
|
function renderMD(md, baseUrlValue) {
|
||||||
md = contentAbs2Relative(md);
|
md = contentAbs2Relative(md);
|
||||||
const renderer = new marked.Renderer();
|
const renderer = new marked.Renderer();
|
||||||
// katex
|
|
||||||
function mathsExpression(expr) {
|
const replacer = ((blockRegex, inlineRegex) => (text) => {
|
||||||
if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
|
text = text.replace(blockRegex, (match, expression) => {
|
||||||
expr = expr.substr(2, expr.length - 4);
|
return katex.renderToString(expression, { displayMode: true });
|
||||||
return katex.renderToString(expr, { displayMode: true });
|
});
|
||||||
} else if (expr.match(/^\$[\s\S]*\$$/)) {
|
|
||||||
expr = expr.substr(1, expr.length - 2);
|
text = text.replace(inlineRegex, (match, expression) => {
|
||||||
return katex.renderToString(expr, { isplayMode: false });
|
return katex.renderToString(expression, { displayMode: false });
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
return text;
|
||||||
|
})(/\$\$([\s\S]+?)\$\$/g, /\$([^\n\s]+?)\$/g);
|
||||||
|
|
||||||
|
const replaceTypes = ["listitems", "paragraph", "tablecell", "text"];
|
||||||
|
replaceTypes.forEach((type) => {
|
||||||
|
const original = renderer[type];
|
||||||
|
renderer[type] = (...args) => {
|
||||||
|
args[0] = replacer(args[0]);
|
||||||
|
return original(...args);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const rendererCode = renderer.code;
|
const rendererCode = renderer.code;
|
||||||
renderer.code = function (code, lang, escaped) {
|
renderer.code = function (code, lang, escaped) {
|
||||||
if (!lang) {
|
if (!lang) {
|
||||||
const math = mathsExpression(code);
|
const math = replacer(code);
|
||||||
if (math) {
|
if (math != text) {
|
||||||
return math;
|
return math;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,8 +156,8 @@ function renderMD(md, baseUrl) {
|
|||||||
|
|
||||||
const rendererCodespan = renderer.codespan;
|
const rendererCodespan = renderer.codespan;
|
||||||
renderer.codespan = function (text) {
|
renderer.codespan = function (text) {
|
||||||
const math = mathsExpression(text);
|
const math = replacer(text);
|
||||||
if (math) {
|
if (math != text) {
|
||||||
return math;
|
return math;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,5 +172,19 @@ function renderMD(md, baseUrl) {
|
|||||||
}
|
}
|
||||||
return rendererLink.call(this, href, title, text);
|
return rendererLink.call(this, href, title, text);
|
||||||
};
|
};
|
||||||
return marked.parse(md, { baseUrl, renderer });
|
|
||||||
|
marked.setOptions({
|
||||||
|
renderer: renderer,
|
||||||
|
pedantic: false,
|
||||||
|
gfm: true,
|
||||||
|
breaks: false,
|
||||||
|
sanitize: false,
|
||||||
|
smartLists: true,
|
||||||
|
smartypants: false,
|
||||||
|
xhtml: false,
|
||||||
|
headerIds: false,
|
||||||
|
katex: katex,
|
||||||
|
});
|
||||||
|
marked.use(baseUrl(baseUrlValue));
|
||||||
|
return marked.parse(md, { renderer });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user