Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/* eslint no-console:0 */2 3import katex from "katex";4import splitAtDelimiters from "./splitAtDelimiters";5import type {DelimiterSpec} from "./splitAtDelimiters";6 7interface RenderMathInElementOptions {8 delimiters?: DelimiterSpec[];9 preProcess?: (math: string) => string;10 ignoredTags?: string[];11 ignoredClasses?: string[];12 errorCallback?: (msg: string, err: Error) => void;13 displayMode?: boolean;14 macros?: Record<string, string>;15}16 17interface RenderMathInElementOptionsCopy {18 delimiters: DelimiterSpec[];19 preProcess?: (math: string) => string;20 ignoredTags: Set<string>;21 ignoredClasses: string[];22 errorCallback: (msg: string, err: Error) => void;23 displayMode?: boolean;24 macros?: Record<string, string>;25}26 27/* Note: optionsCopy is mutated by this method. If it is ever exposed in the28 * API, we should copy it before mutating.29 */30const renderMathInText = function(31 text: string,32 optionsCopy: RenderMathInElementOptionsCopy33) {34 const data = splitAtDelimiters(text, optionsCopy.delimiters);35 if (data.length === 1 && data[0].type === 'text') {36 // There is no formula in the text.37 // Let's return null which means there is no need to replace38 // the current text node with a new one.39 return null;40 }41 42 const fragment = document.createDocumentFragment();43 44 for (let i = 0; i < data.length; i++) {45 if (data[i].type === "text") {46 fragment.appendChild(document.createTextNode(data[i].data));47 } else {48 const span = document.createElement("span");49 let math = data[i].data;50 // Override any display mode defined in the settings with that51 // defined by the text itself52 optionsCopy.displayMode = data[i].display;53 try {54 if (optionsCopy.preProcess) {55 math = optionsCopy.preProcess(math);56 }57 katex.render(math, span, optionsCopy);58 } catch (e) {59 if (!(e instanceof katex.ParseError)) {60 throw e;61 }62 optionsCopy.errorCallback(63 "KaTeX auto-render: Failed to parse `" + data[i].data +64 "` with ",65 e66 );67 fragment.appendChild(document.createTextNode(data[i].rawData!));68 continue;69 }70 fragment.appendChild(span);71 }72 }73 74 return fragment;75};76 77const renderElem = function(78 elem: HTMLElement,79 optionsCopy: RenderMathInElementOptionsCopy80) {81 for (let i = 0; i < elem.childNodes.length; i++) {82 const childNode = elem.childNodes[i];83 if (childNode.nodeType === 3) {84 // Text node85 // Concatenate all sibling text nodes.86 // Webkit browsers split very large text nodes into smaller ones,87 // so the delimiters may be split across different nodes.88 let textContentConcat = childNode.textContent ?? "";89 let sibling = childNode.nextSibling;90 let nSiblings = 0;91 while (sibling && (sibling.nodeType === Node.TEXT_NODE)) {92 textContentConcat += sibling.textContent ?? "";93 sibling = sibling.nextSibling;94 nSiblings++;95 }96 const frag = renderMathInText(textContentConcat, optionsCopy);97 if (frag) {98 // Remove extra text nodes99 for (let j = 0; j < nSiblings; j++) {100 childNode.nextSibling!.remove();101 }102 i += frag.childNodes.length - 1;103 elem.replaceChild(frag, childNode);104 } else {105 // If the concatenated text does not contain math106 // the siblings will not either107 i += nSiblings;108 }109 } else if (childNode.nodeType === 1) {110 // Element node111 const className = ' ' + (childNode as HTMLElement).className + ' ';112 const shouldRender = !optionsCopy.ignoredTags.has(113 childNode.nodeName.toLowerCase()) &&114 optionsCopy.ignoredClasses.every(115 (x: string) => !className.includes(' ' + x + ' '));116 117 if (shouldRender) {118 renderElem(childNode as HTMLElement, optionsCopy);119 }120 }121 // Otherwise, it's something else, and ignore it.122 }123};124 125const renderMathInElement = function(elem: HTMLElement, options?: RenderMathInElementOptions) {126 if (!elem) {127 throw new Error("No element provided to render");128 }129 130 const optionsCopy: Partial<RenderMathInElementOptionsCopy> = {};131 132 Object.assign(optionsCopy, options);133 134 // default options135 optionsCopy.delimiters = optionsCopy.delimiters || [136 {left: "$$", right: "$$", display: true},137 {left: "\\(", right: "\\)", display: false},138 // LaTeX uses $…$, but it ruins the display of normal `$` in text:139 // {left: "$", right: "$", display: false},140 // $ must come after $$141 142 // Render AMS environments even if outside $$…$$ delimiters.143 {left: "\\begin{equation}", right: "\\end{equation}", display: true},144 {left: "\\begin{align}", right: "\\end{align}", display: true},145 {left: "\\begin{alignat}", right: "\\end{alignat}", display: true},146 {left: "\\begin{gather}", right: "\\end{gather}", display: true},147 {left: "\\begin{CD}", right: "\\end{CD}", display: true},148 149 {left: "\\[", right: "\\]", display: true},150 ];151 optionsCopy.ignoredTags = new Set<string>(options?.ignoredTags || [152 "script", "noscript", "style", "textarea", "pre", "code", "option",153 ]);154 optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];155 optionsCopy.errorCallback = optionsCopy.errorCallback || console.error;156 157 // Enable sharing of global macros defined via `\gdef` between different158 // math elements within a single call to `renderMathInElement`.159 optionsCopy.macros = optionsCopy.macros || {};160 161 renderElem(elem, optionsCopy as RenderMathInElementOptionsCopy);162};163 164export default renderMathInElement;165 