@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 4.59 kB
Source Map (JSON)
{"version":3,"file":"rehypeKatex.mjs","names":[],"sources":["../../../src/Markdown/plugins/rehypeKatex.ts"],"sourcesContent":["import type { ElementContent, Root } from 'hast';\nimport { fromHtmlIsomorphic } from 'hast-util-from-html-isomorphic';\nimport { toText } from 'hast-util-to-text';\nimport katex, { type KatexOptions } from 'katex';\nimport { SKIP, visitParents } from 'unist-util-visit-parents';\nimport type { VFile } from 'vfile';\n\n// 内联自 rehype-katex@7,因为它硬依赖 katex ^0.16,下游安装时会得到一份嵌套的\n// 0.16 渲染出 v0.18 之前的 class 名,与我们注入的 v0.18 样式表对不上。\n// 升级 rehype-katex 到支持 katex ^0.18 的版本后可以删掉本文件。\n\nexport type RehypeKatexOptions = Omit<KatexOptions, 'displayMode' | 'throwOnError'>;\n\nconst emptyClasses: unknown[] = [];\n\nexport const rehypeKatex =\n (options?: RehypeKatexOptions) =>\n (tree: Root, file: VFile): undefined => {\n const settings = options || {};\n\n visitParents(tree, 'element', (element, parents) => {\n const classes = Array.isArray(element.properties.className)\n ? element.properties.className\n : emptyClasses;\n const languageMath = classes.includes('language-math');\n const mathDisplay = classes.includes('math-display');\n const mathInline = classes.includes('math-inline');\n let displayMode = mathDisplay;\n\n if (!languageMath && !mathDisplay && !mathInline) return;\n\n let parent: any = parents.at(-1);\n let scope: any = element;\n\n if (\n element.tagName === 'code' &&\n languageMath &&\n parent &&\n parent.type === 'element' &&\n parent.tagName === 'pre'\n ) {\n scope = parent;\n parent = parents.at(-2);\n displayMode = true;\n }\n\n if (!parent) return;\n\n const value = toText(scope, { whitespace: 'pre' });\n\n let result: ElementContent[] | string | undefined;\n\n try {\n result = katex.renderToString(value, { ...settings, displayMode, throwOnError: true });\n } catch (error) {\n const cause = error as Error;\n\n file.message('Could not render math with KaTeX', {\n ancestors: [...parents, element],\n cause,\n place: element.position,\n ruleId: cause.name.toLowerCase(),\n source: 'rehype-katex',\n });\n\n try {\n result = katex.renderToString(value, {\n ...settings,\n displayMode,\n strict: 'ignore',\n throwOnError: false,\n });\n } catch {\n result = [\n {\n children: [{ type: 'text', value }],\n properties: {\n className: ['katex-error'],\n style: 'color:' + (settings.errorColor || '#cc0000'),\n title: String(error),\n },\n tagName: 'span',\n type: 'element',\n },\n ];\n }\n }\n\n if (typeof result === 'string') {\n result = fromHtmlIsomorphic(result, { fragment: true }).children as ElementContent[];\n }\n\n const index = parent.children.indexOf(scope);\n parent.children.splice(index, 1, ...result);\n return SKIP;\n });\n };\n"],"mappings":";;;;;AAaA,MAAM,eAA0B,CAAC;AAEjC,MAAa,eACV,aACA,MAAY,SAA2B;CACtC,MAAM,WAAW,WAAW,CAAC;CAE7B,aAAa,MAAM,YAAY,SAAS,YAAY;EAClD,MAAM,UAAU,MAAM,QAAQ,QAAQ,WAAW,SAAS,IACtD,QAAQ,WAAW,YACnB;EACJ,MAAM,eAAe,QAAQ,SAAS,eAAe;EACrD,MAAM,cAAc,QAAQ,SAAS,cAAc;EACnD,MAAM,aAAa,QAAQ,SAAS,aAAa;EACjD,IAAI,cAAc;EAElB,IAAI,CAAC,gBAAgB,CAAC,eAAe,CAAC,YAAY;EAElD,IAAI,SAAc,QAAQ,GAAG,EAAE;EAC/B,IAAI,QAAa;EAEjB,IACE,QAAQ,YAAY,UACpB,gBACA,UACA,OAAO,SAAS,aAChB,OAAO,YAAY,OACnB;GACA,QAAQ;GACR,SAAS,QAAQ,GAAG,EAAE;GACtB,cAAc;EAChB;EAEA,IAAI,CAAC,QAAQ;EAEb,MAAM,QAAQ,OAAO,OAAO,EAAE,YAAY,MAAM,CAAC;EAEjD,IAAI;EAEJ,IAAI;GACF,SAAS,MAAM,eAAe,OAAO;IAAE,GAAG;IAAU;IAAa,cAAc;GAAK,CAAC;EACvF,SAAS,OAAO;GACd,MAAM,QAAQ;GAEd,KAAK,QAAQ,oCAAoC;IAC/C,WAAW,CAAC,GAAG,SAAS,OAAO;IAC/B;IAC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