@aiquants/markdown-explorer 0.6.0 → 0.7.1
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/CHANGELOG.md +36 -0
- package/README.md +58 -8
- package/dist/EditSurface-5nbW7NTP.cjs +2 -0
- package/dist/EditSurface-CaS1O6Nt.js +371 -0
- package/dist/{ExplorerSplit-CN5TvRj6.js → ExplorerSplit-C9Y02nvj.js} +391 -417
- package/dist/ExplorerSplit-D60N3_X7.cjs +1 -0
- package/dist/MultiLayout-CqTyFZ_i.cjs +1 -0
- package/dist/MultiLayout-DBpUTcCQ.js +947 -0
- package/dist/{NativeScrollBar-DYEv2GLV.js → NativeScrollBar-CiVlg2ob.js} +1 -1
- package/dist/{NativeScrollBar-CHEQgaVx.cjs → NativeScrollBar-QlOtnddZ.cjs} +1 -1
- package/dist/TreeLayout-bk-Ziybl.js +29 -0
- package/dist/TreeLayout-uwvAgr-t.cjs +1 -0
- package/dist/client/MarkdownExplorer.d.cts +2 -1
- package/dist/client/MarkdownExplorer.d.ts +2 -1
- package/dist/client/storage.d.cts +1 -0
- package/dist/client/storage.d.ts +1 -0
- package/dist/core/contracts.d.cts +58 -0
- package/dist/core/contracts.d.ts +58 -0
- package/dist/core/labels.d.cts +49 -0
- package/dist/core/labels.d.ts +49 -0
- package/dist/core/location.d.cts +7 -0
- package/dist/core/location.d.ts +7 -0
- package/dist/dataRoute-CcZ8PK00.cjs +1 -0
- package/dist/dataRoute-D1vE1wFW.js +270 -0
- package/dist/index-Dlkzr6Zn.cjs +13 -0
- package/dist/index-etFEpIE_.js +3199 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +31 -30
- package/dist/location-DmzVAFvo.cjs +1 -0
- package/dist/{location-CtM94G0O.js → location-Q4sgRrUm.js} +202 -193
- package/dist/merge-BR4jKpIc.cjs +5 -0
- package/dist/merge-DfI0ro2L.js +20 -0
- package/dist/sanitizeWorker.cjs +1 -1
- package/dist/sanitizeWorker.js +173 -172
- package/dist/server/errors.d.cts +1 -1
- package/dist/server/errors.d.ts +1 -1
- package/dist/server/ports.d.cts +33 -1
- package/dist/server/ports.d.ts +33 -1
- package/dist/server.cjs +1 -1
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +882 -751
- package/dist/{storage-DA4ItHzl.js → storage-BSthKLSG.js} +14 -13
- package/dist/storage-Dgi4JXaB.cjs +1 -0
- package/dist/storage.cjs +1 -1
- package/dist/storage.js +1 -1
- package/dist/styles/markdown-explorer.css +1 -1
- package/docs/behavior/dom-hooks.md +17 -3
- package/docs/behavior/layout.md +6 -1
- package/docs/behavior/security.md +11 -1
- package/docs/behavior/url-contract.md +13 -4
- package/package.json +11 -3
- package/dist/ExplorerSplit-CUIbSLQm.cjs +0 -1
- package/dist/MultiLayout-BDcQSpa6.cjs +0 -1
- package/dist/MultiLayout-hPmV-bae.js +0 -922
- package/dist/TreeLayout-BDaIyoGT.cjs +0 -1
- package/dist/TreeLayout-CqzxLpoM.js +0 -23
- package/dist/dataRoute-CUXlMhUJ.cjs +0 -1
- package/dist/dataRoute-Ce6EIuu4.js +0 -206
- package/dist/index-B3nShkkt.cjs +0 -3
- package/dist/index-gJp1iXTc.js +0 -2238
- package/dist/location-FavgY_gB.cjs +0 -1
- package/dist/storage-Bkb2Axdi.cjs +0 -1
package/dist/sanitizeWorker.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const te=require("node:worker_threads"),_=require("parse5"),R=require("@aiquants/markdown/embeds"),S=require("isomorphic-dompurify"),F=require("./dataRoute-CUXlMhUJ.cjs"),G=512,re=4096,j=1024,V=Symbol("placement"),$=2,ne=3,se=new Set(["html","head","body"]),b=0,M=1,U=2,W=3,z=4,O=5,k=6,N=7,Q=8,y=9,X=10,w=-1,oe=0,ae=1,ie=2,le=3,ce=4,ue=5,J=6,fe=7,de=t=>{switch(t){case 9:case 10:case 12:case 13:case 32:return oe;case 47:return ae;case 62:return ie;case 61:return le;case 34:return ce;case 39:return ue;default:return J}},Ee=Uint8Array.from({length:128},(t,i)=>de(i)),B=60,q=47,pe=/[\t\n\f\r />="'<]/g,Te=/["<]/g,Ae=/['<]/g,m=-1-U,he=Int8Array.from([[M,y,w,b,b,b,b],[M,y,w,m,m,m,m],[W,y,w,z,U,U,U],[W,y,w,z,m,m,m],[z,N,w,N,O,k,N],[O,O,O,O,Q,O,O],[k,k,k,k,k,Q,k],[M,N,w,N,N,N,N],[M,y,w,m,m,m,m],[M,y,w,m,m,m,m]].flat()),K=t=>t>=65&&t<=90||t>=97&&t<=122,me=(t,i)=>{let u=new Int32Array(X),f=new Int32Array(X),l=0,A=0,n=0;for(;n<t.length;){if(l===0){const E=t.indexOf("<",n);if(E<0)return!1;n=E+1,t.charCodeAt(n)===q&&(n+=1),K(t.charCodeAt(n))&&(u[b]=0,l=1<<b,n+=1);continue}if((l&-230)===0&&n>=A){const E=l===1<<O?Te:l===1<<k?Ae:pe;E.lastIndex=n;const a=E.exec(t);if(a===null)return!1;n=a.index,t.charCodeAt(n)===B&&(A=n+3)}const T=t.charCodeAt(n),g=T<128?Ee[T]:J;let h=0;for(let E=l;E!==0;E&=E-1){const a=31-Math.clz32(E&-E);let r=he[a*fe+g],s=u[a];if(r<w&&(r=-1-r,s+=1,s>i))return!0;if(r===w)continue;const p=1<<r;((h&p)===0||s>f[r])&&(f[r]=s),h|=p}K(T)&&(t.charCodeAt(n-1)===B||t.charCodeAt(n-1)===q&&t.charCodeAt(n-2)===B)&&(h&1<<b)===0&&(f[b]=0,h|=1<<b);const C=u;u=f,f=C,l=h,n+=1}return!1};class L extends Error{status;constructor(i){super(`[markdown-explorer] the nesting guard stopped the parse: ${i}`),this.name="ParseStop",this.status=i}}const ge=(t,i,u,f)=>{if(!Number.isSafeInteger(i)||i<=0)throw new RangeError(`[markdown-explorer] the nesting guard's depth limit must be a positive safe integer; got ${String(i)}`);if(!Number.isSafeInteger(u)||u<=0)throw new RangeError(`[markdown-explorer] the nesting guard's node budget must be a positive safe integer; got ${String(u)}`);if(!Number.isSafeInteger(f)||f<=0)throw new RangeError(`[markdown-explorer] the nesting guard's attribute character budget must be a positive safe integer; got ${String(f)}`);if(me(t,j))return{status:"too-many-attributes"};const l=i+$,A=t.length+ne;let n=null,T=0,g=0,h=0,C=0;const E=e=>{if(g+=e,g>u)throw new L("too-many-nodes")},a=e=>{for(const o of e)C+=o.name.length+o.value.length;if(C>f)throw new L("too-many-attribute-characters")},r=(e,o,c,d)=>{const P={node:e,above:null,levels:o,height:0,keepsChildren:c,attributes:d,first:null,next:null,previous:null},ee=e;return ee[V]=P,P},s=e=>{const c=e[V];if(c===void 0)throw new Error("[markdown-explorer] the nesting guard was handed a node it did not create");return c},p=(e,o)=>{let c=o;for(let d=e;d!==null&&c>d.height;d=d.above)d.height=c,c+=d.levels;if(n!==null&&n.height>l)throw new L("too-deep")},D=e=>{const o=e.above;o?.keepsChildren&&(e.previous===null?o.first=e.next:e.previous.next=e.next,e.next!==null&&(e.next.previous=e.previous)),e.above=null,e.next=null,e.previous=null},I=(e,o)=>{if(!_.defaultTreeAdapter.isElementNode(o))return;const c=s(o),d=s(e);D(c),c.above=d,d.keepsChildren&&(c.next=d.first,d.first!==null&&(d.first.previous=c),d.first=c),o.parentNode=e,p(d,c.height+c.levels)},H={..._.defaultTreeAdapter,createDocument:()=>{const e=_.defaultTreeAdapter.createDocument();return n=r(e,0,!1,0),e},createDocumentFragment:()=>{const e=_.defaultTreeAdapter.createDocumentFragment();return r(e,0,!1,0),e},createElement:(e,o,c)=>{if(T+=1,T>A)throw new L("too-many-elements");E(1+c.length),a(c);const d=_.defaultTreeAdapter.createElement(e,o,c);return r(d,1,!(o===_.html.NS.HTML&&se.has(e)),c.length),d},createCommentNode:e=>{if(h+=1,h>re)throw new L("too-many-comments");return E(1),_.defaultTreeAdapter.createCommentNode(e)},setTemplateContent:(e,o)=>{_.defaultTreeAdapter.setTemplateContent(e,o);const c=s(o);c.above=s(e),p(c.above,c.height)},appendChild:I,insertBefore:(e,o)=>I(e,o),detachNode:e=>{_.defaultTreeAdapter.isElementNode(e)&&(D(s(e)),e.parentNode=null)},getFirstChild:e=>{const o=s(e);if(!o.keepsChildren)throw new Error("[markdown-explorer] the nesting guard was asked for the children of a node whose children it does not keep");return o.first===null?null:o.first.node},getChildNodes:()=>{throw new Error("[markdown-explorer] the nesting guard keeps no child lists for source locations")},onItemPop:e=>{const o=s(e);o.keepsChildren=!1,o.first=null},insertText:()=>{},insertTextBefore:()=>{},adoptAttributes:(e,o)=>{E(o.length),a(o);const c=s(e);if(c.attributes+=o.length,c.attributes>j)throw new L("too-many-attributes")}};let v;try{v=_.parse(t,{scriptingEnabled:!1,treeAdapter:H})}catch(e){if(e instanceof L)return{status:e.status};throw e}return{status:"within",depth:s(v).height-$,elements:T,nodes:g}};class Y extends RangeError{constructor(i){super(`[markdown-explorer] the HTML nests deeper than ${String(i)} levels`),this.name="HtmlNestingLimitError"}}const _e=["script","style","noscript","template","form","button","textarea","select","option","object","embed","base","meta","link","frame","frameset"],Se=["srcdoc","formaction"],we=/^(?:(?:https?|mailto|tel):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/iu,be=new Set(["left","right","center","justify","start","end"]),Ne=new Set(["h1","h2","h3","h4","h5","h6"]),x=1,Oe=3,ke=7,Re=8,Z=Object.freeze({a:Object.freeze(["target","data-link-kind","data-doc-path","data-doc-exists","data-footnote-ref","data-footnote-backref"]),area:Object.freeze(["target"]),img:Object.freeze(["data-asset-src","data-asset-exists"]),section:Object.freeze(["data-footnotes"]),iframe:Object.freeze(["allow","allowfullscreen","frameborder","referrerpolicy"])}),Le=[...new Set(Object.values(Z).flatMap(t=>t.filter(i=>i.startsWith("data-"))))],Ce=new Set(["src","href","xlink:href"]),Ie=/^data:/iu,ye=Object.freeze({FORBID_TAGS:_e,FORBID_ATTR:Se,ADD_TAGS:["iframe"],ADD_ATTR:(t,i)=>Z[i]?.includes(t)===!0,ADD_URI_SAFE_ATTR:Le,ALLOWED_URI_REGEXP:we,ALLOW_DATA_ATTR:!1}),De=t=>{let i=null;for(const u of t.split(";")){const f=u.indexOf(":");if(f<0||u.slice(0,f).trim().toLowerCase()!=="text-align")continue;const l=u.slice(f+1).trim().toLowerCase();be.has(l)&&(i=l)}return i===null?null:`text-align: ${i}`},Me=t=>t.nodeType===Oe?{kind:"text",text:t.nodeValue??""}:t.nodeType===Re||t.nodeType===ke?{kind:"ignored"}:t.nodeType===x&&t.nodeName.toLowerCase()==="a"?{kind:"link",href:t.getAttribute("href")}:{kind:"element"},Ue=t=>{const i=t.getAttribute("type");return i!==null&&i.trim().toLowerCase()==="checkbox"&&t.hasAttribute("disabled")},xe=(t,i,u)=>{if(S.isSupported!==!0)throw new Error("[markdown-explorer] the HTML sanitizer cannot run in this environment");if(!Number.isSafeInteger(u)||u<=0)throw new RangeError(`[markdown-explorer] the HTML sanitizer's depth limit must be a positive integer; got ${String(u)}`);const f=Object.freeze([...t]);let l=new WeakMap,A=new WeakMap;const n=a=>{const r=a.parentNode===null?void 0:l.get(a.parentNode),s=r===void 0?0:r+1;if(l.set(a,s),a.nodeType===x&&s>u)throw new Y(u)},T=(a,r)=>{if(a.nodeType!==x)return;const s=a;(r.tagName==="input"&&!Ue(s)||r.tagName==="iframe"&&R.allowedFrameUrlOf(s.getAttribute("src")?.trim(),f)===null)&&s.parentNode?.removeChild(s)},g=(a,r)=>{const s=a.nodeName.toLowerCase();if(r.attrName==="style"){const p=De(r.attrValue);p===null?r.keepAttr=!1:r.attrValue=p}else if(r.attrName==="allow"&&s==="iframe"){const p=R.reduceFrameFeatures(r.attrValue);p===null?r.keepAttr=!1:r.attrValue=p}else if(r.attrName==="referrerpolicy"&&s==="iframe"){const p=r.attrValue.trim().toLowerCase();R.EMBED_FRAME_REFERRER_POLICIES.has(p)?r.attrValue=p:r.keepAttr=!1}else r.attrName===R.EMBED_LINK_ATTRIBUTE&&s==="p"?A.set(a,a.getAttribute(R.EMBED_LINK_ATTRIBUTE)):Ce.has(r.attrName)&&Ie.test(r.attrValue)?r.keepAttr=!1:r.attrName==="id"&&Ne.has(s)&&(r.forceKeepAttr=!0)},h=a=>{const r=a.getAttribute("data-asset-src");r!==null&&(a.getAttribute("data-asset-exists")!=="false"&&F.isAcceptableDocumentPath(r)?a.setAttribute("src",F.explorerAssetHref(i,r,null)):a.removeAttribute("src"))},C=a=>{const r=a.parentNode;if(r===null)return;const s=A.get(r);s!=null&&R.placeholderLinkOf(s,[...r.childNodes].map(Me))!==null&&r.setAttribute(R.EMBED_LINK_ATTRIBUTE,s)},E=a=>{if(a.nodeType!==x)return;const r=a,s=r.nodeName.toLowerCase();s==="img"?h(r):s==="a"&&C(r);const p=r.getAttribute("target");if(p===null||p.trim().toLowerCase()==="_self")return;const D=r.getAttribute("rel"),I=new Set(D===null?[]:D.split(/\s+/u).filter(H=>H!==""));I.add("noopener"),I.add("noreferrer"),r.setAttribute("rel",[...I].join(" "))};return a=>{l=new WeakMap,A=new WeakMap,S.addHook("beforeSanitizeElements",n),S.addHook("uponSanitizeElement",T),S.addHook("uponSanitizeAttribute",g),S.addHook("afterSanitizeAttributes",E);try{return S.sanitize(a,ye)}finally{S.removeHook("beforeSanitizeElements",n),S.removeHook("uponSanitizeElement",T),S.removeHook("uponSanitizeAttribute",g),S.removeHook("afterSanitizeAttributes",E)}}},He=t=>{if(t===null||typeof t!="object")return!1;const{id:i,input:u}=t;if(typeof i!="number"||u===null||typeof u!="object")return!1;const{html:f,iframeHosts:l,dataPath:A,maxNodes:n,maxAttributeChars:T,maxSanitizedChars:g}=u;return typeof f=="string"&&Array.isArray(l)&&l.every(h=>typeof h=="string")&&typeof A=="string"&&A!==""&&[n,T,g].every(h=>Number.isSafeInteger(h)&&h>0)},ze=()=>{const t=te.parentPort;if(t===null)throw new Error("[markdown-explorer] the sanitizer worker must run in a worker thread");const i=new Map;t.on("message",u=>{if(!He(u))throw new TypeError("[markdown-explorer] the sanitizer worker received a message that is not a job");const{id:f,input:l}=u;let A;try{if(ge(l.html,G,l.maxNodes,l.maxAttributeChars).status!=="within"){t.postMessage({id:f,output:{status:"too-large"}});return}const n=JSON.stringify([l.iframeHosts,l.dataPath]);let T=i.get(n);T===void 0&&(T=xe(l.iframeHosts,l.dataPath,G),i.set(n,T));const g=T(l.html);A=g.length>l.maxSanitizedChars?{status:"too-large"}:{status:"sanitized",html:g}}catch(n){A=n instanceof Y?{status:"too-large"}:{status:"error",message:n instanceof Error?`${n.name}: ${n.message}`:String(n)}}t.postMessage({id:f,output:A})}),t.postMessage({ready:!0})};ze();
|
|
1
|
+
"use strict";const re=require("node:worker_threads"),_=require("parse5"),L=require("@aiquants/markdown/embeds"),H=require("@aiquants/markdown/source-lines"),S=require("isomorphic-dompurify"),G=require("./dataRoute-CcZ8PK00.cjs"),j=512,ne=4096,V=1024,$=Symbol("placement"),W=2,se=3,oe=new Set(["html","head","body"]),N=0,D=1,M=2,Q=3,z=4,O=5,R=6,b=7,q=8,U=9,X=10,w=-1,ae=0,ie=1,le=2,ce=3,ue=4,fe=5,Y=6,de=7,Ee=r=>{switch(r){case 9:case 10:case 12:case 13:case 32:return ae;case 47:return ie;case 62:return le;case 61:return ce;case 34:return ue;case 39:return fe;default:return Y}},Te=Uint8Array.from({length:128},(r,i)=>Ee(i)),P=60,K=47,pe=/[\t\n\f\r />="'<]/g,Ae=/["<]/g,he=/['<]/g,m=-1-M,me=Int8Array.from([[D,U,w,N,N,N,N],[D,U,w,m,m,m,m],[Q,U,w,z,M,M,M],[Q,U,w,z,m,m,m],[z,b,w,b,O,R,b],[O,O,O,O,q,O,O],[R,R,R,R,R,q,R],[D,b,w,b,b,b,b],[D,U,w,m,m,m,m],[D,U,w,m,m,m,m]].flat()),J=r=>r>=65&&r<=90||r>=97&&r<=122,ge=(r,i)=>{let u=new Int32Array(X),f=new Int32Array(X),l=0,A=0,n=0;for(;n<r.length;){if(l===0){const E=r.indexOf("<",n);if(E<0)return!1;n=E+1,r.charCodeAt(n)===K&&(n+=1),J(r.charCodeAt(n))&&(u[N]=0,l=1<<N,n+=1);continue}if((l&-230)===0&&n>=A){const E=l===1<<O?Ae:l===1<<R?he:pe;E.lastIndex=n;const a=E.exec(r);if(a===null)return!1;n=a.index,r.charCodeAt(n)===P&&(A=n+3)}const p=r.charCodeAt(n),g=p<128?Te[p]:Y;let h=0;for(let E=l;E!==0;E&=E-1){const a=31-Math.clz32(E&-E);let e=me[a*de+g],s=u[a];if(e<w&&(e=-1-e,s+=1,s>i))return!0;if(e===w)continue;const T=1<<e;((h&T)===0||s>f[e])&&(f[e]=s),h|=T}J(p)&&(r.charCodeAt(n-1)===P||r.charCodeAt(n-1)===K&&r.charCodeAt(n-2)===P)&&(h&1<<N)===0&&(f[N]=0,h|=1<<N);const I=u;u=f,f=I,l=h,n+=1}return!1};class k extends Error{status;constructor(i){super(`[markdown-explorer] the nesting guard stopped the parse: ${i}`),this.name="ParseStop",this.status=i}}const _e=(r,i,u,f)=>{if(!Number.isSafeInteger(i)||i<=0)throw new RangeError(`[markdown-explorer] the nesting guard's depth limit must be a positive safe integer; got ${String(i)}`);if(!Number.isSafeInteger(u)||u<=0)throw new RangeError(`[markdown-explorer] the nesting guard's node budget must be a positive safe integer; got ${String(u)}`);if(!Number.isSafeInteger(f)||f<=0)throw new RangeError(`[markdown-explorer] the nesting guard's attribute character budget must be a positive safe integer; got ${String(f)}`);if(ge(r,V))return{status:"too-many-attributes"};const l=i+W,A=r.length+se;let n=null,p=0,g=0,h=0,I=0;const E=t=>{if(g+=t,g>u)throw new k("too-many-nodes")},a=t=>{for(const o of t)I+=o.name.length+o.value.length;if(I>f)throw new k("too-many-attribute-characters")},e=(t,o,c,d)=>{const F={node:t,above:null,levels:o,height:0,keepsChildren:c,attributes:d,first:null,next:null,previous:null},te=t;return te[$]=F,F},s=t=>{const c=t[$];if(c===void 0)throw new Error("[markdown-explorer] the nesting guard was handed a node it did not create");return c},T=(t,o)=>{let c=o;for(let d=t;d!==null&&c>d.height;d=d.above)d.height=c,c+=d.levels;if(n!==null&&n.height>l)throw new k("too-deep")},y=t=>{const o=t.above;o?.keepsChildren&&(t.previous===null?o.first=t.next:t.previous.next=t.next,t.next!==null&&(t.next.previous=t.previous)),t.above=null,t.next=null,t.previous=null},C=(t,o)=>{if(!_.defaultTreeAdapter.isElementNode(o))return;const c=s(o),d=s(t);y(c),c.above=d,d.keepsChildren&&(c.next=d.first,d.first!==null&&(d.first.previous=c),d.first=c),o.parentNode=t,T(d,c.height+c.levels)},B={..._.defaultTreeAdapter,createDocument:()=>{const t=_.defaultTreeAdapter.createDocument();return n=e(t,0,!1,0),t},createDocumentFragment:()=>{const t=_.defaultTreeAdapter.createDocumentFragment();return e(t,0,!1,0),t},createElement:(t,o,c)=>{if(p+=1,p>A)throw new k("too-many-elements");E(1+c.length),a(c);const d=_.defaultTreeAdapter.createElement(t,o,c);return e(d,1,!(o===_.html.NS.HTML&&oe.has(t)),c.length),d},createCommentNode:t=>{if(h+=1,h>ne)throw new k("too-many-comments");return E(1),_.defaultTreeAdapter.createCommentNode(t)},setTemplateContent:(t,o)=>{_.defaultTreeAdapter.setTemplateContent(t,o);const c=s(o);c.above=s(t),T(c.above,c.height)},appendChild:C,insertBefore:(t,o)=>C(t,o),detachNode:t=>{_.defaultTreeAdapter.isElementNode(t)&&(y(s(t)),t.parentNode=null)},getFirstChild:t=>{const o=s(t);if(!o.keepsChildren)throw new Error("[markdown-explorer] the nesting guard was asked for the children of a node whose children it does not keep");return o.first===null?null:o.first.node},getChildNodes:()=>{throw new Error("[markdown-explorer] the nesting guard keeps no child lists for source locations")},onItemPop:t=>{const o=s(t);o.keepsChildren=!1,o.first=null},insertText:()=>{},insertTextBefore:()=>{},adoptAttributes:(t,o)=>{E(o.length),a(o);const c=s(t);if(c.attributes+=o.length,c.attributes>V)throw new k("too-many-attributes")}};let v;try{v=_.parse(r,{scriptingEnabled:!1,treeAdapter:B})}catch(t){if(t instanceof k)return{status:t.status};throw t}return{status:"within",depth:s(v).height-W,elements:p,nodes:g}};class Z extends RangeError{constructor(i){super(`[markdown-explorer] the HTML nests deeper than ${String(i)} levels`),this.name="HtmlNestingLimitError"}}const Se=["script","style","noscript","template","form","button","textarea","select","option","object","embed","base","meta","link","frame","frameset"],we=["srcdoc","formaction"],Ne=/^(?:(?:https?|mailto|tel):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/iu,be=new Set(["left","right","center","justify","start","end"]),Oe=new Set(["h1","h2","h3","h4","h5","h6"]),x=1,Re=3,Le=7,ke=8,ee=Object.freeze({a:Object.freeze(["target","data-link-kind","data-doc-path","data-doc-exists","data-footnote-ref","data-footnote-backref"]),area:Object.freeze(["target"]),img:Object.freeze(["data-asset-src","data-asset-exists"]),section:Object.freeze(["data-footnotes"]),iframe:Object.freeze(["allow","allowfullscreen","frameborder","referrerpolicy"])}),Ie=[...new Set([...Object.values(ee).flatMap(r=>r.filter(i=>i.startsWith("data-"))),H.SOURCE_LINES_ATTRIBUTE])],Ce=new Set(["src","href","xlink:href"]),Ue=/^data:/iu,ye=Object.freeze({FORBID_TAGS:Se,FORBID_ATTR:we,ADD_TAGS:["iframe"],ADD_ATTR:(r,i)=>r===H.SOURCE_LINES_ATTRIBUTE||ee[i]?.includes(r)===!0,ADD_URI_SAFE_ATTR:Ie,ALLOWED_URI_REGEXP:Ne,ALLOW_DATA_ATTR:!1}),De=r=>{let i=null;for(const u of r.split(";")){const f=u.indexOf(":");if(f<0||u.slice(0,f).trim().toLowerCase()!=="text-align")continue;const l=u.slice(f+1).trim().toLowerCase();be.has(l)&&(i=l)}return i===null?null:`text-align: ${i}`},Me=r=>r.nodeType===Re?{kind:"text",text:r.nodeValue??""}:r.nodeType===ke||r.nodeType===Le?{kind:"ignored"}:r.nodeType===x&&r.nodeName.toLowerCase()==="a"?{kind:"link",href:r.getAttribute("href")}:{kind:"element"},xe=r=>{const i=r.getAttribute("type");return i!==null&&i.trim().toLowerCase()==="checkbox"&&r.hasAttribute("disabled")},He=(r,i,u)=>{if(S.isSupported!==!0)throw new Error("[markdown-explorer] the HTML sanitizer cannot run in this environment");if(!Number.isSafeInteger(u)||u<=0)throw new RangeError(`[markdown-explorer] the HTML sanitizer's depth limit must be a positive integer; got ${String(u)}`);const f=Object.freeze([...r]);let l=new WeakMap,A=new WeakMap;const n=a=>{const e=a.parentNode===null?void 0:l.get(a.parentNode),s=e===void 0?0:e+1;if(l.set(a,s),a.nodeType===x&&s>u)throw new Z(u)},p=(a,e)=>{if(a.nodeType!==x)return;const s=a;(e.tagName==="input"&&!xe(s)||e.tagName==="iframe"&&L.allowedFrameUrlOf(s.getAttribute("src")?.trim(),f)===null)&&s.parentNode?.removeChild(s)},g=(a,e)=>{const s=a.nodeName.toLowerCase();if(e.attrName==="style"){const T=De(e.attrValue);T===null?e.keepAttr=!1:e.attrValue=T}else if(e.attrName==="allow"&&s==="iframe"){const T=L.reduceFrameFeatures(e.attrValue);T===null?e.keepAttr=!1:e.attrValue=T}else if(e.attrName==="referrerpolicy"&&s==="iframe"){const T=e.attrValue.trim().toLowerCase();L.EMBED_FRAME_REFERRER_POLICIES.has(T)?e.attrValue=T:e.keepAttr=!1}else e.attrName===L.EMBED_LINK_ATTRIBUTE&&s==="p"?A.set(a,a.getAttribute(L.EMBED_LINK_ATTRIBUTE)):Ce.has(e.attrName)&&Ue.test(e.attrValue)||e.attrName===H.SOURCE_LINES_ATTRIBUTE&&H.parseSourceLines(e.attrValue)===null?e.keepAttr=!1:e.attrName==="id"&&Oe.has(s)&&(e.forceKeepAttr=!0)},h=a=>{const e=a.getAttribute("data-asset-src");e!==null&&(a.getAttribute("data-asset-exists")!=="false"&&G.isAcceptableDocumentPath(e)?a.setAttribute("src",G.explorerAssetHref(i,e,null)):a.removeAttribute("src"))},I=a=>{const e=a.parentNode;if(e===null)return;const s=A.get(e);s!=null&&L.placeholderLinkOf(s,[...e.childNodes].map(Me))!==null&&e.setAttribute(L.EMBED_LINK_ATTRIBUTE,s)},E=a=>{if(a.nodeType!==x)return;const e=a,s=e.nodeName.toLowerCase();s==="img"?h(e):s==="a"&&I(e);const T=e.getAttribute("target");if(T===null||T.trim().toLowerCase()==="_self")return;const y=e.getAttribute("rel"),C=new Set(y===null?[]:y.split(/\s+/u).filter(B=>B!==""));C.add("noopener"),C.add("noreferrer"),e.setAttribute("rel",[...C].join(" "))};return a=>{l=new WeakMap,A=new WeakMap,S.addHook("beforeSanitizeElements",n),S.addHook("uponSanitizeElement",p),S.addHook("uponSanitizeAttribute",g),S.addHook("afterSanitizeAttributes",E);try{return S.sanitize(a,ye)}finally{S.removeHook("beforeSanitizeElements",n),S.removeHook("uponSanitizeElement",p),S.removeHook("uponSanitizeAttribute",g),S.removeHook("afterSanitizeAttributes",E)}}},Be=r=>{if(r===null||typeof r!="object")return!1;const{id:i,input:u}=r;if(typeof i!="number"||u===null||typeof u!="object")return!1;const{html:f,iframeHosts:l,dataPath:A,maxNodes:n,maxAttributeChars:p,maxSanitizedChars:g}=u;return typeof f=="string"&&Array.isArray(l)&&l.every(h=>typeof h=="string")&&typeof A=="string"&&A!==""&&[n,p,g].every(h=>Number.isSafeInteger(h)&&h>0)},ze=()=>{const r=re.parentPort;if(r===null)throw new Error("[markdown-explorer] the sanitizer worker must run in a worker thread");const i=new Map;r.on("message",u=>{if(!Be(u))throw new TypeError("[markdown-explorer] the sanitizer worker received a message that is not a job");const{id:f,input:l}=u;let A;try{if(_e(l.html,j,l.maxNodes,l.maxAttributeChars).status!=="within"){r.postMessage({id:f,output:{status:"too-large"}});return}const n=JSON.stringify([l.iframeHosts,l.dataPath]);let p=i.get(n);p===void 0&&(p=He(l.iframeHosts,l.dataPath,j),i.set(n,p));const g=p(l.html);A=g.length>l.maxSanitizedChars?{status:"too-large"}:{status:"sanitized",html:g}}catch(n){A=n instanceof Z?{status:"too-large"}:{status:"error",message:n instanceof Error?`${n.name}: ${n.message}`:String(n)}}r.postMessage({id:f,output:A})}),r.postMessage({ready:!0})};ze();
|
package/dist/sanitizeWorker.js
CHANGED
|
@@ -1,281 +1,282 @@
|
|
|
1
|
-
import { parentPort as
|
|
2
|
-
import { defaultTreeAdapter as
|
|
3
|
-
import { allowedFrameUrlOf as
|
|
1
|
+
import { parentPort as te } from "node:worker_threads";
|
|
2
|
+
import { defaultTreeAdapter as N, parse as re, html as ne } from "parse5";
|
|
3
|
+
import { allowedFrameUrlOf as oe, reduceFrameFeatures as se, EMBED_FRAME_REFERRER_POLICIES as ae, EMBED_LINK_ATTRIBUTE as H, placeholderLinkOf as ie } from "@aiquants/markdown/embeds";
|
|
4
|
+
import { SOURCE_LINES_ATTRIBUTE as P, parseSourceLines as le } from "@aiquants/markdown/source-lines";
|
|
4
5
|
import S from "isomorphic-dompurify";
|
|
5
|
-
import { i as
|
|
6
|
-
const
|
|
7
|
-
switch (
|
|
6
|
+
import { i as ce, e as ue } from "./dataRoute-D1vE1wFW.js";
|
|
7
|
+
const G = 512, fe = 4096, j = 1024, V = /* @__PURE__ */ Symbol("placement"), W = 2, de = 3, pe = /* @__PURE__ */ new Set(["html", "head", "body"]), w = 0, D = 1, M = 2, Q = 3, z = 4, O = 5, k = 6, b = 7, $ = 8, I = 9, X = 10, _ = -1, Ee = 0, me = 1, Te = 2, he = 3, Ae = 4, ge = 5, Y = 6, Se = 7, _e = (r) => {
|
|
8
|
+
switch (r) {
|
|
8
9
|
case 9:
|
|
9
10
|
case 10:
|
|
10
11
|
case 12:
|
|
11
12
|
case 13:
|
|
12
13
|
case 32:
|
|
13
|
-
return
|
|
14
|
+
return Ee;
|
|
14
15
|
case 47:
|
|
15
|
-
return
|
|
16
|
+
return me;
|
|
16
17
|
case 62:
|
|
17
|
-
return
|
|
18
|
+
return Te;
|
|
18
19
|
case 61:
|
|
19
|
-
return me;
|
|
20
|
-
case 34:
|
|
21
20
|
return he;
|
|
21
|
+
case 34:
|
|
22
|
+
return Ae;
|
|
22
23
|
case 39:
|
|
23
|
-
return
|
|
24
|
+
return ge;
|
|
24
25
|
default:
|
|
25
|
-
return
|
|
26
|
+
return Y;
|
|
26
27
|
}
|
|
27
|
-
},
|
|
28
|
+
}, we = Uint8Array.from({ length: 128 }, (r, i) => _e(i)), B = 60, J = 47, Ne = /[\t\n\f\r />="'<]/g, be = /["<]/g, Oe = /['<]/g, A = -1 - M, ke = Int8Array.from(
|
|
28
29
|
[
|
|
29
30
|
[D, I, _, w, w, w, w],
|
|
30
31
|
[D, I, _, A, A, A, A],
|
|
31
|
-
[
|
|
32
|
-
[
|
|
33
|
-
[z,
|
|
34
|
-
[O, O, O, O,
|
|
35
|
-
[k, k, k, k, k,
|
|
36
|
-
[D,
|
|
32
|
+
[Q, I, _, z, M, M, M],
|
|
33
|
+
[Q, I, _, z, A, A, A],
|
|
34
|
+
[z, b, _, b, O, k, b],
|
|
35
|
+
[O, O, O, O, $, O, O],
|
|
36
|
+
[k, k, k, k, k, $, k],
|
|
37
|
+
[D, b, _, b, b, b, b],
|
|
37
38
|
[D, I, _, A, A, A, A],
|
|
38
39
|
[D, I, _, A, A, A, A]
|
|
39
40
|
].flat()
|
|
40
|
-
),
|
|
41
|
-
let u = new Int32Array(
|
|
42
|
-
for (; n <
|
|
43
|
-
if (
|
|
44
|
-
const p =
|
|
41
|
+
), K = (r) => r >= 65 && r <= 90 || r >= 97 && r <= 122, Le = (r, i) => {
|
|
42
|
+
let u = new Int32Array(X), f = new Int32Array(X), l = 0, T = 0, n = 0;
|
|
43
|
+
for (; n < r.length; ) {
|
|
44
|
+
if (l === 0) {
|
|
45
|
+
const p = r.indexOf("<", n);
|
|
45
46
|
if (p < 0) return !1;
|
|
46
|
-
n = p + 1,
|
|
47
|
+
n = p + 1, r.charCodeAt(n) === J && (n += 1), K(r.charCodeAt(n)) && (u[w] = 0, l = 1 << w, n += 1);
|
|
47
48
|
continue;
|
|
48
49
|
}
|
|
49
|
-
if ((
|
|
50
|
-
const p =
|
|
50
|
+
if ((l & -230) === 0 && n >= T) {
|
|
51
|
+
const p = l === 1 << O ? be : l === 1 << k ? Oe : Ne;
|
|
51
52
|
p.lastIndex = n;
|
|
52
|
-
const a = p.exec(
|
|
53
|
+
const a = p.exec(r);
|
|
53
54
|
if (a === null) return !1;
|
|
54
|
-
n = a.index,
|
|
55
|
+
n = a.index, r.charCodeAt(n) === B && (T = n + 3);
|
|
55
56
|
}
|
|
56
|
-
const m =
|
|
57
|
-
let
|
|
58
|
-
for (let p =
|
|
57
|
+
const m = r.charCodeAt(n), g = m < 128 ? we[m] : Y;
|
|
58
|
+
let h = 0;
|
|
59
|
+
for (let p = l; p !== 0; p &= p - 1) {
|
|
59
60
|
const a = 31 - Math.clz32(p & -p);
|
|
60
|
-
let
|
|
61
|
-
if (
|
|
61
|
+
let e = ke[a * Se + g], o = u[a];
|
|
62
|
+
if (e < _ && (e = -1 - e, o += 1, o > i))
|
|
62
63
|
return !0;
|
|
63
|
-
if (
|
|
64
|
-
const E = 1 <<
|
|
65
|
-
((
|
|
64
|
+
if (e === _) continue;
|
|
65
|
+
const E = 1 << e;
|
|
66
|
+
((h & E) === 0 || o > f[e]) && (f[e] = o), h |= E;
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
u = f, f =
|
|
68
|
+
K(m) && (r.charCodeAt(n - 1) === B || r.charCodeAt(n - 1) === J && r.charCodeAt(n - 2) === B) && (h & 1 << w) === 0 && (f[w] = 0, h |= 1 << w);
|
|
69
|
+
const R = u;
|
|
70
|
+
u = f, f = R, l = h, n += 1;
|
|
70
71
|
}
|
|
71
72
|
return !1;
|
|
72
73
|
};
|
|
73
|
-
class
|
|
74
|
+
class L extends Error {
|
|
74
75
|
status;
|
|
75
76
|
constructor(i) {
|
|
76
77
|
super(`[markdown-explorer] the nesting guard stopped the parse: ${i}`), this.name = "ParseStop", this.status = i;
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
|
-
const
|
|
80
|
+
const Re = (r, i, u, f) => {
|
|
80
81
|
if (!Number.isSafeInteger(i) || i <= 0) throw new RangeError(`[markdown-explorer] the nesting guard's depth limit must be a positive safe integer; got ${String(i)}`);
|
|
81
82
|
if (!Number.isSafeInteger(u) || u <= 0) throw new RangeError(`[markdown-explorer] the nesting guard's node budget must be a positive safe integer; got ${String(u)}`);
|
|
82
83
|
if (!Number.isSafeInteger(f) || f <= 0) throw new RangeError(`[markdown-explorer] the nesting guard's attribute character budget must be a positive safe integer; got ${String(f)}`);
|
|
83
|
-
if (
|
|
84
|
-
const
|
|
85
|
-
let n = null, m = 0, g = 0,
|
|
86
|
-
const p = (
|
|
87
|
-
if (g +=
|
|
88
|
-
}, a = (
|
|
89
|
-
for (const s of
|
|
90
|
-
if (
|
|
91
|
-
},
|
|
92
|
-
const
|
|
93
|
-
return
|
|
94
|
-
}, o = (
|
|
95
|
-
const
|
|
96
|
-
if (
|
|
97
|
-
return
|
|
98
|
-
}, E = (
|
|
99
|
-
let
|
|
100
|
-
for (let d =
|
|
101
|
-
d.height =
|
|
102
|
-
if (n !== null && n.height >
|
|
103
|
-
}, y = (
|
|
104
|
-
const s =
|
|
105
|
-
s?.keepsChildren && (
|
|
106
|
-
},
|
|
107
|
-
if (!
|
|
108
|
-
const
|
|
109
|
-
y(
|
|
84
|
+
if (Le(r, j)) return { status: "too-many-attributes" };
|
|
85
|
+
const l = i + W, T = r.length + de;
|
|
86
|
+
let n = null, m = 0, g = 0, h = 0, R = 0;
|
|
87
|
+
const p = (t) => {
|
|
88
|
+
if (g += t, g > u) throw new L("too-many-nodes");
|
|
89
|
+
}, a = (t) => {
|
|
90
|
+
for (const s of t) R += s.name.length + s.value.length;
|
|
91
|
+
if (R > f) throw new L("too-many-attribute-characters");
|
|
92
|
+
}, e = (t, s, c, d) => {
|
|
93
|
+
const F = { node: t, above: null, levels: s, height: 0, keepsChildren: c, attributes: d, first: null, next: null, previous: null }, ee = t;
|
|
94
|
+
return ee[V] = F, F;
|
|
95
|
+
}, o = (t) => {
|
|
96
|
+
const c = t[V];
|
|
97
|
+
if (c === void 0) throw new Error("[markdown-explorer] the nesting guard was handed a node it did not create");
|
|
98
|
+
return c;
|
|
99
|
+
}, E = (t, s) => {
|
|
100
|
+
let c = s;
|
|
101
|
+
for (let d = t; d !== null && c > d.height; d = d.above)
|
|
102
|
+
d.height = c, c += d.levels;
|
|
103
|
+
if (n !== null && n.height > l) throw new L("too-deep");
|
|
104
|
+
}, y = (t) => {
|
|
105
|
+
const s = t.above;
|
|
106
|
+
s?.keepsChildren && (t.previous === null ? s.first = t.next : t.previous.next = t.next, t.next !== null && (t.next.previous = t.previous)), t.above = null, t.next = null, t.previous = null;
|
|
107
|
+
}, C = (t, s) => {
|
|
108
|
+
if (!N.isElementNode(s)) return;
|
|
109
|
+
const c = o(s), d = o(t);
|
|
110
|
+
y(c), c.above = d, d.keepsChildren && (c.next = d.first, d.first !== null && (d.first.previous = c), d.first = c), s.parentNode = t, E(d, c.height + c.levels);
|
|
110
111
|
}, x = {
|
|
111
|
-
...
|
|
112
|
+
...N,
|
|
112
113
|
createDocument: () => {
|
|
113
|
-
const
|
|
114
|
-
return n =
|
|
114
|
+
const t = N.createDocument();
|
|
115
|
+
return n = e(t, 0, !1, 0), t;
|
|
115
116
|
},
|
|
116
117
|
createDocumentFragment: () => {
|
|
117
|
-
const
|
|
118
|
-
return
|
|
118
|
+
const t = N.createDocumentFragment();
|
|
119
|
+
return e(t, 0, !1, 0), t;
|
|
119
120
|
},
|
|
120
|
-
createElement: (
|
|
121
|
-
if (m += 1, m >
|
|
122
|
-
p(1 +
|
|
123
|
-
const d =
|
|
124
|
-
return
|
|
121
|
+
createElement: (t, s, c) => {
|
|
122
|
+
if (m += 1, m > T) throw new L("too-many-elements");
|
|
123
|
+
p(1 + c.length), a(c);
|
|
124
|
+
const d = N.createElement(t, s, c);
|
|
125
|
+
return e(d, 1, !(s === ne.NS.HTML && pe.has(t)), c.length), d;
|
|
125
126
|
},
|
|
126
|
-
createCommentNode: (
|
|
127
|
-
if (
|
|
128
|
-
return p(1),
|
|
127
|
+
createCommentNode: (t) => {
|
|
128
|
+
if (h += 1, h > fe) throw new L("too-many-comments");
|
|
129
|
+
return p(1), N.createCommentNode(t);
|
|
129
130
|
},
|
|
130
|
-
setTemplateContent: (
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
131
|
+
setTemplateContent: (t, s) => {
|
|
132
|
+
N.setTemplateContent(t, s);
|
|
133
|
+
const c = o(s);
|
|
134
|
+
c.above = o(t), E(c.above, c.height);
|
|
134
135
|
},
|
|
135
|
-
appendChild:
|
|
136
|
-
insertBefore: (
|
|
137
|
-
detachNode: (
|
|
138
|
-
|
|
136
|
+
appendChild: C,
|
|
137
|
+
insertBefore: (t, s) => C(t, s),
|
|
138
|
+
detachNode: (t) => {
|
|
139
|
+
N.isElementNode(t) && (y(o(t)), t.parentNode = null);
|
|
139
140
|
},
|
|
140
|
-
getFirstChild: (
|
|
141
|
-
const s = o(
|
|
141
|
+
getFirstChild: (t) => {
|
|
142
|
+
const s = o(t);
|
|
142
143
|
if (!s.keepsChildren) throw new Error("[markdown-explorer] the nesting guard was asked for the children of a node whose children it does not keep");
|
|
143
144
|
return s.first === null ? null : s.first.node;
|
|
144
145
|
},
|
|
145
146
|
getChildNodes: () => {
|
|
146
147
|
throw new Error("[markdown-explorer] the nesting guard keeps no child lists for source locations");
|
|
147
148
|
},
|
|
148
|
-
onItemPop: (
|
|
149
|
-
const s = o(
|
|
149
|
+
onItemPop: (t) => {
|
|
150
|
+
const s = o(t);
|
|
150
151
|
s.keepsChildren = !1, s.first = null;
|
|
151
152
|
},
|
|
152
153
|
insertText: () => {
|
|
153
154
|
},
|
|
154
155
|
insertTextBefore: () => {
|
|
155
156
|
},
|
|
156
|
-
adoptAttributes: (
|
|
157
|
+
adoptAttributes: (t, s) => {
|
|
157
158
|
p(s.length), a(s);
|
|
158
|
-
const
|
|
159
|
-
if (
|
|
159
|
+
const c = o(t);
|
|
160
|
+
if (c.attributes += s.length, c.attributes > j) throw new L("too-many-attributes");
|
|
160
161
|
}
|
|
161
162
|
};
|
|
162
|
-
let
|
|
163
|
+
let v;
|
|
163
164
|
try {
|
|
164
|
-
|
|
165
|
-
} catch (
|
|
166
|
-
if (
|
|
167
|
-
throw
|
|
165
|
+
v = re(r, { scriptingEnabled: !1, treeAdapter: x });
|
|
166
|
+
} catch (t) {
|
|
167
|
+
if (t instanceof L) return { status: t.status };
|
|
168
|
+
throw t;
|
|
168
169
|
}
|
|
169
|
-
return { status: "within", depth: o(
|
|
170
|
+
return { status: "within", depth: o(v).height - W, elements: m, nodes: g };
|
|
170
171
|
};
|
|
171
|
-
class
|
|
172
|
+
class q extends RangeError {
|
|
172
173
|
constructor(i) {
|
|
173
174
|
super(`[markdown-explorer] the HTML nests deeper than ${String(i)} levels`), this.name = "HtmlNestingLimitError";
|
|
174
175
|
}
|
|
175
176
|
}
|
|
176
|
-
const Ce = ["script", "style", "noscript", "template", "form", "button", "textarea", "select", "option", "object", "embed", "base", "meta", "link", "frame", "frameset"],
|
|
177
|
+
const Ce = ["script", "style", "noscript", "template", "form", "button", "textarea", "select", "option", "object", "embed", "base", "meta", "link", "frame", "frameset"], Ie = ["srcdoc", "formaction"], ye = /^(?:(?:https?|mailto|tel):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/iu, De = /* @__PURE__ */ new Set(["left", "right", "center", "justify", "start", "end"]), Me = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]), U = 1, Ue = 3, xe = 7, He = 8, Z = Object.freeze({
|
|
177
178
|
a: Object.freeze(["target", "data-link-kind", "data-doc-path", "data-doc-exists", "data-footnote-ref", "data-footnote-backref"]),
|
|
178
179
|
area: Object.freeze(["target"]),
|
|
179
180
|
img: Object.freeze(["data-asset-src", "data-asset-exists"]),
|
|
180
181
|
section: Object.freeze(["data-footnotes"]),
|
|
181
182
|
iframe: Object.freeze(["allow", "allowfullscreen", "frameborder", "referrerpolicy"])
|
|
182
|
-
}),
|
|
183
|
+
}), ze = [.../* @__PURE__ */ new Set([...Object.values(Z).flatMap((r) => r.filter((i) => i.startsWith("data-"))), P])], Be = /* @__PURE__ */ new Set(["src", "href", "xlink:href"]), Pe = /^data:/iu, ve = Object.freeze({
|
|
183
184
|
FORBID_TAGS: Ce,
|
|
184
|
-
FORBID_ATTR:
|
|
185
|
+
FORBID_ATTR: Ie,
|
|
185
186
|
ADD_TAGS: ["iframe"],
|
|
186
|
-
ADD_ATTR: (
|
|
187
|
-
ADD_URI_SAFE_ATTR:
|
|
188
|
-
ALLOWED_URI_REGEXP:
|
|
187
|
+
ADD_ATTR: (r, i) => r === P || Z[i]?.includes(r) === !0,
|
|
188
|
+
ADD_URI_SAFE_ATTR: ze,
|
|
189
|
+
ALLOWED_URI_REGEXP: ye,
|
|
189
190
|
ALLOW_DATA_ATTR: !1
|
|
190
|
-
}),
|
|
191
|
+
}), Fe = (r) => {
|
|
191
192
|
let i = null;
|
|
192
|
-
for (const u of
|
|
193
|
+
for (const u of r.split(";")) {
|
|
193
194
|
const f = u.indexOf(":");
|
|
194
195
|
if (f < 0 || u.slice(0, f).trim().toLowerCase() !== "text-align") continue;
|
|
195
|
-
const
|
|
196
|
-
|
|
196
|
+
const l = u.slice(f + 1).trim().toLowerCase();
|
|
197
|
+
De.has(l) && (i = l);
|
|
197
198
|
}
|
|
198
199
|
return i === null ? null : `text-align: ${i}`;
|
|
199
|
-
},
|
|
200
|
-
const i =
|
|
201
|
-
return i !== null && i.trim().toLowerCase() === "checkbox" &&
|
|
202
|
-
},
|
|
200
|
+
}, Ge = (r) => r.nodeType === Ue ? { kind: "text", text: r.nodeValue ?? "" } : r.nodeType === He || r.nodeType === xe ? { kind: "ignored" } : r.nodeType === U && r.nodeName.toLowerCase() === "a" ? { kind: "link", href: r.getAttribute("href") } : { kind: "element" }, je = (r) => {
|
|
201
|
+
const i = r.getAttribute("type");
|
|
202
|
+
return i !== null && i.trim().toLowerCase() === "checkbox" && r.hasAttribute("disabled");
|
|
203
|
+
}, Ve = (r, i, u) => {
|
|
203
204
|
if (S.isSupported !== !0) throw new Error("[markdown-explorer] the HTML sanitizer cannot run in this environment");
|
|
204
205
|
if (!Number.isSafeInteger(u) || u <= 0) throw new RangeError(`[markdown-explorer] the HTML sanitizer's depth limit must be a positive integer; got ${String(u)}`);
|
|
205
|
-
const f = Object.freeze([...
|
|
206
|
-
let
|
|
206
|
+
const f = Object.freeze([...r]);
|
|
207
|
+
let l = /* @__PURE__ */ new WeakMap(), T = /* @__PURE__ */ new WeakMap();
|
|
207
208
|
const n = (a) => {
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
}, m = (a,
|
|
209
|
+
const e = a.parentNode === null ? void 0 : l.get(a.parentNode), o = e === void 0 ? 0 : e + 1;
|
|
210
|
+
if (l.set(a, o), a.nodeType === U && o > u) throw new q(u);
|
|
211
|
+
}, m = (a, e) => {
|
|
211
212
|
if (a.nodeType !== U) return;
|
|
212
213
|
const o = a;
|
|
213
|
-
(
|
|
214
|
-
}, g = (a,
|
|
214
|
+
(e.tagName === "input" && !je(o) || e.tagName === "iframe" && oe(o.getAttribute("src")?.trim(), f) === null) && o.parentNode?.removeChild(o);
|
|
215
|
+
}, g = (a, e) => {
|
|
215
216
|
const o = a.nodeName.toLowerCase();
|
|
216
|
-
if (
|
|
217
|
-
const E =
|
|
218
|
-
E === null ?
|
|
219
|
-
} else if (
|
|
220
|
-
const E =
|
|
221
|
-
E === null ?
|
|
222
|
-
} else if (
|
|
223
|
-
const E =
|
|
224
|
-
|
|
225
|
-
} else
|
|
226
|
-
},
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
},
|
|
230
|
-
const
|
|
231
|
-
if (
|
|
232
|
-
const o =
|
|
233
|
-
o != null &&
|
|
217
|
+
if (e.attrName === "style") {
|
|
218
|
+
const E = Fe(e.attrValue);
|
|
219
|
+
E === null ? e.keepAttr = !1 : e.attrValue = E;
|
|
220
|
+
} else if (e.attrName === "allow" && o === "iframe") {
|
|
221
|
+
const E = se(e.attrValue);
|
|
222
|
+
E === null ? e.keepAttr = !1 : e.attrValue = E;
|
|
223
|
+
} else if (e.attrName === "referrerpolicy" && o === "iframe") {
|
|
224
|
+
const E = e.attrValue.trim().toLowerCase();
|
|
225
|
+
ae.has(E) ? e.attrValue = E : e.keepAttr = !1;
|
|
226
|
+
} else e.attrName === H && o === "p" ? T.set(a, a.getAttribute(H)) : Be.has(e.attrName) && Pe.test(e.attrValue) || e.attrName === P && le(e.attrValue) === null ? e.keepAttr = !1 : e.attrName === "id" && Me.has(o) && (e.forceKeepAttr = !0);
|
|
227
|
+
}, h = (a) => {
|
|
228
|
+
const e = a.getAttribute("data-asset-src");
|
|
229
|
+
e !== null && (a.getAttribute("data-asset-exists") !== "false" && ce(e) ? a.setAttribute("src", ue(i, e, null)) : a.removeAttribute("src"));
|
|
230
|
+
}, R = (a) => {
|
|
231
|
+
const e = a.parentNode;
|
|
232
|
+
if (e === null) return;
|
|
233
|
+
const o = T.get(e);
|
|
234
|
+
o != null && ie(o, [...e.childNodes].map(Ge)) !== null && e.setAttribute(H, o);
|
|
234
235
|
}, p = (a) => {
|
|
235
236
|
if (a.nodeType !== U) return;
|
|
236
|
-
const
|
|
237
|
-
o === "img" ?
|
|
238
|
-
const E =
|
|
237
|
+
const e = a, o = e.nodeName.toLowerCase();
|
|
238
|
+
o === "img" ? h(e) : o === "a" && R(e);
|
|
239
|
+
const E = e.getAttribute("target");
|
|
239
240
|
if (E === null || E.trim().toLowerCase() === "_self") return;
|
|
240
|
-
const y =
|
|
241
|
-
|
|
241
|
+
const y = e.getAttribute("rel"), C = new Set(y === null ? [] : y.split(/\s+/u).filter((x) => x !== ""));
|
|
242
|
+
C.add("noopener"), C.add("noreferrer"), e.setAttribute("rel", [...C].join(" "));
|
|
242
243
|
};
|
|
243
244
|
return (a) => {
|
|
244
|
-
|
|
245
|
+
l = /* @__PURE__ */ new WeakMap(), T = /* @__PURE__ */ new WeakMap(), S.addHook("beforeSanitizeElements", n), S.addHook("uponSanitizeElement", m), S.addHook("uponSanitizeAttribute", g), S.addHook("afterSanitizeAttributes", p);
|
|
245
246
|
try {
|
|
246
247
|
return S.sanitize(a, ve);
|
|
247
248
|
} finally {
|
|
248
249
|
S.removeHook("beforeSanitizeElements", n), S.removeHook("uponSanitizeElement", m), S.removeHook("uponSanitizeAttribute", g), S.removeHook("afterSanitizeAttributes", p);
|
|
249
250
|
}
|
|
250
251
|
};
|
|
251
|
-
},
|
|
252
|
-
if (
|
|
253
|
-
const { id: i, input: u } =
|
|
252
|
+
}, We = (r) => {
|
|
253
|
+
if (r === null || typeof r != "object") return !1;
|
|
254
|
+
const { id: i, input: u } = r;
|
|
254
255
|
if (typeof i != "number" || u === null || typeof u != "object") return !1;
|
|
255
|
-
const { html: f, iframeHosts:
|
|
256
|
-
return typeof f == "string" && Array.isArray(
|
|
257
|
-
},
|
|
258
|
-
const
|
|
259
|
-
if (
|
|
256
|
+
const { html: f, iframeHosts: l, dataPath: T, maxNodes: n, maxAttributeChars: m, maxSanitizedChars: g } = u;
|
|
257
|
+
return typeof f == "string" && Array.isArray(l) && l.every((h) => typeof h == "string") && typeof T == "string" && T !== "" && [n, m, g].every((h) => Number.isSafeInteger(h) && h > 0);
|
|
258
|
+
}, Qe = () => {
|
|
259
|
+
const r = te;
|
|
260
|
+
if (r === null) throw new Error("[markdown-explorer] the sanitizer worker must run in a worker thread");
|
|
260
261
|
const i = /* @__PURE__ */ new Map();
|
|
261
|
-
|
|
262
|
-
if (!
|
|
263
|
-
const { id: f, input:
|
|
264
|
-
let
|
|
262
|
+
r.on("message", (u) => {
|
|
263
|
+
if (!We(u)) throw new TypeError("[markdown-explorer] the sanitizer worker received a message that is not a job");
|
|
264
|
+
const { id: f, input: l } = u;
|
|
265
|
+
let T;
|
|
265
266
|
try {
|
|
266
|
-
if (
|
|
267
|
-
|
|
267
|
+
if (Re(l.html, G, l.maxNodes, l.maxAttributeChars).status !== "within") {
|
|
268
|
+
r.postMessage({ id: f, output: { status: "too-large" } });
|
|
268
269
|
return;
|
|
269
270
|
}
|
|
270
|
-
const n = JSON.stringify([
|
|
271
|
+
const n = JSON.stringify([l.iframeHosts, l.dataPath]);
|
|
271
272
|
let m = i.get(n);
|
|
272
|
-
m === void 0 && (m =
|
|
273
|
-
const g = m(
|
|
274
|
-
|
|
273
|
+
m === void 0 && (m = Ve(l.iframeHosts, l.dataPath, G), i.set(n, m));
|
|
274
|
+
const g = m(l.html);
|
|
275
|
+
T = g.length > l.maxSanitizedChars ? { status: "too-large" } : { status: "sanitized", html: g };
|
|
275
276
|
} catch (n) {
|
|
276
|
-
|
|
277
|
+
T = n instanceof q ? { status: "too-large" } : { status: "error", message: n instanceof Error ? `${n.name}: ${n.message}` : String(n) };
|
|
277
278
|
}
|
|
278
|
-
|
|
279
|
-
}),
|
|
279
|
+
r.postMessage({ id: f, output: T });
|
|
280
|
+
}), r.postMessage({ ready: !0 });
|
|
280
281
|
};
|
|
281
|
-
|
|
282
|
+
Qe();
|
package/dist/server/errors.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExplorerFailure, ExplorerFailureReason } from "../core/contracts.cjs";
|
|
2
|
-
export type ExplorerOperation = "authenticate" | "tree" | "children" | "ancestors" | "reveal" | "document" | "text" | "notices" | "refresh" | "boundary" | "asset";
|
|
2
|
+
export type ExplorerOperation = "authenticate" | "tree" | "children" | "ancestors" | "reveal" | "document" | "text" | "edit" | "save" | "preview" | "revisions" | "revision" | "notices" | "refresh" | "boundary" | "asset";
|
|
3
3
|
export type ExplorerLogDetail = {
|
|
4
4
|
readonly operation: ExplorerOperation;
|
|
5
5
|
readonly sourceId?: string;
|
package/dist/server/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExplorerFailure, ExplorerFailureReason } from "../core/contracts.js";
|
|
2
|
-
export type ExplorerOperation = "authenticate" | "tree" | "children" | "ancestors" | "reveal" | "document" | "text" | "notices" | "refresh" | "boundary" | "asset";
|
|
2
|
+
export type ExplorerOperation = "authenticate" | "tree" | "children" | "ancestors" | "reveal" | "document" | "text" | "edit" | "save" | "preview" | "revisions" | "revision" | "notices" | "refresh" | "boundary" | "asset";
|
|
3
3
|
export type ExplorerLogDetail = {
|
|
4
4
|
readonly operation: ExplorerOperation;
|
|
5
5
|
readonly sourceId?: string;
|
package/dist/server/ports.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ActionFunctionArgs, data, HeadersFunction, LoaderFunctionArgs } from "react-router";
|
|
2
2
|
import { ExplorerConfig } from "../core/config.cjs";
|
|
3
|
-
import { ExplorerEntry, ExplorerFailureDocument, ExplorerHeading, ExplorerMediaType, ExplorerNotice, ExplorerPageData } from "../core/contracts.cjs";
|
|
3
|
+
import { ExplorerDocumentEdit, ExplorerEntry, ExplorerFailureDocument, ExplorerGrantRequired, ExplorerHeading, ExplorerMediaType, ExplorerNotice, ExplorerPageData, ExplorerRevision } from "../core/contracts.cjs";
|
|
4
4
|
import { ExplorerPathRules } from "../core/paths.cjs";
|
|
5
5
|
import { ExplorerLogger } from "./errors.cjs";
|
|
6
6
|
import { HtmlSanitizerOptions } from "./sanitizer.cjs";
|
|
@@ -62,6 +62,7 @@ export type ExplorerHostDocument = {
|
|
|
62
62
|
readonly name: string;
|
|
63
63
|
readonly htmlContent: string;
|
|
64
64
|
readonly headings: readonly ExplorerHeading[];
|
|
65
|
+
readonly edit: ExplorerDocumentEdit | null;
|
|
65
66
|
} | {
|
|
66
67
|
readonly kind: "text";
|
|
67
68
|
readonly name: string;
|
|
@@ -82,6 +83,35 @@ export type ExplorerHostDocument = {
|
|
|
82
83
|
readonly name: string;
|
|
83
84
|
readonly version: string;
|
|
84
85
|
} | ExplorerFailureDocument;
|
|
86
|
+
export type ExplorerHostEditSource = {
|
|
87
|
+
readonly kind: "edit";
|
|
88
|
+
readonly text: string;
|
|
89
|
+
readonly version: string;
|
|
90
|
+
readonly history: boolean;
|
|
91
|
+
} | ExplorerGrantRequired;
|
|
92
|
+
export type ExplorerDraft = {
|
|
93
|
+
readonly text: string;
|
|
94
|
+
readonly baseVersion: string;
|
|
95
|
+
};
|
|
96
|
+
export type ExplorerHostSaveResult = {
|
|
97
|
+
readonly kind: "saved";
|
|
98
|
+
readonly version: string;
|
|
99
|
+
} | {
|
|
100
|
+
readonly kind: "conflict";
|
|
101
|
+
readonly version: string;
|
|
102
|
+
readonly text: string;
|
|
103
|
+
} | ExplorerGrantRequired;
|
|
104
|
+
export type ExplorerHostPreview = {
|
|
105
|
+
readonly htmlContent: string;
|
|
106
|
+
readonly headings: readonly ExplorerHeading[];
|
|
107
|
+
};
|
|
108
|
+
export type ExplorerEditPorts<P> = {
|
|
109
|
+
readonly source: (context: ExplorerSourceContext<P>, path: string, readPath: string) => Promise<ExplorerHostEditSource>;
|
|
110
|
+
readonly save: (context: ExplorerSourceContext<P>, path: string, readPath: string, draft: ExplorerDraft) => Promise<ExplorerHostSaveResult>;
|
|
111
|
+
readonly preview: (context: ExplorerSourceContext<P>, path: string, readPath: string, text: string) => Promise<ExplorerHostPreview>;
|
|
112
|
+
readonly revisions: (context: ExplorerSourceContext<P>, path: string, readPath: string) => Promise<readonly ExplorerRevision[]>;
|
|
113
|
+
readonly revision: (context: ExplorerSourceContext<P>, path: string, readPath: string, revisionId: string) => Promise<string>;
|
|
114
|
+
};
|
|
85
115
|
export type MarkdownExplorerServerConfig<P> = {
|
|
86
116
|
readonly config: ExplorerConfig;
|
|
87
117
|
readonly authenticate: (request: Request, mode: ExplorerAuthenticationMode) => Promise<ExplorerAuthentication<P>>;
|
|
@@ -102,6 +132,8 @@ export type MarkdownExplorerServerConfig<P> = {
|
|
|
102
132
|
readonly sanitizer?: HtmlSanitizerOptions;
|
|
103
133
|
readonly deferredTimeoutMs?: number;
|
|
104
134
|
readonly textMaxBytes?: number;
|
|
135
|
+
readonly edit?: ExplorerEditPorts<P>;
|
|
136
|
+
readonly editMaxBytes?: number;
|
|
105
137
|
readonly logger?: ExplorerLogger;
|
|
106
138
|
readonly onTreeRefreshed?: (key: string) => void;
|
|
107
139
|
};
|