Spaces:
Running
Running
Download algospaced-ui/src/components/KatexRenderer.tsx from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 830 Bytes
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/components/KatexRenderer.tsx
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/components/KatexRenderer.tsx
-
curl -L -o KatexRenderer.tsx https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/components/KatexRenderer.tsx
830 Bytes
| import React from 'react'; | |
| import katex from 'katex'; | |
| import 'katex/dist/katex.min.css'; | |
| export interface KatexRendererProps { | |
| math: string; | |
| displayMode?: boolean; | |
| className?: string; | |
| } | |
| export const KatexRenderer: React.FC<KatexRendererProps> = ({ | |
| math, | |
| displayMode = false, | |
| className = '', | |
| }) => { | |
| let html = ''; | |
| try { | |
| html = katex.renderToString(math.trim(), { | |
| displayMode, | |
| throwOnError: false, | |
| output: 'html', | |
| }); | |
| } catch { | |
| const escaped = math | |
| .replace(/&/g, '&') | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>'); | |
| html = `<span class="katex-error text-rose-600 font-mono text-[11px]">${escaped}</span>`; | |
| } | |
| return ( | |
| <span | |
| className={className} | |
| dangerouslySetInnerHTML={{ __html: html }} | |
| /> | |
| ); | |
| }; | |
| export default KatexRenderer; | |