appstore/dist/lib/ui/meditor/helper.js

17 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-12-19 15:30:29 +08:00
const c="\u5728\u6B64\u8F93\u5165\u6587\u672C",h={a:function(e,t,r){let l=t.match(s("href")),i=t.match(s("title")),n=t.match(s("target")),a="";return l=l&&l[1]||null,i=i&&i[1]||null,n=n&&n[1],l?(l=l.replace("viod(0)","").replaceAll("&amp;","&"),a=n?`target=${n}`:"",a+=i?`;title=${i}`:"",`[${r||l}](${l}${a?` "${a}"`:""})`):r||l},em:function(e,t,r){return r&&"_"+r+"_"||""},strong:function(e,t,r){return r&&"**"+r+"**"||""},pre:function(e,t,r){return r=r.replace(/<[/]?code>/g,""),"\n\n```\n"+r+"\n```\n"},code:function(e,t,r){return r&&"`"+r+"`"||""},blockquote:function(e,t,r){return"> "+r.trim()},img:function(e,t,r){var l=t.match(s("src")),i=t.match(s("alt"));return l=l&&l[1]||"",i=i&&i[1]||"","!["+i+"]("+l+")"},p:function(e,t,r){return r?`
`+r:""},br:`
`,"h([1-6])":function(e,t,r,l){return`
`+"#".repeat(t)+" "+l+`
`},hr:`
---
`},v=["header","quote","bold","italic","through","list","order","line","code","codeblock","table","link","image","attach","fullscreen","preview"],g=/<(ul|ol)>(?:(?!<ul|<ol)[\s\S])*?<\/\1>/gi;function p(e,t){return e.replace(new RegExp("^"+t+"|"+t+"$","g"),"")}function s(e,t="i"){return new RegExp(e+`\\s?=\\s?["']?([^"']*)["']?`,t)}function o(e,t){var r="";return["br","hr","img"].indexOf(e)>-1?r="<"+e+"([^>]*?)\\/?>":r="<"+e+"([^>]*?)>([\\s\\S]*?)<\\/"+e+">",new RegExp(r,"gi")}function m(e){try{e=decodeURIComponent(e)}catch{}e=e.replace(/\t/g," ").replace(/<\/?(meta|link|script)[^>]*?>/g,"").replace(/<!--[\w\W]*?-->/g,"").replace(/<xml[^>]*?>[\w\W]*?<\/xml>/g,"").replace(/<style>[\w\W]*?<\/style>/g,"").replace(s("class","g"),"").replace(s("style","g"),"").replace(/<a[^>]*? href\s?=\s?["']?([^"']*)["']?[^>]*?>/g,'<a href="$1">').replace(/<img[^>]*? src\s?=\s?["']?([^"']*)["']?[^>]*?>/g,'<img src="$1">').replace(/<(?!a |img )(\w+) [^>]*>/g,"<$1>").replace(/<svg[^>]*>.*?<\/svg>/g,"{svg not support}");for(let t in h){let r=h[t],l=o(t);if(t==="blockquote")for(;e.match(l);)e=e.replace(l,r);else e=e.replace(l,r);t==="p"&&(l=o("div"),e=e.replace(l,r)),t==="em"&&(l=o("i"),e=e.replace(l,r)),t==="strong"&&(l=o("b"),e=e.replace(l,r))}for(;e.match(g);)e=e.replace(g,function(t){return t=t.replace(/<(ul|ol)>([\s\S]*?)<\/\1>/gi,function(r,l,i){let n=i.split("</li>");n.pop();for(let a=0,f=n.length;a<f;a++){let d=l==="ol"?a+1+". ":"* ";n[a]=d+n[a].replace(/\s*<li>([\s\S]*)/i,function(w,u){return u=u.trim().replace(/\n/g,`
`),u}).replace(/<[\/]?[\w]*[^>]*>/g,"")}return n.join(`
`)}),`
`+t.trim()});return e=e.replace(/<[\/]?[\w]*[^>]*>/g,"").replace(/```([\w\W]*)```/g,function(t,r){return r=r.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">"),"```"+r+"```"}),e}const $={header(){this.$refs.header.classList.add("fadein")},h(e){var t=this.selection(!0)||c;t=t.replace(/^(#+ )?/,"#".repeat(e)+" "),this.insert(t,!0)},quote(){var e=this.selection(!0)||c;e=e.replace(/^(>+ )?/,"> "),this.insert(e,!0)},bold(){var e=this.selection()||c,t=p(e,"\\*\\*");e=e===t?`**${e}**`:t,this.insert(e,!0)},italic(){var e=this.selection()||c,t=p(e,"_");e=e===t?`_${e}_`:t,this.insert(e,!0)},through(){var e=this.selection()||c,t=p(e,"~~");e=e===t?`~~${e}~~`:t,this.insert(e,!0)},list(){var e=this.selection(!0)||c;e=e.replace(/^([+\-*] )?/,"+ "),this.insert(e,!0)},order(){var e=this.selection(!0)||c;e=e.replace(/^(\d+\. )?/,"1. "),this.insert(e,!0)},line(){this.insert(`
---
`,!1)},code(){var e=this.selection()||c,t=p(e,"`");e=e===t?`\`${e}\``:t,this.insert(e,!0)},codeblock(){this.insert("\n```language\n\n```\n")},table(){this.$refs.table.classList.add("fadein")},link(){this.$refs.link.classList.add("fadein")},fullscreen(){this.classList.toggle("fullscreen")},preview(){this.previewEnabled=!this.previewEnabled,this.$refs.view.classList.toggle("active"),this.previewEnabled&&(this.$refs.view.code=this.value)}};export{$ as Addon,v as DEFAULT_TOOLS,m as html2md};