UNPKG

@edc4it/reveal.js-external-code

Version:

[![Version](https://img.shields.io/npm/v/@edc4it/reveal.js-external-code)](#)

410 lines (409 loc) 19.6 kB
(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)}})(); var z = Object.defineProperty; var J = (e, t, s) => t in e ? z(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s; var A = (e, t, s) => J(e, typeof t != "symbol" ? t + "" : t, s); class Q extends Error { constructor(t) { super(t); } } function P(e, t = !1) { const s = t ? -1 : 0; return e.replace(/\s/g, "").split(",").map((i) => { if (/^[\d-]+$/.test(i)) { const [o, c] = i.split("-").map((l) => Number.parseInt(l, 10) + s); return isNaN(c) ? { start: o, end: o } : { start: o, end: c }; } else throw new Q(`could not parse ${i}`); }); } function tt(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, c = t.slice(1); if (o) { const f = P(o, !0); return { lines: c, range: f, annotated: !0 }; } else return { lines: c, annotated: !0 }; } else return { lines: t, annotated: !1 }; } function et(e, t) { return e.map((s, n) => st(n + 1, t) ? s : "…").filter((s, n, i) => !(s === "…" && i[n - 1] === "…")); } function st(e, t) { return t.find((s) => e >= s.start && e <= s.end) !== void 0; } function nt(e, t) { e.parentNode ? e.replaceWith(t) : console.error("The element to be replaced is not attached to the DOM."); } function it(e, t) { e.forEach((s) => { t.setAttribute(s.nodeName, s.nodeValue ?? ""); }); } function rt(e) { return e.split(".").pop(); } class ot { constructor(t, s, n, i, a) { A(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), it([...this.attributes].filter((i) => i.nodeName !== "class"), s), s.innerHTML = this.codeStr, t; } /** * Create the required elements * @private * @returns the wrapper and the code element for further processing */ _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 }; } /** * constructs the list of classes for the code element * @private */ _codeClasses() { const t = rt(this.src), s = this.providedLanguage ?? t ?? ""; return [...this.options.codeBlock.additionalClasses, s]; } /** * Create the footer html with a link to the external code * @param preElement * @private */ _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>`; } /** * Create the outer wrapper * @private */ _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 at(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 ct(e, t) { if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function"); } function lt(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 ut(e, t, s) { return t && lt(e.prototype, t), e; } var dt = Object.defineProperty, u = function(e, t) { return dt(e, "name", { value: t, configurable: !0 }); }, ft = `<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> `, ht = `<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> `, pt = `<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> `, mt = `<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> `, vt = `<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> `, Ot = u(function(e) { return new DOMParser().parseFromString(e, "text/html").body.childNodes[0]; }, "stringToHTML"), m = u(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" }, d = { ERROR: "error", WARNING: "warning", SUCCESS: "success", INFO: "info" }, Y = { OUTLINE: "outline", FILLED: "filled" }, y = { FADE: "fade", SLIDE: "slide" }, v = { CLOSE: m(ft), SUCCESS: m(mt), ERROR: m(ht), WARNING: m(vt), INFO: m(pt) }, x = u(function(e) { e.wrapper.classList.add(r.NOTIFY_FADE), setTimeout(function() { e.wrapper.classList.add(r.NOTIFY_FADE_IN); }, 100); }, "fadeIn"), M = u(function(e) { e.wrapper.classList.remove(r.NOTIFY_FADE_IN), setTimeout(function() { e.wrapper.remove(); }, e.speed); }, "fadeOut"), It = u(function(e) { e.wrapper.classList.add(r.NOTIFY_SLIDE), setTimeout(function() { e.wrapper.classList.add(r.NOTIFY_SLIDE_IN); }, 100); }, "slideIn"), Et = u(function(e) { e.wrapper.classList.remove(r.NOTIFY_SLIDE_IN), setTimeout(function() { e.wrapper.remove(); }, e.speed); }, "slideOut"), D = function() { function e(t) { var s = this; ct(this, e), this.notifyOut = u(function(K) { K(s); }, "notifyOut"); var n = t.notificationsGap, i = n === void 0 ? 20 : n, a = t.notificationsPadding, o = a === void 0 ? 20 : a, c = t.status, f = c === void 0 ? "success" : c, l = t.effect, h = l === void 0 ? y.FADE : l, I = t.type, g = I === void 0 ? "outline" : I, N = t.title, E = t.text, w = t.showIcon, $ = w === void 0 ? !0 : w, T = t.customIcon, B = T === void 0 ? "" : T, b = t.customClass, W = b === void 0 ? "" : b, L = t.speed, Z = L === void 0 ? 500 : L, C = t.showCloseButton, G = C === void 0 ? !0 : C, S = t.autoclose, H = S === void 0 ? !0 : S, _ = t.autotimeout, q = _ === void 0 ? 3e3 : _, F = t.position, X = F === void 0 ? "right top" : F, R = t.customWrapper, V = R === void 0 ? "" : R; if (this.customWrapper = V, this.status = f, this.title = N, this.text = E, this.showIcon = $, this.customIcon = B, this.customClass = W, this.speed = Z, this.effect = h, this.showCloseButton = G, this.autoclose = H, this.autotimeout = q, this.notificationsGap = i, this.notificationsPadding = o, this.type = g, this.position = X, !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 ut(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 = v.CLOSE, this.wrapper.appendChild(n), n.addEventListener("click", function() { s.close(); }); } }, { key: "setWrapper", value: function() { var s = this; switch (this.customWrapper ? this.wrapper = Ot(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 Y.OUTLINE: this.wrapper.classList.add(r.NOTIFY_OUTLINE); break; case Y.FILLED: this.wrapper.classList.add(r.NOTIFY_FILLED); break; default: this.wrapper.classList.add(r.NOTIFY_OUTLINE); } switch (this.status) { case d.SUCCESS: this.wrapper.classList.add(r.NOTIFY_SUCCESS); break; case d.ERROR: this.wrapper.classList.add(r.NOTIFY_ERROR); break; case d.WARNING: this.wrapper.classList.add(r.NOTIFY_WARNING); break; case d.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 = u(function(i) { switch (i) { case d.SUCCESS: return v.SUCCESS; case d.ERROR: return v.ERROR; case d.WARNING: return v.WARNING; case d.INFO: return v.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 y.FADE: this.selectedNotifyInEffect = x, this.selectedNotifyOutEffect = M; break; case y.SLIDE: this.selectedNotifyInEffect = It, this.selectedNotifyOutEffect = Et; break; default: this.selectedNotifyInEffect = x, this.selectedNotifyOutEffect = M; } } }]), e; }(); u(D, "Notify"); var U = D; globalThis.Notify = U; function Nt(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 yt(e, t) { const { range: s, lines: n, annotated: i } = tt(e), a = t ? P(t, i) : void 0, o = s ?? a; return (o ? et(n, o) : n).join(` `); } async function gt(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"), c = i.hasAttribute("data-escape-code"); if (a) { const f = Nt(t.basePath, a); try { const l = await at(f, t); if (l) { const h = c ? l.replace(/[&<>'"]/g, (E) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "'": "&#39;", '"': "&quot;" })[E] || E) : l, I = yt(h, o), N = new ot(i.attributes, a, f, I, t).create(); nt(i, N); } } catch (l) { const h = `exception while processing external code ${a}`; t.enableNotify && new U({ text: h }), console.error(h, l); } } else console.error("object[type='reveal.js/code'] has no src attribute", i); }); return Promise.all(n); } function wt(e) { return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e; } var Tt = function(t) { return bt(t) && !Lt(t); }; function bt(e) { return !!e && typeof e == "object"; } function Lt(e) { var t = Object.prototype.toString.call(e); return t === "[object RegExp]" || t === "[object Date]" || _t(e); } var Ct = typeof Symbol == "function" && Symbol.for, St = Ct ? Symbol.for("react.element") : 60103; function _t(e) { return e.$$typeof === St; } function Ft(e) { return Array.isArray(e) ? [] : {}; } function O(e, t) { return t.clone !== !1 && t.isMergeableObject(e) ? p(Ft(e), e, t) : e; } function Rt(e, t, s) { return e.concat(t).map(function(n) { return O(n, s); }); } function At(e, t) { if (!t.customMerge) return p; var s = t.customMerge(e); return typeof s == "function" ? s : p; } function Yt(e) { return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(e).filter(function(t) { return Object.propertyIsEnumerable.call(e, t); }) : []; } function j(e) { return Object.keys(e).concat(Yt(e)); } function k(e, t) { try { return t in e; } catch { return !1; } } function xt(e, t) { return k(e, t) && !(Object.hasOwnProperty.call(e, t) && Object.propertyIsEnumerable.call(e, t)); } function Mt(e, t, s) { var n = {}; return s.isMergeableObject(e) && j(e).forEach(function(i) { n[i] = O(e[i], s); }), j(t).forEach(function(i) { xt(e, i) || (k(e, i) && s.isMergeableObject(t[i]) ? n[i] = At(i, s)(e[i], t[i], s) : n[i] = O(t[i], s)); }), n; } function p(e, t, s) { s = s || {}, s.arrayMerge = s.arrayMerge || Rt, s.isMergeableObject = s.isMergeableObject || Tt, s.cloneUnlessOtherwiseSpecified = O; var n = Array.isArray(t), i = Array.isArray(e), a = n === i; return a ? n ? s.arrayMerge(e, t, s) : Mt(e, t, s) : O(t, s); } p.all = function(t, s) { if (!Array.isArray(t)) throw new Error("first argument should be an array"); return t.reduce(function(n, i) { return p(n, i, s); }, {}); }; var jt = p, Pt = jt; const Dt = /* @__PURE__ */ wt(Pt), Ut = { basePath: "/", enableNotify: !0, codeBlock: { trim: !0, additionalClasses: [] }, local: { scheme: "vscode://file//" } }, $t = () => ({ id: "external-code", init: (e) => { const t = e.getRevealElement(); if (!t) console.error("Cannot find reveal element"); else { const n = e.getConfig().externalCode ?? {}, i = Dt(Ut, n); return gt(t, i); } } }); export { $t as default };