@p-buddy/parkdown 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command as t } from "@commander-js/extra-typings";
3
3
  import { populateMarkdownInclusions as l, depopulateMarkdownInclusions as r } from "@p-buddy/parkdown";
4
- const f = "0.0.11", p = new t().version(f).option("--nw, --no-write", "Do NOT write result to file (defaults to false)", !1).option("--ni, --no-inclusions", "Do NOT process file inclusions (defaults to false)", !1).option("-d, --depopulate", "Remove populated inclusions from the file", !1).option("-f, --file <flag>", "The file(s) to process", (s, o) => (o.push(s), o), new Array()).parse(), { inclusions: c, depopulate: u, file: n, write: i } = p.opts();
4
+ const f = "0.0.12", p = new t().version(f).option("--nw, --no-write", "Do NOT write result to file (defaults to false)", !1).option("--ni, --no-inclusions", "Do NOT process file inclusions (defaults to false)", !1).option("-d, --depopulate", "Remove populated inclusions from the file", !1).option("-f, --file <flag>", "The file(s) to process", (s, o) => (o.push(s), o), new Array()).parse(), { inclusions: c, depopulate: u, file: n, write: i } = p.opts();
5
5
  n.length === 0 && n.push("README.md");
6
6
  const a = [
7
7
  [l, !c],
package/dist/index.js CHANGED
@@ -1,31 +1,31 @@
1
1
  var fe = Object.defineProperty;
2
2
  var de = (e, t, n) => t in e ? fe(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var B = (e, t, n) => de(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { writeFileSync as Z, readFileSync as ve } from "node:fs";
5
- import { basename as we, dirname as O, join as y, resolve as K } from "node:path";
3
+ var F = (e, t, n) => de(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { writeFileSync as K, readFileSync as we } from "node:fs";
5
+ import { basename as ve, dirname as A, join as y, resolve as Y } from "node:path";
6
6
  import { URLSearchParams as $e } from "node:url";
7
7
  import { unified as Ce } from "unified";
8
8
  import Ee from "remark-parse";
9
9
  import { visit as ye } from "unist-util-visit";
10
- import { dedent as R } from "ts-dedent";
11
- const g = (e, t) => e.position.start.offset - t.position.start.offset;
12
- g.reverse = (e, t) => g(t, e);
13
- const j = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, Se = Ce().use(Ee), A = {
10
+ import { dedent as j } from "ts-dedent";
11
+ const h = (e, t) => e.position.start.offset - t.position.start.offset;
12
+ h.reverse = (e, t) => h(t, e);
13
+ const D = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, Se = Ce().use(Ee), I = {
14
14
  md: (e) => Se.parse(e)
15
15
  }, S = (e, t) => {
16
16
  const n = [];
17
17
  return ye(e, (s, o, r) => {
18
18
  if (s.type !== "root") {
19
19
  if (t && s.type !== t) return;
20
- if (j(s)) {
20
+ if (D(s)) {
21
21
  const i = ((r == null ? void 0 : r.children.length) ?? 0) - 1;
22
22
  n.push({ ...s, siblingIndex: o, siblingCount: i });
23
23
  }
24
24
  }
25
25
  }), n;
26
- }, xe = (e) => e.children.length === 0, Y = (e, t, ...n) => {
26
+ }, xe = (e) => e.children.length === 0, ee = (e, t, ...n) => {
27
27
  if (n.length === 0) throw new Error("No nodes to replace content from");
28
- n.sort(g);
28
+ n.sort(h);
29
29
  const s = n.at(0), o = n.at(-1);
30
30
  return e.slice(0, s.position.start.offset) + t + e.slice(o.position.end.offset);
31
31
  }, Ne = (e, t, n) => {
@@ -38,7 +38,7 @@ const j = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
38
38
  }, Me = (e) => ke(e)[0];
39
39
  class d {
40
40
  constructor(...t) {
41
- B(this, "intervals", []);
41
+ F(this, "intervals", []);
42
42
  for (const [n, s] of t)
43
43
  this.push(n, s);
44
44
  }
@@ -78,7 +78,7 @@ class d {
78
78
  return this.intervals = o;
79
79
  }
80
80
  }
81
- const F = /,\s*(?![^()]*\))/, U = {
81
+ const U = /,\s*(?![^()]*\))/, H = {
82
82
  reserved: {
83
83
  semi: ";",
84
84
  slash: "/",
@@ -114,14 +114,14 @@ const F = /,\s*(?![^()]*\))/, U = {
114
114
  [/p↓:\s+/g, ""]
115
115
  ],
116
116
  url: [
117
- ...Object.entries(U.unsafe),
118
- ...Object.entries(U.reserved)
117
+ ...Object.entries(H.unsafe),
118
+ ...Object.entries(H.reserved)
119
119
  ]
120
- }, H = (e, [t, n]) => e.replaceAll(t, n), D = (e, t = _e) => {
121
- const n = G.static.reduce(H, e);
122
- return G.url.map(([s, o]) => [t + s + t, o]).reduce(H, n).replaceAll(t, " ");
123
- }, Te = (e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), Oe = ["string", "number", "boolean"], Re = (e) => Oe.includes(e), je = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, Ae = (e) => {
124
- const t = e.match(je);
120
+ }, Q = (e, [t, n]) => e.replaceAll(t, n), N = (e, t = _e) => {
121
+ const n = G.static.reduce(Q, e);
122
+ return G.url.map(([s, o]) => [t + s + t, o]).reduce(Q, n).replaceAll(t, " ");
123
+ }, Oe = (e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), Re = ["string", "number", "boolean"], Te = (e) => Re.includes(e), Ae = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, je = (e) => {
124
+ const t = e.match(Ae);
125
125
  if (!t) throw new Error(`Invalid invocation: ${e}`);
126
126
  const [, n, s = ""] = t;
127
127
  if (!s.trim()) return { name: n, parameters: [] };
@@ -154,17 +154,17 @@ const F = /,\s*(?![^()]*\))/, U = {
154
154
  let c;
155
155
  for (; (c = r.exec(o)) !== null; ) {
156
156
  const [, l, a, u] = c, p = u.trim();
157
- if (!Re(p)) throw new Error(`Unsupported type: ${p}`);
157
+ if (!Te(p)) throw new Error(`Unsupported type: ${p}`);
158
158
  i.push({ name: l, optional: a === "?", type: p });
159
159
  }
160
160
  return { name: s, parameters: i };
161
- }, I = (e) => {
161
+ }, P = (e) => {
162
162
  const t = e.map(We).reduce(
163
163
  (n, { name: s, parameters: o }) => n.set(s, o),
164
164
  /* @__PURE__ */ new Map()
165
165
  );
166
166
  return (n) => {
167
- const { name: s, parameters: o } = Ae(n);
167
+ const { name: s, parameters: o } = je(n);
168
168
  if (!t.has(s))
169
169
  throw new Error(`Unknown method: ${s}`);
170
170
  const r = t.get(s);
@@ -194,7 +194,7 @@ const F = /,\s*(?![^()]*\))/, U = {
194
194
  return i;
195
195
  }, { name: s });
196
196
  };
197
- }, Q = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Le = [
197
+ }, z = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Le = [
198
198
  /**
199
199
  * Wraps the content in a markdown-formatted code block.
200
200
  * @param lang The language of the code block (defaults to the file extension).
@@ -224,9 +224,9 @@ const F = /,\s*(?![^()]*\))/, U = {
224
224
  * @example [](<url>?wrap=dropdown(hello_world,,_))
225
225
  */
226
226
  "dropdown(summary: string, open?: boolean, space?: string)"
227
- ], qe = I(Le), f = (e, t = 1) => `
227
+ ], qe = P(Le), f = (e, t = 1) => `
228
228
  `.repeat(t) + e + `
229
- `.repeat(t), T = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${f(e)}</${t}>`, z = (e, t, n) => {
229
+ `.repeat(t), T = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${f(e)}</${t}>`, X = (e, t, n) => {
230
230
  const s = qe(t);
231
231
  switch (s.name) {
232
232
  case "code":
@@ -239,11 +239,11 @@ const F = /,\s*(?![^()]*\))/, U = {
239
239
 
240
240
  `) ? `> ${e}` : f(T(f(e), "blockquote"));
241
241
  case "dropdown":
242
- const c = T(D(s.summary), "summary");
242
+ const c = T(N(s.summary), "summary");
243
243
  return f(T([c, e].join(`
244
244
  `), "details", s.open ? "open" : void 0));
245
245
  }
246
- }, P = (e) => {
246
+ }, W = (e) => {
247
247
  const t = /(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm, n = [];
248
248
  let s;
249
249
  for (; (s = t.exec(e)) !== null; ) {
@@ -251,23 +251,27 @@ const F = /,\s*(?![^()]*\))/, U = {
251
251
  n.push({ range: o, value: r });
252
252
  }
253
253
  return n;
254
- }, Be = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, ee = [
254
+ }, Be = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, te = [
255
255
  "p↓:",
256
256
  "pd:",
257
257
  "parkdown:"
258
258
  ], Fe = [
259
259
  "splice(delete?: number, insert?: string)"
260
260
  ];
261
- I(Fe);
262
- const te = (e, t) => e.range[0] - t.range[0], ne = (e, t, n) => (n ?? P(e)).filter(({ value: s }) => s.includes(t)).sort(te), X = (e) => ({
261
+ P(Fe);
262
+ const ne = (e, t) => e.range[0] - t.range[0], se = (e, t, n) => (n ?? W(e)).filter(({ value: s }) => s.includes(t)).sort(ne), V = (e) => ({
263
263
  isSpace: e === " ",
264
264
  isNewline: e === `
265
265
  ` || e === void 0
266
- }), V = (e) => ee.flatMap((t) => ne(e, t)).sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, s], value: o }) => {
267
- const r = (a, u) => t.slice(0, a) + t.slice(u), i = X(t[n - 1]), c = X(t[s]), l = s === t.length;
268
- return i.isNewline && c.isNewline ? r(n - (l ? 1 : 0), s + 1) : i.isNewline ? r(n, s + (c.isSpace ? 1 : 0)) : r(n - (i.isSpace ? 1 : 0), s);
266
+ }), J = (e) => te.flatMap((t) => se(e, t)).sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, s], value: o }) => {
267
+ const r = (p, b) => t.slice(0, p) + t.slice(b), i = V(t[n - 1]), c = V(t[s]), l = s === t.length;
268
+ let a = n;
269
+ for (; a > 0 && t[a - 1] !== `
270
+ `; ) a--;
271
+ const u = t.slice(a, n).trim() === "";
272
+ return i.isNewline && c.isNewline ? r(n - (l ? 1 : 0), s + 1) : i.isNewline || u ? r(a, s + (c.isSpace || c.isNewline ? 1 : 0)) : r(n - (i.isSpace ? 1 : 0), s);
269
273
  }, e);
270
- ee.map((e) => new RegExp(`^${Te(e)}(\\?[^\\s]+)`));
274
+ te.map((e) => new RegExp(`^${Oe(e)}(\\?[^\\s]+)`));
271
275
  const Ue = [
272
276
  /**
273
277
  * Extract regions from the retrieved content between comments that INCLUDE the specified ids.
@@ -327,65 +331,73 @@ const Ue = [
327
331
  * @example [](<url>?region=remap(specifier,hello_world,hello_universe,_)
328
332
  */
329
333
  "remap(id: string, from: string, to: string, space?: string)"
330
- ], Ge = I(Ue), C = (e, t, n) => {
331
- const s = ne(e, t, n), o = [];
334
+ ], He = P(Ue), w = (e, t, n) => {
335
+ const s = se(e, t, n), o = [];
332
336
  for (let r = 0; r < s.length - 1; r += 2)
333
337
  o.push([s[r], s[r + 1]]);
334
338
  return o;
335
- }, He = (e, ...t) => {
339
+ }, Ge = (e, ...t) => {
336
340
  if (t.length === 0) return e;
337
- const n = P(e), s = ([i, c]) => e.slice(i, c), o = new d(), r = new d();
341
+ const n = W(e), s = ([i, c]) => e.slice(i, c), o = new d(), r = new d();
338
342
  for (const i of t)
339
- for (const [c, l] of C(e, i, n)) {
343
+ for (const [c, l] of w(e, i, n)) {
340
344
  o.push(c.range[1], l.range[0]);
341
345
  const [a, ...u] = s([c.range[1], l.range[0]]), p = u[u.length - 1];
342
346
  r.push(c.range[0], c.range[1] + (a.trim() ? 0 : 1)), r.push(l.range[0], l.range[1] + (p.trim() ? 0 : 1));
343
347
  }
344
- return o.collapse(), r.collapse(), R(
348
+ return o.collapse(), r.collapse(), j(
345
349
  o.subtract(r).map(s).filter(Boolean).join("")
346
350
  ).trim();
347
351
  }, Qe = (e, ...t) => {
348
352
  if (t.length === 0) return e;
349
- const n = P(e), s = ([i, c]) => e.slice(i, c), o = new d();
353
+ const n = W(e), s = ([i, c]) => e.slice(i, c), o = new d();
350
354
  for (const i of t)
351
- for (const [c, l] of C(e, i, n)) {
355
+ for (const [c, l] of w(e, i, n)) {
352
356
  const a = s([l.range[1], l.range[1] + 1]).at(-1);
353
357
  o.push(c.range[0], a === `
354
358
  ` ? l.range[1] + 1 : l.range[1]);
355
359
  }
356
360
  o.collapse();
357
361
  const r = new d([0, e.length]);
358
- return r.subtract(o), R(
362
+ return r.subtract(o), j(
359
363
  r.collapse().map(s).filter(Boolean).join("")
360
364
  ).trim();
361
365
  }, ze = (e, t, n, s) => {
362
366
  if (!t) return e;
363
367
  let o = "", r = 0;
364
- for (const [c, l] of C(e, t))
365
- o += e.slice(r, c.range[1]), o += D(n ?? c.value, s), r = l.range[0];
368
+ for (const [c, l] of w(e, t))
369
+ o += e.slice(r, c.range[1]), o += N(n ?? c.value, s), r = l.range[0];
366
370
  o += e.slice(r);
367
371
  const i = new d([0, o.length]);
368
372
  return i.subtract(new d(
369
- ...C(o, t).flatMap(([c, l]) => [c.range, l.range])
370
- )), R(
373
+ ...w(o, t).flatMap(([c, l]) => [c.range, l.range])
374
+ )), j(
371
375
  i.collapse().map(([c, l]) => o.slice(c, l)).filter(Boolean).join("")
372
376
  ).trim();
373
377
  }, Xe = (e, t, n, s, o) => {
374
378
  if (!t) return e;
375
- const r = n === void 0 ? !1 : n >= 0, i = s ? D(s, o) : "", c = n ?? 0;
376
- return C(e, t).map((l) => l[r ? 1 : 0]).sort(te).reverse().reduce(
379
+ const r = n === void 0 ? !1 : n >= 0, i = s ? N(s, o) : "", c = n ?? 0;
380
+ return w(e, t).map((l) => l[r ? 1 : 0]).sort(ne).reverse().reduce(
377
381
  (l, { range: a }) => r ? l.slice(0, a[1]) + i + l.slice(Math.min(a[1] + c, l.length)) : l.slice(0, Math.max(a[0] + c, 0)) + i + l.slice(a[0]),
378
382
  e
379
383
  );
380
- }, Ve = (e, t, n) => {
381
- if (!t) return V(e);
382
- const s = Ge(t);
384
+ }, Ve = (e, t, n, s, o) => {
385
+ if (!t) return e;
386
+ let r = "", i = 0;
387
+ const c = [n, s].map((l) => N(l, o));
388
+ console.log(c);
389
+ for (const [l, a] of w(e, t))
390
+ r += e.slice(i, l.range[1]), r += e.slice(l.range[1], a.range[0]).replaceAll(c[0], c[1]), i = a.range[0];
391
+ return r += e.slice(i), r;
392
+ }, Je = (e, t, n) => {
393
+ if (!t) return J(e);
394
+ const s = He(t);
383
395
  switch (s.name) {
384
396
  case "extract":
385
- e = He(e, s.id, ...Q(s));
397
+ e = Ge(e, s.id, ...z(s));
386
398
  break;
387
399
  case "remove":
388
- e = Qe(e, s.id, ...Q(s));
400
+ e = Qe(e, s.id, ...z(s));
389
401
  break;
390
402
  case "replace":
391
403
  e = ze(e, s.id, s.with, s.space);
@@ -393,9 +405,12 @@ const Ue = [
393
405
  case "splice":
394
406
  e = Xe(e, s.id, s.deleteCount, s.insert, s.space);
395
407
  break;
408
+ case "remap":
409
+ e = Ve(e, s.id, s.from, s.to, s.space);
410
+ break;
396
411
  }
397
- return n ? V(e) : e;
398
- }, Je = ["http", "./", "../"], Ze = ({ url: e }) => Je.some((t) => e.startsWith(t)), Ke = (e) => j(e) && xe(e) && Ze(e), se = ({ url: e }, t) => `[](${t ? y(t, e) : e})`, m = {
412
+ return n ? J(e) : e;
413
+ }, Ze = ["http", "./", "../"], Ke = ({ url: e }) => Ze.some((t) => e.startsWith(t)), Ye = (e) => D(e) && xe(e) && Ke(e), oe = ({ url: e }, t) => `[](${t ? y(t, e) : e})`, m = {
399
414
  _open: "<!--",
400
415
  _close: "-->",
401
416
  _flag: "p↓",
@@ -410,13 +425,13 @@ const Ue = [
410
425
  `).length}`, n = `chars: ${e.length}`;
411
426
  return E(m._open, m._flag, "length", t, n, m._close);
412
427
  }
413
- }, J = (e) => (t) => j(t) && t.value === m[e], Ye = ({ position: e, url: t, siblingCount: n }, s) => ({ position: e, url: t, headingDepth: s, inline: n >= 1 }), et = ({ position: { start: e }, url: t, siblingCount: n }, { position: { end: s } }, o) => ({ position: { start: e, end: s }, url: t, headingDepth: o, inline: n >= 1 }), tt = (e, t, n) => (e.inline ? E : be)(
414
- se(e, n),
428
+ }, Z = (e) => (t) => D(t) && t.value === m[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 } }, o) => ({ position: { start: e, end: s }, url: t, headingDepth: o, inline: n >= 1 }), nt = (e, t, n) => (e.inline ? E : be)(
429
+ oe(e, n),
415
430
  m.begin,
416
431
  m.lengthOf(t),
417
432
  t,
418
433
  m.end
419
- ), nt = (e) => {
434
+ ), st = (e) => {
420
435
  const t = S(e, "heading").reduce((n, { position: s, depth: o }) => n.set(s.start.line, o), /* @__PURE__ */ new Map());
421
436
  return (n) => {
422
437
  for (let s = n.position.start.line; s >= 1; s--) {
@@ -429,11 +444,11 @@ const Ue = [
429
444
  openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
430
445
  closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
431
446
  openingCommentNotClosed: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)
432
- }, st = (e, t) => {
447
+ }, ot = (e, t) => {
433
448
  const n = [], s = [
434
449
  ...e.map((r) => ({ node: r, type: "open" })),
435
450
  ...t.map((r) => ({ node: r, type: "close" }))
436
- ].sort((r, i) => g(r.node, i.node)), o = [];
451
+ ].sort((r, i) => h(r.node, i.node)), o = [];
437
452
  for (const r of s)
438
453
  if (r.type === "open") o.push(r);
439
454
  else {
@@ -447,9 +462,9 @@ const Ue = [
447
462
  if (o.length > 0)
448
463
  throw x.openingCommentNotClosed(o[0].node);
449
464
  return n;
450
- }, ot = (e, t, n) => {
451
- const s = [...t].sort(g), o = [];
452
- return [...n].sort((r, i) => g.reverse(r.open, i.open)).forEach((r) => {
465
+ }, rt = (e, t, n) => {
466
+ const s = [...t].sort(h), o = [];
467
+ return [...n].sort((r, i) => h.reverse(r.open, i.open)).forEach((r) => {
453
468
  for (; s.length > 0; ) {
454
469
  const i = s.pop();
455
470
  if (i.position.start.offset < r.open.position.start.offset) {
@@ -461,77 +476,77 @@ const Ue = [
461
476
  }
462
477
  throw x.openingCommentDoesNotFollowLink(r.open);
463
478
  }), o.push(...s.reverse()), o.reverse();
464
- }, oe = (e, t) => {
465
- t ?? (t = A.md(e));
466
- const n = nt(t), s = S(t, "link").filter(Ke), o = S(t, "html").sort(g), r = o.filter(J("begin")), i = o.filter(J("end")), c = st(r, i);
467
- return ot(e, s, c).map((a) => Array.isArray(a) ? et(a[0], a[1].close, n(a[0])) : Ye(a, n(a)));
468
- }, rt = (e, { url: t }) => (n) => e(y(O(t), n)), re = (...e) => {
479
+ }, re = (e, t) => {
480
+ t ?? (t = I.md(e));
481
+ const n = st(t), s = S(t, "link").filter(Ye), o = S(t, "html").sort(h), r = o.filter(Z("begin")), i = o.filter(Z("end")), c = ot(r, i);
482
+ return rt(e, s, c).map((a) => Array.isArray(a) ? tt(a[0], a[1].close, n(a[0])) : et(a, n(a)));
483
+ }, it = (e, { url: t }) => (n) => e(y(A(t), n)), ie = (...e) => {
469
484
  const t = e.reduce((n, s) => n + s, 0);
470
485
  return Math.min(Math.max(t, 1), 6);
471
- }, it = (e, t, n) => {
486
+ }, ct = (e, t, n) => {
472
487
  if (t === 0) return e;
473
- n ?? (n = A.md(e));
488
+ n ?? (n = I.md(e));
474
489
  const s = S(n, "heading"), o = e.split(`
475
490
  `);
476
491
  for (const r of s) {
477
- const { depth: i, position: { start: c, end: l } } = r, a = re(i, t), u = o[c.line - 1].slice(i, l.column), p = "#".repeat(a) + u;
492
+ const { depth: i, position: { start: c, end: l } } = r, a = ie(i, t), u = o[c.line - 1].slice(i, l.column), p = "#".repeat(a) + u;
478
493
  o[c.line - 1] = p, r.depth = a;
479
494
  }
480
495
  return o.join(`
481
496
  `);
482
- }, ie = (e) => oe(e).reverse().sort(g.reverse).reduce((t, n) => Y(t, se(n), n), e), ce = (e, t, n, s, o) => {
497
+ }, ce = (e) => re(e).reverse().sort(h.reverse).reduce((t, n) => ee(t, oe(n), n), e), le = (e, t, n, s, o) => {
483
498
  try {
484
- e = ie(e), e = it(e, t);
485
- const r = A.md(e);
486
- return oe(e, r).sort(g).reverse().map((i) => {
487
- var L, q;
488
- const { url: c, headingDepth: l } = i, [a, ...u] = we(c).split("?"), p = a.split(".").pop() ?? "", pe = u.join("?"), N = O(c), W = y(N, a);
499
+ e = ce(e), e = ct(e, t);
500
+ const r = I.md(e);
501
+ return re(e, r).sort(h).reverse().map((i) => {
502
+ var q, B;
503
+ const { url: c, headingDepth: l } = i, [a, ...u] = ve(c).split("?"), p = a.split(".").pop() ?? "", b = u.join("?"), k = A(c), L = y(k, a);
489
504
  if (c.startsWith("./") || c.startsWith("../")) {
490
- let h = n(W);
491
- const v = new $e(pe), b = (L = v.get("region")) == null ? void 0 : L.split(F);
492
- h = (b == null ? void 0 : b.reduce(
493
- (w, $, _, { length: ge }) => Ve(w, $, _ === ge - 1),
494
- h
495
- )) ?? h;
496
- const me = v.has("skip"), ue = v.get("heading") ?? 0, he = v.has("inline");
497
- let { inline: k } = i;
498
- if (he && (k = !0), !me)
505
+ let g = n(L);
506
+ const v = new $e(b), M = (q = v.get("region")) == null ? void 0 : q.split(U);
507
+ g = (M == null ? void 0 : M.reduce(
508
+ ($, C, R, { length: he }) => Je($, C, R === he - 1),
509
+ g
510
+ )) ?? g;
511
+ const me = v.has("skip"), ue = v.get("heading") ?? 0, ge = v.has("inline");
512
+ let { inline: _ } = i;
513
+ if (ge && (_ = !0), !me)
499
514
  if (p === "md") {
500
- const w = rt(n, i), $ = o ? y(o, N) : N, _ = re(l, Number(ue));
501
- h = ce(
502
- h,
515
+ const $ = it(n, i), C = o ? y(o, k) : k, R = ie(l, Number(ue));
516
+ g = le(
517
+ g,
503
518
  /** p↓: ... */
504
- _,
505
- w,
506
- W,
507
- $
519
+ R,
520
+ $,
521
+ L,
522
+ C
508
523
  /** p↓: ... */
509
524
  );
510
- } else /^(js|ts)x?|svelte$/i.test(p) && (h = z(
511
- h,
525
+ } else /^(js|ts)x?|svelte$/i.test(p) && (g = X(
526
+ g,
512
527
  "code",
513
528
  /** p↓: ... */
514
- { extension: p, inline: k }
529
+ { extension: p, inline: _ }
515
530
  /** p↓: ... */
516
531
  ));
517
- const M = (q = v.get("wrap")) == null ? void 0 : q.split(F);
518
- return h = (M == null ? void 0 : M.reduce((w, $) => z(w, $, { extension: p, inline: k }), h)) ?? h, { target: i, content: tt(i, h, o) };
532
+ const O = (B = v.get("wrap")) == null ? void 0 : B.split(U);
533
+ return g = (O == null ? void 0 : O.reduce(($, C) => X($, C, { extension: p, inline: _ }), g)) ?? g, { target: i, content: nt(i, g, o) };
519
534
  } else throw c.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${c}`);
520
- }).reduce((i, { target: c, content: l }) => Y(i, l, c), e);
535
+ }).reduce((i, { target: c, content: l }) => ee(i, l, c), e);
521
536
  } catch (r) {
522
537
  throw new Error(`Error populating inclusions in file ${s ?? "(unknown)"}: ${r}`);
523
538
  }
