better formatting
This commit is contained in:
parent
dcf9225fb8
commit
7ce1305329
2 changed files with 49 additions and 22 deletions
51
index.ts
51
index.ts
|
|
@ -6,34 +6,61 @@ import { renderMarkdown } from "muddle/markdown";
|
|||
import katex from "katex";
|
||||
|
||||
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 {
|
||||
const formulas: string[] = [];
|
||||
const body = source.replace(/\$\$([\s\S]+?)\$\$|(?<!\\)\$([^$\n]+?)\$/g, (_match, display, inline) => {
|
||||
formulas.push(renderLatex(display ?? inline, display !== undefined));
|
||||
return `MUDDLEMATH${formulas.length - 1}END`;
|
||||
});
|
||||
return renderMarkdown(body).replace(/MUDDLEMATH(\d+)END/g, (_match, index) => formulas[Number(index)] ?? "");
|
||||
const body = source.replace(
|
||||
/\$\$([\s\S]+?)\$\$|(?<!\\)\$([^$\n]+?)\$/g,
|
||||
(_match, display, inline) => {
|
||||
formulas.push(renderLatex(display ?? inline, display !== undefined));
|
||||
return `MUDDLEMATH${formulas.length - 1}END`;
|
||||
},
|
||||
);
|
||||
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";
|
||||
return {
|
||||
name: "latex",
|
||||
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");
|
||||
context.loader("latex-markdown", {
|
||||
name: "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",
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
20
package.json
20
package.json
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@muddle/latex",
|
||||
"type": "module",
|
||||
"exports": "./index.ts",
|
||||
"version": "0.1.0",
|
||||
"peerDependencies": {
|
||||
"muddle": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"katex": "^0.19.0"
|
||||
}
|
||||
"name": "@muddle/latex",
|
||||
"type": "module",
|
||||
"exports": "./index.ts",
|
||||
"version": "0.1.0",
|
||||
"peerDependencies": {
|
||||
"muddle": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"katex": "^0.19.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue