better formatting
This commit is contained in:
parent
dcf9225fb8
commit
7ce1305329
2 changed files with 49 additions and 22 deletions
47
index.ts
47
index.ts
|
|
@ -6,34 +6,61 @@ import { renderMarkdown } from "muddle/markdown";
|
||||||
import katex from "katex";
|
import katex from "katex";
|
||||||
|
|
||||||
export function renderLatex(source: string, displayMode = false): string {
|
export function renderLatex(source: string, displayMode = false): string {
|
||||||
return katex.renderToString(source, { displayMode, throwOnError: false, trust: false });
|
return katex.renderToString(source, {
|
||||||
|
displayMode,
|
||||||
|
throwOnError: false,
|
||||||
|
trust: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderMathMarkdown(source: string): string {
|
export function renderMathMarkdown(source: string): string {
|
||||||
const formulas: string[] = [];
|
const formulas: string[] = [];
|
||||||
const body = source.replace(/\$\$([\s\S]+?)\$\$|(?<!\\)\$([^$\n]+?)\$/g, (_match, display, inline) => {
|
const body = source.replace(
|
||||||
|
/\$\$([\s\S]+?)\$\$|(?<!\\)\$([^$\n]+?)\$/g,
|
||||||
|
(_match, display, inline) => {
|
||||||
formulas.push(renderLatex(display ?? inline, display !== undefined));
|
formulas.push(renderLatex(display ?? inline, display !== undefined));
|
||||||
return `MUDDLEMATH${formulas.length - 1}END`;
|
return `MUDDLEMATH${formulas.length - 1}END`;
|
||||||
});
|
},
|
||||||
return renderMarkdown(body).replace(/MUDDLEMATH(\d+)END/g, (_match, index) => formulas[Number(index)] ?? "");
|
);
|
||||||
|
return renderMarkdown(body).replace(
|
||||||
|
/MUDDLEMATH(\d+)END/g,
|
||||||
|
(_match, index) => formulas[Number(index)] ?? "",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LatexIntegrationOptions { route?: string }
|
export interface LatexIntegrationOptions {
|
||||||
|
route?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export function latexIntegration(options: LatexIntegrationOptions = {}): Integration {
|
export function latexIntegration(
|
||||||
|
options: LatexIntegrationOptions = {},
|
||||||
|
): Integration {
|
||||||
const route = options.route ?? "/_muddle/katex.css";
|
const route = options.route ?? "/_muddle/katex.css";
|
||||||
return {
|
return {
|
||||||
name: "latex",
|
name: "latex",
|
||||||
async setup(context: IntegrationContext) {
|
async setup(context: IntegrationContext) {
|
||||||
const base = dirname(fileURLToPath(import.meta.resolve("katex/package.json")));
|
const base = dirname(
|
||||||
|
fileURLToPath(import.meta.resolve("katex/package.json")),
|
||||||
|
);
|
||||||
const css = await readFile(join(base, "dist", "katex.min.css"), "utf8");
|
const css = await readFile(join(base, "dist", "katex.min.css"), "utf8");
|
||||||
context.loader("latex-markdown", {
|
context.loader("latex-markdown", {
|
||||||
name: "latex-markdown",
|
name: "latex-markdown",
|
||||||
extensions: [".latex.md", ".latex.markdown"],
|
extensions: [".latex.md", ".latex.markdown"],
|
||||||
load(file) { return { data: {}, content: renderMathMarkdown(file.source) }; },
|
load(file) {
|
||||||
|
return { data: {}, content: renderMathMarkdown(file.source) };
|
||||||
|
},
|
||||||
});
|
});
|
||||||
context.route("GET", route, () => new Response(css, { headers: { "content-type": "text/css; charset=utf-8", "cache-control": "public, max-age=31536000, immutable" } }));
|
context.route(
|
||||||
|
"GET",
|
||||||
|
route,
|
||||||
|
() =>
|
||||||
|
new Response(css, {
|
||||||
|
headers: {
|
||||||
|
"content-type": "text/css; charset=utf-8",
|
||||||
|
"cache-control": "public, max-age=31536000, immutable",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue