hippofish/packages/client/src/scripts/preprocess.ts
naskya 9489543180 feat: custom KaTeX macro (#9779)
Closes: #9759
Co-authored-by: naskya <m@naskya.net>
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9779
Co-authored-by: naskya <naskya@noreply.codeberg.org>
Co-committed-by: naskya <naskya@noreply.codeberg.org>
2023-03-30 03:11:57 +00:00

23 lines
705 B
TypeScript

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) {
const parsedKaTeXMacro = localStorage.getItem("customKaTeXMacroParsed") ?? "{}";
const maxNumberOfExpansions = 200; // to prevent infinite expansion loops
let nodes = mfm.parse(text);
for (let node of nodes) {
if (node["type"] === "mathInline" || node["type"] === "mathBlock") {
node["props"]["formula"]
= expandKaTeXMacro(node["props"]["formula"], parsedKaTeXMacro, maxNumberOfExpansions);
}
}
text = mfm.toString(nodes);
}
return text;
}