"use client"; import { ReactNode } from "react"; type Props = { text: string; onCopy: (value: string, detail?: string) => void | Promise; }; function inlineMarkdown(value: string, keyPrefix: string): ReactNode[] { const matcher = /(`[^`\n]+`|\*\*[^*\n]+\*\*|~~[^~\n]+~~|\*[^*\n]+\*|\[[^\]]+\]\(https?:\/\/[^\s)]+\)|https?:\/\/[^\s<]+)/g; const output: ReactNode[] = []; let cursor = 0; let match: RegExpExecArray | null; while ((match = matcher.exec(value))) { if (match.index > cursor) output.push(value.slice(cursor, match.index)); const token = match[0]; const key = `${keyPrefix}-${match.index}`; if (token.startsWith("`") && token.endsWith("`")) { output.push({token.slice(1, -1)}); } else if (token.startsWith("**") && token.endsWith("**")) { output.push({inlineMarkdown(token.slice(2, -2), `${key}-strong`)}); } else if (token.startsWith("~~") && token.endsWith("~~")) { output.push({inlineMarkdown(token.slice(2, -2), `${key}-del`)}); } else if (token.startsWith("*") && token.endsWith("*")) { output.push({inlineMarkdown(token.slice(1, -1), `${key}-em`)}); } else if (token.startsWith("[")) { const link = token.match(/^\[([^\]]+)]\((https?:\/\/[^\s)]+)\)$/); if (link) output.push({link[1]}); } else { output.push({token}); } cursor = matcher.lastIndex; } if (cursor < value.length) output.push(value.slice(cursor)); return output; } function isTableDivider(value: string) { const cells = value.trim().replace(/^\||\|$/g, "").split("|").map((cell) => cell.trim()); return cells.length > 1 && cells.every((cell) => /^:?-{3,}:?$/.test(cell)); } function tableCells(value: string) { return value.trim().replace(/^\||\|$/g, "").split("|").map((cell) => cell.trim()); } function proseBlocks(value: string, keyPrefix: string): ReactNode[] { const lines = value.replace(/\r\n/g, "\n").split("\n"); const blocks: ReactNode[] = []; let index = 0; while (index < lines.length) { const line = lines[index]; const trimmed = line.trim(); if (!trimmed) { index += 1; continue; } const heading = trimmed.match(/^(#{1,6})\s*(.+)$/); if (heading) { const level = heading[1].length; const content = inlineMarkdown(heading[2], `${keyPrefix}-h-${index}`); if (level === 1) blocks.push(

{content}

); else if (level === 2) blocks.push(

{content}

); else if (level === 3) blocks.push(

{content}

); else blocks.push(

{content}

); index += 1; continue; } if (/^(-{3,}|\*{3,}|_{3,})$/.test(trimmed)) { blocks.push(
); index += 1; continue; } if (trimmed.startsWith(">")) { const quote: string[] = []; while (index < lines.length && lines[index].trim().startsWith(">")) { quote.push(lines[index].trim().replace(/^>\s?/, "")); index += 1; } blocks.push(
{quote.map((item, quoteIndex) =>

{inlineMarkdown(item, `${keyPrefix}-quote-${index}-${quoteIndex}`)}

)}
); continue; } if (/^[-*+]\s+/.test(trimmed)) { const items: string[] = []; while (index < lines.length && /^[-*+]\s+/.test(lines[index].trim())) { items.push(lines[index].trim().replace(/^[-*+]\s+/, "")); index += 1; } blocks.push(); continue; } if (/^\d+[.)]\s+/.test(trimmed)) { const items: string[] = []; while (index < lines.length && /^\d+[.)]\s+/.test(lines[index].trim())) { items.push(lines[index].trim().replace(/^\d+[.)]\s+/, "")); index += 1; } blocks.push(
    {items.map((item, itemIndex) =>
  1. {inlineMarkdown(item, `${keyPrefix}-ol-${index}-${itemIndex}`)}
  2. )}
); continue; } if (trimmed.includes("|") && index + 1 < lines.length && isTableDivider(lines[index + 1])) { const headers = tableCells(trimmed); index += 2; const rows: string[][] = []; while (index < lines.length && lines[index].trim().includes("|") && lines[index].trim()) { rows.push(tableCells(lines[index])); index += 1; } blocks.push(
{headers.map((header, cellIndex) => )}{rows.map((row, rowIndex) => {headers.map((_, cellIndex) => )})}
{inlineMarkdown(header, `${keyPrefix}-th-${cellIndex}`)}
{inlineMarkdown(row[cellIndex] || "", `${keyPrefix}-td-${rowIndex}-${cellIndex}`)}
, ); continue; } const paragraph: string[] = [trimmed]; index += 1; while (index < lines.length) { const next = lines[index].trim(); if (!next || /^(#{1,6})\s*/.test(next) || /^[-*+]\s+/.test(next) || /^\d+[.)]\s+/.test(next) || next.startsWith(">") || /^(-{3,}|\*{3,}|_{3,})$/.test(next)) break; if (next.includes("|") && index + 1 < lines.length && isTableDivider(lines[index + 1])) break; paragraph.push(next); index += 1; } blocks.push(

{paragraph.map((part, partIndex) => {inlineMarkdown(part, `${keyPrefix}-p-${index}-${partIndex}`)}{partIndex < paragraph.length - 1 &&
}
)}

); } return blocks; } export default function MarkdownMessage({ text, onCopy }: Props) { const blocks: ReactNode[] = []; const fence = /```(?:([a-zA-Z0-9_+.-]+)\n)?([\s\S]*?)```/g; let cursor = 0; let match: RegExpExecArray | null; while ((match = fence.exec(text))) { if (match.index > cursor) blocks.push(...proseBlocks(text.slice(cursor, match.index), `prose-${cursor}`)); const language = match[1] || "code"; const code = match[2].replace(/^\n/, "").replace(/\n$/, ""); blocks.push(
{language}
{code}
, ); cursor = fence.lastIndex; } if (cursor < text.length) blocks.push(...proseBlocks(text.slice(cursor), `prose-${cursor}`)); if (!blocks.length && text) blocks.push(...proseBlocks(text, "prose-empty")); return
{blocks}
; }