524
- }, le = (e) => ve(e, "utf-8"), ae = (e) => {
525
- const t = K(e), n = O(t);
526
- return { markdown: le(t), dir: n, path: t };
527
- }, ft = (e, t = !0) => {
528
- const { dir: n, path: s, markdown: o } = ae(e), i = ce(o, 0, (c) => le(K(n, Me(c))), s);
529
- return t && Z(s, i), i;
539
+ }, ae = (e) => we(e, "utf-8"), pe = (e) => {
540
+ const t = Y(e), n = A(t);
541
+ return { markdown: ae(t), dir: n, path: t };
530
542
  }, dt = (e, t = !0) => {
531
- const { path: n, markdown: s } = ae(e), o = ie(s);
532
- return t && Z(n, o), o;
543
+ const { dir: n, path: s, markdown: o } = pe(e), i = le(o, 0, (c) => ae(Y(n, Me(c))), s);
544
+ return t && K(s, i), i;
545
+ }, wt = (e, t = !0) => {
546
+ const { path: n, markdown: s } = pe(e), o = ce(s);
547
+ return t && K(n, o), o;
533
548
  };
534
549
  export {
535
- dt as depopulateMarkdownInclusions,
536
- ft as populateMarkdownInclusions
550
+ wt as depopulateMarkdownInclusions,
551
+ dt as populateMarkdownInclusions
537
552
  };
