Skip to content

Commit

Permalink
Remove katex from mermaid.tiny
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Mar 1, 2024
1 parent 3a12a63 commit 882e497
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .esbuild/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const buildPackage = async (entryName: keyof typeof packageOptions) => {
{
...iifeOptions,
minify: true,
includeLargeDiagrams: false,
includeLargeFeatures: false,
metafile: shouldVisualize,
}
);
Expand Down
12 changes: 6 additions & 6 deletions .esbuild/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export interface MermaidBuildOptions {
metafile: boolean;
format: 'esm' | 'iife';
entryName: keyof typeof packageOptions;
includeLargeDiagrams: boolean;
includeLargeFeatures: boolean;
}

export const defaultOptions: Omit<MermaidBuildOptions, 'entryName'> = {
minify: false,
metafile: false,
core: false,
format: 'esm',
includeLargeDiagrams: true,
includeLargeFeatures: true,
} as const;

const buildOptions = (override: BuildOptions): BuildOptions => {
Expand All @@ -43,14 +43,14 @@ const buildOptions = (override: BuildOptions): BuildOptions => {

const getFileName = (
fileName: string,
{ core, format, minify, includeLargeDiagrams }: MermaidBuildOptions
{ core, format, minify, includeLargeFeatures }: MermaidBuildOptions
) => {
if (core) {
fileName += '.core';
} else if (format === 'esm') {
fileName += '.esm';
}
if (!includeLargeDiagrams) {
if (!includeLargeFeatures) {
fileName += '.tiny';
}
if (minify) {
Expand All @@ -60,7 +60,7 @@ const getFileName = (
};

export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => {
const { core, entryName, metafile, format, includeLargeDiagrams, minify } = options;
const { core, entryName, metafile, format, includeLargeFeatures, minify } = options;
const external: string[] = ['require', 'fs', 'path'];
const { name, file, packageName } = packageOptions[entryName];
const outFileName = getFileName(name, options);
Expand All @@ -75,7 +75,7 @@ export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => {
chunkNames: `chunks/${outFileName}/[name]-[hash]`,
define: {
// This needs to be stringified for esbuild
includeLargeDiagrams: `${includeLargeDiagrams}`,
includeLargeFeatures: `${includeLargeFeatures}`,
'import.meta.vitest': 'undefined',
},
});
Expand Down
2 changes: 1 addition & 1 deletion .vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions)
],
define: {
// Needs to be string
includeLargeDiagrams: 'true',
includeLargeFeatures: 'true',
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagram-api/diagram-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const addDiagrams = () => {
}
);

if (includeLargeDiagrams) {
if (includeLargeFeatures) {
registerLazyLoadedDiagrams(flowchartElk, mindmap);
}

Expand Down
45 changes: 26 additions & 19 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,29 +341,36 @@ export const renderKatex = async (text: string, config: MermaidConfig): Promise<
return text.replace(katexRegex, 'MathML is unsupported in this environment.');
}

const { default: katex } = await import('katex');
return text
.split(lineBreakRegex)
.map((line) =>
hasKatex(line)
? `
if (includeLargeFeatures) {
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;">
${line}
</div>
`
: `<div>${line}</div>`
)
.join('')
.replace(katexRegex, (_, c) =>
katex
.renderToString(c, {
throwOnError: true,
displayMode: true,
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
})
.replace(/\n/g, ' ')
.replace(/<annotation.*<\/annotation>/g, '')
);
: `<div>${line}</div>`
)
.join('')
.replace(katexRegex, (_, c) =>
katex
.renderToString(c, {
throwOnError: true,
displayMode: true,
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
})
.replace(/\n/g, ' ')
.replace(/<annotation.*<\/annotation>/g, '')
);
}

return text.replace(
katexRegex,
'Katex is unsupported in mermaid.tiny.js. Please use mermaid.js or mermaid.min.js.'
);
};

export default {
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type { DiagramMetadata, DiagramStyleClassDef } from './diagram-api/types.
import { preprocessDiagram } from './preprocess.js';
import { decodeEntities } from './utils.js';

const version = packageVersion + (includeLargeDiagrams ? '' : '-tiny');
const version = packageVersion + (includeLargeFeatures ? '' : '-tiny');

const MAX_TEXTLENGTH = 50_000;
const MAX_TEXTLENGTH_EXCEEDED_MSG =
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line no-var
declare var includeLargeDiagrams: boolean;
declare var includeLargeFeatures: boolean;
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default defineConfig({
},
define: {
// Needs to be string
includeLargeDiagrams: 'true',
includeLargeFeatures: 'true',
'import.meta.vitest': 'undefined',
},
});

0 comments on commit 882e497

Please sign in to comment.