UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

1 lines 2.15 kB
{"version":3,"sources":["../src/components/HtmlMathRenderer/KatexMath.tsx"],"sourcesContent":["import { ReactNode } from 'react';\nimport katex from 'katex';\n\nexport interface KatexMathProps {\n /** LaTeX source to render */\n math: string;\n /** Render as centered display math (block) instead of inline */\n displayMode?: boolean;\n /** Called with the thrown error when KaTeX fails to parse `math` */\n renderError?: (error: unknown) => ReactNode;\n}\n\n/**\n * Renders a single LaTeX expression with KaTeX, calling `katex.renderToString`\n * directly instead of going through `react-katex`.\n *\n * Why not `react-katex`: when bundlers (Vite/esbuild dev optimizer, Rollup)\n * bundle `react-katex` they inline their OWN copy of KaTeX whose function\n * registry ends up broken — symbols render but `\\`-commands (`\\frac`, `\\cdot`,\n * `\\left`, ...) fail with a parse error. Importing `katex` directly here uses\n * the shared, correctly-bundled KaTeX (the same one `rehype-katex` uses), so\n * every command renders. See HtmlMathRenderer for the surrounding pipeline.\n */\nexport const KatexMath = ({\n math,\n displayMode = false,\n renderError,\n}: KatexMathProps) => {\n let html: string;\n try {\n html = katex.renderToString(math, {\n displayMode,\n throwOnError: true,\n });\n } catch (error_) {\n return <>{renderError ? renderError(error_) : null}</>;\n }\n\n const Tag = displayMode ? 'div' : 'span';\n return (\n <Tag\n data-testid=\"react-katex\"\n // KaTeX output is sanitized markup it generates itself from the parsed\n // LaTeX; there is no untrusted HTML passthrough here.\n dangerouslySetInnerHTML={{ __html: html }}\n />\n );\n};\n\nexport default KatexMath;\n"],"mappings":";AACA,OAAO,WAAW;AAkCP;AAZJ,IAAM,YAAY,CAAC;AAAA,EACxB;AAAA,EACA,cAAc;AAAA,EACd;AACF,MAAsB;AACpB,MAAI;AACJ,MAAI;AACF,WAAO,MAAM,eAAe,MAAM;AAAA,MAChC;AAAA,MACA,cAAc;AAAA,IAChB,CAAC;AAAA,EACH,SAAS,QAAQ;AACf,WAAO,gCAAG,wBAAc,YAAY,MAAM,IAAI,MAAK;AAAA,EACrD;AAEA,QAAM,MAAM,cAAc,QAAQ;AAClC,SACE;AAAA,IAAC;AAAA;AAAA,MACC,eAAY;AAAA,MAGZ,yBAAyB,EAAE,QAAQ,KAAK;AAAA;AAAA,EAC1C;AAEJ;","names":[]}