UNPKG

md-editor-rt

Version:

Markdown editor for react, developed in jsx and typescript, dark theme、beautify content by prettier、render articles directly、paste or clip the picture and upload it...

1,345 lines (1,344 loc) 40.8 kB
import { jsx as Q, jsxs as Ge, Fragment as ze } from "react/jsx-runtime"; import { useContext as D, useEffect as E, useRef as I, useState as O, useCallback as U, useMemo as de, memo as Fe, useId as Ue, useImperativeHandle as qe } from "react"; import { p as g, g as A, d as F, f as we, s as xe } from "./config.mjs"; import { E as N } from "./context.mjs"; import { a as Ve, c as We } from "./index2.mjs"; import Ke from "@vavt/copy2clipboard"; import { randomId as fe } from "@vavt/util"; import Ze from "markdown-it"; import Be from "markdown-it-image-figures"; import Je from "markdown-it-sub"; import Xe from "markdown-it-sup"; import { b as $, E as te, B as ee, f as he, P as Ce, g as ge, T as Ye, O as be, U as Te, C as ve, h as Se, F as Ie, i as Ae, j as He, H as Le, k as _e, R as $e, e as Qe, d as et } from "./event-bus.mjs"; import { a as P, u as tt, S as nt, c as rt, z as st } from "./dom.mjs"; import ot from "medium-zoom"; const M = { hljs: `${g}-hljs`, hlcss: `${g}-hlCss`, prettier: `${g}-prettier`, prettierMD: `${g}-prettierMD`, cropperjs: `${g}-cropper`, croppercss: `${g}-cropperCss`, screenfull: `${g}-screenfull`, mermaidM: `${g}-mermaid-m`, mermaid: `${g}-mermaid`, katexjs: `${g}-katex`, katexcss: `${g}-katexCss`, echarts: `${g}-echarts` }, ct = (t, e, r) => { const { editorId: n, usedLanguageText: s, customIcon: l, rootRef: o, setting: a } = D(N), { formatCopiedText: c = (d) => d } = t; E(() => { a.preview && o.current?.querySelectorAll(`#${n} .${g}-preview .${g}-code`).forEach((d) => { let i = -1; const h = d.querySelector( `.${g}-copy-button:not([data-processed])` ); h && (h.onclick = (u) => { u.preventDefault(), clearTimeout(i); const p = (d.querySelector("input:checked + pre code") || d.querySelector("pre code")).textContent || "", { text: b, successTips: f, failTips: w } = s.copyCode; let v = f; Ke(c(p)).catch(() => { v = w; }).finally(() => { h.dataset.isIcon ? h.dataset.tips = v : h.textContent = v, i = window.setTimeout(() => { h.dataset.isIcon ? h.dataset.tips = b : h.textContent = b; }, 1500); }); }, h.setAttribute("data-processed", "true")); }); }, [ l, n, c, e, r, a.preview, o, s.copyCode ]); }, it = (t) => { const { editorId: e, theme: r, rootRef: n } = D(N), s = I(A.editorExtensions.echarts?.instance), [l, o] = O(0), a = I([]), c = I([]), d = I([]), i = U(() => { !t.noEcharts && s.current && o((m) => m + 1); }, [t.noEcharts]); E(() => { i(); }, [r, i]), E(() => { if (t.noEcharts || s.current) return; const { editorExtensions: m, editorExtensionsAttrs: p } = A, b = m.echarts.js; P( "script", { ...p.echarts?.js, src: b, id: M.echarts, onload() { s.current = window.echarts, i(); } }, "echarts" ); }, [t.noEcharts, i]); const h = U( (m = !1) => { const p = d.current; if (!p.length) { m && (a.current.forEach((v) => v?.dispose?.()), c.current.forEach((v) => v?.disconnect?.()), a.current = [], c.current = [], d.current = []); return; } const b = [], f = [], w = []; p.forEach((v, y) => { const x = a.current[y], C = c.current[y]; if (m || !v || !v.isConnected || (n?.current ? !n.current.contains(v) : !1)) { x?.dispose?.(), C?.disconnect?.(); return; } b.push(v), x && f.push(x), C && w.push(C); }), d.current = b, a.current = f, c.current = w; }, [n] ), u = U(() => { if (h(), !t.noEcharts && s.current && n?.current) { const { editorExtensions: m, echartsConfig: p } = A; Array.from( n.current.querySelectorAll( `#${e} div.${g}-echarts:not([data-processed])` ) ).forEach((f) => { if (f.dataset.closed !== "false") try { const w = m.echarts.parseOption(f.innerText, { editorId: e, element: f }), v = p(w), y = s.current.init(f, r); y.setOption(v), f.setAttribute("data-processed", ""), d.current.push(f), a.current.push(y); const x = new ResizeObserver(() => { y.resize(); }); x.observe(f), c.current.push(x); } catch (w) { $.emit(e, te, { name: "echarts", message: w?.message, error: w }); } }); } }, [t.noEcharts, n, e, r, h]); return E(() => () => { h(!0); }, [h]), { reRenderEcharts: l, replaceEcharts: u }; }, at = (t) => { const { highlight: e } = D(N), r = I(A.editorExtensions.highlight.instance), [n, s] = O(!!r.current); return E(() => { t.noHighlight || A.editorExtensions.highlight.instance || tt("link", { ...e.css, rel: "stylesheet", id: M.hlcss }); }, [e.css, t.noHighlight]), E(() => { t.noHighlight || r.current || P( "script", { ...e.js, id: M.hljs, onload() { r.current = window.hljs, s(!0); } }, "hljs" ); }, []), { hljsRef: r, hljsInited: n }; }, lt = (t) => { const e = I(A.editorExtensions.katex.instance), [r, n] = O(!!e.current); return E(() => { if (t.noKatex || e.current) return; const { editorExtensions: s, editorExtensionsAttrs: l } = A; P( "script", { ...l.katex?.js, src: s.katex.js, id: M.katexjs, onload() { e.current = window.katex, n(!0); } }, "katex" ), P("link", { ...l.katex?.css, rel: "stylesheet", href: s.katex.css, id: M.katexcss }); }, [t.noKatex]), { katexRef: e, katexInited: r }; }; class dt { constructor(e) { this.options = e; } options; cache = /* @__PURE__ */ new Map(); isExpired(e) { return e.expiresAt <= Date.now(); } deleteExpired(e, r) { const n = r ?? this.cache.get(e); return !n || !this.isExpired(n) ? !1 : (this.cache.delete(e), !0); } trim() { for (const [e, r] of this.cache) if (!this.deleteExpired(e, r)) break; for (; this.cache.size > this.options.max; ) { const e = this.cache.keys().next().value; if (e === void 0) break; this.cache.delete(e); } } get(e) { const r = this.cache.get(e); if (!(!r || this.deleteExpired(e, r))) return this.cache.delete(e), this.cache.set(e, r), r.value; } set(e, r) { this.cache.delete(e), this.cache.set(e, { value: r, expiresAt: Date.now() + this.options.ttl }), this.trim(); } clear() { this.cache.clear(); } } const le = new dt({ max: 1e3, // 缓存10分钟 ttl: 6e5 }), ut = (t) => { const { editorId: e, theme: r, rootRef: n } = D(N), { noMermaid: s, sanitizeMermaid: l } = t, o = I(A.editorExtensions.mermaid.instance), [a, c] = O(-1), d = U(() => { le.clear(); const h = o.current; if (!s && h) { const u = r === "dark" ? { startOnLoad: !1, theme: "dark" } : { startOnLoad: !1, theme: "base", themeVariables: { background: "#ffffff", primaryColor: "#ffffff", primaryTextColor: "#1f2329", primaryBorderColor: "#b7c0cc", secondaryColor: "#f7f8fa", tertiaryColor: "#f7f8fa", lineColor: "#596273", edgeLabelBackground: "#ffffff", clusterBkg: "#ffffff", clusterBorder: "#b7c0cc" } }; h.initialize(A.mermaidConfig(u)), c((m) => m + 1); } }, [s, r]); E(d, [d]), E(() => { const { editorExtensions: h, editorExtensionsAttrs: u } = A; if (s || o.current) return; const m = h.mermaid.js; /\.mjs/.test(m) ? (P("link", { ...u.mermaid?.js, rel: "modulepreload", href: m, id: M.mermaidM }), import( /* @vite-ignore */ /* webpackIgnore: true */ m ).then((p) => { o.current = p.default, d(); }).catch((p) => { $.emit(e, te, { name: "mermaid", message: `Failed to load mermaid module: ${p.message}`, error: p }); })) : P( "script", { ...u.mermaid?.js, src: m, id: M.mermaid, onload() { o.current = window.mermaid, d(); } }, "mermaid" ); }, [d, e, s]); const i = U(async () => { if (!s && o.current) { const h = n.current?.querySelectorAll(`div.${g}-mermaid`) || [], u = document.createElement("div"), m = document.body.offsetWidth > 1366 ? document.body.offsetWidth : 1366, p = document.body.offsetHeight > 768 ? document.body.offsetHeight : 768; u.style.width = m + "px", u.style.height = p + "px", u.style.position = "fixed", u.style.zIndex = "-10000", u.style.top = "-10000"; let b = h.length; b > 0 && document.body.appendChild(u), await Promise.allSettled( Array.from(h).map((f) => (async (v) => { if (v.dataset.closed === "false") return !1; const y = v.innerText; let x = le.get(y); if (!x) { const C = fe(); let H = { svg: "" }; try { H = await o.current.render( C, y, u ), x = await l(H.svg); const T = document.createElement("p"); T.className = `${g}-mermaid`, T.setAttribute("data-processed", ""), T.setAttribute("data-content", y), T.innerHTML = x, T.children[0]?.removeAttribute("height"), le.set(y, T.innerHTML), v.dataset.line !== void 0 && (T.dataset.line = v.dataset.line), v.replaceWith(T); } catch (T) { $.emit(e, te, { name: "mermaid", message: T.message, error: T }); } --b === 0 && u.remove(); } })(f)) ); } }, [e, s, n, l]); return { reRender: a, replaceMermaid: i }; }, mt = (t, e) => { e = e || {}; const r = 3, n = e.marker || "!", s = n.charCodeAt(0), l = n.length; let o = "", a = ""; const c = (i, h, u, m, p) => { const b = i[h]; return b.type === "admonition_open" ? i[h].attrPush([ "class", `${g}-admonition ${g}-admonition-${b.info}` ]) : b.type === "admonition_title_open" && i[h].attrPush(["class", `${g}-admonition-title`]), p.renderToken(i, h, u); }, d = (i) => { const h = i.trim().split(" ", 2); a = "", o = h[0], h.length > 1 && (a = i.substring(o.length + 2)); }; t.block.ruler.before( "code", "admonition", (i, h, u, m) => { let p, b, f, w = !1, v = i.bMarks[h] + i.tShift[h], y = i.eMarks[h]; if (s !== i.src.charCodeAt(v)) return !1; for (p = v + 1; p <= y && n[(p - v) % l] === i.src[p]; p++) ; const x = Math.floor((p - v) / l); if (x !== r) return !1; p -= (p - v) % l; const C = i.src.slice(v, p), H = i.src.slice(p, y); if (d(H), m) return !0; for (b = h; b++, !(b >= u || (v = i.bMarks[b] + i.tShift[b], y = i.eMarks[b], v < y && i.sCount[b] < i.blkIndent)); ) if (s === i.src.charCodeAt(v) && !(i.sCount[b] - i.blkIndent >= 4)) { for (p = v + 1; p <= y && n[(p - v) % l] === i.src[p]; p++) ; if (!(Math.floor((p - v) / l) < x) && (p -= (p - v) % l, p = i.skipSpaces(p), !(p < y))) { w = !0; break; } } const T = i.parentType, S = i.lineMax; return i.parentType = "root", i.lineMax = b, f = i.push("admonition_open", "div", 1), f.markup = C, f.block = !0, f.info = o, f.map = [h, b], a && (f = i.push("admonition_title_open", "p", 1), f.markup = C + " " + o, f.map = [h, b], f = i.push("inline", "", 0), f.content = a, f.map = [h, i.line - 1], f.children = [], f = i.push("admonition_title_close", "p", -1), f.markup = C + " " + o), i.md.block.tokenize(i, h + 1, b), f = i.push("admonition_close", "div", -1), f.markup = i.src.slice(v, p), f.block = !0, i.parentType = T, i.lineMax = S, i.line = b + (w ? 1 : 0), !0; }, { alt: ["paragraph", "reference", "blockquote", "list"] } ), t.renderer.rules.admonition_open = c, t.renderer.rules.admonition_title_open = c, t.renderer.rules.admonition_title_close = c, t.renderer.rules.admonition_close = c; }, Ee = (t, e) => { const r = t.attrs ? t.attrs.slice() : []; return e.forEach((n) => { const s = t.attrIndex(n[0]); s < 0 ? r.push(n) : (r[s] = r[s].slice(), r[s][1] += ` ${n[1]}`); }), r; }, pt = (t) => Array.from(t).map( (e) => /[A-Za-z0-9_-]/.test(e) ? e : `_${e.codePointAt(0).toString(16)}_` ).join(""), ht = (t, e) => { const r = t.renderer.rules.fence, n = t.utils.unescapeAll, s = /\[(\w*)(?::([\w ]*))?\]/, l = /::(open|close)/, o = pt(e.editorId), a = (u) => u.info ? n(u.info).trim() : "", c = (u) => { const m = a(u), [p = null, b = ""] = (s.exec(m) || []).slice(1); return [p, b]; }, d = (u) => { const m = a(u); return m ? m.split(/(\s+)/g)[0] : ""; }, i = (u) => { const m = u.info.match(l) || [], p = m[1] === "open" || m[1] !== "close" && e.codeFoldable && u.content.trim().split(` `).length < e.autoFoldThreshold, b = m[1] || e.codeFoldable ? "details" : "div", f = m[1] || e.codeFoldable ? "summary" : "div"; return { open: p, tagContainer: b, tagHeader: f }; }, h = (u, m, p, b, f) => { if (u[m].hidden) return ""; const w = e.usedLanguageTextRef.current.copyCode.text || "", v = e.customIconRef.current.copy, y = t.utils.escapeHtml(w), x = v || y, C = !!v, H = `<span class="${g}-collapse-tips">${nt("collapse-tips", e.customIconRef.current)}</span>`, [T] = c(u[m]); if (T === null) { const { open: L, tagContainer: R, tagHeader: oe } = i(u[m]), G = [["class", `${g}-code`]]; L && G.push(["open", ""]); const ue = { attrs: Ee(u[m], G) }; u[m].info = u[m].info.replace(l, ""); const me = r(u, m, p, b, f); return ` <${R} ${f.renderAttrs(ue)}> <${oe} class="${g}-code-head"> <div class="${g}-code-flag"><span></span><span></span><span></span></div> <div class="${g}-code-action"> <span class="${g}-code-lang">${t.utils.escapeHtml(u[m].info.trim())}</span> <span class="${g}-copy-button" data-tips="${y}"${C ? " data-is-icon=true" : ""}>${x}</span> ${e.extraTools instanceof Function ? e.extraTools({ lang: u[m].info.trim() }) : e.extraTools || ""} ${R === "details" ? H : ""} </div> </${oe}> ${me} </${R}> `; } let S, W, Z, j, q = "", B = "", ne = ""; const { open: re, tagContainer: J, tagHeader: V } = i(u[m]), se = [["class", `${g}-code`]]; re && se.push(["open", ""]); const K = { attrs: Ee(u[m], se) }; for (let L = m; L < u.length && (S = u[L], [W, Z] = c(S), W === T); L++) { S.info = S.info.replace(s, "").replace(l, ""), S.hidden = !0; const R = `${g}-codetab-${o}-${m}-${L - m}`; j = L - m > 0 ? "" : "checked", q += ` <li> <input type="radio" id="label-${g}-codetab-label-1-${o}-${m}-${L - m}" name="${g}-codetab-label-${o}-${m}" class="${R}" ${j} > <label for="label-${g}-codetab-label-1-${o}-${m}-${L - m}" onclick="this.getRootNode().querySelectorAll('.${R}').forEach(e => e.click())" > ${t.utils.escapeHtml(Z || d(S))} </label> </li>`, B += ` <div role="tabpanel"> <input type="radio" name="${g}-codetab-pre-${o}-${m}" class="${R}" ${j} role="presentation"> ${r(u, L, p, b, f)} </div>`, ne += ` <input type="radio" name="${g}-codetab-lang-${o}-${m}" class="${R}" ${j} role="presentation"> <span class=${g}-code-lang role="note">${t.utils.escapeHtml(d(S))}</span>`; } return ` <${J} ${f.renderAttrs(K)}> <${V} class="${g}-code-head"> <div class="${g}-code-flag"> <ul class="${g}-codetab-label" role="tablist">${q}</ul> </div> <div class="${g}-code-action"> <span class="${g}-codetab-lang">${ne}</span> <span class="${g}-copy-button" data-tips="${y}"${C ? " data-is-icon=true" : ""}>${x}</span> ${e.extraTools instanceof Function ? e.extraTools({ lang: u[m].info.trim() }) : e.extraTools || ""} ${J === "details" ? H : ""} </div> </${V}> ${B} </${J}> `; }; t.renderer.rules.fence = h, t.renderer.rules.code_block = h; }, ft = (t, e) => { const r = t.renderer.rules.fence.bind(t.renderer.rules); t.renderer.rules.fence = (n, s, l, o, a) => { const c = n[s], d = c.content.trim(); if (c.info === "echarts") { if (c.attrSet("class", `${g}-echarts`), c.attrSet("data-echarts-theme", e.themeRef.current), c.map && c.level === 0) { const i = c.map[1] - 1, u = !!o.srcLines[i]?.trim()?.startsWith("```"); c.attrSet("data-closed", `${u}`), c.attrSet("data-line", String(c.map[0])); } return `<div ${a.renderAttrs(c)} style="width: 100%; aspect-ratio: 4 / 3;">${t.utils.escapeHtml(d)}</div>`; } return r(n, s, l, o, a); }; }, gt = (t, e) => { t.renderer.rules.heading_open = (r, n) => { const s = r[n], l = r[n + 1].children?.reduce((a, c) => a + (["text", "code_inline", "math_inline"].includes(c.type) && c.content || ""), "") || "", o = s.markup.length; return e.headsRef.current.push({ text: l, level: o, line: s.map[0], currentToken: s, nextToken: r[n + 1] }), s.map && s.level === 0 && s.attrSet( "id", e.mdHeadingId({ text: l, level: o, index: e.headsRef.current.length, currentToken: s, nextToken: r[n + 1] }) ), t.renderer.renderToken(r, n, e); }, t.renderer.rules.heading_close = (r, n, s, l, o) => o.renderToken(r, n, s); }, Re = { block: [ { open: "$$", close: "$$" }, { open: "\\[", close: "\\]" } ], inline: [ { open: "$$", close: "$$" }, { open: "$", close: "$" }, { open: "\\[", close: "\\]" }, { open: "\\(", close: "\\)" } ] }, bt = (t) => (e, r) => { const n = t.delimiters; for (const s of n) { if (!e.src.startsWith(s.open, e.pos)) continue; const l = e.pos + s.open.length; let o = l; for (; (o = e.src.indexOf(s.close, o)) !== -1; ) { let a = 0, c = o - 1; for (; c >= 0 && e.src[c] === "\\"; ) a++, c--; if (a % 2 === 0) break; o += s.close.length; } if (o !== -1) { if (o - l === 0) return r || (e.pending += s.open + s.close), e.pos = o + s.close.length, !0; if (!r) { const a = e.push("math_inline", "math", 0); a.markup = s.open, a.content = e.src.slice(l, o); } return e.pos = o + s.close.length, !0; } } return !1; }, vt = (t) => (e, r, n, s) => { const l = t.delimiters, o = e.bMarks[r] + e.tShift[r], a = e.eMarks[r], c = (d, i, h) => { e.line = i; const u = e.push("math_block", "math", 0); return u.block = !0, u.content = d, u.map = [r, e.line], u.markup = h, !0; }; for (const d of l) { const i = o; if (e.src.slice(i, i + d.open.length) !== d.open) continue; const h = i + d.open.length, u = e.src.slice(h, a).trim(), m = u === "", p = u === d.close, b = u.endsWith(d.close); if (!m && !p && !b) continue; if (s) return !0; if (p) return c("", r + 1, d.open); if (!m && b) { const C = u.slice(0, -d.close.length); return c(C, r + 1, d.open); } let f = r + 1, w = !1, v = ""; for (; f < n; f++) { const C = e.bMarks[f] + e.tShift[f], H = e.eMarks[f]; if (C < H && e.tShift[f] < e.blkIndent) break; if (e.src.slice(C, H).trim().endsWith(d.close)) { const S = e.src.slice(0, H).lastIndexOf(d.close); v = e.src.slice(C, S), w = !0; break; } } if (!w) continue; const x = e.getLines(r + 1, f, e.tShift[r], !0) + (v.trim() ? v : ""); return c(x, f + 1, d.open); } return !1; }, $t = (t, { katexRef: e, inlineDelimiters: r, blockDelimiters: n }) => { const s = (a, c, d, i, h = !1) => { const u = { attrs: Ee(a, [["class", c]]) }, m = i.renderAttrs(u); if (!e.current) return `<${d} ${m}>${a.content}</${d}>`; const p = e.current.renderToString( a.content, A.katexConfig({ throwOnError: !1, displayMode: h }) ); return `<${d} ${m} data-processed>${p}</${d}>`; }, l = (a, c, d, i, h) => s(a[c], `${g}-katex-inline`, "span", h), o = (a, c, d, i, h) => s(a[c], `${g}-katex-block`, "p", h, !0); t.inline.ruler.before( "escape", "math_inline", bt({ delimiters: r || Re.inline }) ), t.block.ruler.after( "blockquote", "math_block", vt({ delimiters: n || Re.block }), { alt: ["paragraph", "reference", "blockquote", "list"] } ), t.renderer.rules.math_inline = l, t.renderer.rules.math_block = o; }, Et = (t, e) => { const r = t.renderer.rules.fence.bind(t.renderer.rules); t.renderer.rules.fence = (n, s, l, o, a) => { const c = n[s], d = c.content.trim(); if (c.info === "mermaid") { if (c.attrSet("class", `${g}-mermaid`), c.attrSet("data-mermaid-theme", e.themeRef.current), c.map && c.level === 0) { const h = c.map[1] - 1, m = !!o.srcLines[h]?.trim()?.startsWith("```"); c.attrSet("data-closed", `${m}`), c.attrSet("data-line", String(c.map[0])); } const i = le.get(d); return i ? (c.attrSet("data-processed", ""), c.attrSet("data-content", d), `<p ${a.renderAttrs(c)}>${i}</p>`) : `<div ${a.renderAttrs(c)}>${t.utils.escapeHtml(d)}</div>`; } return r(n, s, l, o, a); }; }, Me = (t, e, r) => { const n = t.attrIndex(e), s = [e, r]; n < 0 ? t.attrPush(s) : (t.attrs = t.attrs || [], t.attrs[n] = s); }, yt = (t) => t.type === "inline", kt = (t) => t.type === "paragraph_open", wt = (t) => t.type === "list_item_open", xt = (t) => t.content.indexOf("[ ] ") === 0 || t.content.indexOf("[x] ") === 0 || t.content.indexOf("[X] ") === 0, Ct = (t, e) => yt(t[e]) && kt(t[e - 1]) && wt(t[e - 2]) && xt(t[e]), Tt = (t, e) => { const r = t[e].level - 1; for (let n = e - 1; n >= 0; n--) if (t[n].level === r) return n; return -1; }, St = (t) => { const e = new t("html_inline", "", 0); return e.content = "<label>", e; }, It = (t) => { const e = new t("html_inline", "", 0); return e.content = "</label>", e; }, At = (t, e, r) => { const n = new r("html_inline", "", 0); return n.content = '<label class="task-list-item-label" for="' + e + '">' + t + "</label>", n.attrs = [["for", e]], n; }, Ht = (t, e, r) => { const n = new e("html_inline", "", 0), s = r.enabled ? " " : ' disabled="" '; return t.content.indexOf("[ ] ") === 0 ? n.content = '<input class="task-list-item-checkbox"' + s + 'type="checkbox">' : (t.content.indexOf("[x] ") === 0 || t.content.indexOf("[X] ") === 0) && (n.content = '<input class="task-list-item-checkbox" checked=""' + s + 'type="checkbox">'), n; }, Lt = (t, e, r) => { if (t.children = t.children || [], t.children.unshift(Ht(t, e, r)), t.children[1].content = t.children[1].content.slice(3), t.content = t.content.slice(3), r.label) if (r.labelAfter) { t.children.pop(); const n = "task-item-" + Math.ceil(Math.random() * (1e4 * 1e3) - 1e3); t.children[0].content = t.children[0].content.slice(0, -1) + ' id="' + n + '">', t.children.push(At(t.content, n, e)); } else t.children.unshift(St(e)), t.children.push(It(e)); }, _t = (t, e = {}) => { t.core.ruler.after("inline", "github-task-lists", (r) => { const n = r.tokens; for (let s = 2; s < n.length; s++) Ct(n, s) && (Lt(n[s], r.Token, e), Me( n[s - 2], "class", "task-list-item" + (e.enabled ? " enabled" : " ") ), Me(n[Tt(n, s - 2)], "class", "contains-task-list")); }); }, Rt = (t) => { t.core.ruler.push("init-line-number", (e) => (e.tokens.forEach((r) => { r.map && (r.attrs || (r.attrs = []), r.attrs.push(["data-line", r.map[0].toString()])); }), !0)); }, Mt = (t, e) => { const { modelValue: r, sanitize: n, mdHeadingId: s, codeFoldable: l, autoFoldThreshold: o, noKatex: a, noMermaid: c, noHighlight: d, onHtmlChanged: i, onGetCatalog: h } = t, { editorConfig: u, markdownItConfig: m, markdownItPlugins: p, editorExtensions: b } = A, { editorId: f, language: w, showCodeRowNumber: v, theme: y, usedLanguageText: x, customIcon: C, rootRef: H, setting: T } = D(N), S = I([]), W = I(y); E(() => { W.current = y; }, [y]); const Z = I(x); E(() => { Z.current = x; }, [x]); const j = I(C); E(() => { j.current = C; }, [C]); const { hljsRef: q, hljsInited: B } = at(t), { katexRef: ne, katexInited: re } = lt(t), { reRender: J, replaceMermaid: V } = ut(t), { reRenderEcharts: se, replaceEcharts: K } = it(t), [L] = O(() => { const k = Ze({ html: !0, breaks: !0, linkify: !0 }); m(k, { editorId: f }); const _ = [ { type: "image", plugin: Be, options: { figcaption: !0, classes: "md-zoom" } }, { type: "admonition", plugin: mt, options: {} }, { type: "taskList", plugin: _t, options: {} }, { type: "heading", plugin: gt, options: { mdHeadingId: s, headsRef: S } }, { type: "code", plugin: ht, options: { editorId: f, usedLanguageTextRef: Z, // showCodeRowNumber, codeFoldable: l, autoFoldThreshold: o, customIconRef: j } }, { type: "sub", plugin: Je, options: {} }, { type: "sup", plugin: Xe, options: {} } ]; a || _.push({ type: "katex", plugin: $t, options: { katexRef: ne } }), c || _.push({ type: "mermaid", plugin: Et, options: { themeRef: W } }), t.noEcharts || _.push({ type: "echarts", plugin: ft, options: { themeRef: W } }), p(_, { editorId: f }).forEach((z) => { k.use(z.plugin, z.options); }); const X = k.options.highlight; return k.set({ highlight: (z, ae, De) => { if (X) { const pe = X(z, ae, De); if (pe) return pe; } let Y; !d && q.current ? q.current.getLanguage(ae) ? Y = q.current.highlight(z, { language: ae, ignoreIllegals: !0 }).value : Y = q.current.highlightAuto(z).value : Y = k.utils.escapeHtml(z); const ke = k.utils.escapeHtml(ae), Ne = v ? Ve( Y.replace(/^\n+|\n+$/g, ""), z.replace(/^\n+|\n+$/g, "") ) : `<span class="${g}-code-block">${Y.replace(/^\n+|\n+$/g, "")}</span>`; return `<pre><code class="language-${ke}" language="${ke}">${Ne}</code></pre>`; } }), Rt(k), k; }), [R, oe] = O(`_article-key_${fe()}`), [G, ue] = O(() => (S.current = [], n( L.render(r, { srcLines: r.split(` `) }) ))), me = de(() => (d || B) && (a || re), [B, re, d, a]), ye = I(!0), ce = U(() => { S.current = []; const k = n( L.render(r, { srcLines: r.split(` `) }) ); ue(k); }, [L, r, n]), ie = U(() => { let k = () => { }, _ = () => { }; const X = H.current?.querySelectorAll( `#${f} p.${g}-mermaid:not([data-closed=false])` ); return _ = rt(X, { customIcon: j.current }), b.mermaid?.enableZoom && (k = st(X, { customIcon: j.current })), [k, _]; }, [b.mermaid?.enableZoom, f, H]); return E(() => { $.emit(f, ee, G), i?.(G), h?.(S.current), $.emit(f, he, S.current); }, [f, G, R, h, i]), E(() => { let k = () => { }, _ = () => { }; return T.preview && (V().then(() => { [k, _] = ie(); }), K(), $.emit(f, he, S.current)), () => { k(), _(); }; }, [f, ie, K, V, T.preview]), E(() => { if (ye.current) { ye.current = !1; return; } const k = setTimeout( () => { ce(); }, e ? 0 : u.renderDelay ); return () => { clearTimeout(k); }; }, [me, y, ce, w, e, u.renderDelay]), E(() => { let k = () => { }, _ = () => { }; return V().then(() => { [k, _] = ie(); }), K(), () => { k(), _(); }; }, [ ie, G, R, J, V, se, K ]), E(() => { const k = () => { $.emit(f, he, S.current); }; return $.on(f, { name: Ce, callback: k }), () => { $.remove(f, Ce, k); }; }, [f]), E(() => { const k = () => { oe(`_article-key_${fe()}`), ce(); }; return $.on(f, { name: ge, callback: k }), () => { $.remove(f, ge, k); }; }, [f, ce]), { html: G, key: R }; }, Pt = (t, e) => { const { editorId: r, setting: n } = D(N); E(() => t.noImgZoomIn ? void 0 : (() => { const l = document.querySelectorAll( `#${r}-preview img:not(.not-zoom):not(.medium-zoom-image)` ), o = ot(l, { background: "#00000073" }); return () => { o.detach(); }; })(), [r, e, t.noImgZoomIn, n]); }, Pe = { checked: { regexp: /- \[x\]/, value: "- [ ]" }, unChecked: { regexp: /- \[\s\]/, value: "- [x]" } }, Ot = (t, e) => { const { editorId: r, rootRef: n } = D(N); E(() => { const s = n.current?.querySelectorAll(".task-list-item.enabled") || [], l = (o) => { o.preventDefault(); const a = o.target.checked ? "unChecked" : "checked", c = o.target.parentElement?.dataset.line; if (!c) return; const d = Number(c), i = t.modelValue.split(` `), h = i[Number(d)].replace( Pe[a].regexp, Pe[a].value ); t.previewOnly ? (i[Number(d)] = h, t.onChange(i.join(` `))) : $.emit(r, Ye, d + 1, h); }; return s.forEach((o) => { o.addEventListener("click", l); }), () => { s.forEach((o) => { o.removeEventListener("click", l); }); }; }, [r, e, t, n]); }, jt = (t, e, r) => { const { onRemount: n } = t, { setting: s } = D(N); E(() => { n?.(); }, [e, r, n]), E(() => { (s.preview || s.htmlPreview) && n?.(); }, [s.preview, s.htmlPreview, n]); }, Oe = (t) => { const r = new DOMParser().parseFromString(t, "text/html"); return Array.from(r.body.childNodes); }, Ft = (t, e) => t.nodeType !== e.nodeType ? !1 : t.nodeType === Node.TEXT_NODE || t.nodeType === Node.COMMENT_NODE ? t.textContent === e.textContent : t.nodeType === Node.ELEMENT_NODE ? t.outerHTML === e.outerHTML : t.isEqualNode ? t.isEqualNode(e) : !1, Dt = (t, e, r) => { const n = Array.from(t.childNodes), s = Math.min(e.length, r.length); let l = -1; for (let a = 0; a < s; a += 1) if (!Ft(e[a], r[a])) { l = a; break; } if (l === -1) if (r.length > e.length) l = e.length; else if (e.length > r.length) l = r.length; else return; const o = Math.min(l, n.length); for (let a = n.length - 1; a >= o; a -= 1) n[a].remove(); for (let a = l; a < e.length; a += 1) t.appendChild(e[a].cloneNode(!0)); }, Nt = ({ html: t, id: e, className: r }) => { const n = I(null), s = I({ __html: t }), l = I(t); return E(() => { const o = n.current; if (!o) return; const a = l.current; if (a === t) return; const c = Oe(t), d = Oe(a); Dt(o, c, d), l.current = t; }, [t]), /* @__PURE__ */ Q( "div", { id: e, className: r, dangerouslySetInnerHTML: s.current, ref: n } ); }, Gt = Fe(Nt), zt = (t) => { const { previewOnly: e = !1, setting: r = { preview: !0 }, previewComponent: n = Gt } = t, { editorId: s, previewTheme: l, showCodeRowNumber: o } = D(N), { html: a, key: c } = Mt(t, !!e); ct(t, a, c), Pt(t, a), Ot(t, a), jt(t, a, c); const d = de(() => /* @__PURE__ */ Q( n, { html: a, id: `${s}-preview`, className: We([ `${g}-preview`, `${l || "default"}-theme`, o && `${g}-scrn` ]) }, c ), [n, s, a, c, l, o]); return /* @__PURE__ */ Ge(ze, { children: [ r.preview && (e ? d : /* @__PURE__ */ Q( "div", { id: `${s}-preview-wrapper`, className: `${g}-preview-wrapper`, children: d }, "content-preview-wrapper" )), r.htmlPreview && /* @__PURE__ */ Q( "div", { id: `${s}-html-wrapper`, className: `${g}-preview-wrapper`, children: /* @__PURE__ */ Q("div", { className: `${g}-html`, children: a }) }, "html-preview-wrapper" ) ] }); }, sn = Fe(zt), on = (t, e) => { const { value: r, modelValue: n, onSave: s } = t, { editorId: l } = e, [o, a] = O({ // 是否已编译成html buildFinished: !1, // 存储当前最新的html html: "" }); E(() => { const c = (d) => { a(() => ({ buildFinished: !0, html: d })); }; return $.on(l, { name: ee, callback: c }), () => { $.remove(l, ee, c); }; }, [l]), E(() => { const c = () => { if (s) { const d = new Promise((i) => { if (o.buildFinished) i(o.html); else { const h = (u) => { i(u), $.remove(l, ee, h); }; $.on(l, { name: ee, callback: h }); } }); s(r || n || "", d); } }; return $.on(l, { name: be, callback: c }), () => { $.remove(l, be, c); }; }, [l, n, s, o.buildFinished, o.html, r]), E(() => { a((c) => ({ ...c, buildFinished: !1 })); }, [r, n]); }, cn = (t) => { const { noPrettier: e, noUploadImg: r } = t; E(() => { const { editorExtensions: n, editorExtensionsAttrs: s } = A, l = e || !!n.prettier.prettierInstance, o = e || !!n.prettier.parserMarkdownInstance; if (!(r || !!n.cropper.instance)) { const { js: c = {}, css: d = {} } = s.cropper || {}; P("link", { ...d, rel: "stylesheet", href: n.cropper.css, id: M.croppercss }), P("script", { ...c, src: n.cropper.js, id: M.cropperjs }); } if (!l) { const { standaloneJs: c = {} } = s.prettier || {}; P("script", { ...c, src: n.prettier.standaloneJs, id: M.prettier }); } if (!o) { const { parserMarkdownJs: c = {} } = s.prettier || {}; P("script", { ...c, src: n.prettier.parserMarkdownJs, id: M.prettierMD }); } }, [e, r]); }, an = (t, e) => { E(() => ($.on(t, { name: te, callback: e }), () => { $.remove(t, te, e); }), [t, e]); }, ln = (t, e) => { const { editorId: r } = e, { onUploadImg: n } = t; E(() => { const s = (l, o) => { n?.(l, (c) => { $.emit(r, $e, "image", { desc: "", urls: c }), o?.(); }); }; return $.on(r, { name: Te, callback: s }), () => { $.remove(r, Te, s); }; }, [r, n]); }, dn = (t, e) => { const { editorId: r } = e, [n, s] = O(!1); return E(() => { const l = (o) => { s(o === void 0 ? (a) => !a : o); }; return $.on(r, { name: ve, callback: l }), () => { $.remove(r, ve, l); }; }, [r]), n; }; let je = ""; const Ut = (t) => { const { theme: e = F.theme, previewTheme: r = F.previewTheme, codeTheme: n = F.codeTheme, language: s = F.language, codeStyleReverse: l = F.codeStyleReverse, codeStyleReverseList: o = F.codeStyleReverseList } = t, a = de(() => { const d = A.editorExtensions.highlight, i = A.editorExtensionsAttrs.highlight, { js: h } = d, u = { ...we, ...d.css }, { js: m, css: p = {} } = i || {}, b = l && o.includes(r) ? "dark" : e, f = u[n] ? u[n][b] : we.atom[b], w = u[n] && p[n] ? p[n][b] : p.atom ? p.atom[b] : {}; return { js: { src: h, ...m }, css: { href: f, ...w } }; }, [l, o, r, e, n]), c = de(() => { const d = { ...xe, ...A.editorConfig.languageUserDefined }; return d[s] ? d[s] : xe["zh-CN"]; }, [s]); return [a, c]; }, un = (t) => { const { preview: e = F.preview, htmlPreview: r = F.htmlPreview, pageFullscreen: n = F.pageFullscreen } = t, [s, l] = Ut(t), [o, a] = O({ pageFullscreen: n, fullscreen: !1, preview: e, htmlPreview: e ? !1 : r, previewOnly: !1 }), c = I(o), d = U((i, h) => { a((u) => { const m = h === void 0 ? !u[i] : h, p = { ...u }; switch (i) { case "preview": { p.htmlPreview = !1, p.previewOnly = !1; break; } case "htmlPreview": { p.preview = !1, p.previewOnly = !1; break; } case "previewOnly": { m ? !p.preview && !p.htmlPreview && (p.preview = !0) : (c.current.preview || (p.preview = !1), c.current.htmlPreview || (p.htmlPreview = !1)); break; } } return c.current[i] = m, p[i] = m, p; }); }, []); return E(() => { je = document.body.style.overflow; }, []), E(() => { o.pageFullscreen || o.fullscreen ? document.body.style.overflow = "hidden" : document.body.style.overflow = je; }, [o.pageFullscreen, o.fullscreen]), [s, l, o, d]; }, mn = (t, e, r, n, s, l) => { const { editorId: o } = e; E(() => { $.emit(o, Se, n.pageFullscreen); }, [o, n.pageFullscreen]), E(() => { $.emit(o, Ie, n.fullscreen); }, [o, n.fullscreen]), E(() => { $.emit(o, Ae, n.preview); }, [o, n.preview]), E(() => { $.emit(o, He, n.previewOnly); }, [o, n.previewOnly]), E(() => { $.emit(o, Le, n.htmlPreview); }, [o, n.htmlPreview]), E(() => { $.emit(o, _e, r); }, [r, o]), qe(t, () => ({ on(c, d) { switch (c) { case "pageFullscreen": { $.on(o, { name: Se, callback(i) { d(i); } }); break; } case "fullscreen": { $.on(o, { name: Ie, callback(i) { d(i); } }); break; } case "preview": { $.on(o, { name: Ae, callback(i) { d(i); } }); break; } case "previewOnly": { $.on(o, { name: He, callback(i) { d(i); } }); break; } case "htmlPreview": { $.on(o, { name: Le, callback(i) { d(i); } }); break; } case "catalog": { $.on(o, { name: _e, callback(i) { d(i); } }); break; } } }, togglePageFullscreen(c) { s("pageFullscreen", c); }, toggleFullscreen(c) { $.emit(o, et, c); }, togglePreview(c) { s("preview", c); }, togglePreviewOnly(c) { s("previewOnly", c); }, toggleHtmlPreview(c) { s("htmlPreview", c); }, toggleCatalog(c) { $.emit(o, ve, c); }, triggerSave() { $.emit(o, be); }, insert(c) { $.emit(o, $e, "universal", { generate: c }); }, focus(c) { l.current?.focus(c); }, rerender() { $.emit(o, ge); }, getSelectedText() { return l.current?.getSelectedText(); }, resetHistory() { l.current?.resetHistory(); }, domEventHandlers(c) { $.emit(o, Qe, c); }, execCommand(c) { $.emit(o, $e, c); }, getEditorView() { return l.current?.getEditorView(); } }), [l, o, s]); }, pn = (t) => { const e = Ue(); return t.id || t.editorId || g + "-" + e.replaceAll(":", ""); }; export { M as C, sn as a, on as b, cn as c, ln as d, an as e, dn as f, un as g, mn as h, Ut as i, pn as u };