-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.htm.zero-md.min.js
8 lines (8 loc) · 5.28 KB
/
index.htm.zero-md.min.js
1
2
3
4
5
6
7
8
/**
* Minified by jsDelivr using Terser v5.3.5.
* Original file: /gh/zerodevx/[email protected]/src/zero-md.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
window,document,window.customElements.define("zero-md",class extends HTMLElement{get version(){return"v1.4.1"}get src(){return this.getAttribute("src")}get manualRender(){return this.hasAttribute("manual-render")}get noShadow(){return this.hasAttribute("no-shadow")}get markedUrl(){return this.getAttribute("marked-url")||window.ZeroMd.config.markedUrl}get prismUrl(){return this.getAttribute("prism-url")||window.ZeroMd.config.prismUrl}get cssUrls(){let e=this.getAttribute("css-urls");return e?JSON.parse(e):window.ZeroMd.config.cssUrls}constructor(){super(),window.ZeroMd=window.ZeroMd||{},window.ZeroMd.config=window.ZeroMd.config||{},window.ZeroMd.config.markedUrl=window.ZeroMd.config.markedUrl||"https://cdn.jsdelivr.net/npm/marked@0/marked.min.js",window.ZeroMd.config.prismUrl=window.ZeroMd.config.prismUrl||"https://cdn.jsdelivr.net/npm/prismjs@1/prism.min.js",window.ZeroMd.config.cssUrls=window.ZeroMd.config.cssUrls||["https://cdn.jsdelivr.net/npm/github-markdown-css@2/github-markdown.min.css","https://cdn.jsdelivr.net/npm/prismjs@1/themes/prism.min.css"],window.ZeroMd.cache=window.ZeroMd.cache||{}}connectedCallback(){this.addEventListener("click",this._hijackLinks.bind(this)),this.addEventListener("zero-md-rendered",function e(){this.removeEventListener("zero-md-rendered",e),window.setTimeout((()=>{this._scrollTo(window.location.hash)}))}.bind(this)),this.manualRender||this.render(),this._fire("zero-md-ready")}_fire(e){this.dispatchEvent(new CustomEvent(e,{bubbles:!0,composed:!0}))}_ajaxGet(e){return new Promise(((t,r)=>{if(!e)return void r(e);let i=new XMLHttpRequest,n=t=>{console.warn("[zero-md] Error getting file",e),r(t)};i.open("GET",e,!0),i.onload=()=>{i.status>=200&&i.status<400?t(i.responseText):n(i)},i.onerror=e=>n(e),i.send()}))}_loadScript(e,t,r,...i){return new Promise(((n,s)=>{if("undefined"===t)if(window.ZeroMd.cache.hasOwnProperty(r))window.addEventListener(r,(function e(){window.removeEventListener(r,e),n()}));else{window.ZeroMd.cache[r]=!0;let t=document.createElement("script");for(let e of i)t.setAttribute(e,"");t.onload=()=>{this._fire(r),n()},t.onerror=t=>{console.warn("[zero-md] Error loading script",e),s(t)},t.src=e,document.head.appendChild(t)}else n()}))}_getStylesheet(e){return new Promise(((t,r)=>{window.ZeroMd.cache[e]?window.ZeroMd.cache[e].loaded?t(window.ZeroMd.cache[e].data):window.addEventListener(e,(function r(){window.removeEventListener(e,r),t(window.ZeroMd.cache[e].data)})):(window.ZeroMd.cache[e]={loaded:!1,data:""},this._ajaxGet(e).then((r=>{window.ZeroMd.cache[e].data=r,window.ZeroMd.cache[e].loaded=!0,this._fire(e),t(r)}),(e=>r(e))))}))}_getInputs(){return new Promise(((e,t)=>{let r=this.querySelector("template")&&this.querySelector("template").content.querySelector("xmp")||!1,i=this.querySelector('script[type="text/markdown"]')||!1;r?e(r.textContent):i?e(function(e){let t=(e=e.replace(/^\n/,"")).match(/^\s+/);return t?e.replace(new RegExp("^"+t[0],"gm"),""):e}(i.textContent)):this._ajaxGet(this.src).then((t=>e(t))).catch((e=>t(e)))}))}_prismHighlight(e,t){return window.Prism.highlight(e,this._detectLang(e,t))}_detectLang(e,t){return t?window.Prism.languages.hasOwnProperty(t)?window.Prism.languages[t]:"es"===t.substr(0,2)?window.Prism.languages.javascript:"c"===t?window.Prism.languages.clike:window.Prism.languages.markup:e.match(/^\s*</)?window.Prism.languages.markup:window.Prism.languages.javascript}_stampDom(e){let t=this.querySelectorAll("[class^=markdown]");t&&t.forEach((e=>this.removeChild(e))),this.shadowRoot&&(this.shadowRoot.innerHTML=""),this.noShadow?this.insertAdjacentHTML("afterbegin",e):(this.shadowRoot||this.attachShadow({mode:"open"})).innerHTML=e}_buildMd(){return new Promise(((e,t)=>{Promise.all([this._getInputs(),this._loadScript(this.markedUrl,typeof window.marked,"zero-md-marked-ready","async"),this._loadScript(this.prismUrl,typeof window.Prism,"zero-md-prism-ready","async","data-manual")]).then((t=>{e('<div class="markdown-body">'+window.marked(t[0],{highlight:this._prismHighlight.bind(this)})+"</div>")}),(e=>{t(e)}))}))}_buildStyles(){return new Promise((e=>{let t='<style class="markdown-style">:host{display:block;position:relative;contain:content;}',r="</style>",i=this.querySelector("template")&&this.querySelector("template").content.querySelector("style")||!1;i?e(t+i.textContent+r):Array.isArray(this.cssUrls)&&this.cssUrls.length?Promise.all(this.cssUrls.map((e=>this._getStylesheet(e)))).then((i=>e(t+i.join("")+r))).catch((()=>e(t+r))):(console.warn("[zero-md] No styles are defined"),e(t+r))}))}_scrollTo(e){if(!e||!this.shadowRoot)return;let t=this.shadowRoot.getElementById(e.substr(1));t&&t.scrollIntoView()}_hijackLinks(e){let t=e.path||e.composedPath();if("A"!==t[0].tagName)return;const r=t[0];r.hash&&r.origin+r.pathname===window.location.origin+window.location.pathname&&(e.metaKey?window.open(r.href,"_blank"):(this._scrollTo(r.hash),window.location=r.href),e.preventDefault())}render(){Promise.all([this._buildStyles(),this._buildMd()]).then((e=>{this._stampDom(e[0]+e[1]),this._fire("zero-md-rendered")}))}});
//# sourceMappingURL=/sm/b936772013f8d3785660442fc2eda2e10044f8ade5113ba5b855fe4c62636b03.map