@@ -1,14 +1,15 @@
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,ae,pe,ue,me,M){"use strict";var nn=Object.defineProperty;var tn=(p,m,d)=>m in p?nn(p,m,{enumerable:!0,configurable:!0,writable:!0,value:d}):p[m]=d;var le=(p,m,d)=>tn(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 b=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,de=pe.unified().use(ue),T={md:e=>de.parse(e)},x=(e,n)=>{const t=[];return me.visit(e,(s,o,r)=>{if(s.type!=="root"){if(n&&s.type!==n)return;if(b(s)){const i=((r==null?void 0:r.children.length)??0)-1;t.push({...s,siblingIndex:o,siblingCount:i})}}}),t},fe=e=>e.children.length===0,_=(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)},he=(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)},k=(...e)=>e.join(" "),ge=(...e)=>e.join(`
2
- `),ve=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},we=e=>ve(e)[0];class w{constructor(...n){le(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,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 L=/,\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
- `}},$e="-",B={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(F.unsafe),...Object.entries(F.reserved)]},U=(e,[n,t])=>e.replaceAll(n,t),j=(e,n=$e)=>{const t=B.static.reduce(U,e);return B.url.map(([s,o])=>[n+s+n,o]).reduce(U,t).replaceAll(n," ")},Ce=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),ye=["string","number","boolean"],Ee=e=>ye.includes(e),Se=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,xe=e=>{const n=e.match(Se);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,s=""]=n;if(!s.trim())return{name:t,parameters:[]};const o=Ne(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},Ne=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?Me(o):void 0).filter((o,r,i)=>o!==void 0||!ke(i,r))},Me=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)},be=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,h]=c,u=h.trim();if(!Ee(u))throw new Error(`Unsupported type: ${u}`);i.push({name:l,optional:a==="?",type:u})}return{name:s,parameters:i}},O=e=>{const n=e.map(be).reduce((t,{name:s,parameters:o})=>t.set(s,o),new Map);return t=>{const{name:s,parameters:o}=xe(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},h)=>{const u=o[h];if(u===void 0){if(l)return i;throw new Error(`Missing required parameter: ${c} for method '${s}'`)}switch(a){case"string":i[c]=u;break;case"number":case"boolean":i[c]=JSON.parse(u);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),Te=O(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),$=(e,n=1)=>`
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,pe,ue,me,de,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 ae=(p,m,d)=>sn(p,typeof m!="symbol"?m+"":m,d);const w=(e,n)=>e.position.start.offset-n.position.start.offset;w.reverse=(e,n)=>w(n,e);const T=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,fe=ue.unified().use(me),j={md:e=>fe.parse(e)},k=(e,n)=>{const t=[];return de.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(w);const s=t.at(0),o=t.at(-1);return e.slice(0,s.position.start.offset)+n+e.slice(o.position.end.offset)},ge=(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)},x=(...e)=>e.join(" "),we=(...e)=>e.join(`
2
+ `),ve=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},$e=e=>ve(e)[0];class v{constructor(...n){ae(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,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 F=/,\s*(?![^()]*\))/,B={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
+ `}},Ce="-",U={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(B.unsafe),...Object.entries(B.reserved)]},H=(e,[n,t])=>e.replaceAll(n,t),N=(e,n=Ce)=>{const t=U.static.reduce(H,e);return U.url.map(([s,o])=>[n+s+n,o]).reduce(H,t).replaceAll(n," ")},ye=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Se=["string","number","boolean"],Ee=e=>Se.includes(e),ke=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,xe=e=>{const n=e.match(ke);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,s=""]=n;if(!s.trim())return{name:t,parameters:[]};const o=be(s);return{name:t,parameters:o}},Ne=(e,n)=>{for(let t=n+1;t<e.length;t++)if(e[t]!==void 0)return!1;return!0},be=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?Me(o):void 0).filter((o,r,i)=>o!==void 0||!Ne(i,r))},Me=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)},Te=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,h]=c,u=h.trim();if(!Ee(u))throw new Error(`Unsupported type: ${u}`);i.push({name:l,optional:a==="?",type:u})}return{name:s,parameters:i}},O=e=>{const n=e.map(Te).reduce((t,{name:s,parameters:o})=>t.set(s,o),new Map);return t=>{const{name:s,parameters:o}=xe(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},h)=>{const u=o[h];if(u===void 0){if(l)return i;throw new Error(`Missing required parameter: ${c} for method '${s}'`)}switch(a){case"string":i[c]=u;break;case"number":case"boolean":i[c]=JSON.parse(u);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),je=O(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),$=(e,n=1)=>`
4
4
  `.repeat(n)+e+`
5
- `.repeat(n),q=(e,n,t)=>`<${n}${t?` ${t}`:""}>${$(e)}</${n}>`,H=(e,n,t)=>{const s=Te(n);switch(s.name){case"code":if(!s.lang&&!s.meta&&(t==null?void 0:t.inline)&&!e.includes(`
5
+ `.repeat(n),R=(e,n,t)=>`<${n}${t?` ${t}`:""}>${$(e)}</${n}>`,Q=(e,n,t)=>{const s=je(n);switch(s.name){case"code":if(!s.lang&&!s.meta&&(t==null?void 0:t.inline)&&!e.includes(`
6
6
  `))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(`
7
7
 
8
- `)?`> ${e}`:$(q($(e),"blockquote"));case"dropdown":const c=q(j(s.summary),"summary");return $(q([c,e].join(`
9
- `),"details",s.open?"open":void 0))}},R=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=je(s[0]).trim();t.push({range:o,value:r})}return t},je=e=>e.startsWith("//")?e.slice(2):e.startsWith("/*")?e.slice(2,-2):e.startsWith("<!--")?e.slice(4,-3):e,Q=["p↓:","pd:","parkdown:"];O(["splice(delete?: number, insert?: string)"]);const z=(e,n)=>e.range[0]-n.range[0],V=(e,n,t)=>(t??R(e)).filter(({value:s})=>s.includes(n)).sort(z),X=e=>({isSpace:e===" ",isNewline:e===`
10
- `||e===void 0}),J=e=>Q.flatMap(n=>V(e,n)).sort((n,t)=>n.range[0]-t.range[0]).reverse().reduce((n,{range:[t,s],value:o})=>{const r=(a,h)=>n.slice(0,a)+n.slice(h),i=X(n[t-1]),c=X(n[s]),l=s===n.length;return i.isNewline&&c.isNewline?r(t-(l?1:0),s+1):i.isNewline?r(t,s+(c.isSpace?1:0)):r(t-(i.isSpace?1:0),s)},e);Q.map(e=>new RegExp(`^${Ce(e)}(\\?[^\\s]+)`));const Oe=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)"]),C=(e,n,t)=>{const s=V(e,n,t),o=[];for(let r=0;r<s.length-1;r+=2)o.push([s[r],s[r+1]]);return o},qe=(e,...n)=>{if(n.length===0)return e;const t=R(e),s=([i,c])=>e.slice(i,c),o=new w,r=new w;for(const i of n)for(const[c,l]of C(e,i,t)){o.push(c.range[1],l.range[0]);const[a,...h]=s([c.range[1],l.range[0]]),u=h[h.length-1];r.push(c.range[0],c.range[1]+(a.trim()?0:1)),r.push(l.range[0],l.range[1]+(u.trim()?0:1))}return o.collapse(),r.collapse(),M.dedent(o.subtract(r).map(s).filter(Boolean).join("")).trim()},Re=(e,...n)=>{if(n.length===0)return e;const t=R(e),s=([i,c])=>e.slice(i,c),o=new w;for(const i of n)for(const[c,l]of C(e,i,t)){const a=s([l.range[1],l.range[1]+1]).at(-1);o.push(c.range[0],a===`
11
- `?l.range[1]+1:l.range[1])}o.collapse();const r=new w([0,e.length]);return r.subtract(o),M.dedent(r.collapse().map(s).filter(Boolean).join("")).trim()},Ie=(e,n,t,s)=>{if(!n)return e;let o="",r=0;for(const[c,l]of C(e,n))o+=e.slice(r,c.range[1]),o+=j(t??c.value,s),r=l.range[0];o+=e.slice(r);const i=new w([0,o.length]);return i.subtract(new w(...C(o,n).flatMap(([c,l])=>[c.range,l.range]))),M.dedent(i.collapse().map(([c,l])=>o.slice(c,l)).filter(Boolean).join("")).trim()},Pe=(e,n,t,s,o)=>{if(!n)return e;const r=t===void 0?!1:t>=0,i=s?j(s,o):"",c=t??0;return C(e,n).map(l=>l[r?1:0]).sort(z).reverse().reduce((l,{range:a})=>r?l.slice(0,a[1])+i+l.slice(Math.min(a[1]+c,l.length)):l.slice(0,Math.max(a[0]+c,0))+i+l.slice(a[0]),e)},Ae=(e,n,t)=>{if(!n)return J(e);const s=Oe(n);switch(s.name){case"extract":e=qe(e,s.id,...G(s));break;case"remove":e=Re(e,s.id,...G(s));break;case"replace":e=Ie(e,s.id,s.with,s.space);break;case"splice":e=Pe(e,s.id,s.deleteCount,s.insert,s.space);break}return t?J(e):e},De=["http","./","../"],We=({url:e})=>De.some(n=>e.startsWith(n)),_e=e=>b(e)&&fe(e)&&We(e),Z=({url:e},n)=>`[](${n?d.join(n,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return k(f._open,f._flag,"BEGIN",f._close)},get end(){return k(f._open,f._flag,"END",f._close)},lengthOf(e){const n=`lines: ${e.split(`
12
- `).length}`,t=`chars: ${e.length}`;return k(f._open,f._flag,"length",n,t,f._close)}},K=e=>n=>b(n)&&n.value===f[e],Le=({position:e,url:n,siblingCount:t},s)=>({position:e,url:n,headingDepth:s,inline:t>=1}),Fe=({position:{start:e},url:n,siblingCount:t},{position:{end:s}},o)=>({position:{start:e,end:s},url:n,headingDepth:o,inline:t>=1}),Be=(e,n,t)=>(e.inline?k:ge)(Z(e,t),f.begin,f.lengthOf(n),n,f.end),Ue=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`)},Ge=(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},He=(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(he(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()},Y=(e,n)=>{n??(n=T.md(e));const t=Ue(n),s=x(n,"link").filter(_e),o=x(n,"html").sort(v),r=o.filter(K("begin")),i=o.filter(K("end")),c=Ge(r,i);return He(e,s,c).map(a=>Array.isArray(a)?Fe(a[0],a[1].close,t(a[0])):Le(a,t(a)))},Qe=(e,{url:n})=>t=>e(d.join(d.dirname(n),t)),ee=(...e)=>{const n=e.reduce((t,s)=>t+s,0);return Math.min(Math.max(n,1),6)},ze=(e,n,t)=>{if(n===0)return e;t??(t=T.md(e));const s=x(t,"heading"),o=e.split(`
13
- `);for(const r of s){const{depth:i,position:{start:c,end:l}}=r,a=ee(i,n),h=o[c.line-1].slice(i,l.column),u="#".repeat(a)+h;o[c.line-1]=u,r.depth=a}return o.join(`
14
- `)},ne=e=>Y(e).reverse().sort(v.reverse).reduce((n,t)=>_(n,Z(t),t),e),te=(e,n,t,s,o)=>{try{e=ne(e),e=ze(e,n);const r=T.md(e);return Y(e,r).sort(v).reverse().map(i=>{var ie,ce;const{url:c,headingDepth:l}=i,[a,...h]=d.basename(c).split("?"),u=a.split(".").pop()??"",Je=h.join("?"),I=d.dirname(c),re=d.join(I,a);if(c.startsWith("./")||c.startsWith("../")){let g=t(re);const y=new ae.URLSearchParams(Je),P=(ie=y.get("region"))==null?void 0:ie.split(L);g=(P==null?void 0:P.reduce((E,S,W,{length:en})=>Ae(E,S,W===en-1),g))??g;const Ze=y.has("skip"),Ke=y.get("heading")??0,Ye=y.has("inline");let{inline:A}=i;if(Ye&&(A=!0),!Ze)if(u==="md"){const E=Qe(t,i),S=o?d.join(o,I):I,W=ee(l,Number(Ke));g=te(g,W,E,re,S)}else/^(js|ts)x?|svelte$/i.test(u)&&(g=H(g,"code",{extension:u,inline:A}));const D=(ce=y.get("wrap"))==null?void 0:ce.split(L);return g=(D==null?void 0:D.reduce((E,S)=>H(E,S,{extension:u,inline:A}),g))??g,{target:i,content:Be(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})=>_(i,l,c),e)}catch(r){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${r}`)}},se=e=>m.readFileSync(e,"utf-8"),oe=e=>{const n=d.resolve(e),t=d.dirname(n);return{markdown:se(n),dir:t,path:n}},Ve=(e,n=!0)=>{const{dir:t,path:s,markdown:o}=oe(e),i=te(o,0,c=>se(d.resolve(t,we(c))),s);return n&&m.writeFileSync(s,i),i},Xe=(e,n=!0)=>{const{path:t,markdown:s}=oe(e),o=ne(s);return n&&m.writeFileSync(t,o),o};p.depopulateMarkdownInclusions=Xe,p.populateMarkdownInclusions=Ve,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
8
+ `)?`> ${e}`:$(R($(e),"blockquote"));case"dropdown":const c=R(N(s.summary),"summary");return $(R([c,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=Oe(s[0]).trim();t.push({range:o,value:r})}return t},Oe=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.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]),c=J(n[s]),l=s===n.length;let a=t;for(;a>0&&n[a-1]!==`
11
+ `;)a--;const h=n.slice(a,t).trim()==="";return i.isNewline&&c.isNewline?r(t-(l?1:0),s+1):i.isNewline||h?r(a,s+(c.isSpace||c.isNewline?1:0)):r(t-(i.isSpace?1:0),s)},e);z.map(e=>new RegExp(`^${ye(e)}(\\?[^\\s]+)`));const Re=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)"]),C=(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},qe=(e,...n)=>{if(n.length===0)return e;const t=q(e),s=([i,c])=>e.slice(i,c),o=new v,r=new v;for(const i of n)for(const[c,l]of C(e,i,t)){o.push(c.range[1],l.range[0]);const[a,...h]=s([c.range[1],l.range[0]]),u=h[h.length-1];r.push(c.range[0],c.range[1]+(a.trim()?0:1)),r.push(l.range[0],l.range[1]+(u.trim()?0:1))}return o.collapse(),r.collapse(),M.dedent(o.subtract(r).map(s).filter(Boolean).join("")).trim()},Ie=(e,...n)=>{if(n.length===0)return e;const t=q(e),s=([i,c])=>e.slice(i,c),o=new v;for(const i of n)for(const[c,l]of C(e,i,t)){const a=s([l.range[1],l.range[1]+1]).at(-1);o.push(c.range[0],a===`
12
+ `?l.range[1]+1:l.range[1])}o.collapse();const r=new v([0,e.length]);return r.subtract(o),M.dedent(r.collapse().map(s).filter(Boolean).join("")).trim()},Ae=(e,n,t,s)=>{if(!n)return e;let o="",r=0;for(const[c,l]of C(e,n))o+=e.slice(r,c.range[1]),o+=N(t??c.value,s),r=l.range[0];o+=e.slice(r);const i=new v([0,o.length]);return i.subtract(new v(...C(o,n).flatMap(([c,l])=>[c.range,l.range]))),M.dedent(i.collapse().map(([c,l])=>o.slice(c,l)).filter(Boolean).join("")).trim()},Pe=(e,n,t,s,o)=>{if(!n)return e;const r=t===void 0?!1:t>=0,i=s?N(s,o):"",c=t??0;return C(e,n).map(l=>l[r?1:0]).sort(V).reverse().reduce((l,{range:a})=>r?l.slice(0,a[1])+i+l.slice(Math.min(a[1]+c,l.length)):l.slice(0,Math.max(a[0]+c,0))+i+l.slice(a[0]),e)},De=(e,n,t,s,o)=>{if(!n)return e;let r="",i=0;const c=[t,s].map(l=>N(l,o));console.log(c);for(const[l,a]of C(e,n))r+=e.slice(i,l.range[1]),r+=e.slice(l.range[1],a.range[0]).replaceAll(c[0],c[1]),i=a.range[0];return r+=e.slice(i),r},We=(e,n,t)=>{if(!n)return Z(e);const s=Re(n);switch(s.name){case"extract":e=qe(e,s.id,...G(s));break;case"remove":e=Ie(e,s.id,...G(s));break;case"replace":e=Ae(e,s.id,s.with,s.space);break;case"splice":e=Pe(e,s.id,s.deleteCount,s.insert,s.space);break;case"remap":e=De(e,s.id,s.from,s.to,s.space);break}return t?Z(e):e},_e=["http","./","../"],Le=({url:e})=>_e.some(n=>e.startsWith(n)),Fe=e=>T(e)&&he(e)&&Le(e),K=({url:e},n)=>`[](${n?d.join(n,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return x(f._open,f._flag,"BEGIN",f._close)},get end(){return x(f._open,f._flag,"END",f._close)},lengthOf(e){const n=`lines: ${e.split(`
13
+ `).length}`,t=`chars: ${e.length}`;return x(f._open,f._flag,"length",n,t,f._close)}},Y=e=>n=>T(n)&&n.value===f[e],Be=({position:e,url:n,siblingCount:t},s)=>({position:e,url:n,headingDepth:s,inline:t>=1}),Ue=({position:{start:e},url:n,siblingCount:t},{position:{end:s}},o)=>({position:{start:e,end:s},url:n,headingDepth:o,inline:t>=1}),He=(e,n,t)=>(e.inline?x:we)(K(e,t),f.begin,f.lengthOf(n),n,f.end),Ge=e=>{const n=k(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`)},Qe=(e,n)=>{const t=[],s=[...e.map(r=>({node:r,type:"open"})),...n.map(r=>({node:r,type:"close"}))].sort((r,i)=>w(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 c=o.pop().node;if(o.length>0)continue;t.push({open:c,close:i})}if(o.length>0)throw b.openingCommentNotClosed(o[0].node);return t},ze=(e,n,t)=>{const s=[...n].sort(w),o=[];return[...t].sort((r,i)=>w.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(ge(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=Ge(n),s=k(n,"link").filter(Fe),o=k(n,"html").sort(w),r=o.filter(Y("begin")),i=o.filter(Y("end")),c=Qe(r,i);return ze(e,s,c).map(a=>Array.isArray(a)?Ue(a[0],a[1].close,t(a[0])):Be(a,t(a)))},Ve=(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)},Xe=(e,n,t)=>{if(n===0)return e;t??(t=j.md(e));const s=k(t,"heading"),o=e.split(`
14
+ `);for(const r of s){const{depth:i,position:{start:c,end:l}}=r,a=ne(i,n),h=o[c.line-1].slice(i,l.column),u="#".repeat(a)+h;o[c.line-1]=u,r.depth=a}return o.join(`
15
+ `)},te=e=>ee(e).reverse().sort(w.reverse).reduce((n,t)=>L(n,K(t),t),e),se=(e,n,t,s,o)=>{try{e=te(e),e=Xe(e,n);const r=j.md(e);return ee(e,r).sort(w).reverse().map(i=>{var ce,le;const{url:c,headingDepth:l}=i,[a,...h]=d.basename(c).split("?"),u=a.split(".").pop()??"",I=h.join("?"),A=d.dirname(c),ie=d.join(A,a);if(c.startsWith("./")||c.startsWith("../")){let g=t(ie);const y=new pe.URLSearchParams(I),P=(ce=y.get("region"))==null?void 0:ce.split(F);g=(P==null?void 0:P.reduce((S,E,_,{length:nn})=>We(S,E,_===nn-1),g))??g;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=Ve(t,i),E=o?d.join(o,A):A,_=ne(l,Number(Ye));g=se(g,_,S,ie,E)}else/^(js|ts)x?|svelte$/i.test(u)&&(g=Q(g,"code",{extension:u,inline:D}));const W=(le=y.get("wrap"))==null?void 0:le.split(F);return g=(W==null?void 0:W.reduce((S,E)=>Q(S,E,{extension:u,inline:D}),g))??g,{target:i,content:He(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})=>L(i,l,c),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{markdown:oe(n),dir:t,path:n}},Je=(e,n=!0)=>{const{dir:t,path:s,markdown:o}=re(e),i=se(o,0,c=>oe(d.resolve(t,$e(c))),s);return n&&m.writeFileSync(s,i),i},Ze=(e,n=!0)=>{const{path:t,markdown:s}=re(e),o=te(s);return n&&m.writeFileSync(t,o),o};p.depopulateMarkdownInclusions=Ze,p.populateMarkdownInclusions=Je,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.11",
5
+ "version": "0.0.12",
6
6
  "main": "./dist/index.js",
7
7
  "bin": "./dist/cli.js",
8
8
  "files": [