2023-03-29 21:11:57 -06:00
|
|
|
import * as mfm from "mfm-js";
|
|
|
|
import { defaultStore } from "@/store";
|
|
|
|
import { expandKaTeXMacro } from "@/scripts/katex-macro";
|
|
|
|
|
|
|
|
export function preprocess(text: string): string {
|
|
|
|
if (defaultStore.state.enableCustomKaTeXMacro) {
|
2023-03-30 20:10:03 -06:00
|
|
|
const parsedKaTeXMacro =
|
|
|
|
localStorage.getItem("customKaTeXMacroParsed") ?? "{}";
|
|
|
|
const maxNumberOfExpansions = 200; // to prevent infinite expansion loops
|
2023-03-29 21:11:57 -06:00
|
|
|
|
|
|
|
let nodes = mfm.parse(text);
|
|
|
|
|
|
|
|
for (let node of nodes) {
|
|
|
|
if (node["type"] === "mathInline" || node["type"] === "mathBlock") {
|
2023-03-30 20:10:03 -06:00
|
|
|
node["props"]["formula"] = expandKaTeXMacro(
|
|
|
|
node["props"]["formula"],
|
|
|
|
parsedKaTeXMacro,
|
|
|
|
maxNumberOfExpansions,
|
|
|
|
);
|
2023-03-29 21:11:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
text = mfm.toString(nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|