deno.land / x / lume@v2.1.4 / plugins / katex.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { katex, KatexOptions } from "../deps/katex.ts";import { renderMathInElement } from "../deps/katex-auto-render/auto-render.ts";import { merge } from "../core/utils/object.ts";
import type Site from "../core/site.ts";
export interface Options { /** The list of extensions this plugin applies to */ extensions?: string[];
/** The css selector to apply katex */ cssSelector?: string;
/** Configuration to pass to katex */ options?: KatexOptions;}
export const defaults: Options = { extensions: [".html"], cssSelector: ".language-math", options: { strict: true, displayMode: true, throwOnError: true, delimiters: [ { left: "$$", right: "$$", display: true }, { left: "\\(", right: "\\)", display: false }, { left: "\\begin{equation}", right: "\\end{equation}", display: true }, { left: "\\begin{align}", right: "\\end{align}", display: true }, { left: "\\begin{alignat}", right: "\\end{alignat}", display: true }, { left: "\\begin{gather}", right: "\\end{gather}", display: true }, { left: "\\begin{CD}", right: "\\end{CD}", display: true }, { left: "\\[", right: "\\]", display: true }, ], ignoredTags: [ "script", "noscript", "style", "textarea", "pre", "code", "option", ], ignoredClasses: [], macros: {}, },};
export default function (userOptions?: Options) { const options = merge(defaults, userOptions); return (site: Site) => { site.process(options.extensions, (pages) => { for (const page of pages) { const { document } = page;
if (!document) { continue; }
document.querySelectorAll(options.cssSelector) .forEach((element) => { try { const rendered = katex.renderToString( element.textContent, options.options, ); const div = document.createElement("div"); div.innerHTML = rendered.trim();
// we've selected the <code> element, we want to also replace the parent <pre> const parent = element.parentElement; if (parent && parent.tagName === "PRE") { parent.replaceWith(div.firstChild!); } else { element.replaceWith(div.firstChild!); } } catch (cause) { throw new Error( `Katex failed to render in the page ${page.outputPath}`, { cause }, ); } });
if (options.options.delimiters) { renderMathInElement(document.body, options.options); } } }); };}
lume

Version Info

Tagged at
6 months ago