@vuux/editor
Version:
Vue Nuxt 富文本编辑器
240 lines (239 loc) • 8.37 kB
JavaScript
import { _Tokenizer as x } from "./Tokenizer.mjs";
import { _defaults as b } from "./defaults.mjs";
import { inline as a, block as p, other as f } from "./rules.mjs";
class g {
tokens;
options;
state;
tokenizer;
inlineQueue;
constructor(e) {
this.tokens = [], this.tokens.links = /* @__PURE__ */ Object.create(null), this.options = e || b, this.options.tokenizer = this.options.tokenizer || new x(), this.tokenizer = this.options.tokenizer, this.tokenizer.options = this.options, this.tokenizer.lexer = this, this.inlineQueue = [], this.state = {
inLink: !1,
inRawBlock: !1,
top: !0
};
const i = {
other: f,
block: p.normal,
inline: a.normal
};
this.options.pedantic ? (i.block = p.pedantic, i.inline = a.pedantic) : this.options.gfm && (i.block = p.gfm, this.options.breaks ? i.inline = a.breaks : i.inline = a.gfm), this.tokenizer.rules = i;
}
static get rules() {
return {
block: p,
inline: a
};
}
static lex(e, i) {
return new g(i).lex(e);
}
static lexInline(e, i) {
return new g(i).inlineTokens(e);
}
lex(e) {
e = e.replace(f.carriageReturn, `
`), this.blockTokens(e, this.tokens);
for (let i = 0; i < this.inlineQueue.length; i++) {
const s = this.inlineQueue[i];
this.inlineTokens(s.src, s.tokens);
}
return this.inlineQueue = [], this.tokens;
}
blockTokens(e, i = [], s = !1) {
for (this.options.pedantic && (e = e.replace(f.tabCharGlobal, " ").replace(f.spaceLine, "")); e; ) {
let t;
if (this.options.extensions?.block?.some((l) => (t = l.call({ lexer: this }, e, i)) ? (e = e.substring(t.raw.length), i.push(t), !0) : !1))
continue;
if (t = this.tokenizer.space(e)) {
e = e.substring(t.raw.length);
const l = i.at(-1);
t.raw.length === 1 && l !== void 0 ? l.raw += `
` : i.push(t);
continue;
}
if (t = this.tokenizer.code(e)) {
e = e.substring(t.raw.length);
const l = i.at(-1);
l?.type === "paragraph" || l?.type === "text" ? (l.raw += (l.raw.endsWith(`
`) ? "" : `
`) + t.raw, l.text += `
` + t.text, this.inlineQueue.at(-1).src = l.text) : i.push(t);
continue;
}
if (t = this.tokenizer.fences(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.heading(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.hr(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.blockquote(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.list(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.html(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.def(e)) {
e = e.substring(t.raw.length);
const l = i.at(-1);
l?.type === "paragraph" || l?.type === "text" ? (l.raw += (l.raw.endsWith(`
`) ? "" : `
`) + t.raw, l.text += `
` + t.raw, this.inlineQueue.at(-1).src = l.text) : this.tokens.links[t.tag] || (this.tokens.links[t.tag] = {
href: t.href,
title: t.title
}, i.push(t));
continue;
}
if (t = this.tokenizer.table(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
if (t = this.tokenizer.lheading(e)) {
e = e.substring(t.raw.length), i.push(t);
continue;
}
let h = e;
if (this.options.extensions?.startBlock) {
let l = 1 / 0;
const n = e.slice(1);
let r;
for (const o of this.options.extensions.startBlock)
r = o.call({ lexer: this }, n), typeof r == "number" && r >= 0 && (l = Math.min(l, r));
l < 1 / 0 && l >= 0 && (h = e.substring(0, l + 1));
}
if (this.state.top && (t = this.tokenizer.paragraph(h))) {
const l = i.at(-1);
s && l?.type === "paragraph" ? (l.raw += (l.raw.endsWith(`
`) ? "" : `
`) + t.raw, l.text += `
` + t.text, this.inlineQueue.pop(), this.inlineQueue.at(-1).src = l.text) : i.push(t), s = h.length !== e.length, e = e.substring(t.raw.length);
continue;
}
if (t = this.tokenizer.text(e)) {
e = e.substring(t.raw.length);
const l = i.at(-1);
l?.type === "text" ? (l.raw += (l.raw.endsWith(`
`) ? "" : `
`) + t.raw, l.text += `
` + t.text, this.inlineQueue.pop(), this.inlineQueue.at(-1).src = l.text) : i.push(t);
continue;
}
if (e) {
const l = "字节无限循环:" + e.charCodeAt(0);
if (this.options.silent) {
console.error(l);
break;
} else
throw new Error(l);
}
}
return this.state.top = !0, i;
}
inline(e, i = []) {
return this.inlineQueue.push({ src: e, tokens: i }), i;
}
inlineTokens(e, i = []) {
let s = e, t = null;
if (this.tokens.links) {
const n = Object.keys(this.tokens.links);
if (n.length > 0)
for (; (t = this.tokenizer.rules.inline.reflinkSearch.exec(s)) != null; )
n.includes(t[0].slice(t[0].lastIndexOf("[") + 1, -1)) && (s = s.slice(0, t.index) + "[" + "a".repeat(t[0].length - 2) + "]" + s.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex));
}
for (; (t = this.tokenizer.rules.inline.anyPunctuation.exec(s)) != null; )
s = s.slice(0, t.index) + "++" + s.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
for (; (t = this.tokenizer.rules.inline.blockSkip.exec(s)) != null; )
s = s.slice(0, t.index) + "[" + "a".repeat(t[0].length - 2) + "]" + s.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
s = this.options.hooks?.emStrongMask?.call({ lexer: this }, s) ?? s;
let h = !1, l = "";
for (; e; ) {
h || (l = ""), h = !1;
let n;
if (this.options.extensions?.inline?.some((o) => (n = o.call({ lexer: this }, e, i)) ? (e = e.substring(n.raw.length), i.push(n), !0) : !1))
continue;
if (n = this.tokenizer.escape(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.tag(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.link(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.reflink(e, this.tokens.links)) {
e = e.substring(n.raw.length);
const o = i.at(-1);
n.type === "text" && o?.type === "text" ? (o.raw += n.raw, o.text += n.text) : i.push(n);
continue;
}
if (n = this.tokenizer.emStrong(e, s, l)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.codespan(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.br(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.del(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (n = this.tokenizer.autolink(e)) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
if (!this.state.inLink && (n = this.tokenizer.url(e))) {
e = e.substring(n.raw.length), i.push(n);
continue;
}
let r = e;
if (this.options.extensions?.startInline) {
let o = 1 / 0;
const k = e.slice(1);
let u;
for (const w of this.options.extensions.startInline)
u = w.call({ lexer: this }, k), typeof u == "number" && u >= 0 && (o = Math.min(o, u));
o < 1 / 0 && o >= 0 && (r = e.substring(0, o + 1));
}
if (n = this.tokenizer.inlineText(r)) {
e = e.substring(n.raw.length), n.raw.slice(-1) !== "_" && (l = n.raw.slice(-1)), h = !0;
const o = i.at(-1);
o?.type === "text" ? (o.raw += n.raw, o.text += n.text) : i.push(n);
continue;
}
if (e) {
const o = "字节无限循环:" + e.charCodeAt(0);
if (this.options.silent) {
console.error(o);
break;
} else
throw new Error(o);
}
}
return i;
}
}
export {
g as _Lexer
};