@vuux/editor
Version:
Vue Nuxt 富文本编辑器
160 lines (159 loc) • 4.2 kB
JavaScript
import { _Renderer as p } from "./Renderer.mjs";
import { _TextRenderer as h } from "./TextRenderer.mjs";
import { _defaults as l } from "./defaults.mjs";
class c {
options;
renderer;
textRenderer;
constructor(n) {
this.options = n || l, this.options.renderer = this.options.renderer || new p(), this.renderer = this.options.renderer, this.renderer.options = this.options, this.renderer.parser = this, this.textRenderer = new h();
}
static parse(n, r) {
return new c(r).parse(n);
}
static parseInline(n, r) {
return new c(r).parseInline(n);
}
parse(n, r = !0) {
let e = "";
for (let i = 0; i < n.length; i++) {
const o = n[i];
if (this.options.extensions?.renderers?.[o.type]) {
const s = o, a = this.options.extensions.renderers[s.type].call({ parser: this }, s);
if (a !== !1 || !["space", "hr", "heading", "code", "table", "blockquote", "list", "html", "def", "paragraph", "text"].includes(s.type)) {
e += a || "";
continue;
}
}
const t = o;
switch (t.type) {
case "space": {
e += this.renderer.space(t);
continue;
}
case "hr": {
e += this.renderer.hr(t);
continue;
}
case "heading": {
e += this.renderer.heading(t);
continue;
}
case "code": {
e += this.renderer.code(t);
continue;
}
case "table": {
e += this.renderer.table(t);
continue;
}
case "blockquote": {
e += this.renderer.blockquote(t);
continue;
}
case "list": {
e += this.renderer.list(t);
continue;
}
case "html": {
e += this.renderer.html(t);
continue;
}
case "def": {
e += this.renderer.def(t);
continue;
}
case "paragraph": {
e += this.renderer.paragraph(t);
continue;
}
case "text": {
let s = t, a = this.renderer.text(s);
for (; i + 1 < n.length && n[i + 1].type === "text"; )
s = n[++i], a += `
` + this.renderer.text(s);
r ? e += this.renderer.paragraph({
type: "paragraph",
raw: a,
text: a,
tokens: [{ type: "text", raw: a, text: a, escaped: !0 }]
}) : e += a;
continue;
}
default: {
const s = '未找到类型为"' + t.type + '"的Token';
if (this.options.silent)
return console.error(s), "";
throw new Error(s);
}
}
}
return e;
}
parseInline(n, r = this.renderer) {
let e = "";
for (let i = 0; i < n.length; i++) {
const o = n[i];
if (this.options.extensions?.renderers?.[o.type]) {
const s = this.options.extensions.renderers[o.type].call({ parser: this }, o);
if (s !== !1 || !["escape", "html", "link", "image", "strong", "em", "codespan", "br", "del", "text"].includes(o.type)) {
e += s || "";
continue;
}
}
const t = o;
switch (t.type) {
case "escape": {
e += r.text(t);
break;
}
case "html": {
e += r.html(t);
break;
}
case "link": {
e += r.link(t);
break;
}
case "image": {
e += r.image(t);
break;
}
case "strong": {
e += r.strong(t);
break;
}
case "em": {
e += r.em(t);
break;
}
case "codespan": {
e += r.codespan(t);
break;
}
case "br": {
e += r.br(t);
break;
}
case "del": {
e += r.del(t);
break;
}
case "text": {
e += r.text(t);
break;
}
default: {
const s = '未找到类型为"' + t.type + '"的Token';
if (this.options.silent)
return console.error(s), "";
throw new Error(s);
}
}
}
return e;
}
}
export {
c as _Parser
};