@p-buddy/parkdown 0.0.4 → 0.0.6

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.
@@ -1,15 +1,13 @@
1
- (function(u,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("stable-hash"),require("ts-dedent"),require("extract-comments")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","stable-hash","ts-dedent","extract-comments"],w):(u=typeof globalThis<"u"?globalThis:u||self,w(u.index={},u.node_fs,u.node_path,u.node_url,u.unified,u.remarkParse,u.unistUtilVisit,u.hash,u.tsDedent,u._extractComments))})(this,function(u,w,f,J,Z,K,Y,ee,x,te){"use strict";const g=(e,n)=>e.position.start.offset-n.position.start.offset;g.reverse=(e,n)=>g(n,e);const N=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,ne=Z.unified().use(K),k={md:e=>ne.parse(e)},E=(e,n)=>{const t=[];return Y.visit(e,(o,s,r)=>{if(o.type!=="root"){if(n&&o.type!==n)return;if(N(o)){const i=ee(r),c=((r==null?void 0:r.children.length)??0)-1;t.push({...o,parentID:i,siblingIndex:s,siblingCount:c})}}}),t},se=e=>e.children.length===0,P=(e,n,...t)=>{if(t.length===0)throw new Error("No nodes to replace content from");t.sort(g);const o=t.at(0),s=t.at(-1);return e.slice(0,o.position.start.offset)+n+e.slice(s.position.end.offset)},oe=(e,n,t)=>{const o=Math.min(n.position.end.offset,t.position.end.offset),s=Math.max(n.position.start.offset,t.position.start.offset);return e.slice(o,s)},A=(...e)=>e.join(" "),re=(...e)=>e.join(`
2
- `),ie=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},ce=e=>ie(e)[0];class v{constructor(){this.intervals=[]}push(n,t){this.intervals.push([Math.min(n,t),Math.max(n,t)])}combine(n){this.intervals.push(...n.intervals)}collapse(){const{intervals:n}=this;if(!n.length)return this.intervals=[];n.sort((r,i)=>r[0]-i[0]);const t=[];let[o,s]=n[0];for(let r=1;r<n.length;r++){const[i,c]=n[r];i<=s?s=Math.max(s,c):(t.push([o,s]),o=i,s=c)}return t.push([o,s]),this.intervals=t}subtract(n){const{intervals:t}=this,{intervals:o}=n;if(!t.length||!o.length)return t;let s=[...t];for(const[r,i]of o){const c=[];for(const[l,a]of s){if(i<=l||r>=a){c.push([l,a]);continue}r>l&&c.push([l,r]),i<a&&c.push([i,a])}s=c}return this.intervals=s}}const L=/,\s*(?![^()]*\))/,le=[[/'''/g,'"'],[/''/g,"'"],[/parkdown:\s+/g,""],[/p▼:\s+/g,""]],ae=(e,n="-")=>{const t=le.reduce((o,[s,r])=>o.replaceAll(s,r),e);return n?t.replaceAll(n," "):t},pe=["string","number","boolean"],ue=e=>pe.includes(e),me=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,de=e=>{const n=e.match(me);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,o=""]=n;if(!o.trim())return{name:t,parameters:[]};const s=fe(o);return{name:t,parameters:s}},he=(e,n)=>{for(let t=n+1;t<e.length;t++)if(e[t]!==void 0)return!1;return!0},fe=e=>{const n=[];let t="",o=!1;for(let s=0;s<e.length;s++){const r=e[s],i=r==="'";if(i&&e.at(s+1)==="'"){const c=e.at(s+2)==="'";o=!o,t+=c?"'''":"''",s+=c?2:1}else i?(o=!o,t+=r):r===","&&!o?(n.push(t.trim()),t=""):t+=r}return n.push(t.trim()),n.map(s=>s===""?void 0:s?ge(s):void 0).filter((s,r,i)=>s!==void 0||!he(i,r))},ge=e=>{const n=e.length>=2&&e[0]==="'"&&e.at(-1)==="'",t=n&&e.length>=4&&e[1]==="'"&&e.at(-2)==="'";return!n||t?e:e.slice(1,-1)},we=e=>{const n=/^(\w+)(?:\(([^)]*)\))?/,t=e.match(n);if(!t)return{name:e};const[,o,s]=t;if(!s)return{name:o};const r=/(\w+)(\?)?:\s*([^,]+)/g,i=[];let c;for(;(c=r.exec(s))!==null;){const[,l,a,p]=c,m=p.trim();if(!ue(m))throw new Error(`Unsupported type: ${m}`);i.push({name:l,optional:a==="?",type:m})}return{name:o,parameters:i}},R=e=>{const n=e.map(we).reduce((t,{name:o,parameters:s})=>t.set(o,s),new Map);return t=>{const{name:o,parameters:s}=de(t);if(!n.has(o))throw new Error(`Unknown method: ${o}`);const r=n.get(o);if(r===void 0)return{name:o};if(s.length>r.length){const i=r.filter(({optional:l})=>!l).length,c=i===r.length?i.toString():`${i} - ${r.length}`;throw new Error(`Too many parameters: ${s.length} for method '${o}' (expected: ${c})`)}return r.reduce((i,{name:c,optional:l,type:a},p)=>{const m=s[p];if(m===void 0){if(l)return i;throw new Error(`Missing required parameter: ${c} for method '${o}'`)}switch(a){case"string":i[c]=m;break;case"number":case"boolean":i[c]=JSON.parse(m);break}if(typeof i[c]!==a)throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${o}'`);return i},{name:o})}},_=e=>Object.entries(e).filter(([n])=>n!=="name"&&!isNaN(Number(n))).map(([n,t])=>t),ve=["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"],$e="-",Ce=R(ve),F=(e,n,t)=>{const o=Ce(n),s=(t==null?void 0:t.inline)&&!e.includes(`
1
+ (function(p,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("stable-hash"),require("ts-dedent"),require("extract-comments")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","stable-hash","ts-dedent","extract-comments"],h):(p=typeof globalThis<"u"?globalThis:p||self,h(p.index={},p.node_fs,p.node_path,p.node_url,p.unified,p.remarkParse,p.unistUtilVisit,p.hash,p.tsDedent,p._extractComments))})(this,function(p,h,f,ie,ce,le,ae,pe,b,ue){"use strict";var Je=Object.defineProperty;var Ze=(p,h,f)=>h in p?Je(p,h,{enumerable:!0,configurable:!0,writable:!0,value:f}):p[h]=f;var re=(p,h,f)=>Ze(p,typeof h!="symbol"?h+"":h,f);const v=(e,n)=>e.position.start.offset-n.position.start.offset;v.reverse=(e,n)=>v(n,e);const j=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,me=ce.unified().use(le),q={md:e=>me.parse(e)},x=(e,n)=>{const t=[];return ae.visit(e,(s,o,r)=>{if(s.type!=="root"){if(n&&s.type!==n)return;if(j(s)){const i=pe(r),c=((r==null?void 0:r.children.length)??0)-1;t.push({...s,parentID:i,siblingIndex:o,siblingCount:c})}}}),t},he=e=>e.children.length===0,A=(e,n,...t)=>{if(t.length===0)throw new Error("No nodes to replace content from");t.sort(v);const s=t.at(0),o=t.at(-1);return e.slice(0,s.position.start.offset)+n+e.slice(o.position.end.offset)},fe=(e,n,t)=>{const s=Math.min(n.position.end.offset,t.position.end.offset),o=Math.max(n.position.start.offset,t.position.start.offset);return e.slice(s,o)},S=(...e)=>e.join(" "),de=(...e)=>e.join(`
2
+ `),ge=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},ve=e=>ge(e)[0];class w{constructor(){re(this,"intervals",[])}push(n,t){this.intervals.push([Math.min(n,t),Math.max(n,t)])}combine(n){this.intervals.push(...n.intervals)}collapse(){const{intervals:n}=this;if(!n.length)return this.intervals=[];n.sort((r,i)=>r[0]-i[0]);const t=[];let[s,o]=n[0];for(let r=1;r<n.length;r++){const[i,c]=n[r];i<=o?o=Math.max(o,c):(t.push([s,o]),s=i,o=c)}return t.push([s,o]),this.intervals=t}subtract(n){const{intervals:t}=this,{intervals:s}=n;if(!t.length||!s.length)return t;let o=[...t];for(const[r,i]of s){const c=[];for(const[l,a]of o){if(i<=l||r>=a){c.push([l,a]);continue}r>l&&c.push([l,r]),i<a&&c.push([i,a])}o=c}return this.intervals=o}}const R=/,\s*(?![^()]*\))/,_={reserved:{semi:";",slash:"/",question:"?",colon:":",at:"@",equal:"=",and:"&"},unsafe:{quote:'"',angle:"<",unangle:">",hash:"#",percent:"%",curly:"{",uncurly:"}",pipe:"|",back:"\\",carrot:"^",tilde:"~",square:"[",unsquare:"]",tick:"`"}},we="-",W={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(_.unsafe),...Object.entries(_.reserved)]},B=(e,[n,t])=>e.replaceAll(n,t),F=(e,n=we)=>{const t=W.static.reduce(B,e);return W.url.map(([s,o])=>[n+s+n,o]).reduce(B,t).replaceAll(n," ")},$e=["string","number","boolean"],Ce=e=>$e.includes(e),ye=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,Ee=e=>{const n=e.match(ye);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,s=""]=n;if(!s.trim())return{name:t,parameters:[]};const o=xe(s);return{name:t,parameters:o}},ke=(e,n)=>{for(let t=n+1;t<e.length;t++)if(e[t]!==void 0)return!1;return!0},xe=e=>{const n=[];let t="",s=!1;for(let o=0;o<e.length;o++){const r=e[o],i=r==="'";if(i&&e.at(o+1)==="'"){const c=e.at(o+2)==="'";s=!s,t+=c?"'''":"''",o+=c?2:1}else i?(s=!s,t+=r):r===","&&!s?(n.push(t.trim()),t=""):t+=r}return n.push(t.trim()),n.map(o=>o===""?void 0:o?Se(o):void 0).filter((o,r,i)=>o!==void 0||!ke(i,r))},Se=e=>{const n=e.length>=2&&e[0]==="'"&&e.at(-1)==="'",t=n&&e.length>=4&&e[1]==="'"&&e.at(-2)==="'";return!n||t?e:e.slice(1,-1)},Me=e=>{const n=/^(\w+)(?:\(([^)]*)\))?/,t=e.match(n);if(!t)return{name:e};const[,s,o]=t;if(!o)return{name:s};const r=/(\w+)(\?)?:\s*([^,]+)/g,i=[];let c;for(;(c=r.exec(o))!==null;){const[,l,a,u]=c,m=u.trim();if(!Ce(m))throw new Error(`Unsupported type: ${m}`);i.push({name:l,optional:a==="?",type:m})}return{name:s,parameters:i}},U=e=>{const n=e.map(Me).reduce((t,{name:s,parameters:o})=>t.set(s,o),new Map);return t=>{const{name:s,parameters:o}=Ee(t);if(!n.has(s))throw new Error(`Unknown method: ${s}`);const r=n.get(s);if(r===void 0)return{name:s};if(o.length>r.length){const i=r.filter(({optional:l})=>!l).length,c=i===r.length?i.toString():`${i} - ${r.length}`;throw new Error(`Too many parameters: ${o.length} for method '${s}' (expected: ${c})`)}return r.reduce((i,{name:c,optional:l,type:a},u)=>{const m=o[u];if(m===void 0){if(l)return i;throw new Error(`Missing required parameter: ${c} for method '${s}'`)}switch(a){case"string":i[c]=m;break;case"number":case"boolean":i[c]=JSON.parse(m);break}if(typeof i[c]!==a)throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${s}'`);return i},{name:s})}},G=e=>Object.entries(e).filter(([n])=>n!=="name"&&!isNaN(Number(n))).map(([n,t])=>t),Ne=U(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),$=(e,n=1)=>`
3
+ `.repeat(n)+e+`
4
+ `.repeat(n),O=(e,n,t)=>`<${n}${t?` ${t}`:""}>${$(e)}</${n}>`,H=(e,n,t)=>{const s=Ne(n);switch(s.name){case"code":if(!s.lang&&!s.meta&&(t==null?void 0:t.inline)&&!e.includes(`
5
+ `))return`\`${e}\``;const r=s.lang??(t==null?void 0:t.extension)??"",i=s.meta?` ${s.meta}`:"";return $(`\`\`\`${r}${i}${$(e)}\`\`\``);case"quote":return t!=null&&t.inline&&!e.includes(`
3
6
 
4
- `);switch(o.name){case"code":const r=o.lang??(t==null?void 0:t.extension)??"",i=o.meta?` ${o.meta}`:"";return`\`\`\`${r}${i}
5
- ${e}
6
- \`\`\``;case"quote":return s?`> ${e}`:`<blockquote>
7
-
8
- ${e}
9
-
10
- </blockquote>
11
- `;case"dropdown":const c=`<details${o.open?" open":""}>`,l=`<summary>${o.summary.split(o.space??$e).join(" ")}</summary>`;return["",c,l,"",e,"</details>",""].join(`
12
- `)}},ye=R(["extract(id: string, 0?: string, 1?: string, 2?: string)","remove(id: string, 0?: string, 1?: string, 2?: string)","replace(id: string, with?: string, space?: string)"]),T=e=>te(e),W=(e,n)=>T(e).filter(({value:t})=>t.includes(n)).sort((t,o)=>t.range[0]-o.range[0]),Se=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),o=T(e),s=new v,r=new v;for(const i of n){const c=o.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],p=c[l+1];s.push(a.range[1],p.range[0]);const[m,...$]=t([a.range[1],p.range[0]]),b=$[$.length-1];r.push(a.range[0],a.range[1]+(m.trim()?0:1)),r.push(p.range[0],p.range[1]+(b.trim()?0:1))}}return s.collapse(),r.collapse(),x.dedent(s.subtract(r).map(t).filter(Boolean).join("")).trim()},Ee=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),o=T(e),s=new v;for(const i of n){const c=o.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],p=c[l+1],m=t([p.range[1],p.range[1]+1]).at(-1);s.push(a.range[0],m===`
13
- `?p.range[1]+1:p.range[1])}}s.collapse();const r=new v;return r.push(0,e.length),r.subtract(s),x.dedent(r.collapse().map(t).filter(Boolean).join("")).trim()},Me=(e,n,t,o)=>{if(!n)return e;const s=W(e,n);if(s.length<2)return e;let r="",i=0;for(let l=0;l<s.length-1;l+=2){const a=s[l],p=s[l+1];r+=e.slice(i,a.range[1]),r+=ae(t??a.value,o),i=p.range[0]}r+=e.slice(i);const c=new v;return c.push(0,r.length),c.subtract(W(r,n).reduce((l,{range:a})=>(l.push(...a),l),new v)),x.dedent(c.collapse().map(([l,a])=>r.slice(l,a)).filter(Boolean).join("")).trim()},xe=(e,n)=>{const t=ye(n);switch(t.name){case"extract":return Se(e,t.id,..._(t));case"remove":return Ee(e,t.id,..._(t));case"replace":return Me(e,t.id,t.with,t.space)}},Ne=["http","./","../"],ke=({url:e})=>Ne.some(n=>e.startsWith(n)),Te=e=>N(e)&&se(e)&&ke(e),I=({url:e},n)=>`[](${n?f.join(n,e):e})`,d={_open:"<!--",_close:"-->",_flag:"p▼",get begin(){return A(d._open,d._flag,"BEGIN",d._close)},get end(){return A(d._open,d._flag,"END",d._close)}},B=e=>n=>N(n)&&n.value===d[e],Ie=({position:e,url:n,siblingCount:t},o)=>({position:e,url:n,headingDepth:o,inline:t>=1}),je=({position:{start:e},url:n,siblingCount:t},{position:{end:o}},s)=>({position:{start:e,end:o},url:n,headingDepth:s,inline:t>=1}),be=(e,n,t)=>e.inline?`${I(e,t)} ${d.begin} ${n} ${d.end}`:re(I(e,t),d.begin,n,d.end),qe=e=>{const n=E(e,"heading").reduce((t,{position:o,depth:s})=>t.set(o.start.line,s),new Map);return t=>{for(let o=t.position.start.line;o>=1;o--){const s=n.get(o);if(s)return s}return 0}},M={openingCommentDoesNotFollowLink:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),closingCommentNotMatchedToOpening:({position:{start:e}})=>new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),openingCommentNotClosed:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)},De=(e,n)=>{const t=[],o=[...e.map(r=>({node:r,type:"open"})),...n.map(r=>({node:r,type:"close"}))].sort((r,i)=>g(r.node,i.node)),s=[];for(const r of o)if(r.type==="open")s.push(r);else{const i=r.node;if(s.length===0)throw M.closingCommentNotMatchedToOpening(i);const c=s.pop().node;if(s.length>0)continue;t.push({open:c,close:i})}if(s.length>0)throw M.openingCommentNotClosed(s[0].node);return t},Oe=(e,n,t)=>{const o=[...n].sort(g),s=[];return[...t].sort((r,i)=>g.reverse(r.open,i.open)).forEach(r=>{for(;o.length>0;){const i=o.pop();if(i.position.start.offset<r.open.position.start.offset){if(oe(e,i,r.open).trim()!=="")throw M.openingCommentDoesNotFollowLink(r.open);return s.push([i,r])}s.push(i)}throw M.openingCommentDoesNotFollowLink(r.open)}),s.push(...o.reverse()),s.reverse()},U=(e,n)=>{n??(n=k.md(e));const t=qe(n),o=E(n,"link").filter(Te),s=E(n,"html").sort(g),r=s.filter(B("begin")),i=s.filter(B("end")),c=De(r,i);return Oe(e,o,c).map(a=>Array.isArray(a)?je(a[0],a[1].close,t(a[0])):Ie(a,t(a)))},Pe=(e,{url:n})=>t=>e(f.join(f.dirname(n),t)),G=(...e)=>{const n=e.reduce((t,o)=>t+o,0);return Math.min(Math.max(n,1),6)},Ae=(e,n,t)=>{if(n===0)return e;t??(t=k.md(e));const o=E(t,"heading"),s=e.split(`
14
- `);for(const r of o){const{depth:i,position:{start:c,end:l}}=r,a=G(i,n),p=s[c.line-1].slice(i,l.column),m="#".repeat(a)+p;s[c.line-1]=m,r.depth=a}return s.join(`
15
- `)},H=e=>U(e).reverse().sort(g.reverse).reduce((n,t)=>P(n,I(t),t),e),Q=(e,n,t,o)=>{e=H(e),e=Ae(e,n);const s=k.md(e);return U(e,s).sort(g).reverse().map(r=>{var X,V;const{url:i,headingDepth:c}=r,[l,...a]=f.basename(i).split("?"),p=l.split(".").pop()??"",m=a.join("?"),$=f.dirname(i),b=f.join($,l);if(i.startsWith("./")||i.startsWith("../")){let h=t(b);const C=new J.URLSearchParams(m),q=(X=C.get("region"))==null?void 0:X.split(L);h=(q==null?void 0:q.reduce((y,S)=>xe(y,S),h))??h;const Fe=C.has("skip"),We=C.get("heading")??0,Be=C.has("inline");let{inline:D}=r;if(Be&&(D=!0),!Fe)if(p==="md"){const y=Pe(t,r),S=o?f.join(o,$):$,Ue=G(c,Number(We));h=Q(h,Ue,y,S)}else/^(js|ts)x?|svelte$/i.test(p)&&(h=F(h,"code",{extension:p,inline:D}));const O=(V=C.get("wrap"))==null?void 0:V.split(L);return h=(O==null?void 0:O.reduce((y,S)=>F(y,S,{extension:p,inline:D}),h))??h,{target:r,content:be(r,h,o)}}else throw i.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${i}`)}).reduce((r,{target:i,content:c})=>P(r,c,i),e)},z=e=>w.readFileSync(e,"utf-8"),j=e=>{const n=f.resolve(e),t=f.dirname(n);return{markdown:z(n),dir:t,path:n}},Le=(e,n=!0)=>{const{dir:t,path:o,markdown:s}=j(e),i=Q(s,0,c=>z(f.resolve(t,ce(c))));return n&&w.writeFileSync(o,i),i},Re=(e,n=!0)=>{const{path:t,markdown:o}=j(e),s=H(o);return n&&w.writeFileSync(t,s),s},_e=(e,n=!0)=>{j(e)};u.depopulateMarkdownInclusions=Re,u.populateMarkdownInclusions=Le,u.remapImportSpecifiers=_e,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
7
+ `)?`> ${e}`:$(O($(e),"blockquote"));case"dropdown":const c=O(F(s.summary),"summary");return $(O([c,e].join(`
8
+ `),"details",s.open?"open":void 0))}},Te=U(["extract(id: string, 0?: string, 1?: string, 2?: string)","remove(id: string, 0?: string, 1?: string, 2?: string)","replace(id: string, with?: string, space?: string)"]),I=e=>ue(e),M=(e,n)=>I(e).filter(({value:t})=>t.includes(n)).sort((t,s)=>t.range[0]-s.range[0]),be=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),s=I(e),o=new w,r=new w;for(const i of n){const c=s.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],u=c[l+1];o.push(a.range[1],u.range[0]);const[m,...T]=t([a.range[1],u.range[0]]),C=T[T.length-1];r.push(a.range[0],a.range[1]+(m.trim()?0:1)),r.push(u.range[0],u.range[1]+(C.trim()?0:1))}}return o.collapse(),r.collapse(),b.dedent(o.subtract(r).map(t).filter(Boolean).join("")).trim()},je=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),s=I(e),o=new w;for(const i of n){const c=s.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],u=c[l+1],m=t([u.range[1],u.range[1]+1]).at(-1);o.push(a.range[0],m===`
9
+ `?u.range[1]+1:u.range[1])}}o.collapse();const r=new w;return r.push(0,e.length),r.subtract(o),b.dedent(r.collapse().map(t).filter(Boolean).join("")).trim()},qe=(e,n,t,s)=>{if(!n)return e;const o=M(e,n);if(o.length<2)return e;let r="",i=0;for(let l=0;l<o.length-1;l+=2){const a=o[l],u=o[l+1];r+=e.slice(i,a.range[1]),r+=F(t??a.value,s),i=u.range[0]}r+=e.slice(i);const c=new w;return c.push(0,r.length),c.subtract(M(r,n).reduce((l,{range:a})=>(l.push(...a),l),new w)),b.dedent(c.collapse().map(([l,a])=>r.slice(l,a)).filter(Boolean).join("")).trim()},Q=e=>({space:e===" ",newline:e===`
10
+ `}),z=e=>[...M(e,"p↓:"),...M(e,"parkdown:")].sort((n,t)=>n.range[0]-t.range[0]).reverse().reduce((n,{range:[t,s],loc:{start:{column:o}}})=>{const r=(l,a)=>n.slice(0,l)+n.slice(a),i={prev:Q(n[t-1]),next:Q(n[s]),startLine:o===0,final:s===n.length};return i.startLine&&(i.next.newline||i.final)?r(t-(i.final?1:0),s+1):i.startLine?r(t,s+(i.next.space?1:0)):r(t-(i.prev.space?1:0),s)},e),Oe=(e,n)=>{if(!n)return z(e);const t=Te(n);switch(t.name){case"extract":e=be(e,t.id,...G(t));break;case"remove":e=je(e,t.id,...G(t));break;case"replace":e=qe(e,t.id,t.with,t.space);break}return z(e)},Ie=["http","./","../"],De=({url:e})=>Ie.some(n=>e.startsWith(n)),Le=e=>j(e)&&he(e)&&De(e),X=({url:e},n)=>`[](${n?f.join(n,e):e})`,d={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return S(d._open,d._flag,"BEGIN",d._close)},get end(){return S(d._open,d._flag,"END",d._close)},lengthOf(e){const n=`lines: ${e.split(`
11
+ `).length}`,t=`chars: ${e.length}`;return S(d._open,d._flag,"length",n,t,d._close)}},V=e=>n=>j(n)&&n.value===d[e],Pe=({position:e,url:n,siblingCount:t},s)=>({position:e,url:n,headingDepth:s,inline:t>=1}),Ae=({position:{start:e},url:n,siblingCount:t},{position:{end:s}},o)=>({position:{start:e,end:s},url:n,headingDepth:o,inline:t>=1}),Re=(e,n,t)=>(e.inline?S:de)(X(e,t),d.begin,d.lengthOf(n),n,d.end),_e=e=>{const n=x(e,"heading").reduce((t,{position:s,depth:o})=>t.set(s.start.line,o),new Map);return t=>{for(let s=t.position.start.line;s>=1;s--){const o=n.get(s);if(o)return o}return 0}},N={openingCommentDoesNotFollowLink:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),closingCommentNotMatchedToOpening:({position:{start:e}})=>new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),openingCommentNotClosed:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)},We=(e,n)=>{const t=[],s=[...e.map(r=>({node:r,type:"open"})),...n.map(r=>({node:r,type:"close"}))].sort((r,i)=>v(r.node,i.node)),o=[];for(const r of s)if(r.type==="open")o.push(r);else{const i=r.node;if(o.length===0)throw N.closingCommentNotMatchedToOpening(i);const c=o.pop().node;if(o.length>0)continue;t.push({open:c,close:i})}if(o.length>0)throw N.openingCommentNotClosed(o[0].node);return t},Be=(e,n,t)=>{const s=[...n].sort(v),o=[];return[...t].sort((r,i)=>v.reverse(r.open,i.open)).forEach(r=>{for(;s.length>0;){const i=s.pop();if(i.position.start.offset<r.open.position.start.offset){if(fe(e,i,r.open).trim()!=="")throw N.openingCommentDoesNotFollowLink(r.open);return o.push([i,r])}o.push(i)}throw N.openingCommentDoesNotFollowLink(r.open)}),o.push(...s.reverse()),o.reverse()},J=(e,n)=>{n??(n=q.md(e));const t=_e(n),s=x(n,"link").filter(Le),o=x(n,"html").sort(v),r=o.filter(V("begin")),i=o.filter(V("end")),c=We(r,i);return Be(e,s,c).map(a=>Array.isArray(a)?Ae(a[0],a[1].close,t(a[0])):Pe(a,t(a)))},Fe=(e,{url:n})=>t=>e(f.join(f.dirname(n),t)),Z=(...e)=>{const n=e.reduce((t,s)=>t+s,0);return Math.min(Math.max(n,1),6)},Ue=(e,n,t)=>{if(n===0)return e;t??(t=q.md(e));const s=x(t,"heading"),o=e.split(`
12
+ `);for(const r of s){const{depth:i,position:{start:c,end:l}}=r,a=Z(i,n),u=o[c.line-1].slice(i,l.column),m="#".repeat(a)+u;o[c.line-1]=m,r.depth=a}return o.join(`
13
+ `)},K=e=>J(e).reverse().sort(v.reverse).reduce((n,t)=>A(n,X(t),t),e),Y=(e,n,t,s,o)=>{try{e=K(e),e=Ue(e,n);const r=q.md(e);return J(e,r).sort(v).reverse().map(i=>{var se,oe;const{url:c,headingDepth:l}=i,[a,...u]=f.basename(c).split("?"),m=a.split(".").pop()??"",T=u.join("?"),C=f.dirname(c),te=f.join(C,a);if(c.startsWith("./")||c.startsWith("../")){let g=t(te);const y=new ie.URLSearchParams(T),D=(se=y.get("region"))==null?void 0:se.split(R);g=(D==null?void 0:D.reduce((E,k)=>Oe(E,k),g))??g;const Qe=y.has("skip"),ze=y.get("heading")??0,Xe=y.has("inline");let{inline:L}=i;if(Xe&&(L=!0),!Qe)if(m==="md"){const E=Fe(t,i),k=o?f.join(o,C):C,Ve=Z(l,Number(ze));g=Y(g,Ve,E,te,k)}else/^(js|ts)x?|svelte$/i.test(m)&&(g=H(g,"code",{extension:m,inline:L}));const P=(oe=y.get("wrap"))==null?void 0:oe.split(R);return g=(P==null?void 0:P.reduce((E,k)=>H(E,k,{extension:m,inline:L}),g))??g,{target:i,content:Re(i,g,o)}}else throw c.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${c}`)}).reduce((i,{target:c,content:l})=>A(i,l,c),e)}catch(r){throw new Error(`Error populating inclusions in file ${s}: ${r}`)}},ee=e=>h.readFileSync(e,"utf-8"),ne=e=>{const n=f.resolve(e),t=f.dirname(n);return{markdown:ee(n),dir:t,path:n}},Ge=(e,n=!0)=>{const{dir:t,path:s,markdown:o}=ne(e),i=Y(o,0,c=>ee(f.resolve(t,ve(c))),s);return n&&h.writeFileSync(s,i),i},He=(e,n=!0)=>{const{path:t,markdown:s}=ne(e),o=K(s);return n&&h.writeFileSync(t,o),o};p.depopulateMarkdownInclusions=He,p.populateMarkdownInclusions=Ge,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@p-buddy/parkdown",
3
3
  "type": "module",
4
4
  "private": false,
5
- "version": "0.0.4",
5
+ "version": "0.0.6",
6
6
  "main": "./dist/index.js",
7
7
  "bin": "./dist/cli.js",
8
8
  "files": [
@@ -37,7 +37,8 @@
37
37
  "build:cli": "vite build --config vite.cli.config.ts",
38
38
  "build": "pnpm run build:lib && pnpm run build:cli",
39
39
  "test": "vitest",
40
- "test:run": "vitest run"
40
+ "test:run": "vitest run",
41
+ "cli": "npx tsx src/cli.ts"
41
42
  },
42
43
  "typings": "./dist/index.d.ts",
43
44
  "exports": {