deno.land / x / lume@v2.1.4 / plugins / katex.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import { 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); } } }); };}
Version Info