@vuux/editor
Version:
Vue Nuxt 富文本编辑器
44 lines (43 loc) • 1.72 kB
JavaScript
import { marked as n } from "../markdown/marked.mjs";
import { parseCustomDirectives as c } from "../utils.mjs";
import { code as i } from "@vuux/code";
const a = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"), g = (t, e = "") => {
const o = {
ts: "typescript",
js: "javascript",
py: "python",
vue: "html",
md: "markdown"
}, r = /* @__PURE__ */ new Set(["c", "css", "dockerfile", "fortran", "go", "golo", "http", "ini", "java", "javascript", "json", "kotlin", "less", "lua", "markdown", "nginx", "perl", "php-template", "php", "powershell", "python-repl", "python", "r", "rust", "scss", "shell", "sql", "stylus", "tp", "typescript", "vbscript-html", "vbscript", "xml", "yaml", "html"]), s = o[e.toLowerCase()] || e.toLowerCase();
if (!r.has(s))
return `<pre><code>${a(t)}</code></pre>`;
try {
const l = i.highlight(t, { language: s, ignoreIllegals: !0 }).value;
return `<pre class="language-${s}"><code>${l}</code></pre>`;
} catch {
return `<pre><code>${a(t)}</code></pre>`;
}
}, p = new n.Renderer(), d = (t) => {
if (!t)
return "";
const e = c(t);
return n.parse(e);
};
n.setOptions({
renderer: p,
gfm: !0,
breaks: !1
});
p.code = ({ text: t, lang: e }) => g(t, e || "");
p.link = ({ href: t, title: e, text: o }) => {
const r = e ? ` title="${e}"` : "";
return `<a href="${t}"${r} target="_blank">${o}</a>`;
};
p.text = (t) => {
let e = t.text;
return e = e.replace(/==(.+?)==\{color:([^}]+)\}/g, (o, r, s) => `<span style="color:${s}">${r}</span>`), e = e.replace(/==(.+?)==\{size:([^}]+)\}/g, (o, r, s) => `<span style="font-size:${s}">${r}</span>`), e;
};
export {
g as highlightCode,
d as markdownToHtml
};