From 3b0687e5571c2091ce582200d2fd7e15222d3efc Mon Sep 17 00:00:00 2001 From: NicolasNewman Date: Mon, 22 Apr 2024 17:12:12 -0500 Subject: [PATCH] feat(mathml): added additional config option for forcing legacy rendering --- packages/mermaid/src/config.type.ts | 7 +++++++ packages/mermaid/src/diagrams/common/common.ts | 5 +++-- packages/mermaid/src/schemas/config.schema.yaml | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/config.type.ts b/packages/mermaid/src/config.type.ts index 79f424315..775d49fb5 100644 --- a/packages/mermaid/src/config.type.ts +++ b/packages/mermaid/src/config.type.ts @@ -120,6 +120,13 @@ export interface MermaidConfig { * */ legacyMathML?: boolean; + /** + * This option forces Mermaid to rely on KaTeX's own stylesheet for rendering MathML. Due to differences between OS + * fonts and browser's MathML implementation, this option is recommended if consistent rendering is important. + * If set to true, ignores legacyMathML. + * + */ + forceLegacyMathML?: boolean; /** * This option controls if the generated ids of nodes in the SVG are * generated randomly or based on a seed. diff --git a/packages/mermaid/src/diagrams/common/common.ts b/packages/mermaid/src/diagrams/common/common.ts index 017b2b091..364dc25ea 100644 --- a/packages/mermaid/src/diagrams/common/common.ts +++ b/packages/mermaid/src/diagrams/common/common.ts @@ -337,11 +337,12 @@ export const renderKatex = async (text: string, config: MermaidConfig): Promise< return text; } - if (!isMathMLSupported() && !config.legacyMathML) { + if (!(isMathMLSupported() || config.legacyMathML || config.forceLegacyMathML)) { return text.replace(katexRegex, 'MathML is unsupported in this environment.'); } const { default: katex } = await import('katex'); + const outputMode = (config.forceLegacyMathML) || (!isMathMLSupported() && config.legacyMathML) ? 'htmlAndMathml' : 'mathml'; return text .split(lineBreakRegex) .map((line) => @@ -357,7 +358,7 @@ export const renderKatex = async (text: string, config: MermaidConfig): Promise< .renderToString(c, { throwOnError: true, displayMode: true, - output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml', + output: outputMode }) .replace(/\n/g, ' ') .replace(//g, '') diff --git a/packages/mermaid/src/schemas/config.schema.yaml b/packages/mermaid/src/schemas/config.schema.yaml index d1bb8ad1b..68da484e0 100644 --- a/packages/mermaid/src/schemas/config.schema.yaml +++ b/packages/mermaid/src/schemas/config.schema.yaml @@ -180,6 +180,13 @@ properties: fall back to legacy rendering for KaTeX. type: boolean default: false + forceLegacyMathML: + description: | + This option forces Mermaid to rely on KaTeX's own stylesheet for rendering MathML. Due to differences between OS + fonts and browser's MathML implementation, this option is recommended if consistent rendering is important. + If set to true, ignores legacyMathML. + type: boolean + default: false deterministicIds: description: | This option controls if the generated ids of nodes in the SVG are