"use strict";
/**
 * Minimal Markdown → HTML for README/docs rendering (no external deps).
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.markdownToHtml = markdownToHtml;
function markdownToHtml(src) {
    const lines = src.replace(/\r\n/g, "\n").split("\n");
    const out = [];
    let inCode = false;
    let codeLang = "";
    let codeBuf = [];
    let inUl = false;
    let inOl = false;
    let inP = false;
    const closeLists = () => {
        if (inUl) {
            out.push("</ul>");
            inUl = false;
        }
        if (inOl) {
            out.push("</ol>");
            inOl = false;
        }
    };
    const closeP = () => {
        if (inP) {
            out.push("</p>");
            inP = false;
        }
    };
    const inline = (s) => {
        let t = escapeHtml(s);
        t = t.replace(/`([^`]+)`/g, "<code>$1</code>");
        t = t.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
        t = t.replace(/\*([^*]+)\*/g, "<em>$1</em>");
        t = t.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
        return t;
    };
    for (const line of lines) {
        if (line.startsWith("```")) {
            if (inCode) {
                out.push(`<pre><code class="language-${escapeHtml(codeLang)}">${escapeHtml(codeBuf.join("\n"))}</code></pre>`);
                inCode = false;
                codeBuf = [];
                codeLang = "";
            }
            else {
                closeP();
                closeLists();
                inCode = true;
                codeLang = line.slice(3).trim();
            }
            continue;
        }
        if (inCode) {
            codeBuf.push(line);
            continue;
        }
        const heading = /^(#{1,6})\s+(.*)$/.exec(line);
        if (heading) {
            closeP();
            closeLists();
            const level = heading[1].length;
            out.push(`<h${level}>${inline(heading[2])}</h${level}>`);
            continue;
        }
        if (/^\s*[-*]\s+/.test(line)) {
            closeP();
            if (!inUl) {
                closeLists();
                out.push("<ul>");
                inUl = true;
            }
            out.push(`<li>${inline(line.replace(/^\s*[-*]\s+/, ""))}</li>`);
            continue;
        }
        if (/^\s*\d+\.\s+/.test(line)) {
            closeP();
            if (!inOl) {
                closeLists();
                out.push("<ol>");
                inOl = true;
            }
            out.push(`<li>${inline(line.replace(/^\s*\d+\.\s+/, ""))}</li>`);
            continue;
        }
        if (line.trim() === "") {
            closeP();
            closeLists();
            continue;
        }
        closeLists();
        if (!inP) {
            out.push("<p>");
            inP = true;
            out.push(inline(line));
        }
        else {
            out.push(" " + inline(line));
        }
    }
    if (inCode) {
        out.push(`<pre><code>${escapeHtml(codeBuf.join("\n"))}</code></pre>`);
    }
    closeP();
    closeLists();
    return sanitizeHtml(out.join("\n"));
}
function escapeHtml(s) {
    return s
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;");
}
/** Strip obviously dangerous tags/attrs (UGC-lite). */
function sanitizeHtml(html) {
    return html
        .replace(/<\s*script[\s\S]*?>[\s\S]*?<\s*\/\s*script\s*>/gi, "")
        .replace(/\son\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "")
        .replace(/javascript:/gi, "");
}
//# sourceMappingURL=markdown.js.map