@p-buddy/parkdown 0.0.13 → 0.0.19

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,16 @@
1
- (function(p,m){typeof exports=="object"&&typeof module<"u"?m(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("ts-dedent")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","ts-dedent"],m):(p=typeof globalThis<"u"?globalThis:p||self,m(p.index={},p.node_fs,p.node_path,p.node_url,p.unified,p.remarkParse,p.unistUtilVisit,p.tsDedent))})(this,function(p,m,d,ue,me,de,ge,M){"use strict";var tn=Object.defineProperty;var sn=(p,m,d)=>m in p?tn(p,m,{enumerable:!0,configurable:!0,writable:!0,value:d}):p[m]=d;var pe=(p,m,d)=>sn(p,typeof m!="symbol"?m+"":m,d);const v=(e,n)=>e.position.start.offset-n.position.start.offset;v.reverse=(e,n)=>v(n,e);const T=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,fe=me.unified().use(de),j={md:e=>fe.parse(e)},x=(e,n)=>{const t=[];return ge.visit(e,(s,o,r)=>{if(s.type!=="root"){if(n&&s.type!==n)return;if(T(s)){const i=((r==null?void 0:r.children.length)??0)-1;t.push({...s,siblingIndex:o,siblingCount:i})}}}),t},he=e=>e.children.length===0,L=(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)},ve=(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)},N=(...e)=>e.join(" "),$e=(...e)=>e.join(`
2
- `),Ce=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},we=e=>Ce(e)[0];class ${constructor(...n){pe(this,"intervals",[]);for(const[t,s]of n)this.push(t,s)}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,l]=n[r];i<=o?o=Math.max(o,l):(t.push([s,o]),s=i,o=l)}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 l=[];for(const[c,a]of o){if(i<=c||r>=a){l.push([c,a]);continue}r>c&&l.push([c,r]),i<a&&l.push([i,a])}o=l}return this.intervals=o}}const B=/,\s*(?![^()]*\))/,F={reserved:{semi:";",slash:"/",question:"?",colon:":",at:"@",equal:"=",and:"&"},unsafe:{quote:'"',angle:"<",unangle:">",hash:"#",percent:"%",curly:"{",uncurly:"}",pipe:"|",back:"\\",carrot:"^",tilde:"~",square:"[",unsquare:"]",tick:"`",line:`
3
- `}},ye="-",U={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(F.unsafe),...Object.entries(F.reserved)]},H=(e,[n,t])=>e.replaceAll(n,t),k=(e,n=ye)=>{const t=U.static.reduce(H,e);return U.url.map(([s,o])=>[n+s+n,o]).reduce(H,t).replaceAll(n," ")},Se=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Ee=["string","number","boolean"],xe=e=>Ee.includes(e),Ne=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,ke=e=>{const n=e.match(Ne);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,s=""]=n;if(!s.trim())return{name:t,parameters:[]};const o=Me(s);return{name:t,parameters:o}},be=(e,n)=>{for(let t=n+1;t<e.length;t++)if(e[t]!==void 0)return!1;return!0},Me=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 l=e.at(o+2)==="'";s=!s,t+=l?"'''":"''",o+=l?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?Te(o):void 0).filter((o,r,i)=>o!==void 0||!be(i,r))},Te=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)},je=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 l;for(;(l=r.exec(o))!==null;){const[,c,a,f]=l,u=f.trim();if(!xe(u))throw new Error(`Unsupported type: ${u}`);i.push({name:c,optional:a==="?",type:u})}return{name:s,parameters:i}},O=e=>{const n=e.map(je).reduce((t,{name:s,parameters:o})=>t.set(s,o),new Map);return t=>{const{name:s,parameters:o}=ke(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:c})=>!c).length,l=i===r.length?i.toString():`${i} - ${r.length}`;throw new Error(`Too many parameters: ${o.length} for method '${s}' (expected: ${l})`)}return r.reduce((i,{name:l,optional:c,type:a},f)=>{const u=o[f];if(u===void 0){if(c)return i;throw new Error(`Missing required parameter: ${l} for method '${s}'`)}switch(a){case"string":i[l]=u;break;case"number":case"boolean":i[l]=JSON.parse(u);break}if(typeof i[l]!==a)throw new Error(`Invalid type: ${l} must be ${a}, got ${typeof i[l]} for method '${s}'`);return i},{name:s})}},G=e=>Object.entries(e).filter(([n])=>n!=="name"&&!isNaN(Number(n))).map(([n,t])=>t),Oe=O(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),C=(e,n=1)=>`
4
- `.repeat(n)+e+`
5
- `.repeat(n),R=(e,n,t)=>`<${n}${t?` ${t}`:""}>${C(e)}</${n}>`,Q=(e,n,t)=>{const s=Oe(n);switch(s.name){case"code":if(!s.lang&&!s.meta&&(t==null?void 0:t.inline)&&!e.includes(`
6
- `))return`\`${e}\``;const r=s.lang??(t==null?void 0:t.extension)??"",i=s.meta?` ${s.meta}`:"";return C(`\`\`\`${r}${i}${C(e)}\`\`\``);case"quote":return t!=null&&t.inline&&!e.includes(`
1
+ (function(u,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("ts-dedent")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","ts-dedent"],d):(u=typeof globalThis<"u"?globalThis:u||self,d(u.index={},u.node_fs,u.node_path,u.node_url,u.unified,u.remarkParse,u.unistUtilVisit,u.tsDedent))})(this,function(u,d,g,K,we,$e,ye,Ce){"use strict";var mt=Object.defineProperty;var dt=(u,d,g)=>d in u?mt(u,d,{enumerable:!0,configurable:!0,writable:!0,value:g}):u[d]=g;var _=(u,d,g)=>dt(u,typeof d!="symbol"?d+"":d,g);const $=(e,t)=>e.position.start.offset-t.position.start.offset;$.reverse=(e,t)=>$(t,e);const U=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,be=we.unified().use($e),B={md:e=>be.parse(e)},A=(e,t)=>{const n=[];return ye.visit(e,(s,r,i)=>{if(s.type!=="root"){if(t&&s.type!==t)return;if(U(s)){const o=((i==null?void 0:i.children.length)??0)-1;n.push({...s,siblingIndex:r,siblingCount:o})}}}),n},Ee=e=>e.children.length===0,Y=(e,t,...n)=>{if(n.length===0)throw new Error("No nodes to replace content from");n.sort($);const s=n.at(0),r=n.at(-1);return e.slice(0,s.position.start.offset)+t+e.slice(r.position.end.offset)},Se=(e,t,n)=>{const s=Math.min(t.position.end.offset,n.position.end.offset),r=Math.max(t.position.start.offset,n.position.start.offset);return e.slice(s,r)},O=(...e)=>e.join(" "),ke=(...e)=>e.join(`
2
+ `),xe=e=>{const t=e.split("?");return t.length>1?[t.slice(0,-1).join("?"),t.at(-1)]:[e,""]},Ne=e=>xe(e)[0],F=/,\s*(?![^()]*\))/,ee={reserved:{semi:";",slash:"/",question:"?",colon:":",at:"@",equal:"=",and:"&"},unsafe:{quote:'"',angle:"<",unangle:">",hash:"#",percent:"%",curly:"{",uncurly:"}",pipe:"|",back:"\\",carrot:"^",tilde:"~",square:"[",unsquare:"]",tick:"`",line:`
3
+ `}},Me="-",te={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(ee.unsafe),...Object.entries(ee.reserved)]},ne=(e,[t,n])=>e.replaceAll(t,n),I=(e,t=Me)=>{const n=te.static.reduce(ne,e);return te.url.map(([s,r])=>[t+s+t,r]).reduce(ne,n).replaceAll(t," ")},Re=["string","number","boolean"],Te=e=>Re.includes(e),je=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,_e=e=>{const t=e.match(je);if(!t)throw new Error(`Invalid invocation: ${e}`);const[,n,s=""]=t;if(!s.trim())return{name:n,parameters:[]};const r=Oe(s);return{name:n,parameters:r}},Ae=(e,t)=>{for(let n=t+1;n<e.length;n++)if(e[n]!==void 0)return!1;return!0},Oe=e=>{const t=[];let n="",s=!1;for(let r=0;r<e.length;r++){const i=e[r],o=i==="'";if(o&&e.at(r+1)==="'"){const l=e.at(r+2)==="'";s=!s,n+=l?"'''":"''",r+=l?2:1}else o?(s=!s,n+=i):i===","&&!s?(t.push(n.trim()),n=""):n+=i}return t.push(n.trim()),t.map(r=>r===""?void 0:r?Ie(r):void 0).filter((r,i,o)=>r!==void 0||!Ae(o,i))},Ie=e=>{const t=e.length>=2&&e[0]==="'"&&e.at(-1)==="'",n=t&&e.length>=4&&e[1]==="'"&&e.at(-2)==="'";return!t||n?e:e.slice(1,-1)},qe=e=>{const t=/^(\w+)(?:\(([^)]*)\))?/,n=e.match(t);if(!n)return{name:e};const[,s,r]=n;if(!r)return{name:s};const i=/(\w+)(\?)?:\s*([^,]+)/g,o=[];let l;for(;(l=i.exec(r))!==null;){const[,c,a,m]=l,p=m.trim();if(!Te(p))throw new Error(`Unsupported type: ${p}`);o.push({name:c,optional:a==="?",type:p})}return{name:s,parameters:o}},M=e=>{const t=e.map(qe).reduce((n,{name:s,parameters:r})=>n.set(s,r),new Map);return n=>{const{name:s,parameters:r}=_e(n);if(!t.has(s))throw new Error(`Unknown method: ${s}`);const i=t.get(s);if(i===void 0)return{name:s};if(r.length>i.length){const o=i.filter(({optional:c})=>!c).length,l=o===i.length?o.toString():`${o} - ${i.length}`;throw new Error(`Too many parameters: ${r.length} for method '${s}' (expected: ${l})`)}return i.reduce((o,{name:l,optional:c,type:a},m)=>{const p=r[m];if(p===void 0){if(c)return o;throw new Error(`Missing required parameter: ${l} for method '${s}'`)}switch(a){case"string":o[l]=p;break;case"number":case"boolean":o[l]=JSON.parse(p);break}if(typeof o[l]!==a)throw new Error(`Invalid type: ${l} must be ${a}, got ${typeof o[l]} for method '${s}'`);return o},{name:s})}},H=e=>Object.entries(e).filter(([t])=>t!=="name"&&!isNaN(Number(t))).map(([t,n])=>n),De=M(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),x=(e,t=1)=>`
4
+ `.repeat(t)+e+`
5
+ `.repeat(t),Q=(e,t,n)=>`<${t}${n?` ${n}`:""}>${x(e)}</${t}>`,se=(e,t,n)=>{const s=De(t);switch(s.name){case"code":if(!s.lang&&!s.meta&&(n==null?void 0:n.inline)&&!e.includes(`
6
+ `))return`\`${e}\``;const i=s.lang??(n==null?void 0:n.extension)??"",o=s.meta?` ${s.meta}`:"";return x(`\`\`\`${i}${o}${x(e)}\`\`\``);case"quote":return n!=null&&n.inline&&!e.includes(`
7
7
 
8
- `)?`> ${e}`:C(R(C(e),"blockquote"));case"dropdown":const l=R(k(s.summary),"summary");return C(R([l,e].join(`
9
- `),"details",s.open?"open":void 0))}},q=e=>{const n=/(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm,t=[];let s;for(;(s=n.exec(e))!==null;){const o=[s.index,s.index+s[0].length],r=Re(s[0]).trim();t.push({range:o,value:r})}return t},Re=e=>e.startsWith("//")?e.slice(2):e.startsWith("/*")?e.slice(2,-2):e.startsWith("<!--")?e.slice(4,-3):e,z=["p↓:","pd:","parkdown:"];O(["splice(delete?: number, insert?: string)"]);const V=(e,n)=>e.range[0]-n.range[0],X=(e,n,t)=>(t??q(e)).filter(({value:s})=>s.split(" ").includes(n)).sort(V),J=e=>({isSpace:e===" ",isNewline:e===`
10
- `||e===void 0}),Z=e=>z.flatMap(n=>X(e,n)).sort((n,t)=>n.range[0]-t.range[0]).reverse().reduce((n,{range:[t,s],value:o})=>{const r=(u,I)=>n.slice(0,u)+n.slice(I),i=J(n[t-1]),l=J(n[s]),c=s===n.length;let a=t;for(;a>0&&n[a-1]!==`
11
- `;)a--;const f=n.slice(a,t).trim()==="";return i.isNewline&&l.isNewline?r(t-(c?1:0),s+1):i.isNewline||f?r(a,s+(l.isSpace||l.isNewline?1:0)):r(t-(i.isSpace?1:0),s)},e);z.map(e=>new RegExp(`^${Se(e)}(\\?[^\\s]+)`));const qe=O(["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)","splice(id: string, deleteCount?: number, insert?: string, space?: string)","remap(id: string, from: string, to?: string, space?: string)"]),w=(e,n,t)=>{const s=X(e,n,t),o=[];for(let r=0;r<s.length-1;r+=2)o.push([s[r],s[r+1]]);return o},Ie=(e,...n)=>{if(n.length===0)return e;const t=q(e),s=([i,l])=>e.slice(i,l),o=new $,r=new $;for(const i of n)for(const[l,c]of w(e,i,t)){o.push(l.range[1],c.range[0]);const[a,...f]=s([l.range[1],c.range[0]]),u=f[f.length-1];r.push(l.range[0],l.range[1]+(a.trim()?0:1)),r.push(c.range[0],c.range[1]+(u.trim()?0:1))}return o.collapse(),r.collapse(),M.dedent(o.subtract(r).map(s).filter(Boolean).join("")).trim()},Ae=(e,...n)=>{if(n.length===0)return e;const t=q(e),s=([i,l])=>e.slice(i,l),o=new $;for(const i of n)for(const[l,c]of w(e,i,t)){const a=s([c.range[1],c.range[1]+1]).at(-1);o.push(l.range[0],a===`
12
- `?c.range[1]+1:c.range[1])}o.collapse();const r=new $([0,e.length]);return r.subtract(o),M.dedent(r.collapse().map(s).filter(Boolean).join("")).trim()},Pe=(e,n,t,s)=>{if(!n)return e;let o="",r=0;for(const[l,c]of w(e,n))o+=e.slice(r,l.range[1]),o+=k(t??l.value,s),r=c.range[0];o+=e.slice(r);const i=new $([0,o.length]);return i.subtract(new $(...w(o,n).flatMap(([l,c])=>[l.range,c.range]))),M.dedent(i.collapse().map(([l,c])=>o.slice(l,c)).filter(Boolean).join("")).trim()},De=(e,n,t,s,o)=>{if(!n)return e;const r=t===void 0?!1:t>=0,i=s?k(s,o):"",l=t??0;return w(e,n).map(c=>c[r?1:0]).sort(V).reverse().reduce((c,{range:a})=>r?c.slice(0,a[1])+i+c.slice(Math.min(a[1]+l,c.length)):c.slice(0,Math.max(a[0]+l,0))+i+c.slice(a[0]),e)},We=(e,n,t,s,o)=>{if(!n)return e;let r="",i=0;const l=[t,s??""].map(c=>k(c,o));console.log(l);for(const[c,a]of w(e,n))r+=e.slice(i,c.range[1]),r+=e.slice(c.range[1],a.range[0]).replaceAll(l[0],l[1]),i=a.range[0];return r+=e.slice(i),r},_e=(e,n,t)=>{if(!n)return Z(e);const s=qe(n);switch(s.name){case"extract":e=Ie(e,s.id,...G(s));break;case"remove":e=Ae(e,s.id,...G(s));break;case"replace":e=Pe(e,s.id,s.with,s.space);break;case"splice":e=De(e,s.id,s.deleteCount,s.insert,s.space);break;case"remap":e=We(e,s.id,s.from,s.to,s.space);break}return t?Z(e):e},Le=["http","./","../"],Be=({url:e})=>Le.some(n=>e.startsWith(n)),Fe=e=>T(e)&&he(e)&&Be(e),K=({url:e},n)=>`[](${n?d.join(n,e):e})`,g={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return N(g._open,g._flag,"BEGIN",g._close)},get end(){return N(g._open,g._flag,"END",g._close)},lengthOf(e){const n=`lines: ${e.split(`
13
- `).length}`,t=`chars: ${e.length}`;return N(g._open,g._flag,"length",n,t,g._close)}},Y=e=>n=>T(n)&&n.value===g[e],Ue=({position:e,url:n,siblingCount:t},s)=>({position:e,url:n,headingDepth:s,inline:t>=1}),He=({position:{start:e},url:n,siblingCount:t},{position:{end:s}},o)=>({position:{start:e,end:s},url:n,headingDepth:o,inline:t>=1}),Ge=(e,n,t)=>(e.inline?N:$e)(K(e,t),g.begin,g.lengthOf(n),n,g.end),Qe=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}},b={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`)},ze=(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 b.closingCommentNotMatchedToOpening(i);const l=o.pop().node;if(o.length>0)continue;t.push({open:l,close:i})}if(o.length>0)throw b.openingCommentNotClosed(o[0].node);return t},Ve=(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(ve(e,i,r.open).trim()!=="")throw b.openingCommentDoesNotFollowLink(r.open);return o.push([i,r])}o.push(i)}throw b.openingCommentDoesNotFollowLink(r.open)}),o.push(...s.reverse()),o.reverse()},ee=(e,n)=>{n??(n=j.md(e));const t=Qe(n),s=x(n,"link").filter(Fe),o=x(n,"html").sort(v),r=o.filter(Y("begin")),i=o.filter(Y("end")),l=ze(r,i);return Ve(e,s,l).map(a=>Array.isArray(a)?He(a[0],a[1].close,t(a[0])):Ue(a,t(a)))},Xe=(e,{url:n})=>t=>e(d.join(d.dirname(n),t)),ne=(...e)=>{const n=e.reduce((t,s)=>t+s,0);return Math.min(Math.max(n,1),6)},Je=(e,n,t)=>{if(n===0)return e;t??(t=j.md(e));const s=x(t,"heading"),o=e.split(`
14
- `);for(const r of s){const{depth:i,position:{start:l,end:c}}=r,a=ne(i,n),f=o[l.line-1].slice(i,c.column),u="#".repeat(a)+f;o[l.line-1]=u,r.depth=a}return o.join(`
15
- `)},te=e=>ee(e).reverse().sort(v.reverse).reduce((n,t)=>L(n,K(t),t),e),se=(e,n,t,s,o)=>{try{e=te(e),e=Je(e,n);const r=j.md(e);return ee(e,r).sort(v).reverse().map(i=>{var ce,ae;const{url:l,headingDepth:c}=i,[a,...f]=d.basename(l).split("?"),u=a.split(".").pop()??"",I=f.join("?"),A=d.dirname(l),le=d.join(A,a);if(l.startsWith("./")||l.startsWith("../")){let h=t(le);const y=new ue.URLSearchParams(I),P=(ce=y.get("region"))==null?void 0:ce.split(B);h=(P==null?void 0:P.reduce((S,E,_,{length:nn})=>_e(S,E,_===nn-1),h))??h;const Ke=y.has("skip"),Ye=y.get("heading")??0,en=y.has("inline");let{inline:D}=i;if(en&&(D=!0),!Ke)if(u==="md"){const S=Xe(t,i),E=o?d.join(o,A):A,_=ne(c,Number(Ye));h=se(h,_,S,le,E)}else/^(js|ts)x?|svelte$/i.test(u)&&(h=Q(h,"code",{extension:u,inline:D}));const W=(ae=y.get("wrap"))==null?void 0:ae.split(B);return h=(W==null?void 0:W.reduce((S,E)=>Q(S,E,{extension:u,inline:D}),h))??h,{target:i,content:Ge(i,h,o)}}else throw l.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${l}`)}).reduce((i,{target:l,content:c})=>L(i,c,l),e)}catch(r){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${r}`)}},oe=e=>m.readFileSync(e,"utf-8"),re=e=>{const n=d.resolve(e),t=d.dirname(n);return{content:oe(n),dir:t,path:n}},ie=(e,n=!0,t)=>{const{dir:s,path:o,content:r}=re(e);t==null||t.unwatch(o),t==null||t.once("change",(c,a)=>{console.log(`Change detected in "${c}". Regenerating "${o}"...`),ie(e,n,t)});const l=se(r,0,c=>{const a=d.resolve(s,we(c));return t==null||t.add(a),oe(a)},o);return n&&m.writeFileSync(o,l),t==null||t.add(o),l},Ze=(e,n=!0,t)=>{const{path:s,content:o}=re(e),r=te(o);return n&&m.writeFileSync(s,r),r};p.depopulateMarkdownInclusions=Ze,p.populateMarkdownInclusions=ie,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
8
+ `)?`> ${e}`:x(Q(x(e),"blockquote"));case"dropdown":const l=Q(I(s.summary),"summary");return x(Q([l,e].join(`
9
+ `),"details",s.open?"open":void 0))}};class v{constructor(...t){_(this,"_intervals",[]);_(this,"collapsed",!0);for(const[n,s]of t)this.push(n,s)}get intervals(){return this._intervals}push(t,n){return n??(n=t+1),this._intervals.push([Math.min(t,n),Math.max(t,n)]),this.collapsed=!1,this}combine(t){for(const[n,s]of t.intervals)this.push(n,s);return this}collapse(t=!1){const{_intervals:n,collapsed:s}=this;if(s&&!t||!n.length)return this;n.sort((l,c)=>l[0]-c[0]);const r=[];let[i,o]=n[0];for(let l=1;l<n.length;l++){const[c,a]=n[l];c<=o?o=Math.max(o,a):(r.push([i,o]),i=c,o=a)}return r.push([i,o]),this._intervals=r,this.collapsed=!0,this}subtract(t){this.collapse(),t.collapse();const{_intervals:n}=this,{_intervals:s}=t;if(!n.length||!s.length)return this;let r=[...n];for(const[i,o]of s){const l=[];for(const[c,a]of r){if(o<=c||i>=a){l.push([c,a]);continue}i>c&&l.push([c,i]),o<a&&l.push([o,a])}r=l}return this._intervals=r,this.collapse(!0),this}test(t,n="head"){const{_intervals:s}=this;switch(n){case"head":return s.some(([r,i])=>t>=r&&t<i);case"tail":return s.some(([r,i])=>t>r&&t<=i);case"both":return s.some(([r,i])=>t>=r&&t<=i);case"none":return s.some(([r,i])=>t>r&&t<i)}}slice(t){this.collapse();const n=[];for(const[s,r]of this._intervals)n.push(t.slice(s,r));return n.filter(Boolean).join("")}offset(t){for(const n of this._intervals)n[0]+=t,n[1]+=t;return this}}const q=e=>{const t=/(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm,n=[];let s;for(;(s=t.exec(e))!==null;){const r=[s.index,s.index+s[0].length],i=Pe(s[0]).trim();n.push({range:r,value:i})}return n},Pe=e=>e.startsWith("//")?e.slice(2):e.startsWith("/*")?e.slice(2,-2):e.startsWith("<!--")?e.slice(4,-3):e,We=["p↓:","pd:","parkdown:"];M(["splice(delete?: number, insert?: string)"]);const re=(e,t)=>e.range[0]-t.range[0],G=(e,t,n)=>(n??q(e)).filter(({value:s})=>s.split(" ").includes(t)).sort(re),ie=e=>({isSpace:e===" ",isNewline:e===`
10
+ `||e===void 0}),oe=e=>We.flatMap(t=>G(e,t)).sort((t,n)=>t.range[0]-n.range[0]).reverse().reduce((t,{range:[n,s],value:r})=>{const i=(b,E)=>t.slice(0,b)+t.slice(E),o=ie(t[n-1]),l=ie(t[s]),c=s===t.length;let a=n;for(;a>0&&t[a-1]!==`
11
+ `;)a--;let m=s;for(;m<t.length&&t[m]!==`
12
+ `;)m++;const p=t.slice(a,n).trim()===""&&t.slice(s,m).trim()==="";return o.isNewline&&l.isNewline?i(n-(c?1:0),s+1):o.isNewline||p?i(a,s+(l.isSpace||l.isNewline?1:0)):i(n-(o.isSpace?1:0),s)},e),Le=M(["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)","remap(id: string, from: string, to?: string, space?: string)","single-line(id: string, includeBoundaries?: boolean)","trim(id: string, inside?: boolean, outside?: boolean)","trim-start(id: string, left?: boolean, right?: boolean)","trim-end(id: string, left?: boolean, right?: boolean)","splice-start(id: string, deleteCount?: number, insert?: string, space?: string)","splice-end(id: string, deleteCount?: number, insert?: string, space?: string)","debug()"]),C=(e,t,n)=>{const s=G(e,t,n),r=[];for(let i=0;i<s.length-1;i+=2)r.push([s[i],s[i+1]]);return r},Ue=(e,t,n)=>new v(...G(e,t,n).map(({range:s})=>s)),D=e=>Ce.dedent(e).trim(),Be=(e,...t)=>{if(t.length===0)return e;const n=q(e),s=new v,r=new v;for(const i of t)for(const[o,l]of C(e,i,n)){s.push(o.range[1],l.range[0]);const[c,...a]=e.slice(o.range[1],l.range[0]),m=a[a.length-1];r.push(o.range[0],o.range[1]+(c.trim()?0:1)),r.push(l.range[0],l.range[1]+(m.trim()?0:1))}return D(s.subtract(r).slice(e))},Fe=(e,...t)=>{if(t.length===0)return e;const n=q(e),s=([i,o])=>e.slice(i,o),r=new v;for(const i of t)for(const[o,l]of C(e,i,n)){const c=s([l.range[1],l.range[1]+1]).at(-1);r.push(o.range[0],c===`
13
+ `?l.range[1]+1:l.range[1])}return D(new v([0,e.length]).subtract(r).slice(e))},He=(e,t,n,s)=>{if(!t)return e;let r="",i=0;for(const[o,l]of C(e,t))r+=e.slice(i,o.range[1]),r+=I(n??o.value,s),i=l.range[0];return r+=e.slice(i),D(new v([0,r.length]).subtract(Ue(r,t)).slice(r))},Qe=(e,t,n,s,r,i)=>{if(!t)return e;const o=!(s===void 0||s<0),l=r?I(r,i):"",c=s??0,a=q(e),m=new v(...a.map(({range:p})=>p));return C(e,t,a).map(p=>p[n==="start"?0:1]).sort(re).reverse().reduce((p,{range:b})=>{const E=new v,W=o?1:-1;let T=Math.abs(c),h=o?b[1]:b[0];for(;T>0&&h<p.length&&h>=0;)m.test(h)||(E.push(h),T--),h+=W;return h=Math.min(Math.max(0,h),p.length),p=p.slice(0,h)+l+p.slice(h),new v([0,p.length]).subtract(E.offset(o?0:l.length)).slice(p)},e)},Ge=(e,t,n,s,r)=>{if(!t)return e;let i="",o=0;[n,s]=[n,s??""].map(l=>I(l,r));for(const[l,c]of C(e,t))i+=e.slice(o,l.range[1]),i+=e.slice(l.range[1],c.range[0]).replaceAll(n,s),o=c.range[0];return i+=e.slice(o),i},ze=(e,t)=>{if(!t)return e;let n="",s=0;for(const[r,i]of C(e,t))n+=e.slice(s,r.range[1]),n+=e.slice(r.range[1],i.range[0]).replaceAll(/[\s\n]+/g," "),s=i.range[0];return n+=e.slice(s),n},le=(e,t,n)=>{var i,o,l,c;if(!t)return e;const s=a=>/\s/.test(e[a]),r=new v;for(const[a,m]of C(e,t)){if((i=n.start)!=null&&i.left){let p=a.range[0]-1;for(;s(p);)r.push(p--)}if((o=n.start)!=null&&o.right){let p=a.range[1];for(;s(p);)r.push(p++)}if((l=n.end)!=null&&l.left){let p=m.range[0]-1;for(;s(p);)r.push(p--)}if((c=n.end)!=null&&c.right){let p=m.range[1];for(;s(p);)r.push(p++)}}return new v([0,e.length]).subtract(r).slice(e)},Ve=(e,t,n)=>{if(!t)return oe(e);const s=Le(t);switch(s.name){case"extract":e=Be(e,s.id,...H(s));break;case"remove":e=Fe(e,s.id,...H(s));break;case"replace":e=He(e,s.id,s.with,s.space);break;case"splice-start":case"splice-end":{const{deleteCount:r,insert:i,space:o,id:l}=s,c=s.name==="splice-start"?"start":"end";e=Qe(e,l,c,r,i,o);break}case"trim-start":case"trim-end":{const{left:r,right:i,id:o}=s,l=s.name==="trim-start"?"start":"end";e=le(e,o,{[l]:{left:r??!0,right:i??!0}});break}case"trim":{const{inside:r,outside:i,id:o}=s;e=le(e,o,{start:{left:i??!0,right:r??!0},end:{left:r??!0,right:i??!0}});break}case"single-line":e=ze(e,s.id);break;case"remap":e=Ge(e,s.id,s.from,s.to,s.space);break}return e=n&&s.name!=="debug"?oe(e):e,n?D(e):e},Xe=["recipe(id: string)"],Je=["recipe(id: string, 0?: string, 1?: string, 2?: string)"],ce={register:M(Xe),apply:M(Je)},R=class R{constructor(){_(this,"recipes",new Map)}tryRegister(t){const n=R.Entries(t);for(let s=0;s<n.length;s++){const[r,i]=n[s];if(r!=="register")continue;const o=ce.register(i);switch(o.name){case"recipe":const l=n.slice(s+1).filter(([c])=>c!=="register").map(([c,a])=>`${c}=${a}`).join("&");this.recipes.set(o.id,l);break;default:throw new Error(`Unknown registration: ${o.name}`)}}}apply(t){const n=R.Entries(t),s=[];for(let r=0;r<n.length;r++){const[i,o]=n[r];if(i!=="apply"){s.push(`${i}=${o}`);continue}const l=ce.apply(o);switch(l.name){case"recipe":s.push(this.recipes.get(l.id)),s.push(...H(l).map(c=>this.recipes.get(c)));break;default:throw new Error(`Unknown registration: ${l.name}`)}}return s.join("&")}};_(R,"Entries",t=>Array.from(new URLSearchParams(t).entries()));let z=R;const Ze=["http","./","../","?"],Ke=({url:e})=>Ze.some(t=>e.startsWith(t)),Ye=e=>U(e)&&Ee(e)&&Ke(e),ae=({url:e},t)=>`[](${t?g.join(t,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return O(f._open,f._flag,"BEGIN",f._close)},get end(){return O(f._open,f._flag,"END",f._close)},lengthOf(e){const t=`lines: ${e.split(`
14
+ `).length}`,n=`chars: ${e.length}`;return O(f._open,f._flag,"length",t,n,f._close)}},pe=e=>t=>U(t)&&t.value===f[e],et=({position:e,url:t,siblingCount:n},s)=>({position:e,url:t,headingDepth:s,inline:n>=1}),tt=({position:{start:e},url:t,siblingCount:n},{position:{end:s}},r)=>({position:{start:e,end:s},url:t,headingDepth:r,inline:n>=1}),nt=(e,t,n)=>(e.inline?O:ke)(ae(e,n),f.begin,f.lengthOf(t),t,f.end),st=e=>{const t=A(e,"heading").reduce((n,{position:s,depth:r})=>n.set(s.start.line,r),new Map);return n=>{for(let s=n.position.start.line;s>=1;s--){const r=t.get(s);if(r)return r}return 0}},P={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`)},rt=(e,t)=>{const n=[],s=[...e.map(i=>({node:i,type:"open"})),...t.map(i=>({node:i,type:"close"}))].sort((i,o)=>$(i.node,o.node)),r=[];for(const i of s)if(i.type==="open")r.push(i);else{const o=i.node;if(r.length===0)throw P.closingCommentNotMatchedToOpening(o);const l=r.pop().node;if(r.length>0)continue;n.push({open:l,close:o})}if(r.length>0)throw P.openingCommentNotClosed(r[0].node);return n},it=(e,t,n)=>{const s=[...t].sort($),r=[];return[...n].sort((i,o)=>$.reverse(i.open,o.open)).forEach(i=>{for(;s.length>0;){const o=s.pop();if(o.position.start.offset<i.open.position.start.offset){if(Se(e,o,i.open).trim()!=="")throw P.openingCommentDoesNotFollowLink(i.open);return r.push([o,i])}r.push(o)}throw P.openingCommentDoesNotFollowLink(i.open)}),r.push(...s.reverse()),r.reverse()},ue=(e,t)=>{t??(t=B.md(e));const n=st(t),s=A(t,"link").filter(Ye),r=A(t,"html").sort($),i=r.filter(pe("begin")),o=r.filter(pe("end")),l=rt(i,o);return it(e,s,l).map(a=>Array.isArray(a)?tt(a[0],a[1].close,n(a[0])):et(a,n(a)))},ot=(e,{url:t})=>n=>e(g.join(g.dirname(t),n)),me=(...e)=>{const t=e.reduce((n,s)=>n+s,0);return Math.min(Math.max(t,1),6)},lt=(e,t,n)=>{if(t===0)return e;n??(n=B.md(e));const s=A(n,"heading"),r=e.split(`
15
+ `);for(const i of s){const{depth:o,position:{start:l,end:c}}=i,a=me(o,t),m=r[l.line-1].slice(o,c.column),p="#".repeat(a)+m;r[l.line-1]=p,i.depth=a}return r.join(`
16
+ `)},de=e=>ue(e).reverse().sort($.reverse).reduce((t,n)=>Y(t,ae(n),n),e),ge=(e,t,n,s,r)=>{try{e=de(e),e=lt(e,t);const i=B.md(e),o=new z;return ue(e,i).sort($).reverse().map(l=>{var E,W,T;const{url:c,headingDepth:a}=l,[m,...p]=g.basename(c).split("?"),b=o.apply(p.join("?"));if(c.startsWith("?")){const S=(E=new K.URLSearchParams(b).get("register"))==null?void 0:E.split(F);S==null||S.forEach(L=>o.tryRegister(L))}else if(c.startsWith("./")||c.startsWith("../")){const h=m.split(".").pop()??"",S=g.dirname(c),L=g.join(S,m);let w=n(L);const j=new K.URLSearchParams(b),V=(W=(k=>{const y=Array.from(j.entries()).filter(([N])=>N===k).map(([N,Z])=>Z);return y.length>=1?y.join(","):void 0})("region"))==null?void 0:W.split(F);w=(V==null?void 0:V.reduce((k,y,N,{length:Z})=>Ve(k,y,N===Z-1),w))??w;const at=j.has("skip"),pt=j.get("heading")??0,ut=j.has("inline");let{inline:X}=l;if(ut&&(X=!0),!at)if(h==="md"){const k=ot(n,l),y=r?g.join(r,S):S,N=me(a,Number(pt));w=ge(w,N,k,L,y)}else/^(js|ts)x?|svelte$/i.test(h)&&(w=se(w,"code",{extension:h,inline:X}));const J=(T=j.get("wrap"))==null?void 0:T.split(F);return w=(J==null?void 0:J.reduce((k,y)=>se(k,y,{extension:h,inline:X}),w))??w,{target:l,content:nt(l,w,r)}}else throw c.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${c}`)}).filter(Boolean).reduce((l,{target:c,content:a})=>Y(l,a,c),e)}catch(i){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${i}`)}},he=e=>d.readFileSync(e,"utf-8"),fe=e=>{const t=g.resolve(e),n=g.dirname(t);return{content:he(t),dir:n,path:t}},ve=(e,t=!0,n)=>{const{dir:s,path:r,content:i}=fe(e);n==null||n.unwatch(r),n==null||n.once("change",(c,a)=>{console.log(`Change detected in "${c}". Regenerating "${r}"...`),ve(e,t,n)});const l=ge(i,0,c=>{const a=g.resolve(s,Ne(c));return n==null||n.add(a),he(a)},r);return t&&d.writeFileSync(r,l),n==null||n.add(r),l},ct=(e,t=!0,n)=>{const{path:s,content:r}=fe(e),i=de(r);return t&&d.writeFileSync(s,i),i};u.depopulateMarkdownInclusions=ct,u.populateMarkdownInclusions=ve,Object.defineProperty(u,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.13",
5
+ "version": "0.0.19",
6
6
  "main": "./dist/index.js",
7
7
  "bin": "./dist/cli.js",
8
8
  "files": [
@@ -36,7 +36,9 @@
36
36
  "build": "pnpm run build:lib && pnpm run build:cli",
37
37
  "test": "vitest",
38
38
  "test:run": "vitest run",
39
- "cli": "npx tsx src/cli.ts"
39
+ "cli": "npx tsx src/cli.ts",
40
+ "increment": "npm version patch",
41
+ "publish:commit": "git add -u && git commit -m 'chore: regenerate docs and update version'"
40
42
  },
41
43
  "typings": "./dist/index.d.ts",
42
44
  "exports": {