@aiquants/markdown-explorer 0.6.0 → 0.7.0

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.
Files changed (65) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +57 -8
  3. package/dist/EditSurface-BCB-0vUK.js +371 -0
  4. package/dist/EditSurface-BZ98L7hy.cjs +2 -0
  5. package/dist/ExplorerSplit-BTRTah7R.cjs +1 -0
  6. package/dist/{ExplorerSplit-CN5TvRj6.js → ExplorerSplit-DpoMtzq3.js} +391 -417
  7. package/dist/MultiLayout-B7OiXgWH.cjs +1 -0
  8. package/dist/MultiLayout-BD2LiwOe.js +947 -0
  9. package/dist/{NativeScrollBar-CHEQgaVx.cjs → NativeScrollBar-B4j3Vigb.cjs} +1 -1
  10. package/dist/{NativeScrollBar-DYEv2GLV.js → NativeScrollBar-EXOKhHP4.js} +1 -1
  11. package/dist/TreeLayout-B9dF2A3h.cjs +1 -0
  12. package/dist/TreeLayout-hn2bfU22.js +29 -0
  13. package/dist/client/MarkdownExplorer.d.cts +2 -1
  14. package/dist/client/MarkdownExplorer.d.ts +2 -1
  15. package/dist/client/storage.d.cts +1 -0
  16. package/dist/client/storage.d.ts +1 -0
  17. package/dist/core/contracts.d.cts +58 -0
  18. package/dist/core/contracts.d.ts +58 -0
  19. package/dist/core/labels.d.cts +49 -0
  20. package/dist/core/labels.d.ts +49 -0
  21. package/dist/core/location.d.cts +7 -0
  22. package/dist/core/location.d.ts +7 -0
  23. package/dist/dataRoute-CcZ8PK00.cjs +1 -0
  24. package/dist/dataRoute-D1vE1wFW.js +270 -0
  25. package/dist/index-C0gh_dRY.js +3188 -0
  26. package/dist/index-CCc6qUbM.cjs +5 -0
  27. package/dist/index.cjs +1 -1
  28. package/dist/index.d.cts +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.js +31 -30
  31. package/dist/location-DmzVAFvo.cjs +1 -0
  32. package/dist/{location-CtM94G0O.js → location-Q4sgRrUm.js} +202 -193
  33. package/dist/merge-0W6xtIxj.cjs +5 -0
  34. package/dist/merge-Bi41vtSe.js +20 -0
  35. package/dist/sanitizeWorker.cjs +1 -1
  36. package/dist/sanitizeWorker.js +173 -172
  37. package/dist/server/errors.d.cts +1 -1
  38. package/dist/server/errors.d.ts +1 -1
  39. package/dist/server/ports.d.cts +33 -1
  40. package/dist/server/ports.d.ts +33 -1
  41. package/dist/server.cjs +1 -1
  42. package/dist/server.d.cts +1 -1
  43. package/dist/server.d.ts +1 -1
  44. package/dist/server.js +882 -751
  45. package/dist/{storage-DA4ItHzl.js → storage-BSthKLSG.js} +14 -13
  46. package/dist/storage-Dgi4JXaB.cjs +1 -0
  47. package/dist/storage.cjs +1 -1
  48. package/dist/storage.js +1 -1
  49. package/dist/styles/markdown-explorer.css +1 -1
  50. package/docs/behavior/dom-hooks.md +17 -3
  51. package/docs/behavior/layout.md +6 -1
  52. package/docs/behavior/security.md +11 -1
  53. package/docs/behavior/url-contract.md +13 -4
  54. package/package.json +11 -3
  55. package/dist/ExplorerSplit-CUIbSLQm.cjs +0 -1
  56. package/dist/MultiLayout-BDcQSpa6.cjs +0 -1
  57. package/dist/MultiLayout-hPmV-bae.js +0 -922
  58. package/dist/TreeLayout-BDaIyoGT.cjs +0 -1
  59. package/dist/TreeLayout-CqzxLpoM.js +0 -23
  60. package/dist/dataRoute-CUXlMhUJ.cjs +0 -1
  61. package/dist/dataRoute-Ce6EIuu4.js +0 -206
  62. package/dist/index-B3nShkkt.cjs +0 -3
  63. package/dist/index-gJp1iXTc.js +0 -2238
  64. package/dist/location-FavgY_gB.cjs +0 -1
  65. package/dist/storage-Bkb2Axdi.cjs +0 -1
