import { a as e, r as t } from "./sanitize-uWpY18N9.js";
import { n, t as r } from "./utils-CSEWuKHo.js";
//#region src/display/markdown-viewer/parser.ts
var i = 0;
function a() {
	i = 0;
}
function o(e, t) {
	let n = t?.supportMermaid ?? !1, r = [], i = y(e, r, n);
	return i = T(i), i = E(i), i = D(i), i = O(i), i = N(i), i = P(i), i = F(i), i = I(i), i = L(i), i = z(i), i = B(i), i = V(i), i = h(i), i = b(i, r), i;
}
var s = "ul|ol|li|h[1-6]|blockquote|table|thead|tbody|tr|th|td|hr", c = RegExp(`(<(?:${s})\\b[^>]*>)(?:\\s*<br>)+`, "g"), l = RegExp(`(?:<br>\\s*)+(</(?:${s})>)`, "g"), u = RegExp(`(</(?:${s})>)(?:\\s*<br>)+`, "g"), d = RegExp(`(?:<br>\\s*)+(<(?:${s})\\b[^>]*>)`, "g"), f = "\0", p = RegExp(`(?:<br>\\s*)+(${f}(?:CB|MM))`, "g"), m = RegExp(`(${f})(?:\\s*<br>)+`, "g");
function h(e) {
	return e.replace(/\n/g, "<br>").replace(c, "$1").replace(l, "$1").replace(u, "$1").replace(d, "$1").replace(p, "$1").replace(m, "$1");
}
var g = "\0CB", _ = "\0MM", v = /\x00MM([^\x01]+)\x01([^\x00]*)\x00/g;
function y(e, t, n) {
	return e.replace(/```(\w*)\n([\s\S]*?)```/g, (e, r, a) => {
		if (n && r === "mermaid") {
			let e = G(H(a));
			return i += 1, `${_}${`cmm-mermaid-ph-${String(i)}`}\x01${e}\x00`;
		}
		i += 1;
		let o = `chat-code-${String(i)}`, s = H(a);
		return t.push({
			language: r,
			code: s,
			id: o
		}), `${g}${String(t.length - 1)}\x00`;
	});
}
function b(e, t) {
	return e = S(e), e = C(e, t), e;
}
function x(e, t) {
	if (!(t < 0 || t >= e.length)) return e[t];
}
function S(e) {
	return e.replace(v, (e, t, n) => `<div id="${W(t)}" class="cmm-mermaid-placeholder" data-mermaid-content="${W(n)}"></div>`);
}
function C(e, t) {
	let n = "", r = 0;
	for (; r < e.length;) {
		let i = e.indexOf(g, r);
		if (i === -1) {
			n += e.slice(r);
			break;
		}
		n += e.slice(r, i);
		let a = i + 3, o = e.indexOf("\0", a);
		if (o === -1) {
			n += e.slice(i);
			break;
		}
		let s = e.slice(a, o), c = Number.parseInt(s, 10), l = String(c) === s ? x(t, c) : void 0;
		l && (n += w(l)), r = o + 1;
	}
	return n;
}
function w(e) {
	let t = e.language.length > 0 ? `<span class="chat-code__lang">${W(e.language)}</span>` : "", n = U(e.code);
	return `<div class="chat-code-block" id="${e.id}" data-lang="${W(e.language)}">
<div class="chat-code__header">
${t}
<button type="button" class="chat-code__copy" data-copy-code="${e.id}" title="Copy code">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>
<span class="chat-code__copy-label">Copy</span>
</button>
</div>
<pre><code class="language-${W(e.language)}">${n}</code></pre>
</div>`;
}
function T(e) {
	return e.replace(/^(#{1,6})\s+(.+)$/gm, (e, t, n) => {
		let r = t.length;
		return `<h${String(r)} class="chat-md-heading chat-md-h${String(r)}">${n}</h${String(r)}>`;
	});
}
function E(e) {
	return e.replace(/^(?:---+|\*\*\*+|___+)\s*$/gm, "<hr class=\"chat-md-hr\">");
}
function D(e) {
	let t = e.split("\n"), n = [], r = !1;
	for (let e of t) {
		let t = /^&gt;\s?(.*)$/.exec(e);
		t ? (r ||= (n.push("<blockquote class=\"chat-md-blockquote\">"), !0), n.push(t[1] ?? "")) : (r &&= (n.push("</blockquote>"), !1), n.push(e));
	}
	return r && n.push("</blockquote>"), n.join("\n");
}
function O(e) {
	let t = e.split("\n"), n = [], r = 0;
	for (; r < t.length;) {
		let e = t[r] ?? "";
		if (k(e) && r + 1 < t.length && A(t[r + 1] ?? "")) {
			let i = [e];
			for (r++, i.push(t[r] ?? ""), r++; r < t.length && k(t[r] ?? "");) i.push(t[r] ?? ""), r++;
			n.push(j(i));
		} else n.push(e), r++;
	}
	return n.join("\n");
}
function k(e) {
	let t = e.trim();
	return t.includes("|") && !t.startsWith("|--");
}
function A(e) {
	let t = e.trim();
	return /^\|?[\s:]*-{2,}[\s:]*(\|[\s:]*-{2,}[\s:]*)*\|?\s*$/.test(t);
}
function j(e) {
	return e.length < 2 ? e.join("\n") : `<table class="chat-md-table"><thead><tr>${M(e[0] ?? "").map((e) => `<th>${e.trim()}</th>`).join("")}</tr></thead><tbody>${e.slice(2).map((e) => `<tr>${M(e).map((e) => `<td>${e.trim()}</td>`).join("")}</tr>`).join("")}</tbody></table>`;
}
function M(e) {
	let t = e.trim();
	return t.startsWith("|") && (t = t.slice(1)), t.endsWith("|") && (t = t.slice(0, -1)), t.split("|");
}
function N(e) {
	let t = e.split("\n"), n = [], r = !1, i = !1;
	for (let e of t) {
		let t = /^(\s*)[-*+]\s+(.+)$/.exec(e), a = /^(\s*)\d+\.\s+(.+)$/.exec(e);
		t ? (i &&= (n.push("</ol>"), !1), r ||= (n.push("<ul class=\"chat-md-list\">"), !0), n.push(`<li>${t[2] ?? ""}</li>`)) : a ? (r &&= (n.push("</ul>"), !1), i ||= (n.push("<ol class=\"chat-md-list\">"), !0), n.push(`<li>${a[2] ?? ""}</li>`)) : (r &&= (n.push("</ul>"), !1), i &&= (n.push("</ol>"), !1), n.push(e));
	}
	return r && n.push("</ul>"), i && n.push("</ol>"), n.join("\n");
}
function P(e) {
	return e.replace(/~~([^~]+)~~/g, "<del>$1</del>");
}
function F(e) {
	return e.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
}
function I(e) {
	return e.replace(/\*([^*]+)\*/g, "<em>$1</em>");
}
function L(e) {
	return e.replace(/`([^`]+)`/g, "<code class=\"chat-md-inline-code\">$1</code>");
}
var R = /^(?:https?:|mailto:|tel:|#|\/)/i;
function z(e) {
	return e.replace(/(?<!!)\[([^\]]+)\]\(([^)]+)\)/g, (e, t, n) => {
		let r = n.trim();
		return R.test(r) ? `<a href="${W(r)}" class="chat-md-link" target="_blank" rel="noopener noreferrer">${W(t)}</a>` : `<a class="chat-md-link">${W(t)}</a>`;
	});
}
function B(e) {
	return e.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (e, t, n) => {
		let r = n.trim();
		return /^(?:https?:|data:image\/|\/)/i.test(r) ? `<img src="${W(r)}" alt="${W(t)}" class="chat-md-image" loading="lazy" />` : `<span class="chat-md-image" role="img" aria-label="${W(t)}">[Image: ${W(t)}]</span>`;
	});
}
function V(e) {
	return e.replace(/<li>\[x\]\s*/gi, "<li class=\"chat-md-task chat-md-task--checked\"><input type=\"checkbox\" checked disabled /> ").replace(/<li>\[ \]\s*/g, "<li class=\"chat-md-task\"><input type=\"checkbox\" disabled /> ");
}
function H(e) {
	return e.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&#39;/g, "'");
}
function U(e) {
	return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function W(e) {
	return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#039;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function G(e) {
	let t = new TextEncoder().encode(e), n = "";
	for (let e of t) n += String.fromCharCode(e);
	return btoa(n);
}
function K(e) {
	let t = atob(e), n = new Uint8Array(t.length);
	for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
	return new TextDecoder().decode(n);
}
var q = {
	transformHeadings: T,
	transformHorizontalRules: E,
	transformBlockquotes: D,
	transformTables: O,
	transformLists: N,
	transformStrikethrough: P,
	transformBold: F,
	transformItalic: I,
	transformInlineCode: L,
	transformLinks: z,
	transformImages: B,
	transformTaskLists: V,
	renderCodeBlock: w,
	decodeHtmlEntities: H,
	escapeForCodeBlock: U
}, J = class {
	el;
	viewerId;
	mermaidTheme;
	supportMermaid;
	currentContent;
	mermaidInstances = [];
	constructor(i) {
		this.viewerId = n("cmm-markdown"), this.currentContent = i.content, this.supportMermaid = i.renderMermaid ?? !1, this.mermaidTheme = i.mermaidTheme ?? "auto", this.el = document.createElement("div"), this.el.id = this.viewerId, this.el.className = "cmm-markdown", i.className && this.el.classList.add(i.className);
		let a = t(this.currentContent);
		e(this.el, o(a, { supportMermaid: this.supportMermaid }), { allowSvg: !0 }), i.container !== void 0 && r(i.container).appendChild(this.el), this.supportMermaid && this.renderMermaidDiagrams();
	}
	setContent(n) {
		this.destroyMermaidInstances(), this.currentContent = n;
		let r = t(this.currentContent);
		e(this.el, o(r, { supportMermaid: this.supportMermaid }), { allowSvg: !0 }), this.supportMermaid && this.renderMermaidDiagrams();
	}
	async renderMermaidDiagrams() {
		let e = this.el.querySelectorAll(".cmm-mermaid-placeholder");
		if (e.length === 0) return;
		let { MermaidDiagram: t } = await import("./visualization/mermaid-diagram/index.js");
		for (let n of e) {
			let e = n.dataset.mermaidContent;
			if (!(e === void 0 || e === "")) try {
				let r = new t({
					container: n,
					content: K(e),
					theme: this.mermaidTheme
				});
				n.classList.remove("cmm-mermaid-placeholder"), n.classList.add("cmm-mermaid-rendered"), this.mermaidInstances.push(r);
			} catch (e) {
				n.classList.add("cmm-mermaid-error");
				let t = document.createElement("div");
				t.className = "cmm-mermaid-diagram__error";
				let r = document.createElement("div");
				r.className = "cmm-mermaid-diagram__error-title", r.textContent = "Failed to render diagram";
				let i = document.createElement("div");
				i.className = "cmm-mermaid-diagram__error-message", i.textContent = String(e), t.append(r, i), n.replaceChildren(t);
			}
		}
	}
	destroyMermaidInstances() {
		for (let e of this.mermaidInstances) e.destroy();
		this.mermaidInstances = [];
	}
	destroy() {
		this.destroyMermaidInstances(), this.el.remove();
	}
};
//#endregion
export { o as a, q as i, K as n, a as o, G as r, J as t };

//# sourceMappingURL=markdown-viewer-CtWzMYrG.js.map