@edc4it/reveal.js-external-code
Version:
[](#)
20 lines (19 loc) • 16.3 kB
JavaScript
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".reveal pre,.external-code-wrapper{width:100%}:is(.external-code-wrapper footer.demo-ref):before{color:#0d99a5;margin-right:.3em}.external-code-wrapper footer.demo-ref{font-size:14px;margin-top:-18px;color:#0d99a5;text-align:right;cursor:help;transition:font-size .4s .1s}:is(.external-code-wrapper footer.demo-ref):hover{font-size:28px;cursor:none}")),document.head.appendChild(e)}}catch(r){console.error("vite-plugin-css-injected-by-js",r)}})();
(function(l,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(l=typeof globalThis<"u"?globalThis:l||self,l.ClipCode=c())})(this,function(){"use strict";var Dt=Object.defineProperty;var Ut=(l,c,m)=>c in l?Dt(l,c,{enumerable:!0,configurable:!0,writable:!0,value:m}):l[c]=m;var B=(l,c,m)=>Ut(l,typeof c!="symbol"?c+"":c,m);class l extends Error{constructor(t){super(t)}}function c(e,t=!1){const s=t?-1:0;return e.replace(/\s/g,"").split(",").map(i=>{if(/^[\d-]+$/.test(i)){const[o,u]=i.split("-").map(d=>Number.parseInt(d,10)+s);return isNaN(u)?{start:o,end:o}:{start:o,end:u}}else throw new l(`could not parse ${i}`)})}function m(e){var n,i;const t=e.split(`
`),s=t[0];if(s.includes("@reveal.js/code")){const o=(i=(n=s.match(/lines=(['"]?(?<range>[\d,\s-]+)['"]?)/))==null?void 0:n.groups)==null?void 0:i.range,u=t.slice(1);if(o){const p=c(o,!0);return{lines:u,range:p,annotated:!0}}else return{lines:u,annotated:!0}}else return{lines:t,annotated:!1}}function W(e,t){return e.map((s,n)=>Z(n+1,t)?s:"…").filter((s,n,i)=>!(s==="…"&&i[n-1]==="…"))}function Z(e,t){return t.find(s=>e>=s.start&&e<=s.end)!==void 0}function G(e,t){e.parentNode?e.replaceWith(t):console.error("The element to be replaced is not attached to the DOM.")}function H(e,t){e.forEach(s=>{t.setAttribute(s.nodeName,s.nodeValue??"")})}function q(e){return e.split(".").pop()}class X{constructor(t,s,n,i,a){B(this,"providedLanguage");var o;this.attributes=t,this.src=s,this.href=n,this.codeStr=i,this.options=a,this.providedLanguage=(o=this.attributes.getNamedItem("data-lang"))==null?void 0:o.value}create(){const{wrapper:t,codeElement:s}=this._createElements(),n=this._codeClasses();return s.classList.add(...n),H([...this.attributes].filter(i=>i.nodeName!=="class"),s),s.innerHTML=this.codeStr,t}_createElements(){const t=this._createWrapper(),s=document.createElement("pre"),n=document.createElement("code"),i=this._footerHtml();return t.appendChild(s),s.appendChild(n),s.insertAdjacentHTML("afterend",i),{wrapper:t,codeElement:n}}_codeClasses(){const t=q(this.src),s=this.providedLanguage??t??"";return[...this.options.codeBlock.additionalClasses,s]}_footerHtml(){var s,n;return`<footer class="demo-ref"><a href="${(s=this.options)!=null&&s.local.absPath?`${this.options.local.scheme}${(n=this.options)==null?void 0:n.local.absPath}/${this.src}`:this.href}">${this.src}</a></footer>`}_createWrapper(){var s;const t=document.createElement("div");return t.classList.add("external-code-wrapper"),t.classList.add(...((s=this.attributes.getNamedItem("class"))==null?void 0:s.value.split(" "))??[]),t}}async function V(e,t){const s=await fetch(e);if(s.ok){const n=await s.text();return t.codeBlock.trim?n.trim():n}else throw new Error(s.statusText)}function K(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function z(e,t){for(var s=0;s<t.length;s++){var n=t[s];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function J(e,t,s){return t&&z(e.prototype,t),e}var Q=Object.defineProperty,f=function(e,t){return Q(e,"name",{value:t,configurable:!0})},tt=`<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="m8.94 8 4.2-4.193a.67.67 0 0 0-.947-.947L8 7.06l-4.193-4.2a.67.67 0 1 0-.947.947L7.06 8l-4.2 4.193a.667.667 0 0 0 .217 1.093.666.666 0 0 0 .73-.146L8 8.94l4.193 4.2a.666.666 0 0 0 1.094-.217.665.665 0 0 0-.147-.73L8.94 8Z" fill="currentColor"/>
</svg>
`,et=`<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16 2.667a13.333 13.333 0 1 0 0 26.666 13.333 13.333 0 0 0 0-26.666Zm0 24A10.667 10.667 0 0 1 5.333 16a10.56 10.56 0 0 1 2.254-6.533l14.946 14.946A10.56 10.56 0 0 1 16 26.667Zm8.413-4.134L9.467 7.587A10.56 10.56 0 0 1 16 5.333 10.667 10.667 0 0 1 26.667 16a10.56 10.56 0 0 1-2.254 6.533Z" fill="currentColor"/>
</svg>
`,st=`<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16 14.667A1.333 1.333 0 0 0 14.667 16v5.333a1.333 1.333 0 0 0 2.666 0V16A1.333 1.333 0 0 0 16 14.667Zm.507-5.227a1.333 1.333 0 0 0-1.014 0 1.334 1.334 0 0 0-.44.28 1.56 1.56 0 0 0-.28.44c-.075.158-.11.332-.106.507a1.332 1.332 0 0 0 .386.946c.13.118.279.213.44.28a1.334 1.334 0 0 0 1.84-1.226 1.4 1.4 0 0 0-.386-.947 1.334 1.334 0 0 0-.44-.28ZM16 2.667a13.333 13.333 0 1 0 0 26.666 13.333 13.333 0 0 0 0-26.666Zm0 24a10.666 10.666 0 1 1 0-21.333 10.666 10.666 0 0 1 0 21.333Z" fill="currentColor"/>
</svg>
`,nt=`<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="m19.627 11.72-5.72 5.733-2.2-2.2a1.334 1.334 0 1 0-1.88 1.881l3.133 3.146a1.333 1.333 0 0 0 1.88 0l6.667-6.667a1.333 1.333 0 1 0-1.88-1.893ZM16 2.667a13.333 13.333 0 1 0 0 26.666 13.333 13.333 0 0 0 0-26.666Zm0 24a10.666 10.666 0 1 1 0-21.333 10.666 10.666 0 0 1 0 21.333Z" fill="currentColor"/>
</svg>
`,it=`<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16.334 17.667a1.334 1.334 0 0 0 1.334-1.333v-5.333a1.333 1.333 0 0 0-2.665 0v5.333a1.333 1.333 0 0 0 1.33 1.333Zm-.508 5.227c.325.134.69.134 1.014 0 .165-.064.314-.159.44-.28a1.56 1.56 0 0 0 .28-.44c.076-.158.112-.332.107-.507a1.332 1.332 0 0 0-.387-.946 1.532 1.532 0 0 0-.44-.28 1.334 1.334 0 0 0-1.838 1.226 1.4 1.4 0 0 0 .385.947c.127.121.277.216.44.28Zm.508 6.773a13.333 13.333 0 1 0 0-26.667 13.333 13.333 0 0 0 0 26.667Zm0-24A10.667 10.667 0 1 1 16.54 27a10.667 10.667 0 0 1-.206-21.333Z" fill="currentColor"/>
</svg>
`,rt=f(function(e){return new DOMParser().parseFromString(e,"text/html").body.childNodes[0]},"stringToHTML"),I=f(function(e){var t=new DOMParser().parseFromString(e,"application/xml");return document.importNode(t.documentElement,!0).outerHTML},"getSvgNode"),r={CONTAINER:"sn-notifications-container",NOTIFY:"sn-notify",NOTIFY_CONTENT:"sn-notify-content",NOTIFY_ICON:"sn-notify-icon",NOTIFY_CLOSE:"sn-notify-close",NOTIFY_TITLE:"sn-notify-title",NOTIFY_TEXT:"sn-notify-text",IS_X_CENTER:"sn-is-x-center",IS_Y_CENTER:"sn-is-y-center",IS_CENTER:"sn-is-center",IS_LEFT:"sn-is-left",IS_RIGHT:"sn-is-right",IS_TOP:"sn-is-top",IS_BOTTOM:"sn-is-bottom",NOTIFY_OUTLINE:"sn-notify-outline",NOTIFY_FILLED:"sn-notify-filled",NOTIFY_ERROR:"sn-notify-error",NOTIFY_WARNING:"sn-notify-warning",NOTIFY_SUCCESS:"sn-notify-success",NOTIFY_INFO:"sn-notify-info",NOTIFY_FADE:"sn-notify-fade",NOTIFY_FADE_IN:"sn-notify-fade-in",NOTIFY_SLIDE:"sn-notify-slide",NOTIFY_SLIDE_IN:"sn-notify-slide-in",NOTIFY_AUTOCLOSE:"sn-notify-autoclose"},h={ERROR:"error",WARNING:"warning",SUCCESS:"success",INFO:"info"},b={OUTLINE:"outline",FILLED:"filled"},w={FADE:"fade",SLIDE:"slide"},E={CLOSE:I(tt),SUCCESS:I(nt),ERROR:I(et),WARNING:I(it),INFO:I(st)},L=f(function(e){e.wrapper.classList.add(r.NOTIFY_FADE),setTimeout(function(){e.wrapper.classList.add(r.NOTIFY_FADE_IN)},100)},"fadeIn"),C=f(function(e){e.wrapper.classList.remove(r.NOTIFY_FADE_IN),setTimeout(function(){e.wrapper.remove()},e.speed)},"fadeOut"),ot=f(function(e){e.wrapper.classList.add(r.NOTIFY_SLIDE),setTimeout(function(){e.wrapper.classList.add(r.NOTIFY_SLIDE_IN)},100)},"slideIn"),at=f(function(e){e.wrapper.classList.remove(r.NOTIFY_SLIDE_IN),setTimeout(function(){e.wrapper.remove()},e.speed)},"slideOut"),S=function(){function e(t){var s=this;K(this,e),this.notifyOut=f(function(Pt){Pt(s)},"notifyOut");var n=t.notificationsGap,i=n===void 0?20:n,a=t.notificationsPadding,o=a===void 0?20:a,u=t.status,p=u===void 0?"success":u,d=t.effect,O=d===void 0?w.FADE:d,y=t.type,A=y===void 0?"outline":y,T=t.title,g=t.text,Y=t.showIcon,St=Y===void 0?!0:Y,x=t.customIcon,_t=x===void 0?"":x,j=t.customClass,Ft=j===void 0?"":j,M=t.speed,Rt=M===void 0?500:M,P=t.showCloseButton,At=P===void 0?!0:P,D=t.autoclose,Yt=D===void 0?!0:D,U=t.autotimeout,xt=U===void 0?3e3:U,k=t.position,jt=k===void 0?"right top":k,$=t.customWrapper,Mt=$===void 0?"":$;if(this.customWrapper=Mt,this.status=p,this.title=T,this.text=g,this.showIcon=St,this.customIcon=_t,this.customClass=Ft,this.speed=Rt,this.effect=O,this.showCloseButton=At,this.autoclose=Yt,this.autotimeout=xt,this.notificationsGap=i,this.notificationsPadding=o,this.type=A,this.position=jt,!this.checkRequirements()){console.error("You must specify 'title' or 'text' at least.");return}this.setContainer(),this.setWrapper(),this.setPosition(),this.showIcon&&this.setIcon(),this.showCloseButton&&this.setCloseButton(),this.setContent(),this.container.prepend(this.wrapper),this.setEffect(),this.notifyIn(this.selectedNotifyInEffect),this.autoclose&&this.autoClose(),this.setObserver()}return J(e,[{key:"checkRequirements",value:function(){return!!(this.title||this.text)}},{key:"setContainer",value:function(){var s=document.querySelector(".".concat(r.CONTAINER));s?this.container=s:(this.container=document.createElement("div"),this.container.classList.add(r.CONTAINER),document.body.appendChild(this.container)),this.notificationsPadding&&this.container.style.setProperty("--sn-notifications-padding","".concat(this.notificationsPadding,"px")),this.notificationsGap&&this.container.style.setProperty("--sn-notifications-gap","".concat(this.notificationsGap,"px"))}},{key:"setPosition",value:function(){this.container.classList[this.position==="center"?"add":"remove"](r.IS_CENTER),this.container.classList[this.position.includes("left")?"add":"remove"](r.IS_LEFT),this.container.classList[this.position.includes("right")?"add":"remove"](r.IS_RIGHT),this.container.classList[this.position.includes("top")?"add":"remove"](r.IS_TOP),this.container.classList[this.position.includes("bottom")?"add":"remove"](r.IS_BOTTOM),this.container.classList[this.position.includes("x-center")?"add":"remove"](r.IS_X_CENTER),this.container.classList[this.position.includes("y-center")?"add":"remove"](r.IS_Y_CENTER)}},{key:"setCloseButton",value:function(){var s=this,n=document.createElement("div");n.classList.add(r.NOTIFY_CLOSE),n.innerHTML=E.CLOSE,this.wrapper.appendChild(n),n.addEventListener("click",function(){s.close()})}},{key:"setWrapper",value:function(){var s=this;switch(this.customWrapper?this.wrapper=rt(this.customWrapper):this.wrapper=document.createElement("div"),this.wrapper.style.setProperty("--sn-notify-transition-duration","".concat(this.speed,"ms")),this.wrapper.classList.add(r.NOTIFY),this.type){case b.OUTLINE:this.wrapper.classList.add(r.NOTIFY_OUTLINE);break;case b.FILLED:this.wrapper.classList.add(r.NOTIFY_FILLED);break;default:this.wrapper.classList.add(r.NOTIFY_OUTLINE)}switch(this.status){case h.SUCCESS:this.wrapper.classList.add(r.NOTIFY_SUCCESS);break;case h.ERROR:this.wrapper.classList.add(r.NOTIFY_ERROR);break;case h.WARNING:this.wrapper.classList.add(r.NOTIFY_WARNING);break;case h.INFO:this.wrapper.classList.add(r.NOTIFY_INFO);break}this.autoclose&&(this.wrapper.classList.add(r.NOTIFY_AUTOCLOSE),this.wrapper.style.setProperty("--sn-notify-autoclose-timeout","".concat(this.autotimeout+this.speed,"ms"))),this.customClass&&this.customClass.split(" ").forEach(function(n){s.wrapper.classList.add(n)})}},{key:"setContent",value:function(){var s=document.createElement("div");s.classList.add(r.NOTIFY_CONTENT);var n,i;this.title&&(n=document.createElement("div"),n.classList.add(r.NOTIFY_TITLE),n.textContent=this.title.trim(),this.showCloseButton||(n.style.paddingRight="0")),this.text&&(i=document.createElement("div"),i.classList.add(r.NOTIFY_TEXT),i.innerHTML=this.text.trim(),this.title||(i.style.marginTop="0")),this.wrapper.appendChild(s),this.title&&s.appendChild(n),this.text&&s.appendChild(i)}},{key:"setIcon",value:function(){var s=f(function(i){switch(i){case h.SUCCESS:return E.SUCCESS;case h.ERROR:return E.ERROR;case h.WARNING:return E.WARNING;case h.INFO:return E.INFO}},"computedIcon"),n=document.createElement("div");n.classList.add(r.NOTIFY_ICON),n.innerHTML=this.customIcon||s(this.status),(this.status||this.customIcon)&&this.wrapper.appendChild(n)}},{key:"setObserver",value:function(){var s=this,n=new IntersectionObserver(function(i){if(i[0].intersectionRatio<=0)s.close();else return},{threshold:0});setTimeout(function(){n.observe(s.wrapper)},this.speed)}},{key:"notifyIn",value:function(t){t(this)}},{key:"autoClose",value:function(){var s=this;setTimeout(function(){s.close()},this.autotimeout+this.speed)}},{key:"close",value:function(){this.notifyOut(this.selectedNotifyOutEffect)}},{key:"setEffect",value:function(){switch(this.effect){case w.FADE:this.selectedNotifyInEffect=L,this.selectedNotifyOutEffect=C;break;case w.SLIDE:this.selectedNotifyInEffect=ot,this.selectedNotifyOutEffect=at;break;default:this.selectedNotifyInEffect=L,this.selectedNotifyOutEffect=C}}}]),e}();f(S,"Notify");var _=S;globalThis.Notify=_;function ct(e,t){const s=window.location.href,n=new URL(s);return`${n.port?`${n.protocol}//${n.hostname}:${n.port}`:`${n.protocol}//${n.hostname}`}${e==="/"?"":e}/${t}`}function lt(e,t){const{range:s,lines:n,annotated:i}=m(e),a=t?c(t,i):void 0,o=s??a;return(o?W(n,o):n).join(`
`)}async function ut(e,t){const s=e.querySelectorAll("object[type='reveal.js/code']"),n=Array.from(s).map(async i=>{const a=i.getAttribute("data-src"),o=i.getAttribute("data-lines"),u=i.hasAttribute("data-escape-code");if(a){const p=ct(t.basePath,a);try{const d=await V(p,t);if(d){const O=u?d.replace(/[&<>'"]/g,g=>({"&":"&","<":"<",">":">","'":"'",'"':"""})[g]||g):d,y=lt(O,o),T=new X(i.attributes,a,p,y,t).create();G(i,T)}}catch(d){const O=`exception while processing external code ${a}`;t.enableNotify&&new _({text:O}),console.error(O,d)}}else console.error("object[type='reveal.js/code'] has no src attribute",i)});return Promise.all(n)}function dt(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var ft=function(t){return ht(t)&&!pt(t)};function ht(e){return!!e&&typeof e=="object"}function pt(e){var t=Object.prototype.toString.call(e);return t==="[object RegExp]"||t==="[object Date]"||Ot(e)}var mt=typeof Symbol=="function"&&Symbol.for,vt=mt?Symbol.for("react.element"):60103;function Ot(e){return e.$$typeof===vt}function It(e){return Array.isArray(e)?[]:{}}function N(e,t){return t.clone!==!1&&t.isMergeableObject(e)?v(It(e),e,t):e}function Et(e,t,s){return e.concat(t).map(function(n){return N(n,s)})}function Nt(e,t){if(!t.customMerge)return v;var s=t.customMerge(e);return typeof s=="function"?s:v}function yt(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(t){return Object.propertyIsEnumerable.call(e,t)}):[]}function F(e){return Object.keys(e).concat(yt(e))}function R(e,t){try{return t in e}catch{return!1}}function gt(e,t){return R(e,t)&&!(Object.hasOwnProperty.call(e,t)&&Object.propertyIsEnumerable.call(e,t))}function wt(e,t,s){var n={};return s.isMergeableObject(e)&&F(e).forEach(function(i){n[i]=N(e[i],s)}),F(t).forEach(function(i){gt(e,i)||(R(e,i)&&s.isMergeableObject(t[i])?n[i]=Nt(i,s)(e[i],t[i],s):n[i]=N(t[i],s))}),n}function v(e,t,s){s=s||{},s.arrayMerge=s.arrayMerge||Et,s.isMergeableObject=s.isMergeableObject||ft,s.cloneUnlessOtherwiseSpecified=N;var n=Array.isArray(t),i=Array.isArray(e),a=n===i;return a?n?s.arrayMerge(e,t,s):wt(e,t,s):N(t,s)}v.all=function(t,s){if(!Array.isArray(t))throw new Error("first argument should be an array");return t.reduce(function(n,i){return v(n,i,s)},{})};var Tt=v,bt=Tt;const Lt=dt(bt),Ct={basePath:"/",enableNotify:!0,codeBlock:{trim:!0,additionalClasses:[]},local:{scheme:"vscode://file//"}};return()=>({id:"external-code",init:e=>{const t=e.getRevealElement();if(!t)console.error("Cannot find reveal element");else{const n=e.getConfig().externalCode??{},i=Lt(Ct,n);return ut(t,i)}}})});