import { Fragment } from 'react';
const TOKEN_RE = /(\*\*[^*\n][\s\S]*?[^*\n]\*\*|`[^`\n]+`|\[[^\]\n]+\]\(https?:\/\/[^)\s]+\))/g;
function renderToken(token, key) {
if (token.startsWith('**') && token.endsWith('**')) {
return (
{token.slice(2, -2)}
);
}
if (token.startsWith('`') && token.endsWith('`')) {
return (
{token.slice(1, -1)}
);
}
const link = token.match(/^\[([^\]\n]+)\]\((https?:\/\/[^)\s]+)\)$/);
if (link) {
return (
{link[1]}
);
}
return token;
}
export function renderInlineMarkdown(text) {
if (!text) return null;
const parts = [];
let lastIndex = 0;
for (const match of text.matchAll(TOKEN_RE)) {
if (match.index > lastIndex) {
parts.push(text.slice(lastIndex, match.index));
}
parts.push(renderToken(match[0], `md-${match.index}`));
lastIndex = match.index + match[0].length;
}
if (lastIndex < text.length) {
parts.push(text.slice(lastIndex));
}
return parts.map((part, index) => (
{part}
));
}
export function MarkdownText({ text }) {
return renderInlineMarkdown(text);
}