vaderjs 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/images/router.png +0 -0
- package/images/state.png +0 -0
- package/package.json +1 -1
- package/ts.config.json +1 -0
- package/vader-min.js +1 -1
- package/vader.js +103 -323
- package/worker-min.js +1 -1
- package/worker.js +242 -167
|
Binary file
|
package/images/state.png
ADDED
|
Binary file
|
package/package.json
CHANGED
package/ts.config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/vader-min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let dom=[],states={},worker=new Worker(new URL("./worker.js",import.meta.url));function markdown(t){let e=t.match(/(#+)(.*)/g);return e&&e.forEach((e=>{if(e.includes("/")||e.includes("<")||e.includes(">"))return;let s=e.split("#").length;t=t.replace(e,`<h${s} class="markdown_heading">${e.replace(/#/g,"")}</h${s}>`)})),t=(t=(t=(t=(t=(t=(t=t.replace(/\*\*(.*?)\*\*/g,((t,e)=>`<b class="markdown_bold">${e}</b>`))).replace(/\*(.*?)\*/g,((t,e)=>`<i class="markdown_italic">${e}</i>`))).replace(/`(.*?)`/g,((t,e)=>`<code>${e}</code>`))).replace(/\[([^\]]+)\]\(([^)]+)\)/g,((t,e,s)=>`<a class="markdown_link" href="${s}">${e}</a>`))).replace(/!\[([^\]]+)\]\(([^)]+)\)/g,((t,e,s)=>`<img class="markdown_image" src="${s}" alt="${e}" />`))).split("\n").map(((t,e,s)=>t.match(/^\s*-\s+(.*?)$/gm)?0!==e&&s[e-1].match(/^\s*-\s+(.*?)$/gm)?e!==s.length-1&&s[e+1].match(/^\s*-\s+(.*?)$/gm)?`<li>${t.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`:`<li>${t.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li></ul>`:`<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${t.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`:t)).join("\n")).split("\n").map(((t,e,s)=>t.match(/^\s*\d+\.\s+(.*?)$/gm)?0!==e&&s[e-1].match(/^\s*\d+\.\s+(.*?)$/gm)?e!==s.length-1&&s[e+1].match(/^\s*\d+\.\s+(.*?)$/gm)?`<li>${t.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`:`<li>${t.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li></ol>`:`<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${t.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`:t)).join("\n")}export const useRef=t=>{const e=document.querySelector(`[ref="${t}"]`);return{current:e,update:t=>{const s=(new DOMParser).parseFromString(t,"text/html").body.firstChild;if(e){!s.isEqualNode(e)&&e.parentNode.replaceChild(s,e)}}}};let components=[];export class Component{constructor(){this.states={},this.name=this.constructor.name,this.executedEffects={},this.storedProps={},this.componentMounted=!1,this.hasMounted=!1,this.$_signal_subscribers=[],this.$_signal_subscribers_ran=[],this.effects={},this.$_useStore_subscribers=[],this.init(),this.Componentcontent=null,this.$_signal_dispatch_event=new CustomEvent("SignalDispatch",{detail:{hasUpdated:!1,state:null}}),this.$_signal_dispatch_cleanup_event=new CustomEvent("Signal_Cleanup_Dispatch",{detail:{state:null,lastState:null}}),this.snapshots=[],this.dom=[],this.cfr=!1}adapter(t){}init(){this.registerComponent()}registerComponent(){components.push(this)}setState(t,e){this.states[t]=e,this.updateComponent()}unmount(){this.componentMounted=!1,this.componentWillUnmount(),document.querySelector(`[data-component="${this.name}"]`).remove(),document.querySelector(`[data-component="${this.name}"]`)||(components=components.filter((t=>t.name!==this.name)))}componentUpdate(t,e,s){}componentDidMount(){}componentWillUnmount(){}signal=(t,e)=>{let s=!1,[n,r]=this.useState(t,e,(()=>{if(this.$_signal_subscribers.length>0){for(var t=0;t<this.$_signal_subscribers.length;t++)if(!s){if(this.$_signal_subscribers[t].runonce&&this.$_signal_subscribers_ran.includes(this.$_signal_subscribers[t]))break;return this.$_signal_subscribers[t].function(n),void this.$_signal_subscribers_ran.push(this.$_signal_subscribers[t])}}else this.$_signal_dispatch_event.detail.hasUpdated=!0,this.$_signal_dispatch_event.detail.state=n,window.dispatchEvent(this.$_signal_dispatch_event)}));return this.$_signal_subscribe=(t,e)=>(this.$_signal_subscribers.push({function:t,runonce:e}),t),this.$_signal_cleanup=t=>{this.lastState=n,this.$_signal_subscribers=this.$_signal_subscribers.filter((e=>e.function!==t)),this.$_signal_dispatch_cleanup_event.detail.state=this.states,this.$_signal_dispatch_cleanup_event.detail.lastState=this.lastState,window.dispatchEvent(this.$_signal_dispatch_event)},this.$_signal_dispatch=()=>{for(var t=0;t<this.$_signal_subscribers.length&&(!this.$_signal_subscribers[t].runonce||!this.$_signal_subscribers_ran.includes(this.$_signal_subscribers[t]));t++)this.$_signal_subscribers[t].function(n),this.$_signal_subscribers_ran.push(this.$_signal_subscribers[t])},this.$_signal_get=()=>n,this.$_signal_call=()=>{s=!0,this.$_signal_dispatch()},this.$_signal_set=t=>{r(t)},{subscribe:this.$_signal_subscribe,cleanup:this.$_signal_cleanup,dispatch:this.$_signal_dispatch,call:this.$_signal_call,set:this.$_signal_set,get:this.$_signal_get}};useAuth(t){let e=t.rulesets;if(!e)throw new Error("No rulesets provided");let s=t.user,n={can:t=>{let n=!1;return e.forEach((e=>{e.action===t&&e.condition(s)&&(n=!0)})),n},hasRole:t=>s.role&&s.role.includes(t),canWithRole:(t,e)=>n.can(t)&&n.hasRole(e),assignRule:t=>{e.some((e=>e.action===t.action))||e.push(t)},revokeRule:t=>{e=e.filter((e=>e.action!==t))},canAnyOf:t=>t.some((t=>n.can(t))),canAllOf:t=>t.every((t=>n.can(t))),canGroup:(t,e="any")=>"any"===e?n.canAnyOf(t):n.canAllOf(t)};return n}useReducer(t,e,s){return this.states[t]||(this.states[t]=s),[this.states[t],s=>{this.states[t]=e(this.states[t],s),this.updateComponent()}]}runEffects(){Object.keys(this.effects).forEach((t=>{this.effects[t].forEach((t=>{this.executedEffects[t]||(t(),this.executedEffects[t]=!0)}))}))}useSyncStore(t,e){let[s,n]=this.useState(t,e||localStorage.getItem(`$_vader_${t}`,(e=>{localStorage.setItem(`$_vader_${t}`,JSON.stringify(e)),this.$_useStore_subscribers.forEach((t=>{t(e)}))}))||{});return{getField:t=>s[t],setField:(t,e)=>{const r={...s,[t]:e};n(r)},subscribe:t=>this.$_useStore_subscribers.push(t),clear:t=>{let e=s;delete e[t],n(e)}}}useState(t,e,s=null){return this.states[t]||(this.states[t]=e),[this.states[t],e=>{this.states[t]=e,this.updateComponent(),"function"==typeof s&&s()}]}useRef(t){const e=this.dom[t];return{current:()=>e,update:t=>{const s=(new DOMParser).parseFromString(t,"text/html").body.firstChild;if(e){!s.isEqualNode(e)&&e.parentNode.replaceChild(s,e)}}}}useEffect(t,e){return this.effects[this.name]||(this.effects[this.name]=[]),this.effects[this.name].push(t),e.length>0?e.forEach((t=>{if(t.set)throw new Error("signal found, do not use effect and signals at the same time - signals are more efficient")})):this.hasMounted||(t(),this.hasMounted=!0),{cleanup:()=>{this.effects[this.name]=this.effects[this.name].filter((e=>e!==t))}}}$Function(t){let e=t.name;return e||(e="anonymous"+Math.floor(1e17*Math.random())),window[e]=t,`window.${e}()`}updateComponent(){const t=document.createDocumentFragment();Object.keys(components).forEach((async e=>{const{name:s}=components[e];let n=document.querySelector(`[data-component="${s}"]`),r={name:s,time:(new Date).getTime(),prev_state:this.states,prev_props:this.storedProps,content:n.innerHTML};if(!n)return;const i=await new Function("useState","useEffect","useAuth","useReducer","useSyncStore","signal","rf","props","render","return `"+await this.render()+"`;")(this.useState,this.useEffect,this.useAuth,this.useReducer,this.useSyncStore,this.signal,this.render);if(i!==n.innerHTML){if(this.snapshots.length>0){this.snapshots[this.snapshots.length-1]!==r&&this.snapshots.push(r)}else this.snapshots.push(r);this.componentUpdate(r.prev_state,r.prev_props,r.content),t.appendChild(document.createRange().createContextualFragment(i)),n.innerHTML="",n.appendChild(t),this.runEffects()}}))}validateClassName(t){return/^[a-zA-Z0-9-_]+$/.test(t)}parseHTML(t){const e=(new DOMParser).parseFromString(t,"text/html");return e.documentElement.querySelectorAll("*").forEach((t=>{if("IMG"===t.nodeName){if(!t.hasAttribute("alt")&&!document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_accessibility --\x3e"))throw new SyntaxError(`Image: ${t.outerHTML} missing alt attribute`);if(t.hasAttribute("alt")&&t.getAttribute("alt").length<1&&!document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_accessibility --\x3e"))throw new SyntaxError(`Image: ${t.outerHTML} alt attribute cannot be empty`);if(t.hasAttribute("src")&&!t.getAttribute("src")?.includes("http")||!t.getAttribute("src")?.includes("https")&&!document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_accessibility --\x3e")){let e=t.getAttribute("src");t.setAttribute("aria-hidden","true"),t.setAttribute("hidden","true");let s=window.location.origin+window.location.pathname.replace(/\/[^\/]*$/,"")+"/public/"+t.getAttribute("src"),n=new Image;n.src=s,n.onerror=()=>{throw t.setAttribute("src",e),new Error(`Image: ${t.outerHTML} not found`)},t.setAttribute("src",s),n.onload=()=>{document.querySelectorAll(`img[src="${s}"]`).forEach((t=>{t.setAttribute("src",s),t.removeAttribute("aria-hidden"),t.removeAttribute("hidden")}))}}}else{if(t.hasAttribute("ref")&&(e[t.getAttribute("ref")]=t),"MARKDOWN"===t.nodeName&&(t.innerHTML=markdown(t.innerHTML.replace(/\\n/g,"\n").trim())),t.hasAttribute("class")){if(!document.documentElement.outerHTML.includes("\x3c!-- #vader-allow_class --\x3e"))throw console.warn("you can disable class errors using, \x3c!-- #vader-allow_class --\x3e"),new Error("class attribute is not allowed, please use className instead")}else t.hasAttribute("className")&&(t.setAttribute("class",t.getAttribute("className")),t.removeAttribute("className"));t.hasAttribute("href")&&t.getAttribute("href").startsWith("/")&&!document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_relative-paths --\x3e")&&t.setAttribute("href",`#/${t.getAttribute("href").replace("/","")}`),!t.hasAttribute("src")||t.getAttribute("src").includes("http")||t.getAttribute("src").includes("https")||document.documentElement.outerHTML.includes("\x3c!-- #vader-disable_relative-paths --\x3e")||t.setAttribute("src",`./public/${t.getAttribute("src")}`)}})),t=e.body.innerHTML,this.Componentcontent=t,t.includes("<div data-component")||(t=`<div data-component="${this.name}">${t}</div>`),markdown(t.replace(/\\n/g,"\n").trim())}html(t,...e){let s=setInterval((()=>{document.querySelector(`[data-component="${this.name}"]`)&&(clearInterval(s),this.componentMounted=!0,document.querySelector(`[data-component="${this.name}"]`)?.querySelectorAll("*").forEach((t=>{t.hasAttribute("ref")&&(this.dom[t.getAttribute("ref")]=t)})),this.componentDidMount())}),100),n=document.createElement("script");n.setAttribute("type","text/javascript"),n.setAttribute("data-component-script",this.name);this.dom;if(this.cfr){worker.postMessage({strings:t,args:e,location:window.location.origin+window.location.pathname.replace(/\/[^\/]*$/,"")+"/public/",name:this.name});let s=new Promise(((t,e)=>{worker.onmessage=e=>{if(e.data.error)throw new Error(e.data.error);this.dom;let s=e.data.js,n=e.data.template;const r=this.useState.bind(this),i=this.useEffect.bind(this),a=this.useReducer.bind(this),o=this.useAuth.bind(this),c=this.useSyncStore.bind(this),l=this.signal.bind(this),u=this.$Function.bind(this);let h=this.states;const d=this.useRef.bind(this);new Function("useState","useEffect","useAuth","useReducer","useSyncStore","signal","rf","dom","render","states","useRef",s)(r,i,o,a,c,l,u,this.dom,this.render,this.states,d),t(new Function("useRef","states","return`"+n+"`")(d,h))},worker.onerror=t=>{e(t)}}));return s}{let s="";for(let n=0;n<t.length;n++)s+=t[n],n<e.length&&(s+=e[n]);return s=new Function("useRef",`return \`${s}\``)(useRef),s.trim().startsWith("<body>")||console.warn("You should wrap your html in a body tag, vader may not grab all html!"),this.parseHTML(s)}}async render(t){}}const Vader={Component:Component,useRef:useRef};export const component=()=>new Component;export const rf=(t,e)=>{window[t]=e};let cache={};async function handletemplate(t){let e=(new DOMParser).parseFromString(t,"text/html"),s=e.documentElement.querySelectorAll("*");if(s.length>0)for(var n=0;n<s.length;n++)if("INCLUDE"===s[n].nodeName){if(!s[n].getAttribute("src")||""===s[n].getAttribute("src"))throw new Error("Include tag must have src attribute");let t=s[n].getAttribute("src")?.split("/").pop()?.split(".")[0],r=await include(s[n].getAttribute("src"));r=r.replace(/`/g,"\\`"),cache[s[n].getAttribute("src")]=r,r=new Function(`return \`${r}\`;`)();let i=(new DOMParser).parseFromString(r,"text/html");i.querySelectorAll("include").forEach((t=>{t.remove()})),e.querySelectorAll(t).forEach((t=>{if(t.attributes.length>0)for(var s=0;s<t.attributes.length;s++)i.body.outerHTML=i.body.outerHTML.replaceAll(`{{${t.attributes[s].name}}}`,t.attributes[s].value);if(t.children.length>0&&i.body.querySelector("slot"))for(s=0;s<t.children.length;s++){i.body.querySelectorAll("slot").forEach((e=>{let s=e.getAttribute("id");console.log(s),(t.hasAttribute("key")&&t.getAttribute("key")===s||!t.hasAttribute("key")&&t.nodeName===s)&&(e.outerHTML=`<div>${t.innerHTML}</div>`)}))}e.body.querySelectorAll("include").forEach((t=>{t.remove()})),e.body.outerHTML=e.body.outerHTML.replace(/`/g,"\\`"),e.body.outerHTML=e.body.outerHTML.replace(t.outerHTML,new Function(`return \`${i.body.outerHTML}\`;`)())}))}return e.body.outerHTML=e.body.outerHTML.replace(/`/g,"\\`"),t=new Function(`return \`${e.body.outerHTML}\`;`)()}export const include=async t=>(!t.startsWith("/")||t.includes("/src/")||document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_relative-paths --\x3e")||(t="/src/"+t),cache[t]?await handletemplate(new Function(`return \`${cache[t]}\`;`)()):fetch(`./${t}`).then((e=>{if(404===e.status)throw new Error(`No file found at ${t}`);return e.text()})).then((async e=>(cache[t]=e,e=await handletemplate(new Function(`return \`${e}\`;`)())))));export default Vader;
|
|
1
|
+
let dom=[],states={},worker=new Worker(new URL("./worker.js",import.meta.url));export const useRef=t=>{const e=document.querySelector(`[ref="${t}"]`);return{current:e,update:t=>{const s=(new DOMParser).parseFromString(t,"text/html").body.firstChild;if(e){!s.isEqualNode(e)&&e.parentNode.replaceChild(s,e)}}}};let components=[];export class Component{constructor(){this.states={},this.name=this.constructor.name,this.executedEffects={},this.storedProps={},this.componentMounted=!1,this.hasMounted=!1,this.$_signal_subscribers=[],this.$_signal_subscribers_ran=[],this.effects={},this.$_useStore_subscribers=[],this.init(),this.Componentcontent=null,this.$_signal_dispatch_event=new CustomEvent("SignalDispatch",{detail:{hasUpdated:!1,state:null}}),this.$_signal_dispatch_cleanup_event=new CustomEvent("Signal_Cleanup_Dispatch",{detail:{state:null,lastState:null}}),this.snapshots=[],this.dom=[],this.cfr=!1}adapter(t){}init(){this.registerComponent()}registerComponent(){components.push(this)}setState(t,e){this.states[t]=e,this.updateComponent()}unmount(){this.componentMounted=!1,this.componentWillUnmount(),document.querySelector(`[data-component="${this.name}"]`).remove(),document.querySelector(`[data-component="${this.name}"]`)||(components=components.filter((t=>t.name!==this.name)))}componentUpdate(t,e,s){}componentDidMount(){}componentWillUnmount(){}signal=(t,e)=>{let s=!1,[n,i]=this.useState(t,e,(()=>{if(this.$_signal_subscribers.length>0){for(var t=0;t<this.$_signal_subscribers.length;t++)if(!s){if(this.$_signal_subscribers[t].runonce&&this.$_signal_subscribers_ran.includes(this.$_signal_subscribers[t]))break;return this.$_signal_subscribers[t].function(n),void this.$_signal_subscribers_ran.push(this.$_signal_subscribers[t])}}else this.$_signal_dispatch_event.detail.hasUpdated=!0,this.$_signal_dispatch_event.detail.state=n,window.dispatchEvent(this.$_signal_dispatch_event)}));return this.$_signal_subscribe=(t,e)=>(this.$_signal_subscribers.push({function:t,runonce:e}),t),this.$_signal_cleanup=t=>{this.lastState=n,this.$_signal_subscribers=this.$_signal_subscribers.filter((e=>e.function!==t)),this.$_signal_dispatch_cleanup_event.detail.state=this.states,this.$_signal_dispatch_cleanup_event.detail.lastState=this.lastState,window.dispatchEvent(this.$_signal_dispatch_event)},this.$_signal_dispatch=()=>{for(var t=0;t<this.$_signal_subscribers.length&&(!this.$_signal_subscribers[t].runonce||!this.$_signal_subscribers_ran.includes(this.$_signal_subscribers[t]));t++)this.$_signal_subscribers[t].function(n),this.$_signal_subscribers_ran.push(this.$_signal_subscribers[t])},this.$_signal_get=()=>n,this.$_signal_call=()=>{s=!0,this.$_signal_dispatch()},this.$_signal_set=t=>{i(t)},{subscribe:this.$_signal_subscribe,cleanup:this.$_signal_cleanup,dispatch:this.$_signal_dispatch,call:this.$_signal_call,set:this.$_signal_set,get:this.$_signal_get}};useAuth(t){let e=t.rulesets;if(!e)throw new Error("No rulesets provided");let s=t.user,n={can:t=>{let n=!1;return e.forEach((e=>{e.action===t&&e.condition(s)&&(n=!0)})),n},hasRole:t=>s.role&&s.role.includes(t),canWithRole:(t,e)=>n.can(t)&&n.hasRole(e),assignRule:t=>{e.some((e=>e.action===t.action))||e.push(t)},revokeRule:t=>{e=e.filter((e=>e.action!==t))},canAnyOf:t=>t.some((t=>n.can(t))),canAllOf:t=>t.every((t=>n.can(t))),canGroup:(t,e="any")=>"any"===e?n.canAnyOf(t):n.canAllOf(t)};return n}useReducer(t,e,s){return this.states[t]||(this.states[t]=s),[this.states[t],s=>{this.states[t]=e(this.states[t],s),this.updateComponent()}]}runEffects(){Object.keys(this.effects).forEach((t=>{this.effects[t].forEach((t=>{this.executedEffects[t]||(t(),this.executedEffects[t]=!0)}))}))}useSyncStore(t,e){let[s,n]=this.useState(t,e||localStorage.getItem(`$_vader_${t}`,(e=>{localStorage.setItem(`$_vader_${t}`,JSON.stringify(e)),this.$_useStore_subscribers.forEach((t=>{t(e)}))}))||{});return{getField:t=>s[t],setField:(t,e)=>{const i={...s,[t]:e};n(i)},subscribe:t=>this.$_useStore_subscribers.push(t),clear:t=>{let e=s;delete e[t],n(e)}}}useState(t,e,s=null){return this.states[t]||(this.states[t]=e),[this.states[t],e=>{this.states[t]=e,this.updateComponent(),"function"==typeof s&&s()}]}useRef(t){const e=this.dom[t];return{current:()=>e,update:t=>{const s=(new DOMParser).parseFromString(t,"text/html").body.firstChild;if(e){!s.isEqualNode(e)&&e.parentNode.replaceChild(s,e)}}}}useEffect(t,e){return this.effects[this.name]||(this.effects[this.name]=[]),this.effects[this.name].push(t),e.length>0?e.forEach((t=>{if(t.set)throw new Error("signal found, do not use effect and signals at the same time - signals are more efficient")})):this.hasMounted||(t(),this.hasMounted=!0),{cleanup:()=>{this.effects[this.name]=this.effects[this.name].filter((e=>e!==t))}}}$Function(t){let e=t.name;return e||(e="anonymous"+Math.floor(1e17*Math.random())),window[e]=t,`window.${e}()`}updateComponent(){const t=document.createDocumentFragment();Object.keys(components).forEach((async e=>{const{name:s}=components[e];let n=document.querySelector(`[data-component="${s}"]`),i={name:s,time:(new Date).getTime(),prev_state:this.states,prev_props:this.storedProps,content:n.innerHTML};if(!n)return;const r=await new Function("useState","useEffect","useAuth","useReducer","useSyncStore","signal","rf","props","render","return `"+await this.render()+"`;")(this.useState,this.useEffect,this.useAuth,this.useReducer,this.useSyncStore,this.signal,this.render);if(r!==n.innerHTML){if(this.snapshots.length>0){this.snapshots[this.snapshots.length-1]!==i&&this.snapshots.push(i)}else this.snapshots.push(i);this.componentUpdate(i.prev_state,i.prev_props,i.content),t.appendChild(document.createRange().createContextualFragment(r)),n.innerHTML="",n.appendChild(t),this.runEffects()}}))}validateClassName(t){return/^[a-zA-Z0-9-_]+$/.test(t)}html(t,...e){let s=setInterval((()=>{document.querySelector(`[data-component="${this.name}"]`)&&(clearInterval(s),this.componentMounted=!0,document.querySelector(`[data-component="${this.name}"]`)?.querySelectorAll("*").forEach((t=>{t.hasAttribute("ref")&&(this.dom[t.getAttribute("ref")]=t)})),this.componentDidMount())}),100),n=document.createElement("script");n.setAttribute("type","text/javascript"),n.setAttribute("data-component-script",this.name),worker.postMessage({strings:t,args:e,location:window.location.origin+window.location.pathname.replace(/\/[^\/]*$/,"")+"/public/",name:this.name});let i=new Promise(((t,e)=>{worker.onmessage=e=>{if(e.data.error)throw new Error(e.data.error);this.dom;let s=e.data.js,n=e.data.template;const i=this.useState.bind(this),r=this.useEffect.bind(this),a=this.useReducer.bind(this),o=this.useAuth.bind(this),c=this.useSyncStore.bind(this),u=this.signal.bind(this),h=this.$Function.bind(this);let l=this.states;const d=this.useRef.bind(this);new Function("useState","useEffect","useAuth","useReducer","useSyncStore","signal","$Function","dom","render","states","useRef",s)(i,r,o,a,c,u,h,this.dom,this.render,this.states,d),t(new Function("useRef","states","signal","useState","useReducer","useAuth","useSyncStore","useRef","$Function","return`"+n+"`")(d,l,u,i,a,o,c,d,h))},worker.onerror=t=>{e(t)}}));return i}async render(t){}}const Vader={Component:Component,useRef:useRef};export const component=()=>new Component;export const rf=(t,e)=>{window[t]=e};let cache={};async function handletemplate(t){let e=(new DOMParser).parseFromString(t,"text/html"),s=e.documentElement.querySelectorAll("*");if(s.length>0)for(var n=0;n<s.length;n++)if("INCLUDE"===s[n].nodeName){if(!s[n].getAttribute("src")||""===s[n].getAttribute("src"))throw new Error("Include tag must have src attribute");let t=s[n].getAttribute("src")?.split("/").pop()?.split(".")[0],i=await include(s[n].getAttribute("src"));i=i.replace(/`/g,"\\`"),cache[s[n].getAttribute("src")]=i,i=new Function(`return \`${i}\`;`)();let r=(new DOMParser).parseFromString(i,"text/html");r.querySelectorAll("include").forEach((t=>{t.remove()})),e.querySelectorAll(t).forEach((t=>{if(t.attributes.length>0)for(var s=0;s<t.attributes.length;s++){let e="{{"+t.attributes[s].name+"}}";r.body.innerHTML.includes(e)&&(r.body.innerHTML=r.body.innerHTML.replaceAll(e,t.attributes[s].value))}if(t.children.length>0&&r.body.querySelector("slot"))for(s=0;s<t.children.length;s++){r.body.querySelectorAll("slot").forEach((e=>{let n=e.getAttribute("id");(t.hasAttribute("key")&&t.getAttribute("key")===n||!t.hasAttribute("key")&&t.nodeName===n)&&t.children[s].innerHTML.length>0&&(e.outerHTML=t.children[s].innerHTML)}))}e.body.querySelectorAll("include").forEach((t=>{t.remove()})),e.body.outerHTML=e.body.outerHTML.replace(/`/g,"\\`"),e.body.outerHTML=e.body.outerHTML.replace(t.outerHTML,new Function(`return \`${r.body.outerHTML}\`;`)())}))}return e.body.outerHTML=e.body.outerHTML.replace(/`/g,"\\`"),t=new Function(`return \`${e.body.outerHTML}\`;`)()}export const include=async t=>(!t.startsWith("/")||t.includes("/src/")||document.documentElement.outerHTML.trim().includes("\x3c!-- #vader-disable_relative-paths --\x3e")||(t="/src/"+t),cache[t]?await handletemplate(new Function(`return \`${cache[t]}\`;`)()):fetch(`./${t}`).then((e=>{if(404===e.status)throw new Error(`No file found at ${t}`);return e.text()})).then((async e=>(cache[t]=e,e=await handletemplate(new Function(`return \`${e}\`;`)())))));export default Vader;
|
package/vader.js
CHANGED
|
@@ -1,96 +1,8 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
let dom = [];
|
|
2
3
|
let states = {};
|
|
3
|
-
let worker = new Worker(new URL("./worker.js", import.meta.url));
|
|
4
|
-
/**
|
|
5
|
-
* @function markdown
|
|
6
|
-
* @param {String} content
|
|
7
|
-
* @description Allows you to convert markdown to html
|
|
8
|
-
*/
|
|
9
|
-
function markdown(content) {
|
|
10
|
-
let headers = content.match(/(#+)(.*)/g);
|
|
11
|
-
if (headers) {
|
|
12
|
-
headers.forEach((header) => {
|
|
13
|
-
if (
|
|
14
|
-
header.includes("/") ||
|
|
15
|
-
header.includes("<") ||
|
|
16
|
-
header.includes(">")
|
|
17
|
-
) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
let level = header.split("#").length;
|
|
21
|
-
content = content.replace(
|
|
22
|
-
header,
|
|
23
|
-
`<h${level} class="markdown_heading">${header.replace(
|
|
24
|
-
/#/g,
|
|
25
|
-
""
|
|
26
|
-
)}</h${level}>`
|
|
27
|
-
);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
content = content.replace(/\*\*(.*?)\*\*/g, (match, text) => {
|
|
32
|
-
return `<b class="markdown_bold">${text}</b>`;
|
|
33
|
-
});
|
|
34
|
-
content = content.replace(/\*(.*?)\*/g, (match, text) => {
|
|
35
|
-
return `<i class="markdown_italic">${text}</i>`;
|
|
36
|
-
});
|
|
37
|
-
content = content.replace(/`(.*?)`/g, (match, text) => {
|
|
38
|
-
return `<code>${text}</code>`;
|
|
39
|
-
});
|
|
40
|
-
content = content.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
|
|
41
|
-
return `<a class="markdown_link" href="${url}">${text}</a>`;
|
|
42
|
-
});
|
|
43
|
-
content = content.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
|
|
44
|
-
return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
|
|
45
|
-
});
|
|
46
|
-
content = content
|
|
47
|
-
.split("\n")
|
|
48
|
-
.map((line, index, arr) => {
|
|
49
|
-
if (line.match(/^\s*-\s+(.*?)$/gm)) {
|
|
50
|
-
if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
|
|
51
|
-
return `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(
|
|
52
|
-
/^\s*-\s+(.*?)$/gm,
|
|
53
|
-
"$1"
|
|
54
|
-
)}</li>`;
|
|
55
|
-
} else if (
|
|
56
|
-
index === arr.length - 1 ||
|
|
57
|
-
!arr[index + 1].match(/^\s*-\s+(.*?)$/gm)
|
|
58
|
-
) {
|
|
59
|
-
return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li></ul>`;
|
|
60
|
-
} else {
|
|
61
|
-
return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li>`;
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
return line;
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
.join("\n");
|
|
68
4
|
|
|
69
|
-
|
|
70
|
-
.split("\n")
|
|
71
|
-
.map((line, index, arr) => {
|
|
72
|
-
if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
|
|
73
|
-
if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
|
|
74
|
-
return `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(
|
|
75
|
-
/^\s*\d+\.\s+(.*?)$/gm,
|
|
76
|
-
"$1"
|
|
77
|
-
)}</li>`;
|
|
78
|
-
} else if (
|
|
79
|
-
index === arr.length - 1 ||
|
|
80
|
-
!arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)
|
|
81
|
-
) {
|
|
82
|
-
return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li></ol>`;
|
|
83
|
-
} else {
|
|
84
|
-
return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li>`;
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
return line;
|
|
88
|
-
}
|
|
89
|
-
})
|
|
90
|
-
.join("\n");
|
|
91
|
-
|
|
92
|
-
return content;
|
|
93
|
-
}
|
|
5
|
+
let worker = new Worker(new URL("./worker.js", import.meta.url));
|
|
94
6
|
|
|
95
7
|
/**
|
|
96
8
|
* @function useRef
|
|
@@ -187,7 +99,7 @@ export class Component {
|
|
|
187
99
|
* @property {Array} snapshots
|
|
188
100
|
* @private
|
|
189
101
|
*/
|
|
190
|
-
|
|
102
|
+
this.snapshots = [];
|
|
191
103
|
/**
|
|
192
104
|
* @property {Object} dom
|
|
193
105
|
* @description Allows you to get reference to DOM element
|
|
@@ -217,8 +129,6 @@ export class Component {
|
|
|
217
129
|
*/
|
|
218
130
|
adapter(options) {
|
|
219
131
|
// allow you to override the compoent logic
|
|
220
|
-
|
|
221
|
-
|
|
222
132
|
}
|
|
223
133
|
init() {
|
|
224
134
|
this.registerComponent();
|
|
@@ -386,7 +296,6 @@ export class Component {
|
|
|
386
296
|
* @param {*} detail
|
|
387
297
|
*/
|
|
388
298
|
this.$_signal_set = (detail) => {
|
|
389
|
-
|
|
390
299
|
setState(detail);
|
|
391
300
|
};
|
|
392
301
|
|
|
@@ -725,7 +634,9 @@ export class Component {
|
|
|
725
634
|
|
|
726
635
|
return {
|
|
727
636
|
cleanup: () => {
|
|
637
|
+
// @ts-ignore
|
|
728
638
|
this.effects[this.name] = this.effects[this.name].filter(
|
|
639
|
+
// @ts-ignore
|
|
729
640
|
(effect) => effect !== effectFn
|
|
730
641
|
);
|
|
731
642
|
}
|
|
@@ -838,143 +749,6 @@ export class Component {
|
|
|
838
749
|
return /^[a-zA-Z0-9-_]+$/.test(className);
|
|
839
750
|
}
|
|
840
751
|
|
|
841
|
-
parseHTML(result) {
|
|
842
|
-
const dom = new DOMParser().parseFromString(result, "text/html");
|
|
843
|
-
const elements = dom.documentElement.querySelectorAll("*");
|
|
844
|
-
|
|
845
|
-
elements.forEach((element) => {
|
|
846
|
-
switch (element.nodeName) {
|
|
847
|
-
case "IMG":
|
|
848
|
-
if (
|
|
849
|
-
!element.hasAttribute("alt") &&
|
|
850
|
-
!document.documentElement.outerHTML
|
|
851
|
-
.trim()
|
|
852
|
-
.includes("<!-- #vader-disable_accessibility -->")
|
|
853
|
-
) {
|
|
854
|
-
throw new SyntaxError(
|
|
855
|
-
`Image: ${element.outerHTML} missing alt attribute`
|
|
856
|
-
);
|
|
857
|
-
} else if (
|
|
858
|
-
element.hasAttribute("alt") &&
|
|
859
|
-
// @ts-ignore
|
|
860
|
-
element.getAttribute("alt").length < 1 &&
|
|
861
|
-
!document.documentElement.outerHTML
|
|
862
|
-
.trim()
|
|
863
|
-
.includes("<!-- #vader-disable_accessibility -->")
|
|
864
|
-
) {
|
|
865
|
-
throw new SyntaxError(
|
|
866
|
-
`Image: ${element.outerHTML} alt attribute cannot be empty`
|
|
867
|
-
);
|
|
868
|
-
} else if (
|
|
869
|
-
(element.hasAttribute("src") &&
|
|
870
|
-
!element.getAttribute("src")?.includes("http")) ||
|
|
871
|
-
(!element.getAttribute("src")?.includes("https") &&
|
|
872
|
-
!document.documentElement.outerHTML
|
|
873
|
-
.trim()
|
|
874
|
-
.includes("<!-- #vader-disable_accessibility -->"))
|
|
875
|
-
) {
|
|
876
|
-
let prevurl = element.getAttribute("src");
|
|
877
|
-
element.setAttribute("aria-hidden", "true");
|
|
878
|
-
element.setAttribute("hidden", "true");
|
|
879
|
-
// if window.lcoation.pathname includes a html file remove it and only use the path
|
|
880
|
-
let url =
|
|
881
|
-
window.location.origin +
|
|
882
|
-
window.location.pathname.replace(/\/[^\/]*$/, "") +
|
|
883
|
-
"/public/" +
|
|
884
|
-
element.getAttribute("src");
|
|
885
|
-
let image = new Image();
|
|
886
|
-
image.src = url;
|
|
887
|
-
image.onerror = () => {
|
|
888
|
-
// @ts-ignore
|
|
889
|
-
element.setAttribute("src", prevurl);
|
|
890
|
-
throw new Error(`Image: ${element.outerHTML} not found`);
|
|
891
|
-
};
|
|
892
|
-
element.setAttribute("src", url);
|
|
893
|
-
|
|
894
|
-
image.onload = () => {
|
|
895
|
-
document.querySelectorAll(`img[src="${url}"]`).forEach((img) => {
|
|
896
|
-
img.setAttribute("src", url);
|
|
897
|
-
img.removeAttribute("aria-hidden");
|
|
898
|
-
img.removeAttribute("hidden");
|
|
899
|
-
});
|
|
900
|
-
};
|
|
901
|
-
}
|
|
902
|
-
break;
|
|
903
|
-
|
|
904
|
-
default:
|
|
905
|
-
if (element.hasAttribute("ref")) {
|
|
906
|
-
// @ts-ignore
|
|
907
|
-
dom[element.getAttribute("ref")] = element;
|
|
908
|
-
}
|
|
909
|
-
if (element.nodeName === "MARKDOWN") {
|
|
910
|
-
element.innerHTML = markdown(
|
|
911
|
-
element.innerHTML.replace(/\\n/g, "\n").trim()
|
|
912
|
-
);
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
if (element.hasAttribute("class")) {
|
|
916
|
-
const allowClassComments = document.documentElement.outerHTML.includes(
|
|
917
|
-
"<!-- #vader-allow_class -->"
|
|
918
|
-
);
|
|
919
|
-
if (!allowClassComments) {
|
|
920
|
-
console.warn(
|
|
921
|
-
"you can disable class errors using, <!-- #vader-allow_class -->"
|
|
922
|
-
);
|
|
923
|
-
throw new Error(
|
|
924
|
-
"class attribute is not allowed, please use className instead"
|
|
925
|
-
);
|
|
926
|
-
}
|
|
927
|
-
} else if (element.hasAttribute("className")) {
|
|
928
|
-
// @ts-ignore
|
|
929
|
-
element.setAttribute("class", element.getAttribute("className"));
|
|
930
|
-
element.removeAttribute("className");
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
if (
|
|
934
|
-
element.hasAttribute("href") &&
|
|
935
|
-
// @ts-ignore
|
|
936
|
-
element.getAttribute("href").startsWith("/") &&
|
|
937
|
-
!document.documentElement.outerHTML
|
|
938
|
-
.trim()
|
|
939
|
-
.includes("<!-- #vader-disable_relative-paths -->")
|
|
940
|
-
) {
|
|
941
|
-
element.setAttribute(
|
|
942
|
-
"href",
|
|
943
|
-
// @ts-ignore
|
|
944
|
-
`#/${element.getAttribute("href").replace("/", "")}`
|
|
945
|
-
);
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
if (
|
|
949
|
-
element.hasAttribute("src") &&
|
|
950
|
-
// @ts-ignore
|
|
951
|
-
!element.getAttribute("src").includes("http") &&
|
|
952
|
-
// @ts-ignore
|
|
953
|
-
!element.getAttribute("src").includes("https") &&
|
|
954
|
-
!document.documentElement.outerHTML.includes(
|
|
955
|
-
`<!-- #vader-disable_relative-paths -->`
|
|
956
|
-
)
|
|
957
|
-
) {
|
|
958
|
-
element.setAttribute(
|
|
959
|
-
"src",
|
|
960
|
-
// @ts-ignore
|
|
961
|
-
`./public/${element.getAttribute("src")}`
|
|
962
|
-
);
|
|
963
|
-
}
|
|
964
|
-
break;
|
|
965
|
-
}
|
|
966
|
-
});
|
|
967
|
-
|
|
968
|
-
result = dom.body.innerHTML;
|
|
969
|
-
|
|
970
|
-
this.Componentcontent = result;
|
|
971
|
-
|
|
972
|
-
if (!result.includes("<div data-component")) {
|
|
973
|
-
result = `<div data-component="${this.name}">${result}</div>`;
|
|
974
|
-
}
|
|
975
|
-
return markdown(result.replace(/\\n/g, "\n").trim());
|
|
976
|
-
}
|
|
977
|
-
|
|
978
752
|
/**
|
|
979
753
|
* The `html` method generates and processes HTML content for a component, performing various validations and tasks.
|
|
980
754
|
*
|
|
@@ -1024,9 +798,9 @@ export class Component {
|
|
|
1024
798
|
|
|
1025
799
|
html(strings, ...args) {
|
|
1026
800
|
// @ts-ignore
|
|
1027
|
-
let
|
|
801
|
+
let timer = setInterval(() => {
|
|
1028
802
|
if (document.querySelector(`[data-component="${this.name}"]`)) {
|
|
1029
|
-
clearInterval(
|
|
803
|
+
clearInterval(timer);
|
|
1030
804
|
this.componentMounted = true;
|
|
1031
805
|
|
|
1032
806
|
document
|
|
@@ -1045,95 +819,92 @@ export class Component {
|
|
|
1045
819
|
script.setAttribute("type", "text/javascript");
|
|
1046
820
|
script.setAttribute(`data-component-script`, this.name);
|
|
1047
821
|
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
822
|
+
worker.postMessage({
|
|
823
|
+
strings,
|
|
824
|
+
args,
|
|
825
|
+
location:
|
|
826
|
+
window.location.origin +
|
|
827
|
+
window.location.pathname.replace(/\/[^\/]*$/, "") +
|
|
828
|
+
"/public/",
|
|
829
|
+
name: this.name
|
|
830
|
+
});
|
|
831
|
+
let promise = new Promise((resolve, reject) => {
|
|
832
|
+
worker.onmessage = (e) => {
|
|
833
|
+
if (e.data.error) {
|
|
834
|
+
throw new Error(e.data.error);
|
|
835
|
+
}
|
|
836
|
+
const dom = this.dom; // Assuming this.dom is an object
|
|
837
|
+
let js = e.data.js;
|
|
838
|
+
let template = e.data.template;
|
|
839
|
+
// Bind the component's context
|
|
840
|
+
|
|
841
|
+
const useState = this.useState.bind(this); // Bind the component's context
|
|
842
|
+
const useEffect = this.useEffect.bind(this); // Bind the component's context
|
|
843
|
+
const useReducer = this.useReducer.bind(this); // Bind the component's context
|
|
844
|
+
const useAuth = this.useAuth.bind(this); // Bind the component's context
|
|
845
|
+
const useSyncStore = this.useSyncStore.bind(this); // Bind the component's context
|
|
846
|
+
const signal = this.signal.bind(this); // Bind the component's context
|
|
847
|
+
const $Function = this.$Function.bind(this); // Bind the component's context
|
|
848
|
+
let states = this.states;
|
|
849
|
+
const useRef = this.useRef.bind(this); // Bind the component's context
|
|
850
|
+
new Function(
|
|
851
|
+
"useState",
|
|
852
|
+
"useEffect",
|
|
853
|
+
"useAuth",
|
|
854
|
+
"useReducer",
|
|
855
|
+
"useSyncStore",
|
|
856
|
+
"signal",
|
|
857
|
+
"$Function",
|
|
858
|
+
"dom",
|
|
859
|
+
"render",
|
|
860
|
+
"states",
|
|
861
|
+
"useRef",
|
|
862
|
+
js
|
|
863
|
+
)(
|
|
864
|
+
useState,
|
|
865
|
+
useEffect,
|
|
866
|
+
useAuth,
|
|
867
|
+
useReducer,
|
|
868
|
+
useSyncStore,
|
|
869
|
+
signal,
|
|
870
|
+
$Function,
|
|
871
|
+
this.dom,
|
|
872
|
+
this.render,
|
|
873
|
+
this.states,
|
|
874
|
+
useRef
|
|
875
|
+
);
|
|
1069
876
|
|
|
1070
|
-
|
|
1071
|
-
const useEffect = this.useEffect.bind(this); // Bind the component's context
|
|
1072
|
-
const useReducer = this.useReducer.bind(this); // Bind the component's context
|
|
1073
|
-
const useAuth = this.useAuth.bind(this); // Bind the component's context
|
|
1074
|
-
const useSyncStore = this.useSyncStore.bind(this); // Bind the component's context
|
|
1075
|
-
const signal = this.signal.bind(this); // Bind the component's context
|
|
1076
|
-
const rf = this.$Function.bind(this); // Bind the component's context
|
|
1077
|
-
let states = this.states;
|
|
1078
|
-
const useRef = this.useRef.bind(this); // Bind the component's context
|
|
877
|
+
resolve(
|
|
1079
878
|
new Function(
|
|
879
|
+
"useRef",
|
|
880
|
+
"states",
|
|
881
|
+
"signal",
|
|
1080
882
|
"useState",
|
|
1081
|
-
"useEffect",
|
|
1082
|
-
"useAuth",
|
|
1083
883
|
"useReducer",
|
|
884
|
+
"useAuth",
|
|
1084
885
|
"useSyncStore",
|
|
1085
|
-
"signal",
|
|
1086
|
-
"rf",
|
|
1087
|
-
"dom",
|
|
1088
|
-
"render",
|
|
1089
|
-
"states",
|
|
1090
886
|
"useRef",
|
|
1091
|
-
|
|
887
|
+
"$Function",
|
|
888
|
+
"return" + "`" + template + "`"
|
|
1092
889
|
)(
|
|
890
|
+
useRef,
|
|
891
|
+
states,
|
|
892
|
+
signal,
|
|
1093
893
|
useState,
|
|
1094
|
-
useEffect,
|
|
1095
|
-
useAuth,
|
|
1096
894
|
useReducer,
|
|
895
|
+
useAuth,
|
|
1097
896
|
useSyncStore,
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
this.render,
|
|
1102
|
-
this.states,
|
|
1103
|
-
useRef
|
|
1104
|
-
);
|
|
1105
|
-
|
|
1106
|
-
resolve(
|
|
1107
|
-
new Function("useRef", "states", "return" + "`" + template + "`")(
|
|
1108
|
-
useRef,
|
|
1109
|
-
states
|
|
1110
|
-
)
|
|
1111
|
-
);
|
|
1112
|
-
};
|
|
1113
|
-
worker.onerror = (e) => {
|
|
1114
|
-
reject(e);
|
|
1115
|
-
};
|
|
1116
|
-
});
|
|
1117
|
-
// @ts-ignore
|
|
1118
|
-
return promise;
|
|
1119
|
-
} else {
|
|
1120
|
-
let result = "";
|
|
1121
|
-
for (let i = 0; i < strings.length; i++) {
|
|
1122
|
-
result += strings[i];
|
|
1123
|
-
if (i < args.length) {
|
|
1124
|
-
result += args[i];
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
result = new Function("useRef", `return \`${result}\``)(useRef);
|
|
1128
|
-
|
|
1129
|
-
if (!result.trim().startsWith("<body>")) {
|
|
1130
|
-
console.warn(
|
|
1131
|
-
"You should wrap your html in a body tag, vader may not grab all html!"
|
|
897
|
+
useRef,
|
|
898
|
+
$Function
|
|
899
|
+
)
|
|
1132
900
|
);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
901
|
+
};
|
|
902
|
+
worker.onerror = (e) => {
|
|
903
|
+
reject(e);
|
|
904
|
+
};
|
|
905
|
+
});
|
|
906
|
+
// @ts-ignore
|
|
907
|
+
return promise;
|
|
1137
908
|
}
|
|
1138
909
|
// write types to ensure it returns a string
|
|
1139
910
|
/**
|
|
@@ -1200,7 +971,7 @@ const Vader = {
|
|
|
1200
971
|
* @returns {Component}
|
|
1201
972
|
*/
|
|
1202
973
|
export const component = () => {
|
|
1203
|
-
return new Component()
|
|
974
|
+
return new Component();
|
|
1204
975
|
};
|
|
1205
976
|
|
|
1206
977
|
/**
|
|
@@ -1242,7 +1013,6 @@ async function handletemplate(data) {
|
|
|
1242
1013
|
filedata = new Function(`return \`${filedata}\`;`)();
|
|
1243
1014
|
let newdom = new DOMParser().parseFromString(filedata, "text/html");
|
|
1244
1015
|
|
|
1245
|
-
|
|
1246
1016
|
newdom.querySelectorAll("include").forEach((el) => {
|
|
1247
1017
|
el.remove();
|
|
1248
1018
|
});
|
|
@@ -1254,22 +1024,29 @@ async function handletemplate(data) {
|
|
|
1254
1024
|
if (el.attributes.length > 0) {
|
|
1255
1025
|
for (var i = 0; i < el.attributes.length; i++) {
|
|
1256
1026
|
// @ts-ignore
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1027
|
+
let t = "{{" + el.attributes[i].name + "}}";
|
|
1028
|
+
if (newdom.body.innerHTML.includes(t)) {
|
|
1029
|
+
// @ts-ignore
|
|
1030
|
+
newdom.body.innerHTML = newdom.body.innerHTML.replaceAll(
|
|
1031
|
+
t,
|
|
1032
|
+
el.attributes[i].value
|
|
1033
|
+
);
|
|
1034
|
+
}
|
|
1261
1035
|
}
|
|
1262
1036
|
}
|
|
1263
1037
|
if (el.children.length > 0 && newdom.body.querySelector("slot")) {
|
|
1264
1038
|
for (var i = 0; i < el.children.length; i++) {
|
|
1265
1039
|
let slots = newdom.body.querySelectorAll("slot");
|
|
1266
1040
|
slots.forEach((slot) => {
|
|
1267
|
-
|
|
1268
1041
|
let id = slot.getAttribute("id");
|
|
1269
|
-
|
|
1270
|
-
if
|
|
1271
|
-
|
|
1272
|
-
|
|
1042
|
+
|
|
1043
|
+
if (
|
|
1044
|
+
(el.hasAttribute("key") && el.getAttribute("key") === id) ||
|
|
1045
|
+
(!el.hasAttribute("key") && el.nodeName === id)
|
|
1046
|
+
) {
|
|
1047
|
+
if (el.children[i].innerHTML.length > 0) {
|
|
1048
|
+
slot.outerHTML = el.children[i].innerHTML;
|
|
1049
|
+
}
|
|
1273
1050
|
}
|
|
1274
1051
|
});
|
|
1275
1052
|
}
|
|
@@ -1312,7 +1089,9 @@ export const include = async (path) => {
|
|
|
1312
1089
|
) {
|
|
1313
1090
|
path = "/src/" + path;
|
|
1314
1091
|
}
|
|
1092
|
+
// @ts-ignore
|
|
1315
1093
|
if (cache[path]) {
|
|
1094
|
+
// @ts-ignore
|
|
1316
1095
|
return await handletemplate(new Function(`return \`${cache[path]}\`;`)());
|
|
1317
1096
|
} else {
|
|
1318
1097
|
return fetch(`./${path}`)
|
|
@@ -1323,6 +1102,7 @@ export const include = async (path) => {
|
|
|
1323
1102
|
return res.text();
|
|
1324
1103
|
})
|
|
1325
1104
|
.then(async (data) => {
|
|
1105
|
+
// @ts-ignore
|
|
1326
1106
|
cache[path] = data;
|
|
1327
1107
|
|
|
1328
1108
|
data = await handletemplate(new Function(`return \`${data}\`;`)());
|
|
@@ -1332,4 +1112,4 @@ export const include = async (path) => {
|
|
|
1332
1112
|
}
|
|
1333
1113
|
};
|
|
1334
1114
|
|
|
1335
|
-
export default Vader
|
|
1115
|
+
export default Vader
|
package/worker-min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
onmessage=e=>{let
|
|
1
|
+
onmessage=e=>{let l=Date.now(),a=e.data.strings,t=e.data.args,s="",r=e.data.location.split("/").slice(0,-1).join("/"),c="";for(let e=0;e<a.length;e++)c+=a[e],e<t.length&&(c+=t[e]);let i=c.match(/--([^>]*)--/gs);if(i)for(;i.length;){let e=i.pop();c=c.replace(e,"")}if(c=c.replace(/(#+)(.*)/g,((e,l,a)=>{if(e.includes("<")&&e.includes(">"))return e;{let e=l.length;return`<h ${e} class="markdown_heading">${a}</h${e}>`}})),c=c.replace(/\*\*(.*?)\*\*/g,((e,l)=>`<b class="markdown_bold">${l}</b>`)),c=c.replace(/\*(.*?)\*/g,((e,l)=>`<i class="markdown_italic">${l}</i>`)),c=c.replace(/`(.*?)`/g,((e,l)=>`<code>${l}</code>`)),c=c.replace(/\[([^\]]+)\]\(([^)]+)\)/g,((e,l,a)=>`<a class="markdown_link" href="${a}">${l}</a>`)),c=c.replace(/!\[([^\]]+)\]\(([^)]+)\)/g,((e,l,a)=>`<img class="markdown_image" src="${a}" alt="${l}" />`)),c.split("\n").forEach(((e,l,a)=>{e.match(/^\s*-\s+(.*?)$/gm)&&(c=0!==l&&a[l-1].match(/^\s*-\s+(.*?)$/gm)?l!==a.length-1&&a[l+1].match(/^\s*-\s+(.*?)$/gm)?c.replace(e,`<li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`):c.replace(e,`<li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li></ul>`):c.replace(e,`<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`))})),c.split("\n").forEach(((e,l,a)=>{e.match(/^\s*\d+\.\s+(.*?)$/gm)&&(c=0!==l&&a[l-1].match(/^\s*\d+\.\s+(.*?)$/gm)?l!==a.length-1&&a[l+1].match(/^\s*\d+\.\s+(.*?)$/gm)?c.replace(e,`<li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`):c.replace(e,`<li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li></ol>`):c.replace(e,`<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`))})),c=c.replace(/^\s*-\s+(.*?)$/gm,((e,l)=>`<li class="markdown_list_item">${l}</li>`)),c=c.replace(/^\s*---\s*$/gm,'<hr class="markdown_horizontal" />'),c=c.replace(/^\s*> (.*)$/gm,((e,l)=>`<blockquote class="markdown_blockquote">${l}</blockquote>`)),c=c.replace(/((?: *\|.*?)+)\n((?: *\|.*?)+)/gm,((e,l,a)=>{const t=l.split("|").slice(1,-1),s=a.split("|").slice(1,-1);let r='<table class="markdown_table">';return r+='<thead class="markdown_table_head"><tr class="markdown_table_row">',t.forEach((e=>{r+=`<th class="markdown_table_header_cell">${e}</th>`})),r+='</tr></thead><tbody class="markdown_table_body"><tr class="markdown_table_row">',s.forEach((e=>{r+=`<td class="markdown_table_body_cell">${e}</td>`})),r+="</tr></tbody></table>",r})),!c.includes("<body>"))throw new Error(`Vader Error: You must enclose your html in a body tag for all components. \n\n${c}`);c=c.replace(/classname/g,"class");let n=c.match(/<img([^>]*)>/g);if(n)for(let l=0;l<n.length;l++){let a=n[l],t=a.match(/src="([^"]*)"/);if(!a.match(/alt="([^"]*)"/)&&!c.includes("\x3c!-- #vader-disable_accessibility --\x3e"))throw new Error(`Vader Error: You must include an alt attribute in the image tag \n\n${a} of class ${e.data.name}. `);t&&t.forEach((l=>{if(l.includes("http")||c.includes("\x3c!-- #vader-disable_relative-paths --\x3e")){if(!l.includes("http")&&l.startsWith(".")&&!c.includes("\x3c!-- #vader-disable_relative-paths --\x3e"))throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${l}`)}else c=c.replace(`src="${l}"`,`src="${r}/${l}"`)}))}let o=c.match(/href="([^"]*)"/g);if(o)for(;o.length;){let l=o.pop();if(l=l.replace('href="',"").replace('"',""),l.includes("http")||c.includes("\x3c!-- #vader-disable_relative-paths --\x3e")){if(!l.includes("http")&&l.startsWith(".")&&!c.includes("\x3c!-- #vader-disable_relative-paths --\x3e"))throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${l}`)}else c=c.replace(`href="${l}"`,`href="#${l}"`)}let d=Date.now()-l,p=!1;(r.includes("localhost")||r.includes("127.0.0.1")&&!p)&&(p=!0,c+=`\${console.log('%c${e.data.name} component rendered in ${d}ms')}`);const h=c.split("<script>");h&&h.forEach(((e,l)=>{if(0===l)c=e;else{if(e.includes("// <![CDATA[ <-- For SVG support"))return;let l=e.split("<\/script>")[0];s+=l}}));let m=c.match(/(\$\(.*?\))/gs);if(m)for(;m.length;){let e=m.pop();c=c.replace(e,`\${${e.replace("$(","").replace(")","")}}`)}let $=c.match(/@title '([^>]*)'/);if($){let e=`{document.title = "${$[1]}", ""}`;c=c.replace($[0],"$"+e)}c=c.replaceAll('=""',"");let g=c.match(/@style{([^>]*)};/gs);if(g)for(let e=0;e<g.length;e++){if(!g[e].includes("style"))continue;g[e].match(/@style{([^>]*)};/).forEach((e=>{let l=e.replace("@style{","").replace("};","");l=l.replaceAll(",",";"),l=l.replaceAll(/'/g," "),c=c.replace(e,`style="${l}"`)}))}postMessage({template:`<div data-component=${e.data.name}>${c}</div>`,js:s||""})};
|
package/worker.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
let
|
|
4
|
-
let
|
|
5
|
-
let
|
|
6
|
-
let
|
|
7
|
-
let l = e.data.location.split('/').slice(0,-1).join('/')
|
|
1
|
+
onmessage = (e) => {
|
|
2
|
+
let time_started = Date.now();
|
|
3
|
+
let strings = e.data.strings;
|
|
4
|
+
let args = e.data.args;
|
|
5
|
+
let js = "";
|
|
6
|
+
let l = e.data.location.split("/").slice(0, -1).join("/");
|
|
8
7
|
let result = "";
|
|
9
8
|
for (let i = 0; i < strings.length; i++) {
|
|
10
9
|
result += strings[i];
|
|
@@ -13,214 +12,290 @@ onmessage = (e)=>{
|
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
let comments = result.match(/--([^>]*)--/gs)
|
|
17
|
-
if(comments){
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
15
|
+
let comments = result.match(/--([^>]*)--/gs);
|
|
16
|
+
if (comments) {
|
|
17
|
+
while (comments.length) {
|
|
18
|
+
let comment = comments.pop();
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
result = result.replace(comment, "");
|
|
21
|
+
}
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
|
|
27
24
|
// Convert headings (e.g., #1-6 Heading => <h1-6>Heading</h1-6>)
|
|
28
25
|
// @ts-ignore
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
});
|
|
26
|
+
result = result.replace(/(#+)(.*)/g, (match, hashes, text) => {
|
|
27
|
+
if (!match.includes("<") || !match.includes(">")) {
|
|
28
|
+
let level = hashes.length;
|
|
29
|
+
return `<h ${level} class="markdown_heading">${text}</h${level}>`;
|
|
30
|
+
} else {
|
|
31
|
+
return match;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
// Convert bold (e.g., **Bold** => <b>Bold</b>)
|
|
36
|
+
result = result.replace(/\*\*(.*?)\*\*/g, (match, text) => {
|
|
37
|
+
return `<b class="markdown_bold">${text}</b>`;
|
|
38
|
+
});
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
40
|
+
// Convert italic (e.g., *Italic* => <i>Italic</i>)
|
|
41
|
+
result = result.replace(/\*(.*?)\*/g, (match, text) => {
|
|
42
|
+
return `<i class="markdown_italic">${text}</i>`;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Convert code (e.g., `code` => <code>code</code>)
|
|
46
|
+
result = result.replace(/`(.*?)`/g, (match, text) => {
|
|
47
|
+
return `<code>${text}</code>`;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Convert links (e.g., [Text](URL) => <a href="URL">Text</a>)
|
|
51
|
+
result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
|
|
52
|
+
return `<a class="markdown_link" href="${url}">${text}</a>`;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Convert images (e.g.,  => <img src="URL" alt="Alt" />)
|
|
56
|
+
result = result.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
|
|
57
|
+
return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Convert unordered lists (e.g., * Item => <ul><li>Item</li></ul>)
|
|
61
|
+
result.split("\n").forEach((line, index, arr) => {
|
|
62
|
+
if (line.match(/^\s*-\s+(.*?)$/gm)) {
|
|
63
|
+
if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
|
|
64
|
+
result = result.replace(
|
|
65
|
+
line,
|
|
66
|
+
`<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(
|
|
67
|
+
/^\s*-\s+(.*?)$/gm,
|
|
68
|
+
"$1"
|
|
69
|
+
)}</li>`
|
|
70
|
+
);
|
|
71
|
+
} else if (
|
|
72
|
+
index === arr.length - 1 ||
|
|
73
|
+
!arr[index + 1].match(/^\s*-\s+(.*?)$/gm)
|
|
74
|
+
) {
|
|
75
|
+
result = result.replace(
|
|
76
|
+
line,
|
|
77
|
+
`<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li></ul>`
|
|
78
|
+
);
|
|
79
|
+
} else {
|
|
80
|
+
result = result.replace(
|
|
81
|
+
line,
|
|
82
|
+
`<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li>`
|
|
83
|
+
);
|
|
77
84
|
}
|
|
78
|
-
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
// Convert ordered lists (e.g., 1. Item => <ol><li>Item</li></ol>) in order
|
|
89
|
+
|
|
90
|
+
result.split("\n").forEach((line, index, arr) => {
|
|
91
|
+
if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
|
|
92
|
+
if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
|
|
93
|
+
result = result.replace(
|
|
94
|
+
line,
|
|
95
|
+
`<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(
|
|
96
|
+
/^\s*\d+\.\s+(.*?)$/gm,
|
|
97
|
+
"$1"
|
|
98
|
+
)}</li>`
|
|
99
|
+
);
|
|
100
|
+
} else if (
|
|
101
|
+
index === arr.length - 1 ||
|
|
102
|
+
!arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)
|
|
103
|
+
) {
|
|
104
|
+
result = result.replace(
|
|
105
|
+
line,
|
|
106
|
+
`<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li></ol>`
|
|
107
|
+
);
|
|
108
|
+
} else {
|
|
109
|
+
result = result.replace(
|
|
110
|
+
line,
|
|
111
|
+
`<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li>`
|
|
112
|
+
);
|
|
91
113
|
}
|
|
92
|
-
|
|
114
|
+
}
|
|
115
|
+
});
|
|
93
116
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
result = result.replace(/^\s*> (.*)$/gm, (match, text) => {
|
|
102
|
-
return `<blockquote class="markdown_blockquote">${text}</blockquote>`;
|
|
103
|
-
});
|
|
117
|
+
result = result.replace(/^\s*-\s+(.*?)$/gm, (match, text) => {
|
|
118
|
+
return `<li class="markdown_list_item">${text}</li>`;
|
|
119
|
+
});
|
|
120
|
+
result = result.replace(
|
|
121
|
+
/^\s*---\s*$/gm,
|
|
122
|
+
'<hr class="markdown_horizontal" />'
|
|
123
|
+
);
|
|
104
124
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
125
|
+
// Convert blockquotes (e.g., > Quote => <blockquote>Quote</blockquote>)
|
|
126
|
+
result = result.replace(/^\s*> (.*)$/gm, (match, text) => {
|
|
127
|
+
return `<blockquote class="markdown_blockquote">${text}</blockquote>`;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Convert tables (e.g., | Header | Cell | => <table><thead><tr><th>Header</th><th>Cell</th></tr></thead></table>)
|
|
131
|
+
result = result.replace(
|
|
132
|
+
/((?: *\|.*?)+)\n((?: *\|.*?)+)/gm,
|
|
133
|
+
(match, header, cell) => {
|
|
134
|
+
const headerCells = header.split("|").slice(1, -1);
|
|
135
|
+
const cells = cell.split("|").slice(1, -1);
|
|
109
136
|
let table = '<table class="markdown_table">';
|
|
110
|
-
table +=
|
|
137
|
+
table +=
|
|
138
|
+
'<thead class="markdown_table_head"><tr class="markdown_table_row">';
|
|
111
139
|
headerCells.forEach((headerCell) => {
|
|
112
140
|
table += `<th class="markdown_table_header_cell">${headerCell}</th>`;
|
|
113
141
|
});
|
|
114
|
-
table +=
|
|
142
|
+
table +=
|
|
143
|
+
'</tr></thead><tbody class="markdown_table_body"><tr class="markdown_table_row">';
|
|
115
144
|
cells.forEach((cell) => {
|
|
116
145
|
table += `<td class="markdown_table_body_cell">${cell}</td>`;
|
|
117
146
|
});
|
|
118
|
-
table +=
|
|
147
|
+
table += "</tr></tbody></table>";
|
|
119
148
|
return table;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
149
|
+
}
|
|
150
|
+
);
|
|
124
151
|
|
|
125
|
-
if(!result.includes(
|
|
126
|
-
|
|
152
|
+
if (!result.includes("<body>")) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Vader Error: You must enclose your html in a body tag for all components. \n\n${result}`
|
|
155
|
+
);
|
|
127
156
|
}
|
|
128
157
|
/**
|
|
129
158
|
* @type {string[]}
|
|
130
159
|
* @description - grabbing all className attributes and replace them with class
|
|
131
160
|
*/
|
|
132
161
|
// @ts-ignore
|
|
133
|
-
|
|
134
|
-
|
|
162
|
+
result = result.replace(/classname/g, "class");
|
|
163
|
+
/**
|
|
135
164
|
* @type {string[]}
|
|
136
165
|
* @description - grabbing all image tags and replace the src attribute with the absolute path
|
|
137
166
|
*/
|
|
138
167
|
// @ts-ignore
|
|
139
|
-
let images = result.match(/<img([^>]*)>/g)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if(!
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
168
|
+
let images = result.match(/<img([^>]*)>/g);
|
|
169
|
+
if (images) {
|
|
170
|
+
for (let i = 0; i < images.length; i++) {
|
|
171
|
+
let image = images[i];
|
|
172
|
+
let src = image.match(/src="([^"]*)"/);
|
|
173
|
+
let alt = image.match(/alt="([^"]*)"/);
|
|
174
|
+
|
|
175
|
+
if (!alt && !result.includes("<!-- #vader-disable_accessibility -->")) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`Vader Error: You must include an alt attribute in the image tag \n\n${image} of class ${e.data.name}. `
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (src) {
|
|
181
|
+
src.forEach((s) => {
|
|
182
|
+
if (
|
|
183
|
+
!s.includes("http") &&
|
|
184
|
+
!result.includes("<!-- #vader-disable_relative-paths -->")
|
|
185
|
+
) {
|
|
186
|
+
result = result.replace(`src="${s}"`, `src="${l}/${s}"`);
|
|
187
|
+
} else if (
|
|
188
|
+
!s.includes("http") &&
|
|
189
|
+
s.startsWith(".") &&
|
|
190
|
+
!result.includes("<!-- #vader-disable_relative-paths -->")
|
|
191
|
+
) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${s}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
155
198
|
}
|
|
199
|
+
}
|
|
156
200
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
cache.add(`${l}/${src[1]}`)
|
|
162
|
-
// @ts-ignore
|
|
163
|
-
console.log('cached', `${l}/${src[1]}`)
|
|
164
|
-
}).catch((err)=>{
|
|
165
|
-
console.log(err)
|
|
166
|
-
})
|
|
167
|
-
}else{
|
|
201
|
+
let href = result.match(/href="([^"]*)"/g);
|
|
202
|
+
if (href) {
|
|
203
|
+
while (href.length) {
|
|
204
|
+
let h = href.pop();
|
|
168
205
|
// @ts-ignore
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${h}`)
|
|
184
|
-
}
|
|
206
|
+
h = h.replace('href="', "").replace('"', "");
|
|
207
|
+
if (
|
|
208
|
+
!h.includes("http") &&
|
|
209
|
+
!result.includes("<!-- #vader-disable_relative-paths -->")
|
|
210
|
+
) {
|
|
211
|
+
result = result.replace(`href="${h}"`, `href="#${h}"`);
|
|
212
|
+
} else if (
|
|
213
|
+
!h.includes("http") &&
|
|
214
|
+
h.startsWith(".") &&
|
|
215
|
+
!result.includes("<!-- #vader-disable_relative-paths -->")
|
|
216
|
+
) {
|
|
217
|
+
throw new Error(
|
|
218
|
+
`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${h}`
|
|
219
|
+
);
|
|
185
220
|
}
|
|
221
|
+
}
|
|
186
222
|
}
|
|
187
|
-
|
|
188
|
-
let time_ended = Date.now()
|
|
189
|
-
let time_taken = time_ended - time_started
|
|
190
|
-
let hasran = false
|
|
191
|
-
if(l.includes(
|
|
192
|
-
|
|
193
|
-
|
|
223
|
+
|
|
224
|
+
let time_ended = Date.now();
|
|
225
|
+
let time_taken = time_ended - time_started;
|
|
226
|
+
let hasran = false;
|
|
227
|
+
if (l.includes("localhost") || (l.includes("127.0.0.1") && !hasran)) {
|
|
228
|
+
hasran = true;
|
|
229
|
+
result += `\$\{console.log('%c${e.data.name} component rendered in ${time_taken}ms')\}`;
|
|
194
230
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const d = result.split('<script>')
|
|
231
|
+
|
|
232
|
+
const d = result.split("<script>");
|
|
198
233
|
|
|
199
234
|
if (d) {
|
|
200
235
|
d.forEach((scriptTag, index) => {
|
|
201
236
|
if (index === 0) {
|
|
202
237
|
result = scriptTag;
|
|
203
238
|
} else {
|
|
204
|
-
if(scriptTag.includes(
|
|
205
|
-
return
|
|
239
|
+
if (scriptTag.includes("// <![CDATA[ <-- For SVG support")) {
|
|
240
|
+
return;
|
|
206
241
|
}
|
|
207
|
-
let script = scriptTag.split(
|
|
242
|
+
let script = scriptTag.split("</script>")[0];
|
|
208
243
|
js += script;
|
|
209
244
|
}
|
|
210
245
|
});
|
|
211
246
|
}
|
|
212
|
-
|
|
213
|
-
let
|
|
214
|
-
if(jstemplates){
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
247
|
+
|
|
248
|
+
let jstemplates = result.match(/(\$\(.*?\))/gs);
|
|
249
|
+
if (jstemplates) {
|
|
250
|
+
while (jstemplates.length) {
|
|
251
|
+
let jstemplate = jstemplates.pop();
|
|
252
|
+
// @ts-ignore
|
|
253
|
+
result = result.replace(
|
|
254
|
+
// @ts-ignore
|
|
255
|
+
jstemplate,
|
|
256
|
+
// @ts-ignore
|
|
257
|
+
`$\{${jstemplate.replace("$(", "").replace(")", "")}\}`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
let title = result.match(/@title '([^>]*)'/);
|
|
263
|
+
if (title) {
|
|
264
|
+
let t = title[1];
|
|
265
|
+
let ti = `{document.title = "${t}", ""}`;
|
|
266
|
+
result = result.replace(title[0], "$" + ti);
|
|
267
|
+
}
|
|
268
|
+
// @ts-ignore
|
|
269
|
+
result = result.replaceAll('=""', '')
|
|
270
|
+
let styles = result.match(/@style{([^>]*)};/gs);
|
|
271
|
+
if (styles) {
|
|
272
|
+
for (let i = 0; i < styles.length; i++) {
|
|
273
|
+
// make sure its in a tag
|
|
274
|
+
|
|
275
|
+
if (!styles[i].includes("style")) {
|
|
276
|
+
continue;
|
|
219
277
|
}
|
|
278
|
+
let style = styles[i];
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
let s = style.match(/@style{([^>]*)};/);
|
|
282
|
+
|
|
283
|
+
// @ts-ignore
|
|
284
|
+
s.forEach((style) => {
|
|
285
|
+
|
|
286
|
+
let st = style.replace("@style{", "").replace("};", "");
|
|
287
|
+
// @ts-ignore
|
|
288
|
+
st = st.replaceAll(',', ';')
|
|
289
|
+
// @ts-ignore
|
|
290
|
+
st = st.replaceAll(/'/g, ' ')
|
|
291
|
+
|
|
292
|
+
result = result.replace(style, `style="${st}"`);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
220
295
|
}
|
|
296
|
+
|
|
221
297
|
postMessage({
|
|
222
298
|
template: `<div data-component=${e.data.name}>${result}</div>`,
|
|
223
|
-
js: js ? js :
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
}
|
|
299
|
+
js: js ? js : ""
|
|
300
|
+
});
|
|
301
|
+
};
|