import { SidebarOverlay } from "./overlay.js"; // Global helper for code block copying in Markdown view window.moonCopyCode = function(button) { try { const container = button.parentElement; if (!container) return; const codeEl = container.querySelector("code"); if (!codeEl) return; const text = codeEl.innerText; navigator.clipboard.writeText(text).then(() => { button.innerText = "Copied!"; button.style.backgroundColor = "#4CAF50"; button.style.borderColor = "#45a049"; button.style.color = "#ffffff"; setTimeout(() => { button.innerText = "Copy"; button.style.backgroundColor = "#333333"; button.style.borderColor = "#555555"; button.style.color = "#aaaaaa"; }, 1500); }).catch(() => { button.innerText = "Error"; setTimeout(() => button.innerText = "Copy", 1500); }); } catch (e) { button.innerText = "Error"; setTimeout(() => button.innerText = "Copy", 1500); } }; function parseInlineMarkdown(html) { if (!html) return ""; html = html.replace(/`([^`]+)`/g, '$1'); html = html.replace(/\*\*\*(.*?)\*\*\*/g, '$1'); html = html.replace(/\*\*(.*?)\*\*/g, '$1'); html = html.replace(/__(.*?)__/g, '$1'); html = html.replace(/\*(.*?)\*/g, '$1'); html = html.replace(/_(.*?)_/g, '$1'); html = html.replace(/~~(.*?)~~/g, '$1'); html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1'); html = html.replace(/\$(?!\s)([^\$]+?)(?$1'); return html; } function parseMarkdown(text) { if (!text) return ""; let rawLines = text.replace(/&/g, "&").replace(/"); inUl = false; } } function closeOl() { if (inOl) { htmlLines.push(""); inOl = false; } } function closeTable() { if (inTable) { let tableHtml = ``; if (tableHeaders.length > 0) { tableHtml += ``; for (let i = 0; i < tableHeaders.length; i++) { const align = tableAlignments[i] || 'left'; tableHtml += ``; } tableHtml += ``; } tableHtml += ""; for (let r = 0; r < tableRows.length; r++) { const row = tableRows[r]; const bg = r % 2 === 0 ? 'rgba(255,255,255,0.015)' : 'transparent'; tableHtml += ``; for (let i = 0; i < tableHeaders.length; i++) { const align = tableAlignments[i] || 'left'; tableHtml += ``; } tableHtml += ``; } tableHtml += "
${parseInlineMarkdown(tableHeaders[i])}
${parseInlineMarkdown(row[i] || '')}
"; htmlLines.push(tableHtml); inTable = false; tableHeaders = []; tableAlignments = []; tableRows = []; } } for (let line of rawLines) { const trimmed = line.trim(); if (trimmed.startsWith("```")) { closeUl(); closeOl(); closeTable(); if (inCodeBlock) { const rCode = codeContent.join("\n"); const langBadge = codeLang ? `
${codeLang}
` : ''; const btnStyle = "position:absolute;top:6px;right:6px;background:#333;color:#aaa;border:1px solid #555;border-radius:3px;padding:3px 8px;font-size:10px;cursor:pointer;"; htmlLines.push(`
${langBadge}
${rCode}
`); codeContent = []; codeLang = ""; inCodeBlock = false; } else { inCodeBlock = true; codeLang = trimmed.slice(3).trim(); } continue; } if (inCodeBlock) { codeContent.push(line); continue; } if (trimmed === "---" || trimmed === "***" || trimmed === "___") { closeUl(); closeOl(); closeTable(); htmlLines.push(`
`); continue; } if (trimmed.startsWith("$$") && trimmed.endsWith("$$")) { closeUl(); closeOl(); closeTable(); htmlLines.push(`
${trimmed.slice(2, -2).trim()}
`); continue; } if (trimmed.startsWith("|") && trimmed.endsWith("|")) { closeUl(); closeOl(); const isSeparator = /^\|[\s\-\:\s\|]+\|$/.test(trimmed); const cells = trimmed.split("|").slice(1, -1).map(c => c.trim()); if (isSeparator) { tableAlignments = cells.map(c => (c.startsWith(":") && c.endsWith(":")) ? "center" : c.endsWith(":") ? "right" : "left"); inTable = true; } else { if (!inTable) { tableHeaders = cells; inTable = true; } else { tableRows.push(cells); } } continue; } else { closeTable(); } if (trimmed.startsWith(">")) { closeUl(); closeOl(); const match = trimmed.match(/^([>\s]+)(.*)/); if (match) { const depth = (match[1].match(/>/g) || []).length; let quote = parseInlineMarkdown(match[2].trim()); for (let d = 0; d < depth; d++) { quote = `
${quote}
`; } htmlLines.push(quote); } continue; } if (line.startsWith("### ")) { closeUl(); closeOl(); htmlLines.push(`

${parseInlineMarkdown(line.slice(4))}

`); continue; } else if (line.startsWith("## ")) { closeUl(); closeOl(); htmlLines.push(`

${parseInlineMarkdown(line.slice(3))}

`); continue; } else if (line.startsWith("# ")) { closeUl(); closeOl(); htmlLines.push(`

${parseInlineMarkdown(line.slice(2))}

`); continue; } const ulMatch = line.match(/^(\s*)([-*])\s(.*)/); if (ulMatch) { closeUl(); if (!inUl) { htmlLines.push(`