@vuux/editor
Version:
Vue Nuxt 富文本编辑器
26 lines (25 loc) • 10.7 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("./defaults.cjs"),x=require("./helpers.cjs"),B=(d,i,e,t,s)=>{const n=i.href,l=i.title||null,o=d[1].replace(s.other.outputLinkReplace,"$1");t.state.inLink=!0;const a={type:d[0].charAt(0)==="!"?"image":"link",raw:e,href:n,title:l,text:o,tokens:t.inlineTokens(o)};return t.state.inLink=!1,a},A=(d,i,e)=>{const t=d.match(e.other.indentCodeCompensation);if(t===null)return i;const s=t[1];return i.split(`
`).map(n=>{const l=n.match(e.other.beginningSpace);if(l===null)return n;const[o]=l;return o.length>=s.length?n.slice(s.length):n}).join(`
`)};class ${options;rules;lexer;constructor(i){this.options=i||S._defaults}space(i){const e=this.rules.block.newline.exec(i);if(e&&e[0].length>0)return{type:"space",raw:e[0]}}code(i){const e=this.rules.block.code.exec(i);if(e){const t=e[0].replace(this.rules.other.codeRemoveIndent,"");return{type:"code",raw:e[0],codeBlockStyle:"indented",text:this.options.pedantic?t:x.rtrim(t,`
`)}}}fences(i){const e=this.rules.block.fences.exec(i);if(e){const t=e[0],s=A(t,e[3]||"",this.rules);return{type:"code",raw:t,lang:e[2]?e[2].trim().replace(this.rules.inline.anyPunctuation,"$1"):e[2],text:s}}}heading(i){const e=this.rules.block.heading.exec(i);if(e){let t=e[2].trim();if(this.rules.other.endingHash.test(t)){const s=x.rtrim(t,"#");(this.options.pedantic||!s||this.rules.other.endingSpaceChar.test(s))&&(t=s.trim())}return{type:"heading",raw:e[0],depth:e[1].length,text:t,tokens:this.lexer.inline(t)}}}hr(i){const e=this.rules.block.hr.exec(i);if(e)return{type:"hr",raw:x.rtrim(e[0],`
`)}}blockquote(i){const e=this.rules.block.blockquote.exec(i);if(e){let t=x.rtrim(e[0],`
`).split(`
`),s="",n="";const l=[];for(;t.length>0;){let o=!1;const a=[];let h;for(h=0;h<t.length;h++)if(this.rules.other.blockquoteStart.test(t[h]))a.push(t[h]),o=!0;else if(!o)a.push(t[h]);else break;t=t.slice(h);const u=a.join(`
`),p=u.replace(this.rules.other.blockquoteSetextReplace,`
$1`).replace(this.rules.other.blockquoteSetextReplace2,"");s=s?`${s}
${u}`:u,n=n?`${n}
${p}`:p;const f=this.lexer.state.top;if(this.lexer.state.top=!0,this.lexer.blockTokens(p,l,!0),this.lexer.state.top=f,t.length===0)break;const r=l.at(-1);if(r?.type==="code")break;if(r?.type==="blockquote"){const g=r,c=g.raw+`
`+t.join(`
`),k=this.blockquote(c);l[l.length-1]=k,s=s.substring(0,s.length-g.raw.length)+k.raw,n=n.substring(0,n.length-g.text.length)+k.text;break}else if(r?.type==="list"){const g=r,c=g.raw+`
`+t.join(`
`),k=this.list(c);l[l.length-1]=k,s=s.substring(0,s.length-r.raw.length)+k.raw,n=n.substring(0,n.length-g.raw.length)+k.raw,t=c.substring(l.at(-1).raw.length).split(`
`);continue}}return{type:"blockquote",raw:s,tokens:l,text:n}}}list(i){let e=this.rules.block.list.exec(i);if(e){let t=e[1].trim();const s=t.length>1,n={type:"list",raw:"",ordered:s,start:s?+t.slice(0,-1):"",loose:!1,items:[]};t=s?`\\d{1,9}\\${t.slice(-1)}`:`\\${t}`,this.options.pedantic&&(t=s?t:"[*+-]");const l=this.rules.other.listItemRegex(t);let o=!1;for(;i;){let h=!1,u="",p="";if(!(e=l.exec(i))||this.rules.block.hr.test(i))break;u=e[0],i=i.substring(u.length);let f=e[2].split(`
`,1)[0].replace(this.rules.other.listReplaceTabs,m=>" ".repeat(3*m.length)),r=i.split(`
`,1)[0],g=!f.trim(),c=0;if(this.options.pedantic?(c=2,p=f.trimStart()):g?c=e[1].length+1:(c=e[2].search(this.rules.other.nonSpaceChar),c=c>4?1:c,p=f.slice(c),c+=e[1].length),g&&this.rules.other.blankLine.test(r)&&(u+=r+`
`,i=i.substring(r.length+1),h=!0),!h){const m=this.rules.other.nextBulletRegex(c),R=this.rules.other.hrRegex(c),T=this.rules.other.fencesBeginRegex(c),L=this.rules.other.headingBeginRegex(c),C=this.rules.other.htmlBeginRegex(c);for(;i;){const w=i.split(`
`,1)[0];let b;if(r=w,this.options.pedantic?(r=r.replace(this.rules.other.listReplaceNesting," "),b=r):b=r.replace(this.rules.other.tabCharGlobal," "),T.test(r)||L.test(r)||C.test(r)||m.test(r)||R.test(r))break;if(b.search(this.rules.other.nonSpaceChar)>=c||!r.trim())p+=`
`+b.slice(c);else{if(g||f.replace(this.rules.other.tabCharGlobal," ").search(this.rules.other.nonSpaceChar)>=4||T.test(f)||L.test(f)||R.test(f))break;p+=`
`+r}!g&&!r.trim()&&(g=!0),u+=w+`
`,i=i.substring(w.length+1),f=b.slice(c)}}n.loose||(o?n.loose=!0:this.rules.other.doubleBlankLine.test(u)&&(o=!0));let k=null,y;this.options.gfm&&(k=this.rules.other.listIsTask.exec(p),k&&(y=k[0]!=="[ ] ",p=p.replace(this.rules.other.listReplaceTask,""))),n.items.push({type:"list_item",raw:u,task:!!k,checked:y,loose:!1,text:p,tokens:[]}),n.raw+=u}const a=n.items.at(-1);if(a)a.raw=a.raw.trimEnd(),a.text=a.text.trimEnd();else return;n.raw=n.raw.trimEnd();for(let h=0;h<n.items.length;h++)if(this.lexer.state.top=!1,n.items[h].tokens=this.lexer.blockTokens(n.items[h].text,[]),!n.loose){const u=n.items[h].tokens.filter(f=>f.type==="space"),p=u.length>0&&u.some(f=>this.rules.other.anyLine.test(f.raw));n.loose=p}if(n.loose)for(let h=0;h<n.items.length;h++)n.items[h].loose=!0;return n}}html(i){const e=this.rules.block.html.exec(i);if(e)return{type:"html",block:!0,raw:e[0],pre:e[1]==="pre"||e[1]==="script"||e[1]==="style",text:e[0]}}def(i){const e=this.rules.block.def.exec(i);if(e){const t=e[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal," "),s=e[2]?e[2].replace(this.rules.other.hrefBrackets,"$1").replace(this.rules.inline.anyPunctuation,"$1"):"",n=e[3]?e[3].substring(1,e[3].length-1).replace(this.rules.inline.anyPunctuation,"$1"):e[3];return{type:"def",tag:t,raw:e[0],href:s,title:n}}}table(i){const e=this.rules.block.table.exec(i);if(!e||!this.rules.other.tableDelimiter.test(e[2]))return;const t=x.splitCells(e[1]),s=e[2].replace(this.rules.other.tableAlignChars,"").split("|"),n=e[3]?.trim()?e[3].replace(this.rules.other.tableRowBlankLine,"").split(`
`):[],l={type:"table",raw:e[0],header:[],align:[],rows:[]};if(t.length===s.length){for(const o of s)this.rules.other.tableAlignRight.test(o)?l.align.push("right"):this.rules.other.tableAlignCenter.test(o)?l.align.push("center"):this.rules.other.tableAlignLeft.test(o)?l.align.push("left"):l.align.push(null);for(let o=0;o<t.length;o++)l.header.push({text:t[o],tokens:this.lexer.inline(t[o]),header:!0,align:l.align[o]});for(const o of n)l.rows.push(x.splitCells(o,l.header.length).map((a,h)=>({text:a,tokens:this.lexer.inline(a),header:!1,align:l.align[h]})));return l}}lheading(i){const e=this.rules.block.lheading.exec(i);if(e)return{type:"heading",raw:e[0],depth:e[2].charAt(0)==="="?1:2,text:e[1],tokens:this.lexer.inline(e[1])}}paragraph(i){const e=this.rules.block.paragraph.exec(i);if(e){const t=e[1].charAt(e[1].length-1)===`
`?e[1].slice(0,-1):e[1];return{type:"paragraph",raw:e[0],text:t,tokens:this.lexer.inline(t)}}}text(i){const e=this.rules.block.text.exec(i);if(e)return{type:"text",raw:e[0],text:e[0],tokens:this.lexer.inline(e[0])}}escape(i){const e=this.rules.inline.escape.exec(i);if(e)return{type:"escape",raw:e[0],text:e[1]}}tag(i){const e=this.rules.inline.tag.exec(i);if(e)return!this.lexer.state.inLink&&this.rules.other.startATag.test(e[0])?this.lexer.state.inLink=!0:this.lexer.state.inLink&&this.rules.other.endATag.test(e[0])&&(this.lexer.state.inLink=!1),!this.lexer.state.inRawBlock&&this.rules.other.startPreScriptTag.test(e[0])?this.lexer.state.inRawBlock=!0:this.lexer.state.inRawBlock&&this.rules.other.endPreScriptTag.test(e[0])&&(this.lexer.state.inRawBlock=!1),{type:"html",raw:e[0],inLink:this.lexer.state.inLink,inRawBlock:this.lexer.state.inRawBlock,block:!1,text:e[0]}}link(i){const e=this.rules.inline.link.exec(i);if(e){const t=e[2].trim();if(!this.options.pedantic&&this.rules.other.startAngleBracket.test(t)){if(!this.rules.other.endAngleBracket.test(t))return;const l=x.rtrim(t.slice(0,-1),"\\");if((t.length-l.length)%2===0)return}else{const l=x.findClosingBracket(e[2],"()");if(l===-2)return;if(l>-1){const a=(e[0].indexOf("!")===0?5:4)+e[1].length+l;e[2]=e[2].substring(0,l),e[0]=e[0].substring(0,a).trim(),e[3]=""}}let s=e[2],n="";if(this.options.pedantic){const l=this.rules.other.pedanticHrefTitle.exec(s);l&&(s=l[1],n=l[3])}else n=e[3]?e[3].slice(1,-1):"";return s=s.trim(),this.rules.other.startAngleBracket.test(s)&&(this.options.pedantic&&!this.rules.other.endAngleBracket.test(t)?s=s.slice(1):s=s.slice(1,-1)),B(e,{href:s&&s.replace(this.rules.inline.anyPunctuation,"$1"),title:n&&n.replace(this.rules.inline.anyPunctuation,"$1")},e[0],this.lexer,this.rules)}}reflink(i,e){let t;if((t=this.rules.inline.reflink.exec(i))||(t=this.rules.inline.nolink.exec(i))){const s=(t[2]||t[1]).replace(this.rules.other.multipleSpaceGlobal," "),n=e[s.toLowerCase()];if(!n){const l=t[0].charAt(0);return{type:"text",raw:l,text:l}}return B(t,n,t[0],this.lexer,this.rules)}}emStrong(i,e,t=""){let s=this.rules.inline.emStrongLDelim.exec(i);if(!s||s[3]&&t.match(this.rules.other.unicodeAlphaNumeric))return;if(!(s[1]||s[2]||"")||!t||this.rules.inline.punctuation.exec(t)){const l=[...s[0]].length-1;let o,a,h=l,u=0;const p=s[0][0]==="*"?this.rules.inline.emStrongRDelimAst:this.rules.inline.emStrongRDelimUnd;for(p.lastIndex=0,e=e.slice(-1*i.length+l);(s=p.exec(e))!=null;){if(o=s[1]||s[2]||s[3]||s[4]||s[5]||s[6],!o)continue;if(a=[...o].length,s[3]||s[4]){h+=a;continue}else if((s[5]||s[6])&&l%3&&!((l+a)%3)){u+=a;continue}if(h-=a,h>0)continue;a=Math.min(a,a+h+u);const f=[...s[0]][0].length,r=i.slice(0,l+s.index+f+a);if(Math.min(l,a)%2){const c=r.slice(1,-1);return{type:"em",raw:r,text:c,tokens:this.lexer.inlineTokens(c)}}const g=r.slice(2,-2);return{type:"strong",raw:r,text:g,tokens:this.lexer.inlineTokens(g)}}}}codespan(i){const e=this.rules.inline.code.exec(i);if(e){let t=e[2].replace(this.rules.other.newLineCharGlobal," ");const s=this.rules.other.nonSpaceChar.test(t),n=this.rules.other.startingSpaceChar.test(t)&&this.rules.other.endingSpaceChar.test(t);return s&&n&&(t=t.substring(1,t.length-1)),{type:"codespan",raw:e[0],text:t}}}br(i){const e=this.rules.inline.br.exec(i);if(e)return{type:"br",raw:e[0]}}del(i){const e=this.rules.inline.del.exec(i);if(e)return{type:"del",raw:e[0],text:e[2],tokens:this.lexer.inlineTokens(e[2])}}autolink(i){const e=this.rules.inline.autolink.exec(i);if(e){let t,s;return e[2]==="@"?(t=e[1],s="mailto:"+t):(t=e[1],s=t),{type:"link",raw:e[0],text:t,href:s,tokens:[{type:"text",raw:t,text:t}]}}}url(i){let e;if(e=this.rules.inline.url.exec(i)){let t,s;if(e[2]==="@")t=e[0],s="mailto:"+t;else{let n;do n=e[0],e[0]=this.rules.inline._backpedal.exec(e[0])?.[0]??"";while(n!==e[0]);t=e[0],e[1]==="www."?s="http://"+e[0]:s=e[0]}return{type:"link",raw:e[0],text:t,href:s,tokens:[{type:"text",raw:t,text:t}]}}}inlineText(i){const e=this.rules.inline.text.exec(i);if(e){const t=this.lexer.state.inRawBlock;return{type:"text",raw:e[0],text:e[0],escaped:t}}}}exports._Tokenizer=$;