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

28 lines
708 B
TypeScript
Raw Normal View History

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