2023-03-30 05:11:57 +02:00
|
|
|
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 {
|
2023-03-30 05:11:57 +02:00
|
|
|
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-03-30 05:11:57 +02:00
|
|
|
|
2023-09-02 01:27:33 +02:00
|
|
|
const nodes = mfm.parse(text);
|
2023-03-30 05:11:57 +02:00
|
|
|
|
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,
|
|
|
|
);
|
2023-03-30 05:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
text = mfm.toString(nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|