Spaces:
Running
Running
File size: 4,698 Bytes
b53e6f3 53d8367 b53e6f3 53d8367 b53e6f3 53d8367 b53e6f3 53d8367 b53e6f3 53d8367 b53e6f3 53d8367 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 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;
});
}
|