hippofish/packages/client/src/scripts/preprocess.ts

28 lines
708 B
TypeScript
Raw Normal View History

import * as mfm from "mfm-js";
import { defaultStore } from "@/store";
import { expandKaTeXMacro } from "@/scripts/katex-macro";
2023-10-19 00:28:20 +02:00
export default function (text: string): string {
if (defaultStore.state.enableCustomKaTeXMacro) {
2023-03-31 04:10:03 +02:00
const parsedKaTeXMacro =
localStorage.getItem("customKaTeXMacroParsed") ?? "{}";
const maxNumberOfExpansions = 200; // to prevent infinite expansion loops
2023-09-02 01:27:33 +02:00
const nodes = mfm.parse(text);
2023-09-02 01:27:33 +02:00
for (const node of nodes) {
if (node.type === "mathInline" || node.type === "mathBlock") {
node.props.formula = expandKaTeXMacro(
node.props.formula,
2023-03-31 04:10:03 +02:00
parsedKaTeXMacro,
maxNumberOfExpansions,
);
}
}
text = mfm.toString(nodes);
}
return text;
}