Spaces:
Running
Running
Download algospaced-ui/src/utils/markdownUtils.tsx from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/utils/markdownUtils.tsx
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/utils/markdownUtils.tsx
-
curl -L -o markdownUtils.tsx https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/utils/markdownUtils.tsx
4.7 kB
| import React, { lazy, Suspense } from 'react'; | |
| const KatexRenderer = lazy(() => import('../components/KatexRenderer')); | |
| export function renderKatex(math: string): string { | |
| const escaped = math | |
| .replace(/&/g, '&') | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>'); | |
| return `<span class="katex-fallback font-mono text-[11px]">${escaped}</span>`; | |
| } | |
| /** | |
| * Tokenize and render inline text elements (inline math, code, bold, italic, links). | |
| */ | |
| export function renderInlineMarkdown(text: string): React.ReactNode[] { | |
| if (!text) return []; | |
| // Regex matching inline tokens in order: | |
| // 1. Display math: $$ ... $$ | |
| // 2. Display math: \[ ... \] | |
| // 3. Inline math: \( ... \) | |
| // 4. Inline math: $ ... $ (non-greedy) | |
| // 5. Inline code: ` ... ` | |
| // 6. Bold: ** ... ** or __ ... __ | |
| // 7. Italic: * ... * or _ ... _ | |
| // 8. Link: [text](url) | |
| const tokenRegex = /(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\([\s\S]*?\\\)|\$(?:\\\$|[^$\n])+?\$|`[^`\n]+`|\*\*[^*\n]+\*\*|__[^_\n]+__|(?<!\*)\*[^*\n]+(?<!\*)\*|(?<!_)_[^_\n]+(?<!_)_|\[[^\]\n]+\]\([^)\n]+\))/g; | |
| const parts = text.split(tokenRegex); | |
| return parts.map((part, index) => { | |
| if (!part) return null; | |
| // 1. Display math $$ ... $$ | |
| if (part.startsWith('$$') && part.endsWith('$$') && part.length >= 4) { | |
| const math = part.slice(2, -2); | |
| if (!math.trim()) return part; | |
| return ( | |
| <Suspense key={index} fallback={<span className="font-mono text-xs opacity-75">{math}</span>}> | |
| <KatexRenderer | |
| math={math} | |
| displayMode={true} | |
| className="inline-block my-1 px-1 overflow-x-auto align-middle" | |
| /> | |
| </Suspense> | |
| ); | |
| } | |
| // 2. Display math \[ ... \] | |
| if (part.startsWith('\\[') && part.endsWith('\\]') && part.length >= 4) { | |
| const math = part.slice(2, -2); | |
| if (!math.trim()) return part; | |
| return ( | |
| <Suspense key={index} fallback={<span className="font-mono text-xs opacity-75">{math}</span>}> | |
| <KatexRenderer | |
| math={math} | |
| displayMode={true} | |
| className="inline-block my-1 px-1 overflow-x-auto align-middle" | |
| /> | |
| </Suspense> | |
| ); | |
| } | |
| // 3. Inline math \( ... \) | |
| if (part.startsWith('\\(') && part.endsWith('\\)') && part.length >= 4) { | |
| const math = part.slice(2, -2); | |
| if (!math.trim()) return part; | |
| return ( | |
| <Suspense key={index} fallback={<span className="font-mono text-xs opacity-75">{math}</span>}> | |
| <KatexRenderer | |
| math={math} | |
| displayMode={false} | |
| className="inline-katex px-0.5 align-baseline" | |
| /> | |
| </Suspense> | |
| ); | |
| } | |
| // 4. Inline math $ ... $ | |
| if (part.startsWith('$') && part.endsWith('$') && part.length >= 2) { | |
| const math = part.slice(1, -1); | |
| if (!math.trim()) return part; | |
| return ( | |
| <Suspense key={index} fallback={<span className="font-mono text-xs opacity-75">{math}</span>}> | |
| <KatexRenderer | |
| math={math} | |
| displayMode={false} | |
| className="inline-katex px-0.5 align-baseline" | |
| /> | |
| </Suspense> | |
| ); | |
| } | |
| // 5. Inline code ` ... ` | |
| if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) { | |
| return ( | |
| <code | |
| key={index} | |
| className="bg-[#1E293B] text-[#4ADE80] px-1.5 py-0.5 rounded text-[11px] font-mono border border-ink-black shadow-[1px_1px_0px_0px_#000]" | |
| > | |
| {part.slice(1, -1)} | |
| </code> | |
| ); | |
| } | |
| // 6. Bold ** ... ** or __ ... __ | |
| if ( | |
| (part.startsWith('**') && part.endsWith('**') && part.length >= 4) || | |
| (part.startsWith('__') && part.endsWith('__') && part.length >= 4) | |
| ) { | |
| return ( | |
| <strong key={index} className="text-ink-black font-extrabold"> | |
| {renderInlineMarkdown(part.slice(2, -2))} | |
| </strong> | |
| ); | |
| } | |
| // 7. Italic * ... * or _ ... _ | |
| if ( | |
| (part.startsWith('*') && part.endsWith('*') && part.length >= 2) || | |
| (part.startsWith('_') && part.endsWith('_') && part.length >= 2) | |
| ) { | |
| return ( | |
| <em key={index} className="italic text-slate-800"> | |
| {renderInlineMarkdown(part.slice(1, -1))} | |
| </em> | |
| ); | |
| } | |
| // 8. Link [text](url) | |
| const linkMatch = part.match(/^\[([^\]]+)\]\(([^)]+)\)$/); | |
| if (linkMatch) { | |
| return ( | |
| <a | |
| key={index} | |
| href={linkMatch[2]} | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| className="text-primary underline hover:text-primary/80 font-bold" | |
| > | |
| {linkMatch[1]} | |
| </a> | |
| ); | |
| } | |
| return part; | |
| }); | |
| } | |