| |
|
|
| import katex from "katex"; |
| import splitAtDelimiters from "./splitAtDelimiters"; |
| import type {DelimiterSpec} from "./splitAtDelimiters"; |
|
|
| interface RenderMathInElementOptions { |
| delimiters?: DelimiterSpec[]; |
| preProcess?: (math: string) => string; |
| ignoredTags?: string[]; |
| ignoredClasses?: string[]; |
| errorCallback?: (msg: string, err: Error) => void; |
| displayMode?: boolean; |
| macros?: Record<string, string>; |
| } |
|
|
| interface RenderMathInElementOptionsCopy { |
| delimiters: DelimiterSpec[]; |
| preProcess?: (math: string) => string; |
| ignoredTags: Set<string>; |
| ignoredClasses: string[]; |
| errorCallback: (msg: string, err: Error) => void; |
| displayMode?: boolean; |
| macros?: Record<string, string>; |
| } |
|
|
| |
| |
| |
| const renderMathInText = function( |
| text: string, |
| optionsCopy: RenderMathInElementOptionsCopy |
| ) { |
| const data = splitAtDelimiters(text, optionsCopy.delimiters); |
| if (data.length === 1 && data[0].type === 'text') { |
| |
| |
| |
| return null; |
| } |
|
|
| const fragment = document.createDocumentFragment(); |
|
|
| for (let i = 0; i < data.length; i++) { |
| if (data[i].type === "text") { |
| fragment.appendChild(document.createTextNode(data[i].data)); |
| } else { |
| const span = document.createElement("span"); |
| let math = data[i].data; |
| |
| |
| optionsCopy.displayMode = data[i].display; |
| try { |
| if (optionsCopy.preProcess) { |
| math = optionsCopy.preProcess(math); |
| } |
| katex.render(math, span, optionsCopy); |
| } catch (e) { |
| if (!(e instanceof katex.ParseError)) { |
| throw e; |
| } |
| optionsCopy.errorCallback( |
| "KaTeX auto-render: Failed to parse `" + data[i].data + |
| "` with ", |
| e |
| ); |
| fragment.appendChild(document.createTextNode(data[i].rawData!)); |
| continue; |
| } |
| fragment.appendChild(span); |
| } |
| } |
|
|
| return fragment; |
| }; |
|
|
| const renderElem = function( |
| elem: HTMLElement, |
| optionsCopy: RenderMathInElementOptionsCopy |
| ) { |
| for (let i = 0; i < elem.childNodes.length; i++) { |
| const childNode = elem.childNodes[i]; |
| if (childNode.nodeType === 3) { |
| |
| |
| |
| |
| let textContentConcat = childNode.textContent ?? ""; |
| let sibling = childNode.nextSibling; |
| let nSiblings = 0; |
| while (sibling && (sibling.nodeType === Node.TEXT_NODE)) { |
| textContentConcat += sibling.textContent ?? ""; |
| sibling = sibling.nextSibling; |
| nSiblings++; |
| } |
| const frag = renderMathInText(textContentConcat, optionsCopy); |
| if (frag) { |
| |
| for (let j = 0; j < nSiblings; j++) { |
| childNode.nextSibling!.remove(); |
| } |
| i += frag.childNodes.length - 1; |
| elem.replaceChild(frag, childNode); |
| } else { |
| |
| |
| i += nSiblings; |
| } |
| } else if (childNode.nodeType === 1) { |
| |
| const className = ' ' + (childNode as HTMLElement).className + ' '; |
| const shouldRender = !optionsCopy.ignoredTags.has( |
| childNode.nodeName.toLowerCase()) && |
| optionsCopy.ignoredClasses.every( |
| (x: string) => !className.includes(' ' + x + ' ')); |
|
|
| if (shouldRender) { |
| renderElem(childNode as HTMLElement, optionsCopy); |
| } |
| } |
| |
| } |
| }; |
|
|
| const renderMathInElement = function(elem: HTMLElement, options?: RenderMathInElementOptions) { |
| if (!elem) { |
| throw new Error("No element provided to render"); |
| } |
|
|
| const optionsCopy: Partial<RenderMathInElementOptionsCopy> = {}; |
|
|
| Object.assign(optionsCopy, options); |
|
|
| |
| optionsCopy.delimiters = optionsCopy.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}, |
| ]; |
| optionsCopy.ignoredTags = new Set<string>(options?.ignoredTags || [ |
| "script", "noscript", "style", "textarea", "pre", "code", "option", |
| ]); |
| optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || []; |
| optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; |
|
|
| |
| |
| optionsCopy.macros = optionsCopy.macros || {}; |
|
|
| renderElem(elem, optionsCopy as RenderMathInElementOptionsCopy); |
| }; |
|
|
| export default renderMathInElement; |
|
|