@soyvolon/docsify-enhanced-command-line-blocks
Version:
Docsify plugin to enhance the usage of the Command Line plugin from Prism code highlighting.
2 lines (1 loc) • 1.94 kB
JavaScript
;const N=d=>(h,O)=>{const r=d.debugLevel||0,p=/\<\!--.*(data-.*)=\"(.*)\".*--\>/gm,v=(t,s)=>{let n=[],g=t.matchAll(p);for(const o of g){o.index,o[0].length;let e=o[1],l=o[2];n.push({key:e,value:l})}const c=s||[];for(let o=0;o<c.length;o++){const e=c[o];n.push({key:e.key,value:e.value})}return n},A=t=>{let s={defaultLang:d.defaultLang||"batch",defaults:[],matchLangs:d.matchLangs||["batch","bash","shell","powershell"]};const n=/\<\!--.*cmd:(\w*):([A-z-]*)(?:=\"(.*)\")?.*--\>/gm;let g=t.matchAll(n);for(const c of g){r>=2&&console.log(JSON.stringify(c));const o=c[1],e=c[2],l=c[3];l?s[o].push({key:e,value:l}):s[o]=e}return r>=1&&console.log(`Page Settings: ${JSON.stringify(s)}`),s};h.beforeEach((t,s)=>{const n=A(t),g=/((```)(cmd)).*?```/sg;let c=0,o=t.matchAll(g);for(const e of o){r>=2&&console.log(JSON.stringify(e));const l=e.index+c,i=e[0].length;let u=n.defaultLang,a=e[0].slice(0,3)+u+e[0].slice(e[1].length);c+=a.length-i,r>=2&&console.log(a),t=t.slice(0,l)+a+t.slice(l+i)}r>=2&&console.log(t),s(t)}),h.afterEach((t,s)=>{const n=A(t),c=new DOMParser().parseFromString(t,"text/html"),o='pre[data-lang="'+n.matchLangs.join('"], pre[data-lang="')+'"]',e=c.querySelectorAll(o);for(const i of e){const u=i.querySelector("code");let a=u.textContent;const S=n.defaults,m=v(a),y=d.defaults||[];let L=m.concat(S.filter(f=>!m.find(b=>b.key==f.key)));L=y.concat(L.filter(f=>!y.find(b=>b.key==f.key))),r>=2&&(console.log("global attrs: "+JSON.stringify(y)),console.log("page attrs: "+JSON.stringify(S)),console.log("local attrs: "+JSON.stringify(m)),console.log("final attrs: "+JSON.stringify(L))),a=a.replace(p,"").trimStart(),u.textContent=a;for(const f of L)i.setAttribute(f.key,f.value);i.classList.add("command-line")}t=c.documentElement.outerHTML,r>=2&&console.log(t),s(t)})},k=window.$docsify||{},T={createCommandLineBlocks:k.createCommandLineBlocks??{}};k.plugins=[].concat(k.plugins||[],N(T.createCommandLineBlocks));