UNPKG

md-editor-rt

Version:

Markdown editor for react, developed in jsx and typescript, dark theme、beautify content by prettier、render articles directly、paste or clip the picture and upload it...

65 lines (64 loc) 27 kB
"use strict";const p=require("react"),a=require("./event-name.cjs"),d=require("./config.cjs"),R=require("./dom.cjs"),F=require("react/jsx-runtime"),D=require("./context.cjs"),fe=require("medium-zoom"),pe=require("@vavt/copy2clipboard"),me=require("markdown-it"),he=require("markdown-it-image-figures"),ge=require("markdown-it-sub"),be=require("markdown-it-sup"),re=require("@vavt/util"),le=require("./index3.cjs"),Ee=require("lru-cache"),L={hljs:`${d.prefix}-hljs`,hlcss:`${d.prefix}-hlCss`,prettier:`${d.prefix}-prettier`,prettierMD:`${d.prefix}-prettierMD`,cropperjs:`${d.prefix}-cropper`,croppercss:`${d.prefix}-cropperCss`,screenfull:`${d.prefix}-screenfull`,mermaidM:`${d.prefix}-mermaid-m`,mermaid:`${d.prefix}-mermaid`,katexjs:`${d.prefix}-katex`,katexcss:`${d.prefix}-katexCss`},xe=(t,e)=>{const{value:s,modelValue:n,onSave:r}=t,{editorId:f}=e,[o,m]=p.useState({buildFinished:!1,html:""});p.useEffect(()=>{const c=h=>{m(()=>({buildFinished:!0,html:h}))};return a.bus.on(f,{name:a.BUILD_FINISHED,callback:c}),()=>{a.bus.remove(f,a.BUILD_FINISHED,c)}},[f]),p.useEffect(()=>{const c=()=>{if(r){const h=new Promise(l=>{if(o.buildFinished)l(o.html);else{const i=u=>{l(u),a.bus.remove(f,a.BUILD_FINISHED,i)};a.bus.on(f,{name:a.BUILD_FINISHED,callback:i})}});r(s||n||"",h)}};return a.bus.on(f,{name:a.ON_SAVE,callback:c}),()=>{a.bus.remove(f,a.ON_SAVE,c)}},[f,n,r,o.buildFinished,o.html,s]),p.useEffect(()=>{m(c=>({...c,buildFinished:!1}))},[s,n])},Ce=t=>{const{noPrettier:e,noUploadImg:s}=t;p.useEffect(()=>{const{editorExtensions:n,editorExtensionsAttrs:r}=d.globalConfig,f=e||!!n.prettier.prettierInstance,o=e||!!n.prettier.parserMarkdownInstance;if(!(s||!!n.cropper.instance)){const{js:c={},css:h={}}=r.cropper||{};R.appendHandler("link",{...h,rel:"stylesheet",href:n.cropper.css,id:L.croppercss}),R.appendHandler("script",{...c,src:n.cropper.js,id:L.cropperjs})}if(!f){const{standaloneJs:c={}}=r.prettier||{};R.appendHandler("script",{...c,src:n.prettier.standaloneJs,id:L.prettier})}if(!o){const{parserMarkdownJs:c={}}=r.prettier||{};R.appendHandler("script",{...c,src:n.prettier.parserMarkdownJs,id:L.prettierMD})}},[e,s])},$e=(t,e)=>{p.useEffect(()=>(a.bus.on(t,{name:a.ERROR_CATCHER,callback:e}),()=>{a.bus.remove(t,a.ERROR_CATCHER,e)}),[t,e])},ve=(t,e)=>{const{editorId:s}=e,{onUploadImg:n}=t;p.useEffect(()=>{const r=(f,o)=>{const m=c=>{a.bus.emit(s,a.REPLACE,"image",{desc:"",urls:c}),o==null||o()};n==null||n(f,m)};return a.bus.on(s,{name:a.UPLOAD_IMAGE,callback:r}),()=>{a.bus.remove(s,a.UPLOAD_IMAGE,r)}},[s,n])},ke=(t,e)=>{const{editorId:s}=e,[n,r]=p.useState(!1);return p.useEffect(()=>{const f=o=>{r(o===void 0?m=>!m:o)};return a.bus.on(s,{name:a.CHANGE_CATALOG_VISIBLE,callback:f}),()=>{a.bus.remove(s,a.CHANGE_CATALOG_VISIBLE,f)}},[s]),n};let se="";const ye=t=>{const{theme:e=d.defaultProps.theme,preview:s=d.defaultProps.preview,htmlPreview:n=d.defaultProps.htmlPreview,pageFullscreen:r=d.defaultProps.pageFullscreen,previewTheme:f=d.defaultProps.previewTheme,codeTheme:o=d.defaultProps.codeTheme,language:m=d.defaultProps.language,codeStyleReverse:c=d.defaultProps.codeStyleReverse,codeStyleReverseList:h=d.defaultProps.codeStyleReverseList}=t,l=p.useMemo(()=>{const E=d.globalConfig.editorExtensions.highlight,C=d.globalConfig.editorExtensionsAttrs.highlight,{js:$}=E,k={...d.codeCss,...E.css},{js:v,css:y={}}=C||{},A=c&&h.includes(f)?"dark":e,_=k[o]?k[o][A]:d.codeCss.atom[A],I=k[o]&&y[o]?y[o][A]:y.atom?y.atom[A]:{};return{js:{src:$,...v},css:{href:_,...I}}},[c,h,f,e,o]),i=p.useMemo(()=>{const E={...d.staticTextDefault,...d.globalConfig.editorConfig.languageUserDefined};return E[m]?E[m]:d.staticTextDefault["zh-CN"]},[m]),[u,g]=p.useState({pageFullscreen:r,fullscreen:!1,preview:s,htmlPreview:s?!1:n,previewOnly:!1}),b=p.useRef(u),x=p.useCallback((E,C)=>{g($=>{const k=C===void 0?!$[E]:C,v={...$};switch(E){case"preview":{v.htmlPreview=!1,v.previewOnly=!1;break}case"htmlPreview":{v.preview=!1,v.previewOnly=!1;break}case"previewOnly":{k?!v.preview&&!v.htmlPreview&&(v.preview=!0):(b.current.preview||(v.preview=!1),b.current.htmlPreview||(v.htmlPreview=!1));break}}return b.current[E]=k,v[E]=k,v})},[]);return p.useEffect(()=>{se=document.body.style.overflow},[]),p.useEffect(()=>{u.pageFullscreen||u.fullscreen?document.body.style.overflow="hidden":document.body.style.overflow=se},[u.pageFullscreen,u.fullscreen]),[l,i,u,x]},we=(t,e,s,n,r,f)=>{const{editorId:o}=e;p.useEffect(()=>{a.bus.emit(o,a.PAGE_FULL_SCREEN_CHANGED,n.pageFullscreen)},[o,n.pageFullscreen]),p.useEffect(()=>{a.bus.emit(o,a.FULL_SCREEN_CHANGED,n.fullscreen)},[o,n.fullscreen]),p.useEffect(()=>{a.bus.emit(o,a.PREVIEW_CHANGED,n.preview)},[o,n.preview]),p.useEffect(()=>{a.bus.emit(o,a.PREVIEW_ONLY_CHANGED,n.previewOnly)},[o,n.previewOnly]),p.useEffect(()=>{a.bus.emit(o,a.HTML_PREVIEW_CHANGED,n.htmlPreview)},[o,n.htmlPreview]),p.useEffect(()=>{a.bus.emit(o,a.CATALOG_VISIBLE_CHANGED,s)},[s,o]),p.useImperativeHandle(t,()=>({on(c,h){switch(c){case"pageFullscreen":{a.bus.on(o,{name:a.PAGE_FULL_SCREEN_CHANGED,callback(l){h(l)}});break}case"fullscreen":{a.bus.on(o,{name:a.FULL_SCREEN_CHANGED,callback(l){h(l)}});break}case"preview":{a.bus.on(o,{name:a.PREVIEW_CHANGED,callback(l){h(l)}});break}case"previewOnly":{a.bus.on(o,{name:a.PREVIEW_ONLY_CHANGED,callback(l){h(l)}});break}case"htmlPreview":{a.bus.on(o,{name:a.HTML_PREVIEW_CHANGED,callback(l){h(l)}});break}case"catalog":{a.bus.on(o,{name:a.CATALOG_VISIBLE_CHANGED,callback(l){h(l)}});break}}},togglePageFullscreen(c){r("pageFullscreen",c)},toggleFullscreen(c){a.bus.emit(o,a.CHANGE_FULL_SCREEN,c)},togglePreview(c){r("preview",c)},togglePreviewOnly(c){r("previewOnly",c)},toggleHtmlPreview(c){r("htmlPreview",c)},toggleCatalog(c){a.bus.emit(o,a.CHANGE_CATALOG_VISIBLE,c)},triggerSave(){a.bus.emit(o,a.ON_SAVE)},insert(c){a.bus.emit(o,a.REPLACE,"universal",{generate:c})},focus(c){var h;(h=f.current)==null||h.focus(c)},rerender(){a.bus.emit(o,a.RERENDER)},getSelectedText(){var c;return(c=f.current)==null?void 0:c.getSelectedText()},resetHistory(){var c;(c=f.current)==null||c.resetHistory()},domEventHandlers(c){a.bus.emit(o,a.EVENT_LISTENER,c)},execCommand(c){a.bus.emit(o,a.REPLACE,c)},getEditorView(){var c;return(c=f.current)==null?void 0:c.getEditorView()}}),[f,o,r])},Ie=t=>{const e=p.useId();return t.id||t.editorId||d.prefix+"-"+e.replaceAll(":","")},Ae=(t,e)=>{const{editorId:s}=p.useContext(D.EditorContext);p.useEffect(()=>t.noImgZoomIn?void 0:(()=>{const r=document.querySelectorAll(`#${s}-preview img:not(.not-zoom):not(.medium-zoom-image)`),f=fe(r,{background:"#00000073"});return()=>{f.detach()}})(),[s,e,t.noImgZoomIn,t.setting])},Se=(t,e,s)=>{const{editorId:n,usedLanguageText:r,customIcon:f,rootRef:o}=p.useContext(D.EditorContext),{formatCopiedText:m=c=>c}=t;p.useEffect(()=>{var c;t.setting.preview&&((c=o.current)==null||c.querySelectorAll(`#${n} .${d.prefix}-preview .${d.prefix}-code`).forEach(h=>{let l=-1;const i=h.querySelector(`.${d.prefix}-copy-button`);i&&(i.onclick=u=>{u.preventDefault(),clearTimeout(l);const b=(h.querySelector("input:checked + pre code")||h.querySelector("pre code")).textContent,{text:x,successTips:E,failTips:C}=r.copyCode;let $=E;pe(m(b)).catch(()=>{$=C}).finally(()=>{i.dataset.isIcon?i.dataset.tips=$:i.innerHTML=$,l=window.setTimeout(()=>{i.dataset.isIcon?i.dataset.tips=x:i.innerHTML=x},1500)})})}))},[f,n,m,e,s,t.setting.preview,o,r.copyCode])},He=t=>{const{highlight:e}=p.useContext(D.EditorContext),s=p.useRef(d.globalConfig.editorExtensions.highlight.instance),[n,r]=p.useState(!!s.current);return p.useEffect(()=>{t.noHighlight||d.globalConfig.editorExtensions.highlight.instance||R.updateHandler("link",{...e.css,rel:"stylesheet",id:L.hlcss})},[e.css,t.noHighlight]),p.useEffect(()=>{t.noHighlight||s.current||R.appendHandler("script",{...e.js,id:L.hljs,onload(){s.current=window.hljs,r(!0)}},"hljs")},[]),{hljsRef:s,hljsInited:n}},X=new Ee.LRUCache({max:1e3,ttl:6e5}),_e=t=>{const{editorId:e,theme:s,rootRef:n}=p.useContext(D.EditorContext),{noMermaid:r,sanitizeMermaid:f}=t,o=p.useRef(d.globalConfig.editorExtensions.mermaid.instance),[m,c]=p.useState(-1),h=p.useCallback(()=>{X.clear();const i=o.current;!r&&i&&(i.initialize(d.globalConfig.mermaidConfig({startOnLoad:!1,theme:s==="dark"?"dark":"default"})),c(u=>u+1))},[r,s]);p.useEffect(h,[h]),p.useEffect(()=>{var b,x;const{editorExtensions:i,editorExtensionsAttrs:u}=d.globalConfig;if(r||o.current)return;const g=i.mermaid.js;/\.mjs/.test(g)?(R.appendHandler("link",{...(b=u.mermaid)==null?void 0:b.js,rel:"modulepreload",href:g,id:L.mermaidM}),import(g).then(E=>{o.current=E.default,h()})):R.appendHandler("script",{...(x=u.mermaid)==null?void 0:x.js,src:g,id:L.mermaid,onload(){o.current=window.mermaid,h()}},"mermaid")},[h,r]);const l=p.useCallback(async()=>{var i;if(!r&&o.current){const u=((i=n.current)==null?void 0:i.querySelectorAll(`div.${d.prefix}-mermaid`))||[],g=document.createElement("div"),b=document.body.offsetWidth>1366?document.body.offsetWidth:1366,x=document.body.offsetHeight>768?document.body.offsetHeight:768;g.style.width=b+"px",g.style.height=x+"px",g.style.position="fixed",g.style.zIndex="-10000",g.style.top="-10000";let E=u.length;E>0&&document.body.appendChild(g),await Promise.allSettled(Array.from(u).map(C=>(async k=>{var A;const v=k.innerText;let y=X.get(v);if(!y){const _=re.randomId();let I={svg:""};try{I=await o.current.render(_,v,g),y=await f(I.svg);const S=document.createElement("p");S.className=`${d.prefix}-mermaid`,S.setAttribute("data-processed",""),S.innerHTML=y,(A=S.children[0])==null||A.removeAttribute("height"),X.set(v,S.innerHTML),k.dataset.line!==void 0&&(S.dataset.line=k.dataset.line),k.replaceWith(S)}catch(S){a.bus.emit(e,a.ERROR_CATCHER,{name:"mermaid",message:S.message,error:S})}--E===0&&g.remove()}})(C)))}},[r,n,f]);return{reRender:m,replaceMermaid:l}},Te=t=>{const e=p.useRef(d.globalConfig.editorExtensions.katex.instance),[s,n]=p.useState(!!e.current);return p.useEffect(()=>{if(t.noKatex||e.current)return;const{editorExtensions:r}=d.globalConfig;R.appendHandler("script",{src:r.katex.js,id:L.katexjs,onload(){e.current=window.katex,n(!0)}},"katex"),R.appendHandler("link",{rel:"stylesheet",href:r.katex.css,id:L.katexcss})},[t.noKatex]),{katexRef:e,katexInited:s}},Re=(t,e)=>{const s=t.renderer.rules.fence.bind(t.renderer.rules);t.renderer.rules.fence=(n,r,f,o,m)=>{const c=n[r],h=c.content.trim();if(c.info==="mermaid"){let l;n[r].map&&n[r].level===0&&(l=n[r].map[0],n[r].attrSet("data-line",String(l)));const i=X.get(h);return i?`<p class="${d.prefix}-mermaid" ${l!==void 0?"data-line="+l:""} data-processed>${i}</p>`:`<div class="${d.prefix}-mermaid" ${l!==void 0?"data-line="+l:""} data-mermaid-theme=${e.themeRef.current}>${t.utils.escapeHtml(h)}</div>`}return s(n,r,f,o,m)}},Y=(t,e)=>{const s=t.attrs?t.attrs.slice():[];return e.forEach(n=>{const r=t.attrIndex(n[0]);r<0?s.push(n):(s[r]=s[r].slice(),s[r][1]+=` ${n[1]}`)}),s},oe={block:[{open:"$$",close:"$$"},{open:"\\[",close:"\\]"}],inline:[{open:"$$",close:"$$"},{open:"$",close:"$"},{open:"\\[",close:"\\]"},{open:"\\(",close:"\\)"}]},Le=t=>(e,s)=>{const n=t.delimiters;let r,f,o;for(const m of n)if(e.src.startsWith(m.open,e.pos)){const c=e.pos+m.open.length;for(r=c;(r=e.src.indexOf(m.close,r))!==-1;){for(o=r-1;e.src[o]==="\\";)o-=1;if((r-o)%2===1)break;r+=m.close.length}if(r===-1)return s||(e.pending+=m.open),e.pos=c,!0;if(r-c===0)return s||(e.pending+=m.open+m.close),e.pos=c+m.close.length,!0;if(!s){const h=e.src.slice(c,r);f=e.push("math_inline","math",0),f.markup=m.open,f.content=h}return e.pos=r+m.close.length,!0}return!1},Pe=t=>(e,s,n,r)=>{const f=t.delimiters;let o,m,c,h,l=!1,i=e.bMarks[s]+e.tShift[s],u=e.eMarks[s];for(const g of f)if(e.src.slice(i,i+g.open.length)===g.open&&e.src.slice(u-g.close.length,u)===g.close){if(i+=g.open.length,o=e.src.slice(i,u),r)return!0;for(o.trim().slice(-g.close.length)===g.close&&(o=o.trim().slice(0,-g.close.length),l=!0),c=s;!l&&(c++,!(c>=n||(i=e.bMarks[c]+e.tShift[c],u=e.eMarks[c],i<u&&e.tShift[c]<e.blkIndent)));)e.src.slice(i,u).trim().slice(-g.close.length)===g.close&&(h=e.src.slice(0,u).lastIndexOf(g.close),m=e.src.slice(i,h),l=!0);e.line=c+1;const b=e.push("math_block","math",0);return b.block=!0,b.content=(o&&o.trim()?o+` `:"")+e.getLines(s+1,c,e.tShift[s],!0)+(m&&m.trim()?m:""),b.map=[s,e.line],b.markup=g.open,!0}return!1},Ne=(t,{katexRef:e,inlineDelimiters:s,blockDelimiters:n})=>{const r=(o,m,c,h,l)=>{const i=o[m],u={attrs:Y(i,[["class",`${d.prefix}-katex-inline`]])};if(e.current){const g=e.current.renderToString(i.content,d.globalConfig.katexConfig({throwOnError:!1}));return`<span ${l.renderAttrs(u)} data-processed>${g}</span>`}else return`<span ${l.renderAttrs(u)}>${i.content}</span>`},f=(o,m,c,h,l)=>{const i=o[m],u={attrs:Y(i,[["class",`${d.prefix}-katex-block`]])};if(e.current){const g=e.current.renderToString(i.content,d.globalConfig.katexConfig({throwOnError:!1,displayMode:!0}));return`<p ${l.renderAttrs(u)} data-processed>${g}</p>`}else return`<p ${l.renderAttrs(u)}>${i.content}</p>`};t.inline.ruler.before("escape","math_inline",Le({delimiters:s||oe.inline})),t.block.ruler.after("blockquote","math_block",Pe({delimiters:n||oe.block}),{alt:["paragraph","reference","blockquote","list"]}),t.renderer.rules.math_inline=r,t.renderer.rules.math_block=f},Me=(t,e)=>{e=e||{};const s=3,n=e.marker||"!",r=n.charCodeAt(0),f=n.length;let o="",m="";const c=(l,i,u,g,b)=>{const x=l[i];return x.type==="admonition_open"?l[i].attrPush(["class",`${d.prefix}-admonition ${d.prefix}-admonition-${x.info}`]):x.type==="admonition_title_open"&&l[i].attrPush(["class",`${d.prefix}-admonition-title`]),b.renderToken(l,i,u)},h=l=>{const i=l.trim().split(" ",2);m="",o=i[0],i.length>1&&(m=l.substring(o.length+2))};t.block.ruler.before("code","admonition",(l,i,u,g)=>{let b,x,E,C=!1,$=l.bMarks[i]+l.tShift[i],k=l.eMarks[i];if(r!==l.src.charCodeAt($))return!1;for(b=$+1;b<=k&&n[(b-$)%f]===l.src[b];b++);const v=Math.floor((b-$)/f);if(v!==s)return!1;b-=(b-$)%f;const y=l.src.slice($,b),A=l.src.slice(b,k);if(h(A),g)return!0;for(x=i;x++,!(x>=u||($=l.bMarks[x]+l.tShift[x],k=l.eMarks[x],$<k&&l.sCount[x]<l.blkIndent));)if(r===l.src.charCodeAt($)&&!(l.sCount[x]-l.blkIndent>=4)){for(b=$+1;b<=k&&n[(b-$)%f]===l.src[b];b++);if(!(Math.floor((b-$)/f)<v)&&(b-=(b-$)%f,b=l.skipSpaces(b),!(b<k))){C=!0;break}}const _=l.parentType,I=l.lineMax;return l.parentType="root",l.lineMax=x,E=l.push("admonition_open","div",1),E.markup=y,E.block=!0,E.info=o,E.map=[i,x],m&&(E=l.push("admonition_title_open","p",1),E.markup=y+" "+o,E.map=[i,x],E=l.push("inline","",0),E.content=m,E.map=[i,l.line-1],E.children=[],E=l.push("admonition_title_close","p",-1),E.markup=y+" "+o),l.md.block.tokenize(l,i+1,x),E=l.push("admonition_close","div",-1),E.markup=l.src.slice($,b),E.block=!0,l.parentType=_,l.lineMax=I,l.line=x+(C?1:0),!0},{alt:["paragraph","reference","blockquote","list"]}),t.renderer.rules.admonition_open=c,t.renderer.rules.admonition_title_open=c,t.renderer.rules.admonition_title_close=c,t.renderer.rules.admonition_close=c},De=(t,e)=>{t.renderer.rules.heading_open=(s,n)=>{var m;const r=s[n],f=((m=s[n+1].children)==null?void 0:m.reduce((c,h)=>c+(["text","code_inline","math_inline"].includes(h.type)&&h.content||""),""))||"",o=r.markup.length;return e.headsRef.current.push({text:f,level:o,line:r.map[0]}),r.map&&r.level===0&&r.attrSet("id",e.mdHeadingId(f,o,e.headsRef.current.length)),t.renderer.renderToken(s,n,e)},t.renderer.rules.heading_close=(s,n,r,f,o)=>o.renderToken(s,n,r)},Oe=(t,e)=>{const s=t.renderer.rules.fence,n=t.utils.unescapeAll,r=/\[(\w*)(?::([\w ]*))?\]/,f=/::(open|close)/,o=i=>i.info?n(i.info).trim():"",m=i=>{const u=o(i),[g=null,b=""]=(r.exec(u)||[]).slice(1);return[g,b]},c=i=>{const u=o(i);return u?u.split(/(\s+)/g)[0]:""},h=i=>{const u=i.info.match(f)||[],g=u[1]==="open"||u[1]!=="close"&&e.codeFoldable&&i.content.trim().split(` `).length<e.autoFoldThreshold,b=u[1]||e.codeFoldable?"details":"div",x=u[1]||e.codeFoldable?"summary":"div";return{open:g,tagContainer:b,tagHeader:x}},l=(i,u,g,b,x)=>{if(i[u].hidden)return"";const E=e.usedLanguageTextRef.current.copyCode.text,C=e.customIconRef.current.copy||E,$=!!e.customIconRef.current.copy,k=`<span class="${d.prefix}-collapse-tips">${R.StrIcon("collapse-tips",e.customIconRef.current)}</span>`,[v]=m(i[u]);if(v===null){const{open:H,tagContainer:T,tagHeader:U}=h(i[u]),Z=[["class",`${d.prefix}-code`]];H&&Z.push(["open",""]);const j={attrs:Y(i[u],Z)};i[u].info=i[u].info.replace(f,"");const ee=s(i,u,g,b,x);return` <${T} ${x.renderAttrs(j)}> <${U} class="${d.prefix}-code-head"> <div class="${d.prefix}-code-flag"><span></span><span></span><span></span></div> <div class="${d.prefix}-code-action"> <span class="${d.prefix}-code-lang">${i[u].info.trim()}</span> <span class="${d.prefix}-copy-button" data-tips="${E}"${$?" data-is-icon=true":""}>${C}</span> ${e.extraTools instanceof Function?e.extraTools({lang:i[u].info.trim()}):e.extraTools||""} ${T==="details"?k:""} </div> </${U}> ${ee} </${T}> `}let y,A,_,I,S="",V="",O="";const{open:G,tagContainer:q,tagHeader:B}=h(i[u]),z=[["class",`${d.prefix}-code`]];G&&z.push(["open",""]);const Q={attrs:Y(i[u],z)};for(let H=u;H<i.length&&(y=i[H],[A,_]=m(y),A===v);H++){y.info=y.info.replace(r,"").replace(f,""),y.hidden=!0;const T=`${d.prefix}-codetab-${e.editorId}-${u}-${H-u}`;I=H-u>0?"":"checked",S+=` <li> <input type="radio" id="label-${d.prefix}-codetab-label-1-${e.editorId}-${u}-${H-u}" name="${d.prefix}-codetab-label-${e.editorId}-${u}" class="${T}" ${I} > <label for="label-${d.prefix}-codetab-label-1-${e.editorId}-${u}-${H-u}" onclick="this.getRootNode().querySelectorAll('.${T}').forEach(e => e.click())" > ${_||c(y)} </label> </li>`,V+=` <div role="tabpanel"> <input type="radio" name="${d.prefix}-codetab-pre-${e.editorId}-${u}" class="${T}" ${I} role="presentation"> ${s(i,H,g,b,x)} </div>`,O+=` <input type="radio" name="${d.prefix}-codetab-lang-${e.editorId}-${u}" class="${T}" ${I} role="presentation"> <span class=${d.prefix}-code-lang role="note">${c(y)}</span>`}return` <${q} ${x.renderAttrs(Q)}> <${B} class="${d.prefix}-code-head"> <div class="${d.prefix}-code-flag"> <ul class="${d.prefix}-codetab-label" role="tablist">${S}</ul> </div> <div class="${d.prefix}-code-action"> <span class="${d.prefix}-codetab-lang">${O}</span> <span class="${d.prefix}-copy-button" data-tips="${E}"${$?" data-is-icon=true":""}>${C}</span> ${e.extraTools instanceof Function?e.extraTools({lang:i[u].info.trim()}):e.extraTools||""} ${q==="details"?k:""} </div> </${B}> ${V} </${q}> `};t.renderer.rules.fence=l,t.renderer.rules.code_block=l},ie=(t,e,s)=>{const n=t.attrIndex(e),r=[e,s];n<0?t.attrPush(r):(t.attrs=t.attrs||[],t.attrs[n]=r)},je=t=>t.type==="inline",Fe=t=>t.type==="paragraph_open",Ge=t=>t.type==="list_item_open",qe=t=>t.content.indexOf("[ ] ")===0||t.content.indexOf("[x] ")===0||t.content.indexOf("[X] ")===0,Ue=(t,e)=>je(t[e])&&Fe(t[e-1])&&Ge(t[e-2])&&qe(t[e]),Ve=(t,e)=>{const s=t[e].level-1;for(let n=e-1;n>=0;n--)if(t[n].level===s)return n;return-1},ze=t=>{const e=new t("html_inline","",0);return e.content="<label>",e},We=t=>{const e=new t("html_inline","",0);return e.content="</label>",e},Ke=(t,e,s)=>{const n=new s("html_inline","",0);return n.content='<label class="task-list-item-label" for="'+e+'">'+t+"</label>",n.attrs=[{for:e}],n},Be=(t,e,s)=>{const n=new e("html_inline","",0),r=s.enabled?" ":' disabled="" ';return t.content.indexOf("[ ] ")===0?n.content='<input class="task-list-item-checkbox"'+r+'type="checkbox">':(t.content.indexOf("[x] ")===0||t.content.indexOf("[X] ")===0)&&(n.content='<input class="task-list-item-checkbox" checked=""'+r+'type="checkbox">'),n},Ze=(t,e,s)=>{if(t.children=t.children||[],t.children.unshift(Be(t,e,s)),t.children[1].content=t.children[1].content.slice(3),t.content=t.content.slice(3),s.label)if(s.labelAfter){t.children.pop();const n="task-item-"+Math.ceil(Math.random()*(1e4*1e3)-1e3);t.children[0].content=t.children[0].content.slice(0,-1)+' id="'+n+'">',t.children.push(Ke(t.content,n,e))}else t.children.unshift(ze(e)),t.children.push(We(e))},Je=(t,e={})=>{t.core.ruler.after("inline","github-task-lists",s=>{const n=s.tokens;for(let r=2;r<n.length;r++)Ue(n,r)&&(Ze(n[r],s.Token,e),ie(n[r-2],"class","task-list-item"+(e.enabled?" enabled":" ")),ie(n[Ve(n,r-2)],"class","contains-task-list"))})},Xe=t=>{t.core.ruler.push("init-line-number",e=>(e.tokens.forEach(s=>{s.map&&(s.attrs||(s.attrs=[]),s.attrs.push(["data-line",s.map[0].toString()]))}),!0))},Ye=(t,e)=>{const{modelValue:s,sanitize:n,mdHeadingId:r,codeFoldable:f,autoFoldThreshold:o,noKatex:m,noMermaid:c,noHighlight:h,setting:l,onHtmlChanged:i,onGetCatalog:u}=t,{editorConfig:g,markdownItConfig:b,markdownItPlugins:x,editorExtensions:E}=d.globalConfig,{editorId:C,language:$,showCodeRowNumber:k,theme:v,usedLanguageText:y,customIcon:A,rootRef:_}=p.useContext(D.EditorContext),I=p.useRef([]),S=p.useRef(v);p.useEffect(()=>{S.current=v},[v]);const V=p.useRef(y);p.useEffect(()=>{V.current=y},[y]);const O=p.useRef(A);p.useEffect(()=>{O.current=A},[A]);const{hljsRef:G,hljsInited:q}=He(t),{katexRef:B,katexInited:z}=Te(t),{reRender:Q,replaceMermaid:H}=_e(t),[T]=p.useState(()=>{const w=me({html:!0,breaks:!0,linkify:!0});b(w,{editorId:C});const P=[{type:"image",plugin:he,options:{figcaption:!0,classes:"md-zoom"}},{type:"admonition",plugin:Me,options:{}},{type:"taskList",plugin:Je,options:{}},{type:"heading",plugin:De,options:{mdHeadingId:r,headsRef:I}},{type:"code",plugin:Oe,options:{editorId:C,usedLanguageTextRef:V,codeFoldable:f,autoFoldThreshold:o,customIconRef:O}},{type:"sub",plugin:ge,options:{}},{type:"sup",plugin:be,options:{}}];m||P.push({type:"katex",plugin:Ne,options:{katexRef:B}}),c||P.push({type:"mermaid",plugin:Re,options:{themeRef:S}}),x(P,{editorId:C}).forEach(M=>{w.use(M.plugin,M.options)});const N=w.options.highlight;return w.set({highlight:(M,W,ue)=>{if(N){const te=N(M,W,ue);if(te)return te}let K;!h&&G.current?G.current.getLanguage(W)?K=G.current.highlight(M,{language:W,ignoreIllegals:!0}).value:K=G.current.highlightAuto(M).value:K=T.utils.escapeHtml(M);const de=k?le.generateCodeRowNumber(K.replace(/^\n+|\n+$/g,""),M.replace(/^\n+|\n+$/g,"")):`<span class="${d.prefix}-code-block">${K.replace(/^\n+|\n+$/g,"")}</span>`;return`<pre><code class="language-${W}" language=${W}>${de}</code></pre>`}}),Xe(w),w}),[U,Z]=p.useState(`_article-key_${re.randomId()}`),[j,ee]=p.useState(()=>(I.current=[],n(T.render(s)))),ae=p.useMemo(()=>(h||q)&&(m||z),[q,z,h,m]),ne=p.useRef(!0),J=p.useCallback(()=>{I.current=[];const w=n(T.render(s));ee(w)},[T,s,n]);return p.useEffect(()=>{a.bus.emit(C,a.BUILD_FINISHED,j),i==null||i(j),u==null||u(I.current),a.bus.emit(C,a.CATALOG_CHANGED,I.current)},[C,j,U,u,i]),p.useEffect(()=>{let w=()=>{};return l.preview&&(H().then(()=>{var P,N;(P=E.mermaid)!=null&&P.enableZoom&&(w(),w=R.zoomMermaid((N=_.current)==null?void 0:N.querySelectorAll(`#${C} .${d.prefix}-mermaid`),{customIcon:O.current}))}),a.bus.emit(C,a.CATALOG_CHANGED,I.current)),()=>{w()}},[C,H,_,l.preview]),p.useEffect(()=>{if(ne.current){ne.current=!1;return}const w=setTimeout(()=>{J()},e?0:g.renderDelay);return()=>{clearTimeout(w)}},[ae,v,J,$,e,g.renderDelay]),p.useEffect(()=>{let w=()=>{};return H().then(()=>{var P,N;(P=E.mermaid)!=null&&P.enableZoom&&(w(),w=R.zoomMermaid((N=_.current)==null?void 0:N.querySelectorAll(`#${C} p.${d.prefix}-mermaid`),{customIcon:O.current}))}),()=>{w()}},[C,j,U,Q,H,_]),p.useEffect(()=>{const w=()=>{a.bus.emit(C,a.CATALOG_CHANGED,I.current)};return a.bus.on(C,{name:a.PUSH_CATALOG,callback:w}),()=>{a.bus.remove(C,a.PUSH_CATALOG,w)}},[C]),p.useEffect(()=>{const w=()=>{Z(`_article-key_${re.randomId()}`),J()};return a.bus.on(C,{name:a.RERENDER,callback:w}),()=>{a.bus.remove(C,a.RERENDER,w)}},[C,J]),{html:j,key:U}},ce={checked:{regexp:/- \[x\]/,value:"- [ ]"},unChecked:{regexp:/- \[\s\]/,value:"- [x]"}},Qe=(t,e)=>{const{editorId:s,rootRef:n}=p.useContext(D.EditorContext);p.useEffect(()=>{var o;const r=((o=n.current)==null?void 0:o.querySelectorAll(".task-list-item.enabled"))||[],f=m=>{var g;m.preventDefault();const c=m.target.checked?"unChecked":"checked",h=(g=m.target.parentElement)==null?void 0:g.dataset.line;if(!h)return;const l=Number(h),i=t.modelValue.split(` `),u=i[Number(l)].replace(ce[c].regexp,ce[c].value);t.previewOnly?(i[Number(l)]=u,t.onChange(i.join(` `))):a.bus.emit(s,a.TASK_STATE_CHANGED,l+1,u)};return r.forEach(m=>{m.addEventListener("click",f)}),()=>{r.forEach(m=>{m.removeEventListener("click",f)})}},[s,e,t,n])},et=(t,e,s)=>{const{setting:n,onRemount:r}=t;p.useEffect(()=>{r==null||r()},[e,s,r]),p.useEffect(()=>{(n.preview||n.htmlPreview)&&(r==null||r())},[n.preview,n.htmlPreview,r])},tt=t=>{const s=new DOMParser().parseFromString(t,"text/html");return Array.from(s.body.childNodes)},rt=(t,e)=>{const s=[],n=[];if(t.forEach((r,f)=>{const o=e[f];if(!o){s.push({index:f,newNode:r});return}(r.nodeType!==o.nodeType||r.textContent!==o.textContent||r.nodeType===1&&r.outerHTML!==o.outerHTML)&&s.push({index:f,newNode:r})}),e.length>t.length)for(let r=t.length;r<e.length;r++)n.push(e[r]);return{updates:s,deletes:n}},nt=({html:t})=>{const{editorId:e,previewTheme:s,showCodeRowNumber:n}=p.useContext(D.EditorContext),r=p.useRef({__html:t}),f=p.useRef(null);return p.useEffect(()=>{if(!f.current)return;const o=tt(t),m=Array.from(f.current.childNodes||[]),{updates:c,deletes:h}=rt(o,m);h.forEach(l=>{l.remove()}),c.forEach(({index:l,newNode:i})=>{const u=f.current.childNodes[l];u?f.current.replaceChild(i.cloneNode(!0),u):f.current.appendChild(i.cloneNode(!0))})},[t]),F.jsx("div",{id:`${e}-preview`,className:le.classnames([`${d.prefix}-preview`,`${s}-theme`,n&&`${d.prefix}-scrn`]),dangerouslySetInnerHTML:r.current,ref:f})},st=t=>{const{previewOnly:e=!1}=t,{editorId:s}=p.useContext(D.EditorContext),{html:n,key:r}=Ye(t,!!e);return Se(t,n,r),Ae(t,n),Qe(t,n),et(t,n,r),F.jsxs(F.Fragment,{children:[t.setting.preview&&F.jsx("div",{id:`${s}-preview-wrapper`,className:`${d.prefix}-preview-wrapper`,children:F.jsx(nt,{html:n},r)},"content-preview-wrapper"),!e&&t.setting.htmlPreview&&F.jsx("div",{id:`${s}-html-wrapper`,className:`${d.prefix}-preview-wrapper`,children:F.jsx("div",{className:`${d.prefix}-html`,children:n})},"html-preview-wrapper")]})};exports.CDN_IDS=L;exports.ContentPreview=st;exports.useCatalog=ke;exports.useConfig=ye;exports.useEditorId=Ie;exports.useErrorCatcher=$e;exports.useExpansion=Ce;exports.useExpose=we;exports.useOnSave=xe;exports.useUploadImg=ve;