Remove katex from mermaid.tiny

This commit is contained in:
Sidharth Vinod
2024-03-01 12:54:42 +05:30
parent 3a12a63c32
commit 882e4971eb
8 changed files with 38 additions and 31 deletions

View File

@@ -32,7 +32,7 @@ const buildPackage = async (entryName: keyof typeof packageOptions) => {
{ {
...iifeOptions, ...iifeOptions,
minify: true, minify: true,
includeLargeDiagrams: false, includeLargeFeatures: false,
metafile: shouldVisualize, metafile: shouldVisualize,
} }
); );

View File

@@ -14,7 +14,7 @@ export interface MermaidBuildOptions {
metafile: boolean; metafile: boolean;
format: 'esm' | 'iife'; format: 'esm' | 'iife';
entryName: keyof typeof packageOptions; entryName: keyof typeof packageOptions;
includeLargeDiagrams: boolean; includeLargeFeatures: boolean;
} }
export const defaultOptions: Omit<MermaidBuildOptions, 'entryName'> = { export const defaultOptions: Omit<MermaidBuildOptions, 'entryName'> = {
@@ -22,7 +22,7 @@ export const defaultOptions: Omit<MermaidBuildOptions, 'entryName'> = {
metafile: false, metafile: false,
core: false, core: false,
format: 'esm', format: 'esm',
includeLargeDiagrams: true, includeLargeFeatures: true,
} as const; } as const;
const buildOptions = (override: BuildOptions): BuildOptions => { const buildOptions = (override: BuildOptions): BuildOptions => {
@@ -43,14 +43,14 @@ const buildOptions = (override: BuildOptions): BuildOptions => {
const getFileName = ( const getFileName = (
fileName: string, fileName: string,
{ core, format, minify, includeLargeDiagrams }: MermaidBuildOptions { core, format, minify, includeLargeFeatures }: MermaidBuildOptions
) => { ) => {
if (core) { if (core) {
fileName += '.core'; fileName += '.core';
} else if (format === 'esm') { } else if (format === 'esm') {
fileName += '.esm'; fileName += '.esm';
} }
if (!includeLargeDiagrams) { if (!includeLargeFeatures) {
fileName += '.tiny'; fileName += '.tiny';
} }
if (minify) { if (minify) {
@@ -60,7 +60,7 @@ const getFileName = (
}; };
export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => { 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 external: string[] = ['require', 'fs', 'path'];
const { name, file, packageName } = packageOptions[entryName]; const { name, file, packageName } = packageOptions[entryName];
const outFileName = getFileName(name, options); const outFileName = getFileName(name, options);
@@ -75,7 +75,7 @@ export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => {
chunkNames: `chunks/${outFileName}/[name]-[hash]`, chunkNames: `chunks/${outFileName}/[name]-[hash]`,
define: { define: {
// This needs to be stringified for esbuild // This needs to be stringified for esbuild
includeLargeDiagrams: `${includeLargeDiagrams}`, includeLargeFeatures: `${includeLargeFeatures}`,
'import.meta.vitest': 'undefined', 'import.meta.vitest': 'undefined',
}, },
}); });

View File

@@ -95,7 +95,7 @@ export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions)
], ],
define: { define: {
// Needs to be string // Needs to be string
includeLargeDiagrams: 'true', includeLargeFeatures: 'true',
}, },
}; };

View File

@@ -68,7 +68,7 @@ export const addDiagrams = () => {
} }
); );
if (includeLargeDiagrams) { if (includeLargeFeatures) {
registerLazyLoadedDiagrams(flowchartElk, mindmap); registerLazyLoadedDiagrams(flowchartElk, mindmap);
} }

View File

@@ -341,29 +341,36 @@ export const renderKatex = async (text: string, config: MermaidConfig): Promise<
return text.replace(katexRegex, 'MathML is unsupported in this environment.'); return text.replace(katexRegex, 'MathML is unsupported in this environment.');
} }
const { default: katex } = await import('katex'); if (includeLargeFeatures) {
return text const { default: katex } = await import('katex');
.split(lineBreakRegex) return text
.map((line) => .split(lineBreakRegex)
hasKatex(line) .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, (_, 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,
'Katex is unsupported in mermaid.tiny.js. Please use mermaid.js or mermaid.min.js.'
);
}; };
export default { export default {

View File

@@ -32,7 +32,7 @@ import type { DiagramMetadata, DiagramStyleClassDef } from './diagram-api/types.
import { preprocessDiagram } from './preprocess.js'; import { preprocessDiagram } from './preprocess.js';
import { decodeEntities } from './utils.js'; import { decodeEntities } from './utils.js';
const version = packageVersion + (includeLargeDiagrams ? '' : '-tiny'); const version = packageVersion + (includeLargeFeatures ? '' : '-tiny');
const MAX_TEXTLENGTH = 50_000; const MAX_TEXTLENGTH = 50_000;
const MAX_TEXTLENGTH_EXCEEDED_MSG = const MAX_TEXTLENGTH_EXCEEDED_MSG =

View File

@@ -1,2 +1,2 @@
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
declare var includeLargeDiagrams: boolean; declare var includeLargeFeatures: boolean;

View File

@@ -36,7 +36,7 @@ export default defineConfig({
}, },
define: { define: {
// Needs to be string // Needs to be string
includeLargeDiagrams: 'true', includeLargeFeatures: 'true',
'import.meta.vitest': 'undefined', 'import.meta.vitest': 'undefined',
}, },
}); });