import { escapeHtml } from './escape';
type PlurimathCtor = new (data: string, format: string) => {
toAsciimath(): string;
toLatex(): string;
toMathml(): string;
toHtml(): string;
toOmml(): string;
toDisplay(lang: string): string;
};
let Plurimath: PlurimathCtor | null = null;
let loading: Promise | null = null;
export async function loadPlurimath(): Promise {
if (Plurimath) return Plurimath;
if (loading) return loading;
loading = import('@plurimath/plurimath').then(m => {
Plurimath = m.default as PlurimathCtor;
return Plurimath;
});
return loading;
}
export function renderToMathML(expr: string, format: string): string | null {
if (!Plurimath) return null;
try {
const p = new Plurimath(expr, format);
return p.toMathml().replace('display="block"', 'display="inline"').trim();
} catch {
return null;
}
}
export function mathToHtml(expr: string, format: string, bold: boolean): string {
const mathml = renderToMathML(expr, format);
if (mathml) {
return `${mathml}`;
}
return `${escapeHtml(expr)}`;
}