@p-buddy/parkdown 0.0.9 → 0.0.11

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.8", 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.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();
5
5
  n.length === 0 && n.push("README.md");
6
6
  const a = [
7
7
  [l, !c],
package/dist/index.js CHANGED
@@ -1,20 +1,20 @@
1
- var de = Object.defineProperty;
2
- var we = (e, t, n) => t in e ? de(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var U = (e, t, n) => we(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { writeFileSync as Y, readFileSync as ve } from "node:fs";
5
- import { basename as $e, dirname as P, join as x, resolve as ee } from "node:path";
6
- import { URLSearchParams as Ce } from "node:url";
7
- import { unified as ye } from "unified";
1
+ var fe = Object.defineProperty;
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";
6
+ import { URLSearchParams as $e } from "node:url";
7
+ import { unified as Ce } from "unified";
8
8
  import Ee from "remark-parse";
9
- import { visit as Se } from "unist-util-visit";
10
- import { dedent as A } from "ts-dedent";
11
- const f = (e, t) => e.position.start.offset - t.position.start.offset;
12
- f.reverse = (e, t) => f(t, e);
13
- const j = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, xe = ye().use(Ee), D = {
14
- md: (e) => xe.parse(e)
15
- }, N = (e, t) => {
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 = {
14
+ md: (e) => Se.parse(e)
15
+ }, S = (e, t) => {
16
16
  const n = [];
17
- return Se(e, (s, o, r) => {
17
+ return ye(e, (s, o, r) => {
18
18
  if (s.type !== "root") {
19
19
  if (t && s.type !== t) return;
20
20
  if (j(s)) {
@@ -23,22 +23,24 @@ const j = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
23
23
  }
24
24
  }
25
25
  }), n;
26
- }, Ne = (e) => e.children.length === 0, te = (e, t, ...n) => {
26
+ }, xe = (e) => e.children.length === 0, Y = (e, t, ...n) => {
27
27
  if (n.length === 0) throw new Error("No nodes to replace content from");
28
- n.sort(f);
28
+ n.sort(g);
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
- }, be = (e, t, n) => {
31
+ }, Ne = (e, t, n) => {
32
32
  const s = Math.min(t.position.end.offset, n.position.end.offset), o = Math.max(t.position.start.offset, n.position.start.offset);
33
33
  return e.slice(s, o);
34
- }, S = (...e) => e.join(" "), ke = (...e) => e.join(`
35
- `), Me = (e) => {
34
+ }, E = (...e) => e.join(" "), be = (...e) => e.join(`
35
+ `), ke = (e) => {
36
36
  const t = e.split("?");
37
37
  return t.length > 1 ? [t.slice(0, -1).join("?"), t.at(-1)] : [e, ""];
38
- }, _e = (e) => Me(e)[0];
39
- class v {
40
- constructor() {
41
- U(this, "intervals", []);
38
+ }, Me = (e) => ke(e)[0];
39
+ class d {
40
+ constructor(...t) {
41
+ B(this, "intervals", []);
42
+ for (const [n, s] of t)
43
+ this.push(n, s);
42
44
  }
43
45
  push(t, n) {
44
46
  this.intervals.push([Math.min(t, n), Math.max(t, n)]);
@@ -53,8 +55,8 @@ class v {
53
55
  const n = [];
54
56
  let [s, o] = t[0];
55
57
  for (let r = 1; r < t.length; r++) {
56
- const [i, l] = t[r];
57
- i <= o ? o = Math.max(o, l) : (n.push([s, o]), s = i, o = l);
58
+ const [i, c] = t[r];
59
+ i <= o ? o = Math.max(o, c) : (n.push([s, o]), s = i, o = c);
58
60
  }
59
61
  return n.push([s, o]), this.intervals = n;
60
62
  }
@@ -63,20 +65,20 @@ class v {
63
65
  if (!n.length || !s.length) return n;
64
66
  let o = [...n];
65
67
  for (const [r, i] of s) {
66
- const l = [];
67
- for (const [c, a] of o) {
68
- if (i <= c || r >= a) {
69
- l.push([c, a]);
68
+ const c = [];
69
+ for (const [l, a] of o) {
70
+ if (i <= l || r >= a) {
71
+ c.push([l, a]);
70
72
  continue;
71
73
  }
72
- r > c && l.push([c, r]), i < a && l.push([i, a]);
74
+ r > l && c.push([l, r]), i < a && c.push([i, a]);
73
75
  }
74
- o = l;
76
+ o = c;
75
77
  }
76
78
  return this.intervals = o;
77
79
  }
78
80
  }
79
- const O = /,\s*(?![^()]*\))/, Q = {
81
+ const F = /,\s*(?![^()]*\))/, U = {
80
82
  reserved: {
81
83
  semi: ";",
82
84
  slash: "/",
@@ -100,9 +102,11 @@ const O = /,\s*(?![^()]*\))/, Q = {
100
102
  tilde: "~",
101
103
  square: "[",
102
104
  unsquare: "]",
103
- tick: "`"
105
+ tick: "`",
106
+ line: `
107
+ `
104
108
  }
105
- }, Re = "-", G = {
109
+ }, _e = "-", G = {
106
110
  static: [
107
111
  ["'''", '"'],
108
112
  ["''", "'"],
@@ -110,14 +114,14 @@ const O = /,\s*(?![^()]*\))/, Q = {
110
114
  [/p↓:\s+/g, ""]
111
115
  ],
112
116
  url: [
113
- ...Object.entries(Q.unsafe),
114
- ...Object.entries(Q.reserved)
117
+ ...Object.entries(U.unsafe),
118
+ ...Object.entries(U.reserved)
115
119
  ]
116
- }, H = (e, [t, n]) => e.replaceAll(t, n), ne = (e, t = Re) => {
120
+ }, H = (e, [t, n]) => e.replaceAll(t, n), D = (e, t = _e) => {
117
121
  const n = G.static.reduce(H, e);
118
122
  return G.url.map(([s, o]) => [t + s + t, o]).reduce(H, n).replaceAll(t, " ");
119
- }, Te = (e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), Oe = ["string", "number", "boolean"], Pe = (e) => Oe.includes(e), Ae = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, je = (e) => {
120
- const t = e.match(Ae);
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);
121
125
  if (!t) throw new Error(`Invalid invocation: ${e}`);
122
126
  const [, n, s = ""] = t;
123
127
  if (!s.trim()) return { name: n, parameters: [] };
@@ -133,64 +137,64 @@ const O = /,\s*(?![^()]*\))/, Q = {
133
137
  for (let o = 0; o < e.length; o++) {
134
138
  const r = e[o], i = r === "'";
135
139
  if (i && e.at(o + 1) === "'") {
136
- const l = e.at(o + 2) === "'";
137
- s = !s, n += l ? "'''" : "''", o += l ? 2 : 1;
140
+ const c = e.at(o + 2) === "'";
141
+ s = !s, n += c ? "'''" : "''", o += c ? 2 : 1;
138
142
  } else i ? (s = !s, n += r) : r === "," && !s ? (t.push(n.trim()), n = "") : n += r;
139
143
  }
140
- return t.push(n.trim()), t.map((o) => o === "" ? void 0 : o ? Le(o) : void 0).filter((o, r, i) => o !== void 0 || !De(i, r));
141
- }, Le = (e) => {
144
+ return t.push(n.trim()), t.map((o) => o === "" ? void 0 : o ? Pe(o) : void 0).filter((o, r, i) => o !== void 0 || !De(i, r));
145
+ }, Pe = (e) => {
142
146
  const t = e.length >= 2 && e[0] === "'" && e.at(-1) === "'", n = t && e.length >= 4 && e[1] === "'" && e.at(-2) === "'";
143
147
  return !t || n ? e : e.slice(1, -1);
144
- }, qe = (e) => {
148
+ }, We = (e) => {
145
149
  const t = /^(\w+)(?:\(([^)]*)\))?/, n = e.match(t);
146
150
  if (!n) return { name: e };
147
151
  const [, s, o] = n;
148
152
  if (!o) return { name: s };
149
153
  const r = /(\w+)(\?)?:\s*([^,]+)/g, i = [];
150
- let l;
151
- for (; (l = r.exec(o)) !== null; ) {
152
- const [, c, a, p] = l, m = p.trim();
153
- if (!Pe(m)) throw new Error(`Unsupported type: ${m}`);
154
- i.push({ name: c, optional: a === "?", type: m });
154
+ let c;
155
+ for (; (c = r.exec(o)) !== null; ) {
156
+ const [, l, a, u] = c, p = u.trim();
157
+ if (!Re(p)) throw new Error(`Unsupported type: ${p}`);
158
+ i.push({ name: l, optional: a === "?", type: p });
155
159
  }
156
160
  return { name: s, parameters: i };
157
161
  }, I = (e) => {
158
- const t = e.map(qe).reduce(
162
+ const t = e.map(We).reduce(
159
163
  (n, { name: s, parameters: o }) => n.set(s, o),
160
164
  /* @__PURE__ */ new Map()
161
165
  );
162
166
  return (n) => {
163
- const { name: s, parameters: o } = je(n);
167
+ const { name: s, parameters: o } = Ae(n);
164
168
  if (!t.has(s))
165
169
  throw new Error(`Unknown method: ${s}`);
166
170
  const r = t.get(s);
167
171
  if (r === void 0)
168
172
  return { name: s };
169
173
  if (o.length > r.length) {
170
- const i = r.filter(({ optional: c }) => !c).length, l = i === r.length ? i.toString() : `${i} - ${r.length}`;
171
- throw new Error(`Too many parameters: ${o.length} for method '${s}' (expected: ${l})`);
174
+ const i = r.filter(({ optional: l }) => !l).length, c = i === r.length ? i.toString() : `${i} - ${r.length}`;
175
+ throw new Error(`Too many parameters: ${o.length} for method '${s}' (expected: ${c})`);
172
176
  }
173
- return r.reduce((i, { name: l, optional: c, type: a }, p) => {
174
- const m = o[p];
175
- if (m === void 0) {
176
- if (c) return i;
177
- throw new Error(`Missing required parameter: ${l} for method '${s}'`);
177
+ return r.reduce((i, { name: c, optional: l, type: a }, u) => {
178
+ const p = o[u];
179
+ if (p === void 0) {
180
+ if (l) return i;
181
+ throw new Error(`Missing required parameter: ${c} for method '${s}'`);
178
182
  }
179
183
  switch (a) {
180
184
  case "string":
181
- i[l] = m;
185
+ i[c] = p;
182
186
  break;
183
187
  case "number":
184
188
  case "boolean":
185
- i[l] = JSON.parse(m);
189
+ i[c] = JSON.parse(p);
186
190
  break;
187
191
  }
188
- if (typeof i[l] !== a)
189
- throw new Error(`Invalid type: ${l} must be ${a}, got ${typeof i[l]} for method '${s}'`);
192
+ if (typeof i[c] !== a)
193
+ throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${s}'`);
190
194
  return i;
191
195
  }, { name: s });
192
196
  };
193
- }, z = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), We = [
197
+ }, Q = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Le = [
194
198
  /**
195
199
  * Wraps the content in a markdown-formatted code block.
196
200
  * @param lang The language of the code block (defaults to the file extension).
@@ -220,65 +224,51 @@ const O = /,\s*(?![^()]*\))/, Q = {
220
224
  * @example [](<url>?wrap=dropdown(hello_world,,_))
221
225
  */
222
226
  "dropdown(summary: string, open?: boolean, space?: string)"
223
- ], Be = I(We), w = (e, t = 1) => `
227
+ ], qe = I(Le), f = (e, t = 1) => `
224
228
  `.repeat(t) + e + `
225
- `.repeat(t), T = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${w(e)}</${t}>`, X = (e, t, n) => {
226
- const s = Be(t);
229
+ `.repeat(t), T = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${f(e)}</${t}>`, z = (e, t, n) => {
230
+ const s = qe(t);
227
231
  switch (s.name) {
228
232
  case "code":
229
233
  if (!s.lang && !s.meta && (n == null ? void 0 : n.inline) && !e.includes(`
230
234
  `)) return `\`${e}\``;
231
235
  const r = s.lang ?? (n == null ? void 0 : n.extension) ?? "", i = s.meta ? ` ${s.meta}` : "";
232
- return w(`\`\`\`${r}${i}${w(e)}\`\`\``);
236
+ return f(`\`\`\`${r}${i}${f(e)}\`\`\``);
233
237
  case "quote":
234
238
  return n != null && n.inline && !e.includes(`
235
239
 
236
- `) ? `> ${e}` : w(T(w(e), "blockquote"));
240
+ `) ? `> ${e}` : f(T(f(e), "blockquote"));
237
241
  case "dropdown":
238
- const l = T(ne(s.summary), "summary");
239
- return w(T([l, e].join(`
242
+ const c = T(D(s.summary), "summary");
243
+ return f(T([c, e].join(`
240
244
  `), "details", s.open ? "open" : void 0));
241
245
  }
242
- }, L = (e) => {
246
+ }, P = (e) => {
243
247
  const t = /(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm, n = [];
244
248
  let s;
245
249
  for (; (s = t.exec(e)) !== null; ) {
246
- const o = [s.index, s.index + s[0].length], r = Fe(s[0]).trim();
250
+ const o = [s.index, s.index + s[0].length], r = Be(s[0]).trim();
247
251
  n.push({ range: o, value: r });
248
252
  }
249
253
  return n;
250
- }, Fe = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, se = [
254
+ }, Be = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, ee = [
251
255
  "p↓:",
252
256
  "pd:",
253
257
  "parkdown:"
254
- ], Ue = [
258
+ ], Fe = [
255
259
  "splice(delete?: number, insert?: string)"
256
- ], V = I(Ue), d = (e, t, n) => (n ?? L(e)).filter(({ value: s }) => s.includes(t)).sort((s, o) => s.range[0] - o.range[0]), J = (e) => ({
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) => ({
257
263
  isSpace: e === " ",
258
264
  isNewline: e === `
259
265
  ` || e === void 0
260
- }), Z = (e) => se.flatMap((t) => d(e, t)).sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, s], value: o }) => {
261
- const r = (a, p) => t.slice(0, a) + t.slice(p), i = J(t[n - 1]), l = J(t[s]), c = s === t.length;
262
- return i.isNewline && l.isNewline ? r(n - (c ? 1 : 0), s + 1) : i.isNewline ? r(n, s + (l.isSpace ? 1 : 0)) : r(n - (i.isSpace ? 1 : 0), s);
263
- }, e), oe = se.map((e) => new RegExp(`^${Te(e)}(\\?[^\\s]+)`)), Qe = (e) => oe.map((t) => {
264
- var n;
265
- return (n = t.exec(e)) == null ? void 0 : n[1];
266
- }).filter(Boolean).map((t) => new URLSearchParams(t))[0], q = (e, t) => (Array.isArray(t) ? t.flatMap((n) => d(e, n)) : d(e, t)).filter(({ value: n }) => oe.some((s) => s.test(n))).sort((n, s) => n.range[0] - s.range[0]).reverse().reduce((n, { range: [s, o], value: r }) => {
267
- var c, a;
268
- const i = Qe(r), l = ["back", "front"].map((p) => [p, i.get(p)]).filter(([p, m]) => !!m).reduce(
269
- (p, [m, u]) => p.set(m, u.split(O)),
270
- /* @__PURE__ */ new Map()
271
- );
272
- return n = ((c = l.get("back")) == null ? void 0 : c.reduce((p, m) => {
273
- const u = V(m);
274
- if (u.name === "splice")
275
- return p.slice(0, o) + (u.insert ?? "") + p.slice(o + (u.delete ?? 0));
276
- }, n)) ?? n, n = ((a = l.get("front")) == null ? void 0 : a.reduce((p, m) => {
277
- const u = V(m);
278
- if (u.name === "splice")
279
- return p.slice(0, s - (u.delete ?? 0)) + (u.insert ?? "") + p.slice(s);
280
- }, n)) ?? n, n;
281
- }, e), Ge = [
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);
269
+ }, e);
270
+ ee.map((e) => new RegExp(`^${Te(e)}(\\?[^\\s]+)`));
271
+ const Ue = [
282
272
  /**
283
273
  * Extract regions from the retrieved content between comments that INCLUDE the specified ids.
284
274
  * @param id The id of the comment to extract.
@@ -308,95 +298,126 @@ const O = /,\s*(?![^()]*\))/, Q = {
308
298
  * @example [](<url>?region=replace(specifier,new-content))
309
299
  * @example [](<url>?region=replace(specifier,new_content,_)
310
300
  */
311
- "replace(id: string, with?: string, space?: string)"
312
- ], He = I(Ge), ze = (e, ...t) => {
301
+ "replace(id: string, with?: string, space?: string)",
302
+ /**
303
+ * Splice the retrieved content at the boundary of a comment region (which must INCLUDE the specified id).
304
+ *
305
+ * **NOTE:** Unlike `extract`, `remove`, and `replace`, `splice` does remove the comment from the content after processing.
306
+ * @param id The id of the comment regions to act on.
307
+ * @param deleteCount The number of characters to delete at either the beginning or end of the comment region.
308
+ * Specifying a number greater than or equal to 0 indicates the action should be taken at the end of the comment region.
309
+ * Specifying undefined or a number less than 0 indicates the action should be taken at the beginning of the comment region.
310
+ * @param insert The content to insert.
311
+ * @param space The space character to use between words in the content to insert (defaults to `-`).
312
+ * @example [](<url>?region=splice(specifier,-1)) // Delete one character at the beginning of the comment region.
313
+ * @example [](<url>?region=splice(specifier,undefined,new-content)) // Insert at the beginning of the comment region.
314
+ * @example [](<url>?region=splice(specifier,0,new-content)) // Insert at the end of the comment region.
315
+ * @example [](<url>?region=splice(specifier,1,new-content)) // Delete one character at the end of the comment region and insert.
316
+ */
317
+ "splice(id: string, deleteCount?: number, insert?: string, space?: string)",
318
+ /**
319
+ * Remap the content within a comment region (which must INCLUDE the specified id).
320
+ *
321
+ * **NOTE:** Unlike `extract`, `remove`, and `replace`, `remap` does not remove the comment from the content after processing.
322
+ * @param id The id of the comment regions to act on.
323
+ * @param from The content to replace.
324
+ * @param to The content to replace with.
325
+ * @param space The space character to use between words in the content to replace (defaults to `-`).
326
+ * @example [](<url>?region=remap(specifier,hello-world,hello-universe))
327
+ * @example [](<url>?region=remap(specifier,hello_world,hello_universe,_)
328
+ */
329
+ "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 = [];
332
+ for (let r = 0; r < s.length - 1; r += 2)
333
+ o.push([s[r], s[r + 1]]);
334
+ return o;
335
+ }, He = (e, ...t) => {
313
336
  if (t.length === 0) return e;
314
- e = q(e, t);
315
- const n = L(e), s = ([i, l]) => e.slice(i, l), o = new v(), r = new v();
316
- for (const i of t) {
317
- const l = d(e, i, n);
318
- for (let c = 0; c < l.length - 1; c += 2) {
319
- const a = l[c], p = l[c + 1];
320
- o.push(a.range[1], p.range[0]);
321
- const [m, ...u] = s([a.range[1], p.range[0]]), $ = u[u.length - 1];
322
- r.push(a.range[0], a.range[1] + (m.trim() ? 0 : 1)), r.push(p.range[0], p.range[1] + ($.trim() ? 0 : 1));
337
+ const n = P(e), s = ([i, c]) => e.slice(i, c), o = new d(), r = new d();
338
+ for (const i of t)
339
+ for (const [c, l] of C(e, i, n)) {
340
+ o.push(c.range[1], l.range[0]);
341
+ const [a, ...u] = s([c.range[1], l.range[0]]), p = u[u.length - 1];
342
+ r.push(c.range[0], c.range[1] + (a.trim() ? 0 : 1)), r.push(l.range[0], l.range[1] + (p.trim() ? 0 : 1));
323
343
  }
324
- }
325
- return o.collapse(), r.collapse(), A(
344
+ return o.collapse(), r.collapse(), R(
326
345
  o.subtract(r).map(s).filter(Boolean).join("")
327
346
  ).trim();
328
- }, Xe = (e, ...t) => {
347
+ }, Qe = (e, ...t) => {
329
348
  if (t.length === 0) return e;
330
- e = q(e, t);
331
- const n = L(e), s = ([i, l]) => e.slice(i, l), o = new v();
332
- for (const i of t) {
333
- const l = d(e, i, n);
334
- for (let c = 0; c < l.length - 1; c += 2) {
335
- const a = l[c], p = l[c + 1], m = s([p.range[1], p.range[1] + 1]).at(-1);
336
- o.push(a.range[0], m === `
337
- ` ? p.range[1] + 1 : p.range[1]);
349
+ const n = P(e), s = ([i, c]) => e.slice(i, c), o = new d();
350
+ for (const i of t)
351
+ for (const [c, l] of C(e, i, n)) {
352
+ const a = s([l.range[1], l.range[1] + 1]).at(-1);
353
+ o.push(c.range[0], a === `
354
+ ` ? l.range[1] + 1 : l.range[1]);
338
355
  }
339
- }
340
356
  o.collapse();
341
- const r = new v();
342
- return r.push(0, e.length), r.subtract(o), A(
357
+ const r = new d([0, e.length]);
358
+ return r.subtract(o), R(
343
359
  r.collapse().map(s).filter(Boolean).join("")
344
360
  ).trim();
345
- }, Ve = (e, t, n, s) => {
361
+ }, ze = (e, t, n, s) => {
346
362
  if (!t) return e;
347
- e = q(e, t);
348
- const o = d(e, t);
349
- if (o.length < 2) return e;
350
- let r = "", i = 0;
351
- for (let c = 0; c < o.length - 1; c += 2) {
352
- const a = o[c], p = o[c + 1];
353
- r += e.slice(i, a.range[1]), r += ne(n ?? a.value, s), i = p.range[0];
354
- }
355
- r += e.slice(i);
356
- const l = new v();
357
- return l.push(0, r.length), l.subtract(
358
- d(r, t).reduce((c, { range: a }) => (c.push(...a), c), new v())
359
- ), A(
360
- l.collapse().map(([c, a]) => r.slice(c, a)).filter(Boolean).join("")
363
+ 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];
366
+ o += e.slice(r);
367
+ const i = new d([0, o.length]);
368
+ return i.subtract(new d(
369
+ ...C(o, t).flatMap(([c, l]) => [c.range, l.range])
370
+ )), R(
371
+ i.collapse().map(([c, l]) => o.slice(c, l)).filter(Boolean).join("")
361
372
  ).trim();
362
- }, Je = (e, t, n) => {
363
- if (!t) return Z(e);
364
- const s = He(t);
373
+ }, Xe = (e, t, n, s, o) => {
374
+ 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(
377
+ (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
+ e
379
+ );
380
+ }, Ve = (e, t, n) => {
381
+ if (!t) return V(e);
382
+ const s = Ge(t);
365
383
  switch (s.name) {
366
384
  case "extract":
367
- e = ze(e, s.id, ...z(s));
385
+ e = He(e, s.id, ...Q(s));
368
386
  break;
369
387
  case "remove":
370
- e = Xe(e, s.id, ...z(s));
388
+ e = Qe(e, s.id, ...Q(s));
371
389
  break;
372
390
  case "replace":
373
- e = Ve(e, s.id, s.with, s.space);
391
+ e = ze(e, s.id, s.with, s.space);
392
+ break;
393
+ case "splice":
394
+ e = Xe(e, s.id, s.deleteCount, s.insert, s.space);
374
395
  break;
375
396
  }
376
- return n ? Z(e) : e;
377
- }, Ze = ["http", "./", "../"], Ke = ({ url: e }) => Ze.some((t) => e.startsWith(t)), Ye = (e) => j(e) && Ne(e) && Ke(e), re = ({ url: e }, t) => `[](${t ? x(t, e) : e})`, h = {
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 = {
378
399
  _open: "<!--",
379
400
  _close: "-->",
380
401
  _flag: "p↓",
381
402
  get begin() {
382
- return S(h._open, h._flag, "BEGIN", h._close);
403
+ return E(m._open, m._flag, "BEGIN", m._close);
383
404
  },
384
405
  get end() {
385
- return S(h._open, h._flag, "END", h._close);
406
+ return E(m._open, m._flag, "END", m._close);
386
407
  },
387
408
  lengthOf(e) {
388
409
  const t = `lines: ${e.split(`
389
410
  `).length}`, n = `chars: ${e.length}`;
390
- return S(h._open, h._flag, "length", t, n, h._close);
411
+ return E(m._open, m._flag, "length", t, n, m._close);
391
412
  }
392
- }, K = (e) => (t) => j(t) && t.value === h[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 ? S : ke)(
393
- re(e, n),
394
- h.begin,
395
- h.lengthOf(t),
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),
415
+ m.begin,
416
+ m.lengthOf(t),
396
417
  t,
397
- h.end
398
- ), st = (e) => {
399
- const t = N(e, "heading").reduce((n, { position: s, depth: o }) => n.set(s.start.line, o), /* @__PURE__ */ new Map());
418
+ m.end
419
+ ), nt = (e) => {
420
+ const t = S(e, "heading").reduce((n, { position: s, depth: o }) => n.set(s.start.line, o), /* @__PURE__ */ new Map());
400
421
  return (n) => {
401
422
  for (let s = n.position.start.line; s >= 1; s--) {
402
423
  const o = t.get(s);
@@ -404,113 +425,113 @@ const O = /,\s*(?![^()]*\))/, Q = {
404
425
  }
405
426
  return 0;
406
427
  };
407
- }, b = {
428
+ }, x = {
408
429
  openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
409
430
  closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
410
431
  openingCommentNotClosed: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)
411
- }, ot = (e, t) => {
432
+ }, st = (e, t) => {
412
433
  const n = [], s = [
413
434
  ...e.map((r) => ({ node: r, type: "open" })),
414
435
  ...t.map((r) => ({ node: r, type: "close" }))
415
- ].sort((r, i) => f(r.node, i.node)), o = [];
436
+ ].sort((r, i) => g(r.node, i.node)), o = [];
416
437
  for (const r of s)
417
438
  if (r.type === "open") o.push(r);
418
439
  else {
419
440
  const i = r.node;
420
441
  if (o.length === 0)
421
- throw b.closingCommentNotMatchedToOpening(i);
422
- const l = o.pop().node;
442
+ throw x.closingCommentNotMatchedToOpening(i);
443
+ const c = o.pop().node;
423
444
  if (o.length > 0) continue;
424
- n.push({ open: l, close: i });
445
+ n.push({ open: c, close: i });
425
446
  }
426
447
  if (o.length > 0)
427
- throw b.openingCommentNotClosed(o[0].node);
448
+ throw x.openingCommentNotClosed(o[0].node);
428
449
  return n;
429
- }, rt = (e, t, n) => {
430
- const s = [...t].sort(f), o = [];
431
- return [...n].sort((r, i) => f.reverse(r.open, i.open)).forEach((r) => {
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) => {
432
453
  for (; s.length > 0; ) {
433
454
  const i = s.pop();
434
455
  if (i.position.start.offset < r.open.position.start.offset) {
435
- if (be(e, i, r.open).trim() !== "")
436
- throw b.openingCommentDoesNotFollowLink(r.open);
456
+ if (Ne(e, i, r.open).trim() !== "")
457
+ throw x.openingCommentDoesNotFollowLink(r.open);
437
458
  return o.push([i, r]);
438
459
  }
439
460
  o.push(i);
440
461
  }
441
- throw b.openingCommentDoesNotFollowLink(r.open);
462
+ throw x.openingCommentDoesNotFollowLink(r.open);
442
463
  }), o.push(...s.reverse()), o.reverse();
443
- }, ie = (e, t) => {
444
- t ?? (t = D.md(e));
445
- const n = st(t), s = N(t, "link").filter(Ye), o = N(t, "html").sort(f), r = o.filter(K("begin")), i = o.filter(K("end")), l = ot(r, i);
446
- return rt(e, s, l).map((a) => Array.isArray(a) ? tt(a[0], a[1].close, n(a[0])) : et(a, n(a)));
447
- }, it = (e, { url: t }) => (n) => e(x(P(t), n)), le = (...e) => {
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) => {
448
469
  const t = e.reduce((n, s) => n + s, 0);
449
470
  return Math.min(Math.max(t, 1), 6);
450
- }, lt = (e, t, n) => {
471
+ }, it = (e, t, n) => {
451
472
  if (t === 0) return e;
452
- n ?? (n = D.md(e));
453
- const s = N(n, "heading"), o = e.split(`
473
+ n ?? (n = A.md(e));
474
+ const s = S(n, "heading"), o = e.split(`
454
475
  `);
455
476
  for (const r of s) {
456
- const { depth: i, position: { start: l, end: c } } = r, a = le(i, t), p = o[l.line - 1].slice(i, c.column), m = "#".repeat(a) + p;
457
- o[l.line - 1] = m, r.depth = a;
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;
478
+ o[c.line - 1] = p, r.depth = a;
458
479
  }
459
480
  return o.join(`
460
481
  `);
461
- }, ce = (e) => ie(e).reverse().sort(f.reverse).reduce((t, n) => te(t, re(n), n), e), ae = (e, t, n, s, o) => {
482
+ }, ie = (e) => oe(e).reverse().sort(g.reverse).reduce((t, n) => Y(t, se(n), n), e), ce = (e, t, n, s, o) => {
462
483
  try {
463
- e = ce(e), e = lt(e, t);
464
- const r = D.md(e);
465
- return ie(e, r).sort(f).reverse().map((i) => {
466
- var B, F;
467
- const { url: l, headingDepth: c } = i, [a, ...p] = $e(l).split("?"), m = a.split(".").pop() ?? "", u = p.join("?"), $ = P(l), W = x($, a);
468
- if (l.startsWith("./") || l.startsWith("../")) {
469
- let g = n(W);
470
- const C = new Ce(u), k = (B = C.get("region")) == null ? void 0 : B.split(O);
471
- g = (k == null ? void 0 : k.reduce(
472
- (y, E, R, { length: fe }) => Je(y, E, R === fe - 1),
473
- g
474
- )) ?? g;
475
- const ue = C.has("skip"), he = C.get("heading") ?? 0, ge = C.has("inline");
476
- let { inline: M } = i;
477
- if (ge && (M = !0), !ue)
478
- if (m === "md") {
479
- const y = it(n, i), E = o ? x(o, $) : $, R = le(c, Number(he));
480
- g = ae(
481
- g,
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);
489
+ 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)
499
+ if (p === "md") {
500
+ const w = rt(n, i), $ = o ? y(o, N) : N, _ = re(l, Number(ue));
501
+ h = ce(
502
+ h,
482
503
  /** p↓: ... */
483
- R,
484
- y,
504
+ _,
505
+ w,
485
506
  W,
486
- E
507
+ $
487
508
  /** p↓: ... */
488
509
  );
489
- } else /^(js|ts)x?|svelte$/i.test(m) && (g = X(
490
- g,
510
+ } else /^(js|ts)x?|svelte$/i.test(p) && (h = z(
511
+ h,
491
512
  "code",
492
513
  /** p↓: ... */
493
- { extension: m, inline: M }
514
+ { extension: p, inline: k }
494
515
  /** p↓: ... */
495
516
  ));
496
- const _ = (F = C.get("wrap")) == null ? void 0 : F.split(O);
497
- return g = (_ == null ? void 0 : _.reduce((y, E) => X(y, E, { extension: m, inline: M }), g)) ?? g, { target: i, content: nt(i, g, o) };
498
- } else throw l.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${l}`);
499
- }).reduce((i, { target: l, content: c }) => te(i, c, l), e);
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) };
519
+ } 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);
500
521
  } catch (r) {
501
522
  throw new Error(`Error populating inclusions in file ${s ?? "(unknown)"}: ${r}`);
502
523
  }
503
- }, pe = (e) => ve(e, "utf-8"), me = (e) => {
504
- const t = ee(e), n = P(t);
505
- return { markdown: pe(t), dir: n, path: t };
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;
506
530
  }, dt = (e, t = !0) => {
507
- const { dir: n, path: s, markdown: o } = me(e), i = ae(o, 0, (l) => pe(ee(n, _e(l))), s);
508
- return t && Y(s, i), i;
509
- }, wt = (e, t = !0) => {
510
- const { path: n, markdown: s } = me(e), o = ce(s);
511
- return t && Y(n, o), o;
531
+ const { path: n, markdown: s } = ae(e), o = ie(s);
532
+ return t && Z(n, o), o;
512
533
  };
513
534
  export {
514
- wt as depopulateMarkdownInclusions,
515
- dt as populateMarkdownInclusions
535
+ dt as depopulateMarkdownInclusions,
536
+ ft as populateMarkdownInclusions
516
537
  };
@@ -1,13 +1,14 @@
1
- (function(m,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):(m=typeof globalThis<"u"?globalThis:m||self,d(m.index={},m.node_fs,m.node_path,m.node_url,m.unified,m.remarkParse,m.unistUtilVisit,m.tsDedent))})(this,function(m,d,h,ue,me,de,he,T){"use strict";var tn=Object.defineProperty;var sn=(m,d,h)=>d in m?tn(m,d,{enumerable:!0,configurable:!0,writable:!0,value:h}):m[d]=h;var pe=(m,d,h)=>sn(m,typeof d!="symbol"?d+"":d,h);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,ge=me.unified().use(de),q={md:e=>ge.parse(e)},N=(e,n)=>{const t=[];return he.visit(e,(s,o,r)=>{if(s.type!=="root"){if(n&&s.type!==n)return;if(j(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,B=(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)},we=(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)},M=(...e)=>e.join(" "),ve=(...e)=>e.join(`
2
- `),$e=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},ye=e=>$e(e)[0];class y{constructor(){pe(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 O=/,\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:"`"}},Ce="-",U={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(F.unsafe),...Object.entries(F.reserved)]},Q=(e,[n,t])=>e.replaceAll(n,t),G=(e,n=Ce)=>{const t=U.static.reduce(Q,e);return U.url.map(([s,o])=>[n+s+n,o]).reduce(Q,t).replaceAll(n," ")},Se=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Ee=["string","number","boolean"],xe=e=>Ee.includes(e),ke=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,Ne=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}},Me=(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?Te(o):void 0).filter((o,r,i)=>o!==void 0||!Me(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 c;for(;(c=r.exec(o))!==null;){const[,l,a,p]=c,u=p.trim();if(!xe(u))throw new Error(`Unsupported type: ${u}`);i.push({name:l,optional:a==="?",type:u})}return{name:s,parameters:i}},P=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}=Ne(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},p)=>{const u=o[p];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})}},H=e=>Object.entries(e).filter(([n])=>n!=="name"&&!isNaN(Number(n))).map(([n,t])=>t),qe=P(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),C=(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,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)=>`
3
4
  `.repeat(n)+e+`
4
- `.repeat(n),R=(e,n,t)=>`<${n}${t?` ${t}`:""}>${C(e)}</${n}>`,z=(e,n,t)=>{const s=qe(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 C(`\`\`\`${r}${i}${C(e)}\`\`\``);case"quote":return t!=null&&t.inline&&!e.includes(`
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(`
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(`
6
7
 
7
- `)?`> ${e}`:C(R(C(e),"blockquote"));case"dropdown":const c=R(G(s.summary),"summary");return C(R([c,e].join(`
8
- `),"details",s.open?"open":void 0))}},I=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,V=["p↓:","pd:","parkdown:"],X=P(["splice(delete?: number, insert?: string)"]),$=(e,n,t)=>(t??I(e)).filter(({value:s})=>s.includes(n)).sort((s,o)=>s.range[0]-o.range[0]),J=e=>({isSpace:e===" ",isNewline:e===`
9
- `||e===void 0}),Z=e=>V.flatMap(n=>$(e,n)).sort((n,t)=>n.range[0]-t.range[0]).reverse().reduce((n,{range:[t,s],value:o})=>{const r=(a,p)=>n.slice(0,a)+n.slice(p),i=J(n[t-1]),c=J(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),K=V.map(e=>new RegExp(`^${Se(e)}(\\?[^\\s]+)`)),Pe=e=>K.map(n=>{var t;return(t=n.exec(e))==null?void 0:t[1]}).filter(Boolean).map(n=>new URLSearchParams(n))[0],A=(e,n)=>(Array.isArray(n)?n.flatMap(t=>$(e,t)):$(e,n)).filter(({value:t})=>K.some(s=>s.test(t))).sort((t,s)=>t.range[0]-s.range[0]).reverse().reduce((t,{range:[s,o],value:r})=>{var l,a;const i=Pe(r),c=["back","front"].map(p=>[p,i.get(p)]).filter(([p,u])=>!!u).reduce((p,[u,g])=>p.set(u,g.split(O)),new Map);return t=((l=c.get("back"))==null?void 0:l.reduce((p,u)=>{const g=X(u);if(g.name==="splice")return p.slice(0,o)+(g.insert??"")+p.slice(o+(g.delete??0))},t))??t,t=((a=c.get("front"))==null?void 0:a.reduce((p,u)=>{const g=X(u);if(g.name==="splice")return p.slice(0,s-(g.delete??0))+(g.insert??"")+p.slice(s)},t))??t,t},e),Re=P(["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)"]),Ie=(e,...n)=>{if(n.length===0)return e;e=A(e,n);const t=I(e),s=([i,c])=>e.slice(i,c),o=new y,r=new y;for(const i of n){const c=$(e,i,t);for(let l=0;l<c.length-1;l+=2){const a=c[l],p=c[l+1];o.push(a.range[1],p.range[0]);const[u,...g]=s([a.range[1],p.range[0]]),S=g[g.length-1];r.push(a.range[0],a.range[1]+(u.trim()?0:1)),r.push(p.range[0],p.range[1]+(S.trim()?0:1))}}return o.collapse(),r.collapse(),T.dedent(o.subtract(r).map(s).filter(Boolean).join("")).trim()},Ae=(e,...n)=>{if(n.length===0)return e;e=A(e,n);const t=I(e),s=([i,c])=>e.slice(i,c),o=new y;for(const i of n){const c=$(e,i,t);for(let l=0;l<c.length-1;l+=2){const a=c[l],p=c[l+1],u=s([p.range[1],p.range[1]+1]).at(-1);o.push(a.range[0],u===`
10
- `?p.range[1]+1:p.range[1])}}o.collapse();const r=new y;return r.push(0,e.length),r.subtract(o),T.dedent(r.collapse().map(s).filter(Boolean).join("")).trim()},De=(e,n,t,s)=>{if(!n)return e;e=A(e,n);const o=$(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],p=o[l+1];r+=e.slice(i,a.range[1]),r+=G(t??a.value,s),i=p.range[0]}r+=e.slice(i);const c=new y;return c.push(0,r.length),c.subtract($(r,n).reduce((l,{range:a})=>(l.push(...a),l),new y)),T.dedent(c.collapse().map(([l,a])=>r.slice(l,a)).filter(Boolean).join("")).trim()},Le=(e,n,t)=>{if(!n)return Z(e);const s=Re(n);switch(s.name){case"extract":e=Ie(e,s.id,...H(s));break;case"remove":e=Ae(e,s.id,...H(s));break;case"replace":e=De(e,s.id,s.with,s.space);break}return t?Z(e):e},We=["http","./","../"],_e=({url:e})=>We.some(n=>e.startsWith(n)),Be=e=>j(e)&&fe(e)&&_e(e),Y=({url:e},n)=>`[](${n?h.join(n,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return M(f._open,f._flag,"BEGIN",f._close)},get end(){return M(f._open,f._flag,"END",f._close)},lengthOf(e){const n=`lines: ${e.split(`
11
- `).length}`,t=`chars: ${e.length}`;return M(f._open,f._flag,"length",n,t,f._close)}},ee=e=>n=>j(n)&&n.value===f[e],Fe=({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}),Qe=(e,n,t)=>(e.inline?M:ve)(Y(e,t),f.begin,f.lengthOf(n),n,f.end),Ge=e=>{const n=N(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`)},He=(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 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(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(we(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()},ne=(e,n)=>{n??(n=q.md(e));const t=Ge(n),s=N(n,"link").filter(Be),o=N(n,"html").sort(v),r=o.filter(ee("begin")),i=o.filter(ee("end")),c=He(r,i);return ze(e,s,c).map(a=>Array.isArray(a)?Ue(a[0],a[1].close,t(a[0])):Fe(a,t(a)))},Ve=(e,{url:n})=>t=>e(h.join(h.dirname(n),t)),te=(...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=q.md(e));const s=N(t,"heading"),o=e.split(`
12
- `);for(const r of s){const{depth:i,position:{start:c,end:l}}=r,a=te(i,n),p=o[c.line-1].slice(i,l.column),u="#".repeat(a)+p;o[c.line-1]=u,r.depth=a}return o.join(`
13
- `)},se=e=>ne(e).reverse().sort(v.reverse).reduce((n,t)=>B(n,Y(t),t),e),oe=(e,n,t,s,o)=>{try{e=se(e),e=Xe(e,n);const r=q.md(e);return ne(e,r).sort(v).reverse().map(i=>{var le,ae;const{url:c,headingDepth:l}=i,[a,...p]=h.basename(c).split("?"),u=a.split(".").pop()??"",g=p.join("?"),S=h.dirname(c),ce=h.join(S,a);if(c.startsWith("./")||c.startsWith("../")){let w=t(ce);const E=new ue.URLSearchParams(g),D=(le=E.get("region"))==null?void 0:le.split(O);w=(D==null?void 0:D.reduce((x,k,_,{length:nn})=>Le(x,k,_===nn-1),w))??w;const Ke=E.has("skip"),Ye=E.get("heading")??0,en=E.has("inline");let{inline:L}=i;if(en&&(L=!0),!Ke)if(u==="md"){const x=Ve(t,i),k=o?h.join(o,S):S,_=te(l,Number(Ye));w=oe(w,_,x,ce,k)}else/^(js|ts)x?|svelte$/i.test(u)&&(w=z(w,"code",{extension:u,inline:L}));const W=(ae=E.get("wrap"))==null?void 0:ae.split(O);return w=(W==null?void 0:W.reduce((x,k)=>z(x,k,{extension:u,inline:L}),w))??w,{target:i,content:Qe(i,w,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})=>B(i,l,c),e)}catch(r){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${r}`)}},re=e=>d.readFileSync(e,"utf-8"),ie=e=>{const n=h.resolve(e),t=h.dirname(n);return{markdown:re(n),dir:t,path:n}},Je=(e,n=!0)=>{const{dir:t,path:s,markdown:o}=ie(e),i=oe(o,0,c=>re(h.resolve(t,ye(c))),s);return n&&d.writeFileSync(s,i),i},Ze=(e,n=!0)=>{const{path:t,markdown:s}=ie(e),o=se(s);return n&&d.writeFileSync(t,o),o};m.depopulateMarkdownInclusions=Ze,m.populateMarkdownInclusions=Je,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
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"})});
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.9",
5
+ "version": "0.0.11",
6
6
  "main": "./dist/index.js",
7
7
  "bin": "./dist/cli.js",
8
8
  "files": [