const { useEffect, useMemo, useRef, useState } = React; marked.setOptions({ gfm: true, breaks: true }); function cn(...parts) { return parts.filter(Boolean).join(" "); } function uid() { if (window.crypto && typeof window.crypto.randomUUID === "function") { return window.crypto.randomUUID(); } return `${Date.now()}-${Math.random().toString(16).slice(2)}`; } function renderMd(text, sources = []) { const html = DOMPurify.sanitize(marked.parse(text || "")); if (!sources.length) return html; const sourceByIndex = new Map( sources.map((source) => [String(source.index), source]), ); const doc = new DOMParser().parseFromString( `
${html}
`, "text/html", ); const root = doc.body.firstElementChild; const walker = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT, { acceptNode(node) { if (!/\[(\d+)\]/.test(node.nodeValue || "")) return NodeFilter.FILTER_REJECT; if (node.parentElement?.closest("a, code, pre")) return NodeFilter.FILTER_REJECT; return NodeFilter.FILTER_ACCEPT; }, }); const nodes = []; while (walker.nextNode()) nodes.push(walker.currentNode); for (const node of nodes) { const textValue = node.nodeValue || ""; const fragment = doc.createDocumentFragment(); let lastIndex = 0; for (const match of textValue.matchAll(/\[(\d+)\]/g)) { const source = sourceByIndex.get(match[1]); if (!source) continue; if (match.index > lastIndex) { fragment.append( doc.createTextNode(textValue.slice(lastIndex, match.index)), ); } const link = doc.createElement("a"); link.className = "citation-link"; link.textContent = match[0]; link.href = sourceUrl(source) || `#source-${source.index}`; if (sourceUrl(source)) { link.target = "_blank"; link.rel = "noopener noreferrer"; } fragment.append(link); lastIndex = match.index + match[0].length; } if (lastIndex < textValue.length) { fragment.append(doc.createTextNode(textValue.slice(lastIndex))); } node.parentNode.replaceChild(fragment, node); } return root.innerHTML; } function Button({ className, children, ...props }) { return ( ); } const Input = React.forwardRef(function Input({ className, ...props }, ref) { return ; }); const Textarea = React.forwardRef(function Textarea( { className, ...props }, ref, ) { return (