mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-09 15:24:13 +01:00
refactor: Optimize renderKatex
This commit is contained in:
@@ -80,6 +80,7 @@
|
|||||||
"logmsg",
|
"logmsg",
|
||||||
"lucida",
|
"lucida",
|
||||||
"markdownish",
|
"markdownish",
|
||||||
|
"mathml",
|
||||||
"matthieu",
|
"matthieu",
|
||||||
"matthieumorel",
|
"matthieumorel",
|
||||||
"mdast",
|
"mdast",
|
||||||
|
|||||||
@@ -91,6 +91,7 @@
|
|||||||
"@types/d3-shape": "^3.1.1",
|
"@types/d3-shape": "^3.1.1",
|
||||||
"@types/dompurify": "^3.0.2",
|
"@types/dompurify": "^3.0.2",
|
||||||
"@types/jsdom": "^21.1.1",
|
"@types/jsdom": "^21.1.1",
|
||||||
|
"@types/katex": "^0.16.7",
|
||||||
"@types/lodash-es": "^4.17.7",
|
"@types/lodash-es": "^4.17.7",
|
||||||
"@types/micromatch": "^4.0.2",
|
"@types/micromatch": "^4.0.2",
|
||||||
"@types/prettier": "^2.7.2",
|
"@types/prettier": "^2.7.2",
|
||||||
|
|||||||
@@ -318,33 +318,37 @@ export const calculateMathMLDimensions = async (text: string, config: MermaidCon
|
|||||||
* @returns String containing MathML if KaTeX is supported, or an error message if it is not and stylesheets aren't present
|
* @returns String containing MathML if KaTeX is supported, or an error message if it is not and stylesheets aren't present
|
||||||
*/
|
*/
|
||||||
export const renderKatex = async (text: string, config: MermaidConfig): Promise<string> => {
|
export const renderKatex = async (text: string, config: MermaidConfig): Promise<string> => {
|
||||||
if (hasKatex(text) && (isMathMLSupported() || config.legacyMathML)) {
|
if (!hasKatex(text)) {
|
||||||
// @ts-ignore @types/katex does not work
|
return text;
|
||||||
const katex = (await import('katex')).default;
|
}
|
||||||
return text
|
|
||||||
.split(lineBreakRegex)
|
if (!isMathMLSupported() && !config.legacyMathML) {
|
||||||
.map((line) =>
|
return text.replace(katexRegex, 'MathML is unsupported in this environment.');
|
||||||
hasKatex(line)
|
}
|
||||||
? `
|
|
||||||
|
const { default: katex } = await import('katex');
|
||||||
|
return text
|
||||||
|
.split(lineBreakRegex)
|
||||||
|
.map((line) =>
|
||||||
|
hasKatex(line)
|
||||||
|
? `
|
||||||
<div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">
|
<div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">
|
||||||
${line}
|
${line}
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
: `<div>${line}</div>`
|
: `<div>${line}</div>`
|
||||||
)
|
)
|
||||||
.join('')
|
.join('')
|
||||||
.replace(katexRegex, (r, c) =>
|
.replace(katexRegex, (_, c) =>
|
||||||
katex
|
katex
|
||||||
.renderToString(c, {
|
.renderToString(c, {
|
||||||
throwOnError: true,
|
throwOnError: true,
|
||||||
displayMode: true,
|
displayMode: true,
|
||||||
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
|
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
|
||||||
})
|
})
|
||||||
.replace(/\n/g, ' ')
|
.replace(/\n/g, ' ')
|
||||||
.replace(/<annotation.*<\/annotation>/g, '')
|
.replace(/<annotation.*<\/annotation>/g, '')
|
||||||
);
|
);
|
||||||
}
|
|
||||||
return text.replace(katexRegex, 'MathML is unsupported in this environment.');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@@ -279,6 +279,9 @@ importers:
|
|||||||
'@types/jsdom':
|
'@types/jsdom':
|
||||||
specifier: ^21.1.1
|
specifier: ^21.1.1
|
||||||
version: 21.1.1
|
version: 21.1.1
|
||||||
|
'@types/katex':
|
||||||
|
specifier: ^0.16.7
|
||||||
|
version: 0.16.7
|
||||||
'@types/lodash-es':
|
'@types/lodash-es':
|
||||||
specifier: ^4.17.7
|
specifier: ^4.17.7
|
||||||
version: 4.17.7
|
version: 4.17.7
|
||||||
@@ -4944,6 +4947,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==}
|
resolution: {integrity: sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/@types/katex@0.16.7:
|
||||||
|
resolution: {integrity: sha512-HMwFiRujE5PjrgwHQ25+bsLJgowjGjm5Z8FVSf0N6PwgJrwxH0QxzHYDcKsTfV3wva0vzrpqMTJS2jXPr5BMEQ==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/@types/keyv@3.1.4:
|
/@types/keyv@3.1.4:
|
||||||
resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==}
|
resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==}
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|||||||
Reference in New Issue
Block a user