@@ -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();
@@ -1,281 +1,282 @@
1
- import { parentPort as ee } from "node:worker_threads";
2
- import { defaultTreeAdapter as b, parse as te, html as re } from "parse5";
3
- import { allowedFrameUrlOf as ne, reduceFrameFeatures as oe, EMBED_FRAME_REFERRER_POLICIES as se, EMBED_LINK_ATTRIBUTE as H, placeholderLinkOf as ae } from "@aiquants/markdown/embeds";
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 ie, e as ce } from "./dataRoute-Ce6EIuu4.js";
6
- const F = 512, le = 4096, G = 1024, j = /* @__PURE__ */ Symbol("placement"), V = 2, ue = 3, fe = /* @__PURE__ */ new Set(["html", "head", "body"]), w = 0, D = 1, M = 2, W = 3, z = 4, O = 5, k = 6, N = 7, Q = 8, I = 9, $ = 10, _ = -1, de = 0, pe = 1, Ee = 2, me = 3, he = 4, Te = 5, K = 6, Ae = 7, ge = (t) => {
7
- switch (t) {
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 de;
14
+ return Ee;
14
15
  case 47:
15
- return pe;
16
+ return me;
16
17
  case 62:
17
- return Ee;
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 Te;
24
+ return ge;
24
25
  default:
25
- return K;
26
+ return Y;
26
27
  }
27
- }, Se = Uint8Array.from({ length: 128 }, (t, i) => ge(i)), v = 60, X = 47, _e = /[\t\n\f\r />="'<]/g, we = /["<]/g, be = /['<]/g, A = -1 - M, Ne = Int8Array.from(
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
- [W, I, _, z, M, M, M],
32
- [W, I, _, z, A, A, A],
33
- [z, N, _, N, O, k, N],
34
- [O, O, O, O, Q, O, O],
35
- [k, k, k, k, k, Q, k],
36
- [D, N, _, N, N, N, N],
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
- ), J = (t) => t >= 65 && t <= 90 || t >= 97 && t <= 122, Oe = (t, i) => {
41
- let u = new Int32Array($), f = new Int32Array($), c = 0, h = 0, n = 0;
42
- for (; n < t.length; ) {
43
- if (c === 0) {
44
- const p = t.indexOf("<", n);
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, t.charCodeAt(n) === X && (n += 1), J(t.charCodeAt(n)) && (u[w] = 0, c = 1 << w, n += 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 ((c & -230) === 0 && n >= h) {
50
- const p = c === 1 << O ? we : c === 1 << k ? be : _e;
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(t);
53
+ const a = p.exec(r);
53
54
  if (a === null) return !1;
54
- n = a.index, t.charCodeAt(n) === v && (h = n + 3);
55
+ n = a.index, r.charCodeAt(n) === B && (T = n + 3);
55
56
  }
56
- const m = t.charCodeAt(n), g = m < 128 ? Se[m] : K;
57
- let T = 0;
58
- for (let p = c; p !== 0; p &= p - 1) {
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 r = Ne[a * Ae + g], o = u[a];
61
- if (r < _ && (r = -1 - r, o += 1, o > i))
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 (r === _) continue;
64
- const E = 1 << r;
65
- ((T & E) === 0 || o > f[r]) && (f[r] = o), T |= E;
64
+ if (e === _) continue;
65
+ const E = 1 << e;
66
+ ((h & E) === 0 || o > f[e]) && (f[e] = o), h |= E;
66
67
  }
67
- J(m) && (t.charCodeAt(n - 1) === v || t.charCodeAt(n - 1) === X && t.charCodeAt(n - 2) === v) && (T & 1 << w) === 0 && (f[w] = 0, T |= 1 << w);
68
- const L = u;
69
- u = f, f = L, c = T, n += 1;
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 C extends Error {
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 ke = (t, i, u, f) => {
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 (Oe(t, G)) return { status: "too-many-attributes" };
84
- const c = i + V, h = t.length + ue;
85
- let n = null, m = 0, g = 0, T = 0, L = 0;
86
- const p = (e) => {
87
- if (g += e, g > u) throw new C("too-many-nodes");
88
- }, a = (e) => {
89
- for (const s of e) L += s.name.length + s.value.length;
90
- if (L > f) throw new C("too-many-attribute-characters");
91
- }, r = (e, s, l, d) => {
92
- const P = { node: e, above: null, levels: s, height: 0, keepsChildren: l, attributes: d, first: null, next: null, previous: null }, Z = e;
93
- return Z[j] = P, P;
94
- }, o = (e) => {
95
- const l = e[j];
96
- if (l === void 0) throw new Error("[markdown-explorer] the nesting guard was handed a node it did not create");
97
- return l;
98
- }, E = (e, s) => {
99
- let l = s;
100
- for (let d = e; d !== null && l > d.height; d = d.above)
101
- d.height = l, l += d.levels;
102
- if (n !== null && n.height > c) throw new C("too-deep");
103
- }, y = (e) => {
104
- const s = e.above;
105
- s?.keepsChildren && (e.previous === null ? s.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;
106
- }, R = (e, s) => {
107
- if (!b.isElementNode(s)) return;
108
- const l = o(s), d = o(e);
109
- y(l), l.above = d, d.keepsChildren && (l.next = d.first, d.first !== null && (d.first.previous = l), d.first = l), s.parentNode = e, E(d, l.height + l.levels);
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
- ...b,
112
+ ...N,
112
113
  createDocument: () => {
113
- const e = b.createDocument();
114
- return n = r(e, 0, !1, 0), e;
114
+ const t = N.createDocument();
115
+ return n = e(t, 0, !1, 0), t;
115
116
  },
116
117
  createDocumentFragment: () => {
117
- const e = b.createDocumentFragment();
118
- return r(e, 0, !1, 0), e;
118
+ const t = N.createDocumentFragment();
119
+ return e(t, 0, !1, 0), t;
119
120
  },
120
- createElement: (e, s, l) => {
121
- if (m += 1, m > h) throw new C("too-many-elements");
122
- p(1 + l.length), a(l);
123
- const d = b.createElement(e, s, l);
124
- return r(d, 1, !(s === re.NS.HTML && fe.has(e)), l.length), d;
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: (e) => {
127
- if (T += 1, T > le) throw new C("too-many-comments");
128
- return p(1), b.createCommentNode(e);
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: (e, s) => {
131
- b.setTemplateContent(e, s);
132
- const l = o(s);
133
- l.above = o(e), E(l.above, l.height);
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: R,
136
- insertBefore: (e, s) => R(e, s),
137
- detachNode: (e) => {
138
- b.isElementNode(e) && (y(o(e)), e.parentNode = null);
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: (e) => {
141
- const s = o(e);
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: (e) => {
149
- const s = o(e);
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: (e, s) => {
157
+ adoptAttributes: (t, s) => {
157
158
  p(s.length), a(s);
158
- const l = o(e);
159
- if (l.attributes += s.length, l.attributes > G) throw new C("too-many-attributes");
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 B;
163
+ let v;
163
164
  try {
164
- B = te(t, { scriptingEnabled: !1, treeAdapter: x });
165
- } catch (e) {
166
- if (e instanceof C) return { status: e.status };
167
- throw e;
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(B).height - V, elements: m, nodes: g };
170
+ return { status: "within", depth: o(v).height - W, elements: m, nodes: g };
170
171
  };
171
- class Y extends RangeError {
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"], Le = ["srcdoc", "formaction"], Re = /^(?:(?:https?|mailto|tel):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/iu, Ie = /* @__PURE__ */ new Set(["left", "right", "center", "justify", "start", "end"]), ye = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]), U = 1, De = 3, Me = 7, Ue = 8, q = Object.freeze({
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
- }), xe = [...new Set(Object.values(q).flatMap((t) => t.filter((i) => i.startsWith("data-"))))], He = /* @__PURE__ */ new Set(["src", "href", "xlink:href"]), ze = /^data:/iu, ve = Object.freeze({
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: Le,
185
+ FORBID_ATTR: Ie,
185
186
  ADD_TAGS: ["iframe"],
186
- ADD_ATTR: (t, i) => q[i]?.includes(t) === !0,
187
- ADD_URI_SAFE_ATTR: xe,
188
- ALLOWED_URI_REGEXP: Re,
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
- }), Be = (t) => {
191
+ }), Fe = (r) => {
191
192
  let i = null;
192
- for (const u of t.split(";")) {
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 c = u.slice(f + 1).trim().toLowerCase();
196
- Ie.has(c) && (i = c);
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
- }, Pe = (t) => t.nodeType === De ? { kind: "text", text: t.nodeValue ?? "" } : t.nodeType === Ue || t.nodeType === Me ? { kind: "ignored" } : t.nodeType === U && t.nodeName.toLowerCase() === "a" ? { kind: "link", href: t.getAttribute("href") } : { kind: "element" }, Fe = (t) => {
200
- const i = t.getAttribute("type");
201
- return i !== null && i.trim().toLowerCase() === "checkbox" && t.hasAttribute("disabled");
202
- }, Ge = (t, i, u) => {
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([...t]);
206
- let c = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakMap();
206
+ const f = Object.freeze([...r]);
207
+ let l = /* @__PURE__ */ new WeakMap(), T = /* @__PURE__ */ new WeakMap();
207
208
  const n = (a) => {
208
- const r = a.parentNode === null ? void 0 : c.get(a.parentNode), o = r === void 0 ? 0 : r + 1;
209
- if (c.set(a, o), a.nodeType === U && o > u) throw new Y(u);
210
- }, m = (a, r) => {
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
- (r.tagName === "input" && !Fe(o) || r.tagName === "iframe" && ne(o.getAttribute("src")?.trim(), f) === null) && o.parentNode?.removeChild(o);
214
- }, g = (a, r) => {
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 (r.attrName === "style") {
217
- const E = Be(r.attrValue);
218
- E === null ? r.keepAttr = !1 : r.attrValue = E;
219
- } else if (r.attrName === "allow" && o === "iframe") {
220
- const E = oe(r.attrValue);
221
- E === null ? r.keepAttr = !1 : r.attrValue = E;
222
- } else if (r.attrName === "referrerpolicy" && o === "iframe") {
223
- const E = r.attrValue.trim().toLowerCase();
224
- se.has(E) ? r.attrValue = E : r.keepAttr = !1;
225
- } else r.attrName === H && o === "p" ? h.set(a, a.getAttribute(H)) : He.has(r.attrName) && ze.test(r.attrValue) ? r.keepAttr = !1 : r.attrName === "id" && ye.has(o) && (r.forceKeepAttr = !0);
226
- }, T = (a) => {
227
- const r = a.getAttribute("data-asset-src");
228
- r !== null && (a.getAttribute("data-asset-exists") !== "false" && ie(r) ? a.setAttribute("src", ce(i, r, null)) : a.removeAttribute("src"));
229
- }, L = (a) => {
230
- const r = a.parentNode;
231
- if (r === null) return;
232
- const o = h.get(r);
233
- o != null && ae(o, [...r.childNodes].map(Pe)) !== null && r.setAttribute(H, o);
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 r = a, o = r.nodeName.toLowerCase();
237
- o === "img" ? T(r) : o === "a" && L(r);
238
- const E = r.getAttribute("target");
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 = r.getAttribute("rel"), R = new Set(y === null ? [] : y.split(/\s+/u).filter((x) => x !== ""));
241
- R.add("noopener"), R.add("noreferrer"), r.setAttribute("rel", [...R].join(" "));
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
- c = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakMap(), S.addHook("beforeSanitizeElements", n), S.addHook("uponSanitizeElement", m), S.addHook("uponSanitizeAttribute", g), S.addHook("afterSanitizeAttributes", p);
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
- }, je = (t) => {
252
- if (t === null || typeof t != "object") return !1;
253
- const { id: i, input: u } = t;
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: c, dataPath: h, maxNodes: n, maxAttributeChars: m, maxSanitizedChars: g } = u;
256
- return typeof f == "string" && Array.isArray(c) && c.every((T) => typeof T == "string") && typeof h == "string" && h !== "" && [n, m, g].every((T) => Number.isSafeInteger(T) && T > 0);
257
- }, Ve = () => {
258
- const t = ee;
259
- if (t === null) throw new Error("[markdown-explorer] the sanitizer worker must run in a worker thread");
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
- t.on("message", (u) => {
262
- if (!je(u)) throw new TypeError("[markdown-explorer] the sanitizer worker received a message that is not a job");
263
- const { id: f, input: c } = u;
264
- let h;
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 (ke(c.html, F, c.maxNodes, c.maxAttributeChars).status !== "within") {
267
- t.postMessage({ id: f, output: { status: "too-large" } });
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([c.iframeHosts, c.dataPath]);
271
+ const n = JSON.stringify([l.iframeHosts, l.dataPath]);
271
272
  let m = i.get(n);
272
- m === void 0 && (m = Ge(c.iframeHosts, c.dataPath, F), i.set(n, m));
273
- const g = m(c.html);
274
- h = g.length > c.maxSanitizedChars ? { status: "too-large" } : { status: "sanitized", html: g };
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
- h = n instanceof Y ? { status: "too-large" } : { status: "error", message: n instanceof Error ? `${n.name}: ${n.message}` : String(n) };
277
+ T = n instanceof q ? { status: "too-large" } : { status: "error", message: n instanceof Error ? `${n.name}: ${n.message}` : String(n) };
277
278
  }
278
- t.postMessage({ id: f, output: h });
279
- }), t.postMessage({ ready: !0 });
279
+ r.postMessage({ id: f, output: T });
280
+ }), r.postMessage({ ready: !0 });
280
281
  };
281
- Ve();
282
+ Qe();
@@ -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;
@@ -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;
@@ -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
  };