import { join, resolve } from "node:path";
import { InternalError } from "../util/internalError.js";
import { getSettings } from "../util/settings.js";
export class MarkdownSite {
    #pages = new Map();
    buildPagePath(slug) {
        const settings = getSettings();
        return resolve(join(settings.output.pageOutDir, `${slug}.md`));
    }
    hasPage(path) {
        return this.#pages.has(path);
    }
    createPage(path) {
        const renderer = this.getRenderer({
            currentPagePath: path,
        });
        this.#pages.set(path, renderer);
        return renderer;
    }
    render() {
        const pages = {};
        for (const [path, renderer] of this.#pages) {
            pages[path] = renderer.render();
        }
        return pages;
    }
    getRenderer(_options) {
        throw new InternalError("getRenderer was not overridden");
    }
}
export const rendererLines = Symbol();
export class MarkdownRenderer {
    #isFinalized = false;
    [rendererLines] = [];
    // TODO: don't escape if they're already escaped
    escapeText(text, { escape }) {
        switch (escape) {
            case "markdown":
                return (text
                    .replaceAll("\\", "\\\\")
                    .replaceAll("`", "\\`")
                    .replaceAll("*", "\\*")
                    .replaceAll("_", "\\_")
                    .replaceAll("{", "\\{")
                    .replaceAll("}", "\\}")
                    .replaceAll("[", "\\[")
                    .replaceAll("]", "\\]")
                    .replaceAll("<", "\\<")
                    .replaceAll(">", "\\>")
                    .replaceAll("(", "\\(")
                    .replaceAll(")", "\\)")
                    .replaceAll("#", "\\#")
                    .replaceAll("+", "\\+")
                    // .replace("-", "\\-")
                    // .replace(".", "\\.")
                    .replaceAll("!", "\\!")
                    .replaceAll("|", "\\|"));
            case "mdx":
                return text.replaceAll("{", "\\{").replaceAll("}", "\\}");
            case "html":
                return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
            case "none":
                return text;
        }
    }
    insertFrontMatter(_) {
        throw new Error("This renderer does not support front matter");
    }
    appendHeading(level, text, { escape = "markdown" } = {}) {
        this[rendererLines].push(`#`.repeat(level) + " " + this.escapeText(text, { escape }));
    }
    appendText(text, { escape = "mdx" } = {}) {
        this[rendererLines].push(this.escapeText(text, { escape }));
    }
    appendCodeBlock(text, options) {
        if (options?.variant === "raw") {
            this[rendererLines].push(`<pre>
<code>
${text}
</code>
</pre>`);
        }
        else {
            this[rendererLines].push(`\`\`\`${options?.language ?? ""}\n${text}\n\`\`\``);
        }
    }
    appendList(items, { escape = "markdown" } = {}) {
        this[rendererLines].push(items.map((item) => "- " + this.escapeText(item, { escape })).join("\n"));
    }
    appendRaw(text) {
        this[rendererLines].push(text);
    }
    beginExpandableSection(title, { isOpenOnLoad = false, escape = "markdown", }) {
        this[rendererLines].push(`<details ${isOpenOnLoad ? "open" : ""}>`);
        this[rendererLines].push(`<summary>${this.escapeText(title, { escape })}</summary>`);
    }
    endExpandableSection() {
        this[rendererLines].push("</details>");
    }
    appendSidebarLink(_) {
        throw new Error("This renderer does not support sidebar links");
    }
    appendTryItNow(_) {
        throw new Error("This renderer does not support try it now");
    }
    render() {
        if (this.#isFinalized) {
            throw new InternalError("Renderer has already been finalized");
        }
        const data = this[rendererLines].join("\n\n");
        this.#isFinalized = true;
        return data;
    }
}
//# sourceMappingURL=markdown.js.map