@p-buddy/parkdown 0.0.25 → 0.0.27

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/README.md CHANGED
@@ -44,7 +44,7 @@ populateMarkdownInclusions(file, writeFile);
44
44
 
45
45
  [](./.assets/authoring.md)
46
46
  <!-- p↓ BEGIN -->
47
- <!-- p↓ length lines: 663 chars: 22786 -->
47
+ <!-- p↓ length lines: 690 chars: 23471 -->
48
48
  ## Authoring
49
49
 
50
50
  You author inclusions in your markdown files using a link with no text i.e. `[](<url>)`, where `<url>` points to some local or remote text resource (e.g.`./other.md`, `https://example.com/remote.md`).
@@ -55,7 +55,7 @@ These links can be rendered either [inline](#inline) or [block](#block), dependi
55
55
 
56
56
  Inline inclusions occur when your _text-less_ link has 1 or more siblings (meaning it's **not** the only node in a [paragraph](https://www.markdownguide.org/basic-syntax/#paragraphs-1)).
57
57
 
58
- > **CAUTION:** [parkdown](https://www.npmjs.com/package/@p-buddy/parkdown) will not protect you from authoring inline inclusions that should actually be [block](#block) inclusions to be valid markdown.
58
+ > **CAUTION:** [parkdown](https://www.npmjs.com/package/@p-buddy/parkdown) will not protect you from authoring inline inclusions that create invalid markdown (and thus should instead be [block](#block) inclusions).
59
59
 
60
60
  There are two equivalent ways to author inline inclusions, [single-line](#single-line) or [multi-line](#multi-line), and which you choose depends solely on how you want your raw markdown to look (it will **not** affect the rendered output).
61
61
 
@@ -256,7 +256,7 @@ Before...
256
256
 
257
257
  [](.assets/query.md?heading=-1)
258
258
  <!-- p↓ BEGIN -->
259
- <!-- p↓ length lines: 450 chars: 18080 -->
259
+ <!-- p↓ length lines: 477 chars: 18752 -->
260
260
  ### Query parameters
261
261
 
262
262
  You can pass query parameters to your inclusion links to control how their content is processed and included within your markdown.
@@ -265,28 +265,33 @@ You can pass query parameters to your inclusion links to control how their conte
265
265
 
266
266
  [](src/include.ts?&region=extract(query))
267
267
  <!-- p↓ BEGIN -->
268
- <!-- p↓ length lines: 14 chars: 560 -->
268
+ <!-- p↓ length lines: 16 chars: 520 -->
269
269
 
270
270
  ```ts
271
271
  const params = new URLSearchParams(query);
272
272
  const entries = (key: string) => {
273
- const values = Array.from(params.entries()).filter(([k]) => k === key).map(([_, v]) => v);
273
+ const values = Array.from(params.entries())
274
+ .filter(([k]) => k === key)
275
+ .map(([_, v]) => v);
274
276
  return values.length >= 1 ? values.join(",") : undefined;
275
277
  };
276
- const regions = entries("region")?.split(COMMA_NOT_IN_PARENTHESIS);
277
- const skip = params.has("skip");
278
- const headingModfiier = params.get("heading") ?? 0;
279
- const inlineOverride = params.has("inline");
280
- const wraps = params.get("wrap")?.split(COMMA_NOT_IN_PARENTHESIS);
278
+ const regions = entries("region")?.split(COMMA_NOT_IN_PARENTHESIS);
279
+ const skip = params.has("skip");
280
+ const headingModfiier = params.get("heading") ?? 0;
281
+ const inlineOverride = params.has("inline");
282
+ const wraps = params.get("wrap")?.split(COMMA_NOT_IN_PARENTHESIS);
281
283
  ```
282
284
 
283
285
  <!-- p↓ END -->
284
286
 
287
+ [](.assets/region.md?heading=-1)
288
+ <!-- p↓ BEGIN -->
289
+ <!-- p↓ length lines: 183 chars: 8530 -->
285
290
  #### `region`
286
291
 
287
- Either extract, remove, or replace content from the included file based on the provided specifier(s).
292
+ Modify content from the included file based on regions designated by comments.
288
293
 
289
- Specifiers will be searched for within the file's comments, and are expected to come in pairs / bookend the desired region, like so:
294
+ Comments that act as region boundaries will be identified by their specifiers, and are expected to come in pairs / bookend the desired region, like so:
290
295
 
291
296
  ```ts
292
297
  /** some-specifier */
@@ -294,10 +299,31 @@ Specifiers will be searched for within the file's comments, and are expected to
294
299
  /** some-specifier */
295
300
  ```
296
301
 
297
- Though comment regions can be nested, it is **CRITICAL** that regions that are retrieved with the _same_ specifier are **NOT** nested.
302
+ > **NOTE:** Because comments are expected to come in consecutive pairs, nesting regions that are retrieved with the _same_ specifier will *NOT* work as expected (since the nesting will not be respected).
298
303
 
299
304
  Identifiers will be searched for within the text of a comment, split by spaces (i.e. `/* some-specifier */` has a single identifier, but `/* some specifier */` can be identified by either `some` or `specifier`).
300
305
 
306
+ It is generally **BEST PRACTICE** to include a _parkdown prefix_ in your comment text, as all parkdown-prefixed comments will be stripped as a post-processing step. Otherwise, non-prefixed comment boundaries will be left in the included content, regardless of how they are processed.
307
+
308
+ The supported prefixes are:
309
+
310
+ [](src/comments.ts?region=extract(prefixes))
311
+ <!-- p↓ BEGIN -->
312
+ <!-- p↓ length lines: 12 chars: 101 -->
313
+
314
+ ```ts
315
+ const prefixes = [
316
+ "(pd)",
317
+ "(p↓)",
318
+ "(parkdown)",
319
+ "pd:",
320
+ "p↓:",
321
+ "parkdown:",
322
+ ]
323
+ ```
324
+
325
+ <!-- p↓ END -->
326
+
301
327
  Below is the currently supported API for the `region` query parameter, where each defined method signature can be _invoked_ as a value for the `region` parameter, for example:
302
328
 
303
329
  - `[](<url>?region=method(argument))`
@@ -443,6 +469,7 @@ const definitions = [
443
469
  ]
444
470
  ```
445
471
 
472
+ <!-- p↓ END -->
446
473
  <!-- p↓ END -->
447
474
 
448
475
  #### `skip`
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { Command as r } from "@commander-js/extra-typings";
3
3
  import { populateMarkdownInclusions as l, depopulateMarkdownInclusions as a } from "@p-buddy/parkdown";
4
4
  import f from "chokidar";
5
- const c = "0.0.25", p = new r().version(c).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", (e, o) => (o.push(e), o), new Array()).option("-w, --watch", "Watch the file(s) for changes and update automatically", !1).parse(), { inclusions: u, depopulate: d, file: t, write: n, watch: h } = p.opts();
5
+ const c = "0.0.27", p = new r().version(c).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", (e, o) => (o.push(e), o), new Array()).option("-w, --watch", "Watch the file(s) for changes and update automatically", !1).parse(), { inclusions: u, depopulate: d, file: t, write: n, watch: h } = p.opts();
6
6
  t.length === 0 && t.push("README.md");
7
7
  const m = [
8
8
  [l, !u],
package/dist/index.js CHANGED
@@ -1,20 +1,20 @@
1
- var be = Object.defineProperty;
2
- var ye = (e, t, n) => t in e ? be(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var k = (e, t, n) => ye(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { writeFileSync as ie, readFileSync as Ce } from "node:fs";
5
- import { basename as Ee, dirname as Q, join as O, resolve as le } from "node:path";
6
- import { URLSearchParams as Se } from "node:url";
7
- import { unified as xe } from "unified";
1
+ var Ce = Object.defineProperty;
2
+ var Ee = (e, t, n) => t in e ? Ce(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var _ = (e, t, n) => Ee(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { writeFileSync as ae, readFileSync as Se } from "node:fs";
5
+ import { basename as xe, dirname as Q, join as A, resolve as ce } from "node:path";
6
+ import { URLSearchParams as _e } from "node:url";
7
+ import { unified as Ne } from "unified";
8
8
  import ke from "remark-parse";
9
- import { visit as _e } from "unist-util-visit";
10
- import { dedent as Ne } from "ts-dedent";
9
+ import { visit as Me } from "unist-util-visit";
10
+ import { dedent as Ae } from "ts-dedent";
11
11
  const f = (e, t) => e.position.start.offset - t.position.start.offset;
12
12
  f.reverse = (e, t) => f(t, e);
13
- const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, Me = xe().use(ke), z = {
14
- md: (e) => Me.parse(e)
15
- }, R = (e, t) => {
13
+ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, Oe = Ne().use(ke), z = {
14
+ md: (e) => Oe.parse(e)
15
+ }, O = (e, t) => {
16
16
  const n = [];
17
- return _e(e, (s, r, o) => {
17
+ return Me(e, (s, r, o) => {
18
18
  if (s.type !== "root") {
19
19
  if (t && s.type !== t) return;
20
20
  if (G(s)) {
@@ -23,19 +23,19 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
23
23
  }
24
24
  }
25
25
  }), n;
26
- }, Ae = (e) => e.children.length === 0, ae = (e, t, ...n) => {
26
+ }, Re = (e) => e.children.length === 0, pe = (e, t, ...n) => {
27
27
  if (n.length === 0) throw new Error("No nodes to replace content from");
28
28
  n.sort(f);
29
29
  const s = n.at(0), r = n.at(-1);
30
30
  return e.slice(0, s.position.start.offset) + t + e.slice(r.position.end.offset);
31
- }, Oe = (e, t, n) => {
31
+ }, Te = (e, t, n) => {
32
32
  const s = Math.min(t.position.end.offset, n.position.end.offset), r = Math.max(t.position.start.offset, n.position.start.offset);
33
33
  return e.slice(s, r);
34
- }, A = (...e) => e.join(" "), Re = (...e) => e.join(`
35
- `), Te = (e) => {
34
+ }, M = (...e) => e.join(" "), We = (...e) => e.join(`
35
+ `), Ie = (e) => {
36
36
  const t = e.split("?");
37
37
  return t.length > 1 ? [t.slice(0, -1).join("?"), t.at(-1)] : [e, ""];
38
- }, Ie = (e) => Te(e)[0], J = /,\s*(?![^()]*\))/, Z = {
38
+ }, Pe = (e) => Ie(e)[0], J = /,\s*(?![^()]*\))/, Z = {
39
39
  reserved: {
40
40
  semi: ";",
41
41
  slash: "/",
@@ -63,7 +63,7 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
63
63
  line: `
64
64
  `
65
65
  }
66
- }, Pe = "-", K = {
66
+ }, je = "-", K = {
67
67
  static: [
68
68
  ["'''", '"'],
69
69
  ["''", "'"],
@@ -74,21 +74,21 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
74
74
  ...Object.entries(Z.unsafe),
75
75
  ...Object.entries(Z.reserved)
76
76
  ]
77
- }, Y = (e, [t, n]) => e.replaceAll(t, n), I = (e, t = Pe) => {
77
+ }, Y = (e, [t, n]) => e.replaceAll(t, n), W = (e, t = je) => {
78
78
  const n = K.static.reduce(Y, e);
79
79
  return K.url.map(([s, r]) => [t + s + t, r]).reduce(Y, n).replaceAll(t, " ");
80
- }, We = ["string", "number", "boolean"], je = (e) => We.includes(e), De = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, Le = (e) => {
81
- const t = e.match(De);
80
+ }, De = ["string", "number", "boolean"], Le = (e) => De.includes(e), Ue = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, qe = (e) => {
81
+ const t = e.match(Ue);
82
82
  if (!t) throw new Error(`Invalid invocation: ${e}`);
83
83
  const [, n, s = ""] = t;
84
84
  if (!s.trim()) return { name: n, parameters: [] };
85
- const r = qe(s);
85
+ const r = Fe(s);
86
86
  return { name: n, parameters: r };
87
- }, Ue = (e, t) => {
87
+ }, Be = (e, t) => {
88
88
  for (let n = t + 1; n < e.length; n++)
89
89
  if (e[n] !== void 0) return !1;
90
90
  return !0;
91
- }, qe = (e) => {
91
+ }, Fe = (e) => {
92
92
  const t = [];
93
93
  let n = "", s = !1;
94
94
  for (let r = 0; r < e.length; r++) {
@@ -98,46 +98,46 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
98
98
  s = !s, n += l ? "'''" : "''", r += l ? 2 : 1;
99
99
  } else i ? (s = !s, n += o) : o === "," && !s ? (t.push(n.trim()), n = "") : n += o;
100
100
  }
101
- return t.push(n.trim()), t.map((r) => r === "" ? void 0 : r ? Be(r) : void 0).filter((r, o, i) => r !== void 0 || !Ue(i, o));
102
- }, Be = (e) => {
101
+ return t.push(n.trim()), t.map((r) => r === "" ? void 0 : r ? He(r) : void 0).filter((r, o, i) => r !== void 0 || !Be(i, o));
102
+ }, He = (e) => {
103
103
  const t = e.length >= 2 && e[0] === "'" && e.at(-1) === "'", n = t && e.length >= 4 && e[1] === "'" && e.at(-2) === "'";
104
104
  return !t || n ? e : e.slice(1, -1);
105
- }, Fe = (e) => {
106
- const t = /^(\w+)(?:\(([^)]*)\))?/, n = e.match(t);
105
+ }, Qe = (e) => {
106
+ const t = /^([\w-_]+)(?:\(([^)]*)\))?/, n = e.match(t);
107
107
  if (!n) return { name: e };
108
108
  const [, s, r] = n;
109
109
  if (!r) return { name: s };
110
- const o = /(\w+)(\?)?:\s*([^,]+)/g, i = [];
110
+ const o = /([\w-_]+)(\?)?:\s*([^,]+)/g, i = [];
111
111
  let l;
112
112
  for (; (l = o.exec(r)) !== null; ) {
113
- const [, a, c, u] = l, p = u.trim();
114
- if (!je(p)) throw new Error(`Unsupported type: ${p}`);
115
- i.push({ name: a, optional: c === "?", type: p });
113
+ const [, c, a, u] = l, p = u.trim();
114
+ if (!Le(p)) throw new Error(`Unsupported type: ${p}`);
115
+ i.push({ name: c, optional: a === "?", type: p });
116
116
  }
117
117
  return { name: s, parameters: i };
118
- }, N = (e) => {
119
- const t = e.map(Fe).reduce(
118
+ }, R = (e) => {
119
+ const t = e.map(Qe).reduce(
120
120
  (n, { name: s, parameters: r }) => n.set(s, r),
121
121
  /* @__PURE__ */ new Map()
122
122
  );
123
123
  return (n) => {
124
- const { name: s, parameters: r } = Le(n);
124
+ const { name: s, parameters: r } = qe(n);
125
125
  if (!t.has(s))
126
126
  throw new Error(`Unknown method: ${s}`);
127
127
  const o = t.get(s);
128
128
  if (o === void 0)
129
129
  return { name: s };
130
130
  if (r.length > o.length) {
131
- const i = o.filter(({ optional: a }) => !a).length, l = i === o.length ? i.toString() : `${i} - ${o.length}`;
131
+ const i = o.filter(({ optional: c }) => !c).length, l = i === o.length ? i.toString() : `${i} - ${o.length}`;
132
132
  throw new Error(`Too many parameters: ${r.length} for method '${s}' (expected: ${l})`);
133
133
  }
134
- return o.reduce((i, { name: l, optional: a, type: c }, u) => {
134
+ return o.reduce((i, { name: l, optional: c, type: a }, u) => {
135
135
  const p = r[u];
136
136
  if (p === void 0) {
137
- if (a) return i;
137
+ if (c) return i;
138
138
  throw new Error(`Missing required parameter: ${l} for method '${s}'`);
139
139
  }
140
- switch (c) {
140
+ switch (a) {
141
141
  case "string":
142
142
  i[l] = p;
143
143
  break;
@@ -146,12 +146,12 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
146
146
  i[l] = JSON.parse(p);
147
147
  break;
148
148
  }
149
- if (typeof i[l] !== c)
150
- throw new Error(`Invalid type: ${l} must be ${c}, got ${typeof i[l]} for method '${s}'`);
149
+ if (typeof i[l] !== a)
150
+ throw new Error(`Invalid type: ${l} must be ${a}, got ${typeof i[l]} for method '${s}'`);
151
151
  return i;
152
152
  }, { name: s });
153
153
  };
154
- }, F = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), He = [
154
+ }, F = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Ge = [
155
155
  /**
156
156
  * Wraps the content in a markdown-formatted code block.
157
157
  * @param lang The language of the code block (defaults to the file extension).
@@ -181,10 +181,10 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
181
181
  * @example [](<url>?wrap=dropdown(hello_world,,_))
182
182
  */
183
183
  "dropdown(summary: string, open?: boolean, space?: string)"
184
- ], Qe = N(He), E = (e, t = 1) => `
184
+ ], ze = R(Ge), E = (e, t = 1) => `
185
185
  `.repeat(t) + e + `
186
186
  `.repeat(t), B = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${E(e)}</${t}>`, ee = (e, t, n) => {
187
- const s = Qe(t);
187
+ const s = ze(t);
188
188
  switch (s.name) {
189
189
  case "code":
190
190
  if (!s.lang && !s.meta && (n == null ? void 0 : n.inline) && !e.includes(`
@@ -196,15 +196,15 @@ const G = (e) => e.position !== void 0 && e.position.start.offset !== void 0 &&
196
196
 
197
197
  `) ? `> ${e}` : E(B(E(e), "blockquote"));
198
198
  case "dropdown":
199
- const l = B(I(s.summary), "summary");
199
+ const l = B(W(s.summary), "summary");
200
200
  return E(B([l, e].join(`
201
201
  `), "details", s.open ? "open" : void 0));
202
202
  }
203
203
  };
204
204
  class d {
205
205
  constructor(...t) {
206
- k(this, "_intervals", []);
207
- k(this, "collapsed", !0);
206
+ _(this, "_intervals", []);
207
+ _(this, "collapsed", !0);
208
208
  for (const [n, s] of t)
209
209
  this.push(n, s);
210
210
  }
@@ -222,12 +222,12 @@ class d {
222
222
  collapse(t = !1) {
223
223
  const { _intervals: n, collapsed: s } = this;
224
224
  if (s && !t || !n.length) return this;
225
- n.sort((l, a) => l[0] - a[0]);
225
+ n.sort((l, c) => l[0] - c[0]);
226
226
  const r = [];
227
227
  let [o, i] = n[0];
228
228
  for (let l = 1; l < n.length; l++) {
229
- const [a, c] = n[l];
230
- a <= i ? i = Math.max(i, c) : (r.push([o, i]), o = a, i = c);
229
+ const [c, a] = n[l];
230
+ c <= i ? i = Math.max(i, a) : (r.push([o, i]), o = c, i = a);
231
231
  }
232
232
  return r.push([o, i]), this._intervals = r, this.collapsed = !0, this;
233
233
  }
@@ -238,12 +238,12 @@ class d {
238
238
  let r = [...n];
239
239
  for (const [o, i] of s) {
240
240
  const l = [];
241
- for (const [a, c] of r) {
242
- if (i <= a || o >= c) {
243
- l.push([a, c]);
241
+ for (const [c, a] of r) {
242
+ if (i <= c || o >= a) {
243
+ l.push([c, a]);
244
244
  continue;
245
245
  }
246
- o > a && l.push([a, o]), i < c && l.push([i, c]);
246
+ o > c && l.push([c, o]), i < a && l.push([i, a]);
247
247
  }
248
248
  r = l;
249
249
  }
@@ -275,37 +275,36 @@ class d {
275
275
  return this;
276
276
  }
277
277
  }
278
- const P = (e) => {
278
+ const I = (e) => {
279
279
  const t = /(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm, n = [];
280
280
  let s;
281
281
  for (; (s = t.exec(e)) !== null; ) {
282
- const r = [s.index, s.index + s[0].length], o = Ge(s[0]).trim();
282
+ const r = [s.index, s.index + s[0].length], o = Xe(s[0]).trim();
283
283
  n.push({ range: r, value: o });
284
284
  }
285
285
  return n;
286
- }, Ge = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, ze = [
287
- "p↓:",
286
+ }, Xe = (e) => e.startsWith("//") ? e.slice(2) : e.startsWith("/*") ? e.slice(2, -2) : e.startsWith("<!--") ? e.slice(4, -3) : e, Ve = [
287
+ "(pd)",
288
+ "(p↓)",
289
+ "(parkdown)",
288
290
  "pd:",
291
+ "p↓:",
289
292
  "parkdown:"
290
- ], Xe = [
291
- "splice(delete?: number, insert?: string)"
292
- ];
293
- N(Xe);
294
- const ce = (e, t) => e.range[0] - t.range[0], X = (e, t, n) => (n ?? P(e)).filter(({ value: s }) => s.split(" ").includes(t)).sort(ce), te = (e) => ({
293
+ ], ue = (e, t) => e.range[0] - t.range[0], X = (e, t, n) => (n ?? I(e)).filter(({ value: s }) => s.split(" ").includes(t)).sort(ue), te = (e) => ({
295
294
  isSpace: e === " ",
296
295
  isNewline: e === `
297
296
  ` || e === void 0
298
- }), ne = (e) => ze.flatMap((t) => X(e, t)).sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, s], value: r }) => {
299
- const o = (b, y) => t.slice(0, b) + t.slice(y), i = te(t[n - 1]), l = te(t[s]), a = s === t.length;
300
- let c = n;
301
- for (; c > 0 && t[c - 1] !== `
302
- `; ) c--;
297
+ }), ne = (e) => Ve.flatMap((t) => X(e, t)).sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, s], value: r }) => {
298
+ const o = (b, y) => t.slice(0, b) + t.slice(y), i = te(t[n - 1]), l = te(t[s]), c = s === t.length;
299
+ let a = n;
300
+ for (; a > 0 && t[a - 1] !== `
301
+ `; ) a--;
303
302
  let u = s;
304
303
  for (; u < t.length && t[u] !== `
305
304
  `; ) u++;
306
- const p = t.slice(c, n).trim() === "" && t.slice(s, u).trim() === "";
307
- return i.isNewline && l.isNewline ? o(n - (a ? 1 : 0), s + 1) : i.isNewline || p ? o(c, s + (l.isSpace || l.isNewline ? 1 : 0)) : o(n - (i.isSpace ? 1 : 0), s);
308
- }, e), Ve = [
305
+ const p = t.slice(a, n).trim() === "" && t.slice(s, u).trim() === "";
306
+ return i.isNewline && l.isNewline ? o(n - (c ? 1 : 0), s + 1) : i.isNewline || p ? o(a, s + (l.isSpace || l.isNewline ? 1 : 0)) : o(n - (i.isSpace ? 1 : 0), s);
307
+ }, e), Je = [
309
308
  /**
310
309
  * Extract regions from the retrieved content between comments that INCLUDE the specified ids.
311
310
  * @param id The id of the comment to extract.
@@ -410,143 +409,145 @@ const ce = (e, t) => e.range[0] - t.range[0], X = (e, t, n) => (n ?? P(e)).filte
410
409
  * Helpful when trying to determine fine-grained edits (e.g. trimming, splicing, etc.).
411
410
  */
412
411
  "debug()"
413
- ], Je = N(Ve), $ = (e, t, n) => {
412
+ ], Ze = R(Je), $ = (e, t, n) => {
414
413
  const s = X(e, t, n), r = [];
415
414
  for (let o = 0; o < s.length - 1; o += 2)
416
415
  r.push([s[o], s[o + 1]]);
417
416
  return r;
418
- }, Ze = (e, t, n) => new d(...X(e, t, n).map(({ range: s }) => s)), W = (e) => Ne(e).trim(), Ke = (e, ...t) => {
417
+ }, Ke = (e, t, n) => new d(...X(e, t, n).map(({ range: s }) => s)), P = (e) => Ae(e).trim(), se = (e, t) => e[t] !== void 0 && /[^\S\r\n]/.test(e[t]), re = (e, t) => Math.max(0, Math.min(e.length, t)), Ye = (e, ...t) => {
419
418
  if (t.length === 0) return e;
420
- const n = P(e), s = new d(), r = new d();
421
- for (const o of t)
422
- for (const [i, l] of $(e, o, n)) {
423
- s.push(i.range[1], l.range[0]);
424
- const [a, ...c] = e.slice(i.range[1], l.range[0]), u = c[c.length - 1];
425
- r.push(i.range[0], i.range[1] + (a.trim() ? 0 : 1)), r.push(l.range[0], l.range[1] + (u.trim() ? 0 : 1));
426
- }
427
- return W(s.subtract(r).slice(e));
428
- }, Ye = (e, ...t) => {
419
+ const n = I(e);
420
+ return P(
421
+ new d(...t.flatMap(
422
+ (s) => $(e, s, n).map(([{ range: [r] }, { range: [, o] }]) => {
423
+ for (; se(e, r); ) r--;
424
+ for (; se(e, o); ) o++;
425
+ return [re(e, r), re(e, o)];
426
+ })
427
+ )).slice(e)
428
+ );
429
+ }, et = (e, ...t) => {
429
430
  if (t.length === 0) return e;
430
- const n = P(e), s = ([o, i]) => e.slice(o, i), r = new d();
431
- for (const o of t)
432
- for (const [i, l] of $(e, o, n)) {
433
- const a = s([l.range[1], l.range[1] + 1]).at(-1);
434
- r.push(i.range[0], a === `
435
- ` ? l.range[1] + 1 : l.range[1]);
436
- }
437
- return W(new d([0, e.length]).subtract(r).slice(e));
438
- }, et = (e, t, n, s) => {
431
+ const n = I(e);
432
+ return P(
433
+ new d([0, e.length]).subtract(
434
+ new d(...t.flatMap(
435
+ (s) => $(e, s, n).map(([{ range: [, r] }, { range: [o] }]) => [r, o])
436
+ ))
437
+ ).slice(e)
438
+ );
439
+ }, tt = (e, t, n, s) => {
439
440
  if (!t) return e;
440
441
  let r = "", o = 0;
441
442
  for (const [i, l] of $(e, t))
442
- r += e.slice(o, i.range[1]), r += I(n ?? i.value, s), o = l.range[0];
443
- return r += e.slice(o), W(
444
- new d([0, r.length]).subtract(Ze(r, t)).slice(r)
443
+ r += e.slice(o, i.range[1]), r += W(n ?? i.value, s), o = l.range[0];
444
+ return r += e.slice(o), P(
445
+ new d([0, r.length]).subtract(Ke(r, t)).slice(r)
445
446
  );
446
- }, tt = (e, t, n, s, r, o) => {
447
+ }, nt = (e, t, n, s, r, o) => {
447
448
  if (!t) return e;
448
- const i = !(s === void 0 || s < 0), l = r ? I(r, o) : "", a = s ?? 0, c = P(e), u = new d(...c.map(({ range: p }) => p));
449
- return $(e, t, c).map((p) => p[n === "start" ? 0 : 1]).sort(ce).reverse().reduce((p, { range: b }) => {
450
- const y = new d(), M = i ? 1 : -1;
451
- let S = Math.abs(a), m = i ? b[1] : b[0];
449
+ const i = !(s === void 0 || s < 0), l = r ? W(r, o) : "", c = s ?? 0, a = I(e), u = new d(...a.map(({ range: p }) => p));
450
+ return $(e, t, a).map((p) => p[n === "start" ? 0 : 1]).sort(ue).reverse().reduce((p, { range: b }) => {
451
+ const y = new d(), k = i ? 1 : -1;
452
+ let S = Math.abs(c), m = i ? b[1] : b[0];
452
453
  for (; S > 0 && m < p.length && m >= 0; )
453
- u.test(m) || (y.push(m), S--), m += M;
454
+ u.test(m) || (y.push(m), S--), m += k;
454
455
  return m = Math.min(Math.max(0, m), p.length), p = p.slice(0, m) + l + p.slice(m), new d([0, p.length]).subtract(y.offset(i ? 0 : l.length)).slice(p);
455
456
  }, e);
456
- }, nt = (e, t, n, s, r) => {
457
+ }, st = (e, t, n, s, r) => {
457
458
  let o = "", i = 0;
458
- [n, s] = [n, s ?? ""].map((a) => I(a, r));
459
+ [n, s] = [n, s ?? ""].map((c) => W(c, r));
459
460
  const l = t ? $(e, t) : [[{ range: [0, 0] }, { range: [e.length, e.length] }]];
460
- for (const [a, c] of l)
461
- o += e.slice(i, a.range[1]), o += e.slice(a.range[1], c.range[0]).replaceAll(n, s), i = c.range[0];
461
+ for (const [c, a] of l)
462
+ o += e.slice(i, c.range[1]), o += e.slice(c.range[1], a.range[0]).replaceAll(n, s), i = a.range[0];
462
463
  return o += e.slice(i), o;
463
- }, st = (e, t) => {
464
+ }, rt = (e, t) => {
464
465
  if (!t) return e;
465
466
  let n = "", s = 0;
466
467
  for (const [r, o] of $(e, t))
467
468
  n += e.slice(s, r.range[1]), n += e.slice(r.range[1], o.range[0]).replaceAll(/[\s\n]+/g, " "), s = o.range[0];
468
469
  return n += e.slice(s), n;
469
- }, se = (e, t, n) => {
470
- var o, i, l, a;
470
+ }, oe = (e, t, n) => {
471
+ var o, i, l, c;
471
472
  if (!t) return e;
472
- const s = (c) => /\s/.test(e[c]), r = new d();
473
- for (const [c, u] of $(e, t)) {
473
+ const s = (a) => /\s/.test(e[a]), r = new d();
474
+ for (const [a, u] of $(e, t)) {
474
475
  if ((o = n.start) != null && o.left) {
475
- let p = c.range[0] - 1;
476
+ let p = a.range[0] - 1;
476
477
  for (; s(p); ) r.push(p--);
477
478
  }
478
479
  if ((i = n.start) != null && i.right) {
479
- let p = c.range[1];
480
+ let p = a.range[1];
480
481
  for (; s(p); ) r.push(p++);
481
482
  }
482
483
  if ((l = n.end) != null && l.left) {
483
484
  let p = u.range[0] - 1;
484
485
  for (; s(p); ) r.push(p--);
485
486
  }
486
- if ((a = n.end) != null && a.right) {
487
+ if ((c = n.end) != null && c.right) {
487
488
  let p = u.range[1];
488
489
  for (; s(p); ) r.push(p++);
489
490
  }
490
491
  }
491
492
  return new d([0, e.length]).subtract(r).slice(e);
492
- }, rt = (e, t, n) => {
493
+ }, ot = (e, t, n) => {
493
494
  if (!t) return ne(e);
494
- const s = Je(t);
495
+ const s = Ze(t);
495
496
  switch (s.name) {
496
497
  case "extract":
497
- e = Ke(e, s.id, ...F(s));
498
+ e = Ye(e, s.id, ...F(s));
498
499
  break;
499
500
  case "remove":
500
- e = Ye(e, s.id, ...F(s));
501
+ e = et(e, s.id, ...F(s));
501
502
  break;
502
503
  case "replace":
503
- e = et(e, s.id, s.with, s.space);
504
+ e = tt(e, s.id, s.with, s.space);
504
505
  break;
505
506
  case "splice-start":
506
507
  case "splice-end": {
507
- const { deleteCount: r, insert: o, space: i, id: l } = s, a = s.name === "splice-start" ? "start" : "end";
508
- e = tt(e, l, a, r, o, i);
508
+ const { deleteCount: r, insert: o, space: i, id: l } = s, c = s.name === "splice-start" ? "start" : "end";
509
+ e = nt(e, l, c, r, o, i);
509
510
  break;
510
511
  }
511
512
  case "trim-start":
512
513
  case "trim-end": {
513
514
  const { left: r, right: o, id: i } = s, l = s.name === "trim-start" ? "start" : "end";
514
- e = se(e, i, { [l]: { left: r ?? !0, right: o ?? !0 } });
515
+ e = oe(e, i, { [l]: { left: r ?? !0, right: o ?? !0 } });
515
516
  break;
516
517
  }
517
518
  case "trim": {
518
519
  const { inside: r, outside: o, id: i } = s;
519
- e = se(e, i, { start: { left: o ?? !0, right: r ?? !0 }, end: { left: r ?? !0, right: o ?? !0 } });
520
+ e = oe(e, i, { start: { left: o ?? !0, right: r ?? !0 }, end: { left: r ?? !0, right: o ?? !0 } });
520
521
  break;
521
522
  }
522
523
  case "single-line":
523
- e = st(e, s.id);
524
+ e = rt(e, s.id);
524
525
  break;
525
526
  case "remap":
526
- e = nt(e, s.id, s.from, s.to, s.space);
527
+ e = st(e, s.id, s.from, s.to, s.space);
527
528
  break;
528
529
  }
529
- return e = n && s.name !== "debug" ? ne(e) : e, n ? W(e) : e;
530
- }, ot = [
530
+ return s.name === "debug" && console.log("debug!!!", s), e = n && s.name !== "debug" ? ne(e) : e, n ? P(e) : e;
531
+ }, it = [
531
532
  "recipe(id: string)"
532
- ], it = [
533
+ ], lt = [
533
534
  "recipe(id: string, 0?: string, 1?: string, 2?: string)"
534
- ], re = {
535
- register: N(ot),
536
- apply: N(it)
537
- }, _ = class _ {
535
+ ], ie = {
536
+ register: R(it),
537
+ apply: R(lt)
538
+ }, N = class N {
538
539
  constructor() {
539
- k(this, "recipes", /* @__PURE__ */ new Map());
540
+ _(this, "recipes", /* @__PURE__ */ new Map());
540
541
  }
541
542
  tryStore(t) {
542
- const n = _.Entries(t);
543
+ const n = N.Entries(this.apply(t));
543
544
  for (let s = 0; s < n.length; s++) {
544
545
  const [r, o] = n[s];
545
546
  if (r !== "register") continue;
546
- const i = re.register(o);
547
+ const i = ie.register(o);
547
548
  switch (i.name) {
548
549
  case "recipe":
549
- const l = n.slice(s + 1).filter(([a]) => a !== "register").map(([a, c]) => `${a}=${c}`).join("&");
550
+ const l = n.slice(s + 1).filter(([c]) => c !== "register").map(([c, a]) => `${c}=${a}`).join("&");
550
551
  this.recipes.set(i.id, l);
551
552
  break;
552
553
  default:
@@ -555,17 +556,17 @@ const ce = (e, t) => e.range[0] - t.range[0], X = (e, t, n) => (n ?? P(e)).filte
555
556
  }
556
557
  }
557
558
  apply(t) {
558
- const n = _.Entries(t), s = [];
559
+ const n = N.Entries(t), s = [];
559
560
  for (let r = 0; r < n.length; r++) {
560
561
  const [o, i] = n[r];
561
562
  if (o !== "apply") {
562
563
  s.push(`${o}=${i}`);
563
564
  continue;
564
565
  }
565
- const l = re.apply(i);
566
+ const l = ie.apply(i);
566
567
  switch (l.name) {
567
568
  case "recipe":
568
- s.push(this.recipes.get(l.id)), s.push(...F(l).map((a) => this.recipes.get(a)));
569
+ s.push(this.recipes.get(l.id)), s.push(...F(l).map((c) => this.recipes.get(c)));
569
570
  break;
570
571
  default:
571
572
  throw new Error(`Unknown registration: ${l.name}`);
@@ -574,31 +575,31 @@ const ce = (e, t) => e.range[0] - t.range[0], X = (e, t, n) => (n ?? P(e)).filte
574
575
  return s.join("&");
575
576
  }
576
577
  };
577
- k(_, "Entries", (t) => Array.from(new URLSearchParams(t).entries()));
578
- let H = _;
579
- const lt = ["http", "./", "../", "?"], at = ({ url: e }) => lt.some((t) => e.startsWith(t)), ct = (e) => G(e) && Ae(e) && at(e), pe = ({ url: e }, t) => `[](${t ? O(t, e) : e})`, g = {
578
+ _(N, "Entries", (t) => Array.from(new URLSearchParams(t).entries()));
579
+ let H = N;
580
+ const at = ["http", "./", "../", "?"], ct = ({ url: e }) => at.some((t) => e.startsWith(t)), pt = (e) => G(e) && Re(e) && ct(e), me = ({ url: e }, t) => `[](${t ? A(t, e) : e})`, h = {
580
581
  _open: "<!--",
581
582
  _close: "-->",
582
583
  _flag: "p↓",
583
584
  get begin() {
584
- return A(g._open, g._flag, "BEGIN", g._close);
585
+ return M(h._open, h._flag, "BEGIN", h._close);
585
586
  },
586
587
  get end() {
587
- return A(g._open, g._flag, "END", g._close);
588
+ return M(h._open, h._flag, "END", h._close);
588
589
  },
589
590
  lengthOf(e) {
590
591
  const t = `lines: ${e.split(`
591
592
  `).length}`, n = `chars: ${e.length}`;
592
- return A(g._open, g._flag, "length", t, n, g._close);
593
+ return M(h._open, h._flag, "length", t, n, h._close);
593
594
  }
594
- }, oe = (e) => (t) => G(t) && t.value === g[e], pt = ({ position: e, url: t, siblingCount: n }, s) => ({ position: e, url: t, headingDepth: s, inline: n >= 1 }), ut = ({ position: { start: e }, url: t, siblingCount: n }, { position: { end: s } }, r) => ({ position: { start: e, end: s }, url: t, headingDepth: r, inline: n >= 1 }), mt = (e, t, n) => (e.inline ? A : Re)(
595
- pe(e, n),
596
- g.begin,
597
- g.lengthOf(t),
595
+ }, le = (e) => (t) => G(t) && t.value === h[e], ut = ({ position: e, url: t, siblingCount: n }, s) => ({ position: e, url: t, headingDepth: s, inline: n >= 1 }), mt = ({ position: { start: e }, url: t, siblingCount: n }, { position: { end: s } }, r) => ({ position: { start: e, end: s }, url: t, headingDepth: r, inline: n >= 1 }), ht = (e, t, n) => (e.inline ? M : We)(
596
+ me(e, n),
597
+ h.begin,
598
+ h.lengthOf(t),
598
599
  t,
599
- g.end
600
+ h.end
600
601
  ), gt = (e) => {
601
- const t = R(e, "heading").reduce((n, { position: s, depth: r }) => n.set(s.start.line, r), /* @__PURE__ */ new Map());
602
+ const t = O(e, "heading").reduce((n, { position: s, depth: r }) => n.set(s.start.line, r), /* @__PURE__ */ new Map());
602
603
  return (n) => {
603
604
  for (let s = n.position.start.line; s >= 1; s--) {
604
605
  const r = t.get(s);
@@ -610,7 +611,7 @@ const lt = ["http", "./", "../", "?"], at = ({ url: e }) => lt.some((t) => e.sta
610
611
  openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
611
612
  closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
612
613
  openingCommentNotClosed: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)
613
- }, ht = (e, t) => {
614
+ }, dt = (e, t) => {
614
615
  const n = [], s = [
615
616
  ...e.map((o) => ({ node: o, type: "open" })),
616
617
  ...t.map((o) => ({ node: o, type: "close" }))
@@ -628,13 +629,13 @@ const lt = ["http", "./", "../", "?"], at = ({ url: e }) => lt.some((t) => e.sta
628
629
  if (r.length > 0)
629
630
  throw T.openingCommentNotClosed(r[0].node);
630
631
  return n;
631
- }, dt = (e, t, n) => {
632
+ }, ft = (e, t, n) => {
632
633
  const s = [...t].sort(f), r = [];
633
634
  return [...n].sort((o, i) => f.reverse(o.open, i.open)).forEach((o) => {
634
635
  for (; s.length > 0; ) {
635
636
  const i = s.pop();
636
637
  if (i.position.start.offset < o.open.position.start.offset) {
637
- if (Oe(e, i, o.open).trim() !== "")
638
+ if (Te(e, i, o.open).trim() !== "")
638
639
  throw T.openingCommentDoesNotFollowLink(o.open);
639
640
  return r.push([i, o]);
640
641
  }
@@ -642,50 +643,50 @@ const lt = ["http", "./", "../", "?"], at = ({ url: e }) => lt.some((t) => e.sta
642
643
  }
643
644
  throw T.openingCommentDoesNotFollowLink(o.open);
644
645
  }), r.push(...s.reverse()), r.reverse();
645
- }, ue = (e, t) => {
646
+ }, he = (e, t) => {
646
647
  t ?? (t = z.md(e));
647
- const n = gt(t), s = R(t, "link").filter(ct), r = R(t, "html").sort(f), o = r.filter(oe("begin")), i = r.filter(oe("end")), l = ht(o, i);
648
- return dt(e, s, l).map((c) => Array.isArray(c) ? ut(c[0], c[1].close, n(c[0])) : pt(c, n(c)));
649
- }, ft = (e, { url: t }) => (n) => e(O(Q(t), n)), me = (...e) => {
648
+ const n = gt(t), s = O(t, "link").filter(pt), r = O(t, "html").sort(f), o = r.filter(le("begin")), i = r.filter(le("end")), l = dt(o, i);
649
+ return ft(e, s, l).map((a) => Array.isArray(a) ? mt(a[0], a[1].close, n(a[0])) : ut(a, n(a)));
650
+ }, vt = (e, { url: t }) => (n) => e(A(Q(t), n)), ge = (...e) => {
650
651
  const t = e.reduce((n, s) => n + s, 0);
651
652
  return Math.min(Math.max(t, 1), 6);
652
- }, vt = (e, t, n) => {
653
+ }, wt = (e, t, n) => {
653
654
  if (t === 0) return e;
654
655
  n ?? (n = z.md(e));
655
- const s = R(n, "heading"), r = e.split(`
656
+ const s = O(n, "heading"), r = e.split(`
656
657
  `);
657
658
  for (const o of s) {
658
- const { depth: i, position: { start: l, end: a } } = o, c = me(i, t), u = r[l.line - 1].slice(i, a.column), p = "#".repeat(c) + u;
659
- r[l.line - 1] = p, o.depth = c;
659
+ const { depth: i, position: { start: l, end: c } } = o, a = ge(i, t), u = r[l.line - 1].slice(i, c.column), p = "#".repeat(a) + u;
660
+ r[l.line - 1] = p, o.depth = a;
660
661
  }
661
662
  return r.join(`
662
663
  `);
663
- }, ge = (e) => ue(e).reverse().sort(f.reverse).reduce((t, n) => ae(t, pe(n), n), e), he = (e, t, n, s, r) => {
664
+ }, de = (e) => he(e).reverse().sort(f.reverse).reduce((t, n) => pe(t, me(n), n), e), fe = (e, t, n, s, r) => {
664
665
  try {
665
- e = ge(e), e = vt(e, t);
666
- const o = z.md(e), i = new H(), l = ue(e, o).sort(f);
667
- return l.filter(({ url: a }) => a.startsWith("?")).forEach(({ url: a }) => i.tryStore(a)), l.reverse().map((a) => {
668
- var M, S;
669
- const { url: c, headingDepth: u } = a, [p, ...b] = Ee(c).split("?"), y = i.apply(b.join("?"));
670
- if (!c.startsWith("?"))
671
- if (c.startsWith("./") || c.startsWith("../")) {
672
- const m = p.split(".").pop() ?? "", j = Q(c), V = O(j, p);
673
- let h = n(V);
674
- const x = new Se(y), D = (M = ((w) => {
666
+ e = de(e), e = wt(e, t);
667
+ const o = z.md(e), i = new H(), l = he(e, o).sort(f);
668
+ return l.filter(({ url: c }) => c.startsWith("?")).forEach(({ url: c }) => i.tryStore(c)), l.reverse().map((c) => {
669
+ var k, S;
670
+ const { url: a, headingDepth: u } = c, [p, ...b] = xe(a).split("?"), y = i.apply(b.join("?"));
671
+ if (!a.startsWith("?"))
672
+ if (a.startsWith("./") || a.startsWith("../")) {
673
+ const m = p.split(".").pop() ?? "", j = Q(a), V = A(j, p);
674
+ let g = n(V);
675
+ const x = new _e(y), D = (k = ((w) => {
675
676
  const v = Array.from(x.entries()).filter(([C]) => C === w).map(([C, q]) => q);
676
677
  return v.length >= 1 ? v.join(",") : void 0;
677
- })("region")) == null ? void 0 : M.split(J);
678
- h = (D == null ? void 0 : D.reduce(
679
- (w, v, C, { length: q }) => rt(w, v, C === q - 1),
680
- h
681
- )) ?? h;
682
- const ve = x.has("skip"), we = x.get("heading") ?? 0, $e = x.has("inline");
683
- let { inline: L } = a;
684
- if ($e && (L = !0), !ve)
678
+ })("region")) == null ? void 0 : k.split(J);
679
+ g = (D == null ? void 0 : D.reduce(
680
+ (w, v, C, { length: q }) => ot(w, v, C === q - 1),
681
+ g
682
+ )) ?? g;
683
+ const $e = x.has("skip"), be = x.get("heading") ?? 0, ye = x.has("inline");
684
+ let { inline: L } = c;
685
+ if (ye && (L = !0), !$e)
685
686
  if (m === "md") {
686
- const w = ft(n, a), v = r ? O(r, j) : j, C = me(u, Number(we));
687
- h = he(
688
- h,
687
+ const w = vt(n, c), v = r ? A(r, j) : j, C = ge(u, Number(be));
688
+ g = fe(
689
+ g,
689
690
  /** p↓: ... */
690
691
  C,
691
692
  w,
@@ -693,38 +694,38 @@ const lt = ["http", "./", "../", "?"], at = ({ url: e }) => lt.some((t) => e.sta
693
694
  v
694
695
  /** p↓: ... */
695
696
  );
696
- } else /^(js|ts)x?|svelte$/i.test(m) && (h = ee(
697
- h,
697
+ } else /^(js|ts)x?|svelte$/i.test(m) && (g = ee(
698
+ g,
698
699
  "code",
699
700
  /** p↓: ... */
700
701
  { extension: m, inline: L }
701
702
  /** p↓: ... */
702
703
  ));
703
704
  const U = (S = x.get("wrap")) == null ? void 0 : S.split(J);
704
- return h = (U == null ? void 0 : U.reduce((w, v) => ee(w, v, { extension: m, inline: L }), h)) ?? h, { target: a, content: mt(a, h, r) };
705
- } else throw c.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${c}`);
706
- }).filter(Boolean).reduce((a, { target: c, content: u }) => ae(a, u, c), e);
705
+ return g = (U == null ? void 0 : U.reduce((w, v) => ee(w, v, { extension: m, inline: L }), g)) ?? g, { target: c, content: ht(c, g, r) };
706
+ } else throw a.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${a}`);
707
+ }).filter(Boolean).reduce((c, { target: a, content: u }) => pe(c, u, a), e);
707
708
  } catch (o) {
708
709
  throw new Error(`Error populating inclusions in file ${s ?? "(unknown)"}: ${o}`);
709
710
  }
710
- }, de = (e) => Ce(e, "utf-8"), fe = (e) => {
711
- const t = le(e), n = Q(t);
712
- return { content: de(t), dir: n, path: t };
713
- }, wt = (e, t = !0, n) => {
714
- const { dir: s, path: r, content: o } = fe(e);
715
- n == null || n.unwatch(r), n == null || n.once("change", (a, c) => {
716
- console.log(`Change detected in "${a}". Regenerating "${r}"...`), wt(e, t, n);
711
+ }, ve = (e) => Se(e, "utf-8"), we = (e) => {
712
+ const t = ce(e), n = Q(t);
713
+ return { content: ve(t), dir: n, path: t };
714
+ }, $t = (e, t = !0, n) => {
715
+ const { dir: s, path: r, content: o } = we(e);
716
+ n == null || n.unwatch(r), n == null || n.once("change", (c, a) => {
717
+ console.log(`Change detected in "${c}". Regenerating "${r}"...`), $t(e, t, n);
717
718
  });
718
- const l = he(o, 0, (a) => {
719
- const c = le(s, Ie(a));
720
- return n == null || n.add(c), de(c);
719
+ const l = fe(o, 0, (c) => {
720
+ const a = ce(s, Pe(c));
721
+ return n == null || n.add(a), ve(a);
721
722
  }, r);
722
- return t && ie(r, l), n == null || n.add(r), l;
723
- }, Nt = (e, t = !0, n) => {
724
- const { path: s, content: r } = fe(e), o = ge(r);
725
- return t && ie(s, o), o;
723
+ return t && ae(r, l), n == null || n.add(r), l;
724
+ }, Mt = (e, t = !0, n) => {
725
+ const { path: s, content: r } = we(e), o = de(r);
726
+ return t && ae(s, o), o;
726
727
  };
727
728
  export {
728
- Nt as depopulateMarkdownInclusions,
729
- wt as populateMarkdownInclusions
729
+ Mt as depopulateMarkdownInclusions,
730
+ $t as populateMarkdownInclusions
730
731
  };
@@ -1,16 +1,15 @@
1
- (function(d,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):(d=typeof globalThis<"u"?globalThis:d||self,m(d.index={},d.node_fs,d.node_path,d.node_url,d.unified,d.remarkParse,d.unistUtilVisit,d.tsDedent))})(this,function(d,m,g,ve,we,$e,ye,Ce){"use strict";var dt=Object.defineProperty;var mt=(d,m,g)=>m in d?dt(d,m,{enumerable:!0,configurable:!0,writable:!0,value:g}):d[m]=g;var j=(d,m,g)=>mt(d,typeof m!="symbol"?m+"":m,g);const $=(e,t)=>e.position.start.offset-t.position.start.offset;$.reverse=(e,t)=>$(t,e);const D=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,be=we.unified().use($e),L={md:e=>be.parse(e)},A=(e,t)=>{const n=[];return ye.visit(e,(s,r,i)=>{if(s.type!=="root"){if(t&&s.type!==t)return;if(D(s)){const o=((i==null?void 0:i.children.length)??0)-1;n.push({...s,siblingIndex:r,siblingCount:o})}}}),n},Ee=e=>e.children.length===0,J=(e,t,...n)=>{if(n.length===0)throw new Error("No nodes to replace content from");n.sort($);const s=n.at(0),r=n.at(-1);return e.slice(0,s.position.start.offset)+t+e.slice(r.position.end.offset)},Se=(e,t,n)=>{const s=Math.min(t.position.end.offset,n.position.end.offset),r=Math.max(t.position.start.offset,n.position.start.offset);return e.slice(s,r)},O=(...e)=>e.join(" "),ke=(...e)=>e.join(`
2
- `),xe=e=>{const t=e.split("?");return t.length>1?[t.slice(0,-1).join("?"),t.at(-1)]:[e,""]},Ne=e=>xe(e)[0],Z=/,\s*(?![^()]*\))/,K={reserved:{semi:";",slash:"/",question:"?",colon:":",at:"@",equal:"=",and:"&"},unsafe:{quote:'"',angle:"<",unangle:">",hash:"#",percent:"%",curly:"{",uncurly:"}",pipe:"|",back:"\\",carrot:"^",tilde:"~",square:"[",unsquare:"]",tick:"`",line:`
3
- `}},Me="-",Y={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(K.unsafe),...Object.entries(K.reserved)]},ee=(e,[t,n])=>e.replaceAll(t,n),I=(e,t=Me)=>{const n=Y.static.reduce(ee,e);return Y.url.map(([s,r])=>[t+s+t,r]).reduce(ee,n).replaceAll(t," ")},Te=["string","number","boolean"],_e=e=>Te.includes(e),je=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,Ae=e=>{const t=e.match(je);if(!t)throw new Error(`Invalid invocation: ${e}`);const[,n,s=""]=t;if(!s.trim())return{name:n,parameters:[]};const r=Ie(s);return{name:n,parameters:r}},Oe=(e,t)=>{for(let n=t+1;n<e.length;n++)if(e[n]!==void 0)return!1;return!0},Ie=e=>{const t=[];let n="",s=!1;for(let r=0;r<e.length;r++){const i=e[r],o=i==="'";if(o&&e.at(r+1)==="'"){const l=e.at(r+2)==="'";s=!s,n+=l?"'''":"''",r+=l?2:1}else o?(s=!s,n+=i):i===","&&!s?(t.push(n.trim()),n=""):n+=i}return t.push(n.trim()),t.map(r=>r===""?void 0:r?qe(r):void 0).filter((r,i,o)=>r!==void 0||!Oe(o,i))},qe=e=>{const t=e.length>=2&&e[0]==="'"&&e.at(-1)==="'",n=t&&e.length>=4&&e[1]==="'"&&e.at(-2)==="'";return!t||n?e:e.slice(1,-1)},Re=e=>{const t=/^(\w+)(?:\(([^)]*)\))?/,n=e.match(t);if(!n)return{name:e};const[,s,r]=n;if(!r)return{name:s};const i=/(\w+)(\?)?:\s*([^,]+)/g,o=[];let l;for(;(l=i.exec(r))!==null;){const[,a,c,u]=l,p=u.trim();if(!_e(p))throw new Error(`Unsupported type: ${p}`);o.push({name:a,optional:c==="?",type:p})}return{name:s,parameters:o}},N=e=>{const t=e.map(Re).reduce((n,{name:s,parameters:r})=>n.set(s,r),new Map);return n=>{const{name:s,parameters:r}=Ae(n);if(!t.has(s))throw new Error(`Unknown method: ${s}`);const i=t.get(s);if(i===void 0)return{name:s};if(r.length>i.length){const o=i.filter(({optional:a})=>!a).length,l=o===i.length?o.toString():`${o} - ${i.length}`;throw new Error(`Too many parameters: ${r.length} for method '${s}' (expected: ${l})`)}return i.reduce((o,{name:l,optional:a,type:c},u)=>{const p=r[u];if(p===void 0){if(a)return o;throw new Error(`Missing required parameter: ${l} for method '${s}'`)}switch(c){case"string":o[l]=p;break;case"number":case"boolean":o[l]=JSON.parse(p);break}if(typeof o[l]!==c)throw new Error(`Invalid type: ${l} must be ${c}, got ${typeof o[l]} for method '${s}'`);return o},{name:s})}},U=e=>Object.entries(e).filter(([t])=>t!=="name"&&!isNaN(Number(t))).map(([t,n])=>n),Pe=N(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),E=(e,t=1)=>`
1
+ (function(u,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("ts-dedent")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","ts-dedent"],d):(u=typeof globalThis<"u"?globalThis:u||self,d(u.index={},u.node_fs,u.node_path,u.node_url,u.unified,u.remarkParse,u.unistUtilVisit,u.tsDedent))})(this,function(u,d,h,ye,$e,Ce,be,Ee){"use strict";var ht=Object.defineProperty;var gt=(u,d,h)=>d in u?ht(u,d,{enumerable:!0,configurable:!0,writable:!0,value:h}):u[d]=h;var T=(u,d,h)=>gt(u,typeof d!="symbol"?d+"":d,h);const y=(e,t)=>e.position.start.offset-t.position.start.offset;y.reverse=(e,t)=>y(t,e);const D=e=>e.position!==void 0&&e.position.start.offset!==void 0&&e.position.end.offset!==void 0,Se=$e.unified().use(Ce),L={md:e=>Se.parse(e)},j=(e,t)=>{const n=[];return be.visit(e,(s,r,i)=>{if(s.type!=="root"){if(t&&s.type!==t)return;if(D(s)){const o=((i==null?void 0:i.children.length)??0)-1;n.push({...s,siblingIndex:r,siblingCount:o})}}}),n},ke=e=>e.children.length===0,J=(e,t,...n)=>{if(n.length===0)throw new Error("No nodes to replace content from");n.sort(y);const s=n.at(0),r=n.at(-1);return e.slice(0,s.position.start.offset)+t+e.slice(r.position.end.offset)},xe=(e,t,n)=>{const s=Math.min(t.position.end.offset,n.position.end.offset),r=Math.max(t.position.start.offset,n.position.start.offset);return e.slice(s,r)},A=(...e)=>e.join(" "),Ne=(...e)=>e.join(`
2
+ `),Me=e=>{const t=e.split("?");return t.length>1?[t.slice(0,-1).join("?"),t.at(-1)]:[e,""]},_e=e=>Me(e)[0],Z=/,\s*(?![^()]*\))/,K={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
+ `}},Te="-",Y={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(K.unsafe),...Object.entries(K.reserved)]},ee=(e,[t,n])=>e.replaceAll(t,n),O=(e,t=Te)=>{const n=Y.static.reduce(ee,e);return Y.url.map(([s,r])=>[t+s+t,r]).reduce(ee,n).replaceAll(t," ")},je=["string","number","boolean"],Ae=e=>je.includes(e),Oe=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,Ie=e=>{const t=e.match(Oe);if(!t)throw new Error(`Invalid invocation: ${e}`);const[,n,s=""]=t;if(!s.trim())return{name:n,parameters:[]};const r=Re(s);return{name:n,parameters:r}},qe=(e,t)=>{for(let n=t+1;n<e.length;n++)if(e[n]!==void 0)return!1;return!0},Re=e=>{const t=[];let n="",s=!1;for(let r=0;r<e.length;r++){const i=e[r],o=i==="'";if(o&&e.at(r+1)==="'"){const l=e.at(r+2)==="'";s=!s,n+=l?"'''":"''",r+=l?2:1}else o?(s=!s,n+=i):i===","&&!s?(t.push(n.trim()),n=""):n+=i}return t.push(n.trim()),t.map(r=>r===""?void 0:r?We(r):void 0).filter((r,i,o)=>r!==void 0||!qe(o,i))},We=e=>{const t=e.length>=2&&e[0]==="'"&&e.at(-1)==="'",n=t&&e.length>=4&&e[1]==="'"&&e.at(-2)==="'";return!t||n?e:e.slice(1,-1)},Pe=e=>{const t=/^([\w-_]+)(?:\(([^)]*)\))?/,n=e.match(t);if(!n)return{name:e};const[,s,r]=n;if(!r)return{name:s};const i=/([\w-_]+)(\?)?:\s*([^,]+)/g,o=[];let l;for(;(l=i.exec(r))!==null;){const[,c,a,m]=l,p=m.trim();if(!Ae(p))throw new Error(`Unsupported type: ${p}`);o.push({name:c,optional:a==="?",type:p})}return{name:s,parameters:o}},I=e=>{const t=e.map(Pe).reduce((n,{name:s,parameters:r})=>n.set(s,r),new Map);return n=>{const{name:s,parameters:r}=Ie(n);if(!t.has(s))throw new Error(`Unknown method: ${s}`);const i=t.get(s);if(i===void 0)return{name:s};if(r.length>i.length){const o=i.filter(({optional:c})=>!c).length,l=o===i.length?o.toString():`${o} - ${i.length}`;throw new Error(`Too many parameters: ${r.length} for method '${s}' (expected: ${l})`)}return i.reduce((o,{name:l,optional:c,type:a},m)=>{const p=r[m];if(p===void 0){if(c)return o;throw new Error(`Missing required parameter: ${l} for method '${s}'`)}switch(a){case"string":o[l]=p;break;case"number":case"boolean":o[l]=JSON.parse(p);break}if(typeof o[l]!==a)throw new Error(`Invalid type: ${l} must be ${a}, got ${typeof o[l]} for method '${s}'`);return o},{name:s})}},U=e=>Object.entries(e).filter(([t])=>t!=="name"&&!isNaN(Number(t))).map(([t,n])=>n),De=I(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),E=(e,t=1)=>`
4
4
  `.repeat(t)+e+`
5
- `.repeat(t),B=(e,t,n)=>`<${t}${n?` ${n}`:""}>${E(e)}</${t}>`,te=(e,t,n)=>{const s=Pe(t);switch(s.name){case"code":if(!s.lang&&!s.meta&&(n==null?void 0:n.inline)&&!e.includes(`
5
+ `.repeat(t),B=(e,t,n)=>`<${t}${n?` ${n}`:""}>${E(e)}</${t}>`,te=(e,t,n)=>{const s=De(t);switch(s.name){case"code":if(!s.lang&&!s.meta&&(n==null?void 0:n.inline)&&!e.includes(`
6
6
  `))return`\`${e}\``;const i=s.lang??(n==null?void 0:n.extension)??"",o=s.meta?` ${s.meta}`:"";return E(`\`\`\`${i}${o}${E(e)}\`\`\``);case"quote":return n!=null&&n.inline&&!e.includes(`
7
7
 
8
- `)?`> ${e}`:E(B(E(e),"blockquote"));case"dropdown":const l=B(I(s.summary),"summary");return E(B([l,e].join(`
9
- `),"details",s.open?"open":void 0))}};class v{constructor(...t){j(this,"_intervals",[]);j(this,"collapsed",!0);for(const[n,s]of t)this.push(n,s)}get intervals(){return this._intervals}push(t,n){return n??(n=t+1),this._intervals.push([Math.min(t,n),Math.max(t,n)]),this.collapsed=!1,this}combine(t){for(const[n,s]of t.intervals)this.push(n,s);return this}collapse(t=!1){const{_intervals:n,collapsed:s}=this;if(s&&!t||!n.length)return this;n.sort((l,a)=>l[0]-a[0]);const r=[];let[i,o]=n[0];for(let l=1;l<n.length;l++){const[a,c]=n[l];a<=o?o=Math.max(o,c):(r.push([i,o]),i=a,o=c)}return r.push([i,o]),this._intervals=r,this.collapsed=!0,this}subtract(t){this.collapse(),t.collapse();const{_intervals:n}=this,{_intervals:s}=t;if(!n.length||!s.length)return this;let r=[...n];for(const[i,o]of s){const l=[];for(const[a,c]of r){if(o<=a||i>=c){l.push([a,c]);continue}i>a&&l.push([a,i]),o<c&&l.push([o,c])}r=l}return this._intervals=r,this.collapse(!0),this}test(t,n="head"){const{_intervals:s}=this;switch(n){case"head":return s.some(([r,i])=>t>=r&&t<i);case"tail":return s.some(([r,i])=>t>r&&t<=i);case"both":return s.some(([r,i])=>t>=r&&t<=i);case"none":return s.some(([r,i])=>t>r&&t<i)}}slice(t){this.collapse();const n=[];for(const[s,r]of this._intervals)n.push(t.slice(s,r));return n.filter(Boolean).join("")}offset(t){for(const n of this._intervals)n[0]+=t,n[1]+=t;return this}}const q=e=>{const t=/(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm,n=[];let s;for(;(s=t.exec(e))!==null;){const r=[s.index,s.index+s[0].length],i=We(s[0]).trim();n.push({range:r,value:i})}return n},We=e=>e.startsWith("//")?e.slice(2):e.startsWith("/*")?e.slice(2,-2):e.startsWith("<!--")?e.slice(4,-3):e,De=["p↓:","pd:","parkdown:"];N(["splice(delete?: number, insert?: string)"]);const ne=(e,t)=>e.range[0]-t.range[0],F=(e,t,n)=>(n??q(e)).filter(({value:s})=>s.split(" ").includes(t)).sort(ne),se=e=>({isSpace:e===" ",isNewline:e===`
10
- `||e===void 0}),re=e=>De.flatMap(t=>F(e,t)).sort((t,n)=>t.range[0]-n.range[0]).reverse().reduce((t,{range:[n,s],value:r})=>{const i=(S,k)=>t.slice(0,S)+t.slice(k),o=se(t[n-1]),l=se(t[s]),a=s===t.length;let c=n;for(;c>0&&t[c-1]!==`
11
- `;)c--;let u=s;for(;u<t.length&&t[u]!==`
12
- `;)u++;const p=t.slice(c,n).trim()===""&&t.slice(s,u).trim()==="";return o.isNewline&&l.isNewline?i(n-(a?1:0),s+1):o.isNewline||p?i(c,s+(l.isSpace||l.isNewline?1:0)):i(n-(o.isSpace?1:0),s)},e),Le=N(["extract(id: string, 0?: string, 1?: string, 2?: string)","remove(id: string, 0?: string, 1?: string, 2?: string)","replace(id: string, with?: string, space?: string)","remap(id?: string, from: string, to?: string, space?: string)","single-line(id: string, includeBoundaries?: boolean)","trim(id: string, inside?: boolean, outside?: boolean)","trim-start(id: string, left?: boolean, right?: boolean)","trim-end(id: string, left?: boolean, right?: boolean)","splice-start(id: string, deleteCount?: number, insert?: string, space?: string)","splice-end(id: string, deleteCount?: number, insert?: string, space?: string)","debug()"]),C=(e,t,n)=>{const s=F(e,t,n),r=[];for(let i=0;i<s.length-1;i+=2)r.push([s[i],s[i+1]]);return r},Ue=(e,t,n)=>new v(...F(e,t,n).map(({range:s})=>s)),R=e=>Ce.dedent(e).trim(),Be=(e,...t)=>{if(t.length===0)return e;const n=q(e),s=new v,r=new v;for(const i of t)for(const[o,l]of C(e,i,n)){s.push(o.range[1],l.range[0]);const[a,...c]=e.slice(o.range[1],l.range[0]),u=c[c.length-1];r.push(o.range[0],o.range[1]+(a.trim()?0:1)),r.push(l.range[0],l.range[1]+(u.trim()?0:1))}return R(s.subtract(r).slice(e))},Fe=(e,...t)=>{if(t.length===0)return e;const n=q(e),s=([i,o])=>e.slice(i,o),r=new v;for(const i of t)for(const[o,l]of C(e,i,n)){const a=s([l.range[1],l.range[1]+1]).at(-1);r.push(o.range[0],a===`
13
- `?l.range[1]+1:l.range[1])}return R(new v([0,e.length]).subtract(r).slice(e))},He=(e,t,n,s)=>{if(!t)return e;let r="",i=0;for(const[o,l]of C(e,t))r+=e.slice(i,o.range[1]),r+=I(n??o.value,s),i=l.range[0];return r+=e.slice(i),R(new v([0,r.length]).subtract(Ue(r,t)).slice(r))},Qe=(e,t,n,s,r,i)=>{if(!t)return e;const o=!(s===void 0||s<0),l=r?I(r,i):"",a=s??0,c=q(e),u=new v(...c.map(({range:p})=>p));return C(e,t,c).map(p=>p[n==="start"?0:1]).sort(ne).reverse().reduce((p,{range:S})=>{const k=new v,W=o?1:-1;let T=Math.abs(a),h=o?S[1]:S[0];for(;T>0&&h<p.length&&h>=0;)u.test(h)||(k.push(h),T--),h+=W;return h=Math.min(Math.max(0,h),p.length),p=p.slice(0,h)+l+p.slice(h),new v([0,p.length]).subtract(k.offset(o?0:l.length)).slice(p)},e)},Ge=(e,t,n,s,r)=>{let i="",o=0;[n,s]=[n,s??""].map(a=>I(a,r));const l=t?C(e,t):[[{range:[0,0]},{range:[e.length,e.length]}]];for(const[a,c]of l)i+=e.slice(o,a.range[1]),i+=e.slice(a.range[1],c.range[0]).replaceAll(n,s),o=c.range[0];return i+=e.slice(o),i},ze=(e,t)=>{if(!t)return e;let n="",s=0;for(const[r,i]of C(e,t))n+=e.slice(s,r.range[1]),n+=e.slice(r.range[1],i.range[0]).replaceAll(/[\s\n]+/g," "),s=i.range[0];return n+=e.slice(s),n},ie=(e,t,n)=>{var i,o,l,a;if(!t)return e;const s=c=>/\s/.test(e[c]),r=new v;for(const[c,u]of C(e,t)){if((i=n.start)!=null&&i.left){let p=c.range[0]-1;for(;s(p);)r.push(p--)}if((o=n.start)!=null&&o.right){let p=c.range[1];for(;s(p);)r.push(p++)}if((l=n.end)!=null&&l.left){let p=u.range[0]-1;for(;s(p);)r.push(p--)}if((a=n.end)!=null&&a.right){let p=u.range[1];for(;s(p);)r.push(p++)}}return new v([0,e.length]).subtract(r).slice(e)},Ve=(e,t,n)=>{if(!t)return re(e);const s=Le(t);switch(s.name){case"extract":e=Be(e,s.id,...U(s));break;case"remove":e=Fe(e,s.id,...U(s));break;case"replace":e=He(e,s.id,s.with,s.space);break;case"splice-start":case"splice-end":{const{deleteCount:r,insert:i,space:o,id:l}=s,a=s.name==="splice-start"?"start":"end";e=Qe(e,l,a,r,i,o);break}case"trim-start":case"trim-end":{const{left:r,right:i,id:o}=s,l=s.name==="trim-start"?"start":"end";e=ie(e,o,{[l]:{left:r??!0,right:i??!0}});break}case"trim":{const{inside:r,outside:i,id:o}=s;e=ie(e,o,{start:{left:i??!0,right:r??!0},end:{left:r??!0,right:i??!0}});break}case"single-line":e=ze(e,s.id);break;case"remap":e=Ge(e,s.id,s.from,s.to,s.space);break}return e=n&&s.name!=="debug"?re(e):e,n?R(e):e},Xe=["recipe(id: string)"],Je=["recipe(id: string, 0?: string, 1?: string, 2?: string)"],oe={register:N(Xe),apply:N(Je)},M=class M{constructor(){j(this,"recipes",new Map)}tryStore(t){const n=M.Entries(t);for(let s=0;s<n.length;s++){const[r,i]=n[s];if(r!=="register")continue;const o=oe.register(i);switch(o.name){case"recipe":const l=n.slice(s+1).filter(([a])=>a!=="register").map(([a,c])=>`${a}=${c}`).join("&");this.recipes.set(o.id,l);break;default:throw new Error(`Unknown registration: ${o.name}`)}}}apply(t){const n=M.Entries(t),s=[];for(let r=0;r<n.length;r++){const[i,o]=n[r];if(i!=="apply"){s.push(`${i}=${o}`);continue}const l=oe.apply(o);switch(l.name){case"recipe":s.push(this.recipes.get(l.id)),s.push(...U(l).map(a=>this.recipes.get(a)));break;default:throw new Error(`Unknown registration: ${l.name}`)}}return s.join("&")}};j(M,"Entries",t=>Array.from(new URLSearchParams(t).entries()));let H=M;const Ze=["http","./","../","?"],Ke=({url:e})=>Ze.some(t=>e.startsWith(t)),Ye=e=>D(e)&&Ee(e)&&Ke(e),le=({url:e},t)=>`[](${t?g.join(t,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return O(f._open,f._flag,"BEGIN",f._close)},get end(){return O(f._open,f._flag,"END",f._close)},lengthOf(e){const t=`lines: ${e.split(`
14
- `).length}`,n=`chars: ${e.length}`;return O(f._open,f._flag,"length",t,n,f._close)}},ae=e=>t=>D(t)&&t.value===f[e],et=({position:e,url:t,siblingCount:n},s)=>({position:e,url:t,headingDepth:s,inline:n>=1}),tt=({position:{start:e},url:t,siblingCount:n},{position:{end:s}},r)=>({position:{start:e,end:s},url:t,headingDepth:r,inline:n>=1}),nt=(e,t,n)=>(e.inline?O:ke)(le(e,n),f.begin,f.lengthOf(t),t,f.end),st=e=>{const t=A(e,"heading").reduce((n,{position:s,depth:r})=>n.set(s.start.line,r),new Map);return n=>{for(let s=n.position.start.line;s>=1;s--){const r=t.get(s);if(r)return r}return 0}},P={openingCommentDoesNotFollowLink:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),closingCommentNotMatchedToOpening:({position:{start:e}})=>new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),openingCommentNotClosed:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)},rt=(e,t)=>{const n=[],s=[...e.map(i=>({node:i,type:"open"})),...t.map(i=>({node:i,type:"close"}))].sort((i,o)=>$(i.node,o.node)),r=[];for(const i of s)if(i.type==="open")r.push(i);else{const o=i.node;if(r.length===0)throw P.closingCommentNotMatchedToOpening(o);const l=r.pop().node;if(r.length>0)continue;n.push({open:l,close:o})}if(r.length>0)throw P.openingCommentNotClosed(r[0].node);return n},it=(e,t,n)=>{const s=[...t].sort($),r=[];return[...n].sort((i,o)=>$.reverse(i.open,o.open)).forEach(i=>{for(;s.length>0;){const o=s.pop();if(o.position.start.offset<i.open.position.start.offset){if(Se(e,o,i.open).trim()!=="")throw P.openingCommentDoesNotFollowLink(i.open);return r.push([o,i])}r.push(o)}throw P.openingCommentDoesNotFollowLink(i.open)}),r.push(...s.reverse()),r.reverse()},ce=(e,t)=>{t??(t=L.md(e));const n=st(t),s=A(t,"link").filter(Ye),r=A(t,"html").sort($),i=r.filter(ae("begin")),o=r.filter(ae("end")),l=rt(i,o);return it(e,s,l).map(c=>Array.isArray(c)?tt(c[0],c[1].close,n(c[0])):et(c,n(c)))},ot=(e,{url:t})=>n=>e(g.join(g.dirname(t),n)),pe=(...e)=>{const t=e.reduce((n,s)=>n+s,0);return Math.min(Math.max(t,1),6)},lt=(e,t,n)=>{if(t===0)return e;n??(n=L.md(e));const s=A(n,"heading"),r=e.split(`
15
- `);for(const i of s){const{depth:o,position:{start:l,end:a}}=i,c=pe(o,t),u=r[l.line-1].slice(o,a.column),p="#".repeat(c)+u;r[l.line-1]=p,i.depth=c}return r.join(`
16
- `)},ue=e=>ce(e).reverse().sort($.reverse).reduce((t,n)=>J(t,le(n),n),e),de=(e,t,n,s,r)=>{try{e=ue(e),e=lt(e,t);const i=L.md(e),o=new H,l=ce(e,i).sort($);return l.filter(({url:a})=>a.startsWith("?")).forEach(({url:a})=>o.tryStore(a)),l.reverse().map(a=>{var W,T;const{url:c,headingDepth:u}=a,[p,...S]=g.basename(c).split("?"),k=o.apply(S.join("?"));if(!c.startsWith("?"))if(c.startsWith("./")||c.startsWith("../")){const h=p.split(".").pop()??"",Q=g.dirname(c),fe=g.join(Q,p);let w=n(fe);const _=new ve.URLSearchParams(k),G=(W=(b=>{const y=Array.from(_.entries()).filter(([x])=>x===b).map(([x,X])=>X);return y.length>=1?y.join(","):void 0})("region"))==null?void 0:W.split(Z);w=(G==null?void 0:G.reduce((b,y,x,{length:X})=>Ve(b,y,x===X-1),w))??w;const ct=_.has("skip"),pt=_.get("heading")??0,ut=_.has("inline");let{inline:z}=a;if(ut&&(z=!0),!ct)if(h==="md"){const b=ot(n,a),y=r?g.join(r,Q):Q,x=pe(u,Number(pt));w=de(w,x,b,fe,y)}else/^(js|ts)x?|svelte$/i.test(h)&&(w=te(w,"code",{extension:h,inline:z}));const V=(T=_.get("wrap"))==null?void 0:T.split(Z);return w=(V==null?void 0:V.reduce((b,y)=>te(b,y,{extension:h,inline:z}),w))??w,{target:a,content:nt(a,w,r)}}else throw c.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${c}`)}).filter(Boolean).reduce((a,{target:c,content:u})=>J(a,u,c),e)}catch(i){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${i}`)}},me=e=>m.readFileSync(e,"utf-8"),ge=e=>{const t=g.resolve(e),n=g.dirname(t);return{content:me(t),dir:n,path:t}},he=(e,t=!0,n)=>{const{dir:s,path:r,content:i}=ge(e);n==null||n.unwatch(r),n==null||n.once("change",(a,c)=>{console.log(`Change detected in "${a}". Regenerating "${r}"...`),he(e,t,n)});const l=de(i,0,a=>{const c=g.resolve(s,Ne(a));return n==null||n.add(c),me(c)},r);return t&&m.writeFileSync(r,l),n==null||n.add(r),l},at=(e,t=!0,n)=>{const{path:s,content:r}=ge(e),i=ue(r);return t&&m.writeFileSync(s,i),i};d.depopulateMarkdownInclusions=at,d.populateMarkdownInclusions=he,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
8
+ `)?`> ${e}`:E(B(E(e),"blockquote"));case"dropdown":const l=B(O(s.summary),"summary");return E(B([l,e].join(`
9
+ `),"details",s.open?"open":void 0))}};class w{constructor(...t){T(this,"_intervals",[]);T(this,"collapsed",!0);for(const[n,s]of t)this.push(n,s)}get intervals(){return this._intervals}push(t,n){return n??(n=t+1),this._intervals.push([Math.min(t,n),Math.max(t,n)]),this.collapsed=!1,this}combine(t){for(const[n,s]of t.intervals)this.push(n,s);return this}collapse(t=!1){const{_intervals:n,collapsed:s}=this;if(s&&!t||!n.length)return this;n.sort((l,c)=>l[0]-c[0]);const r=[];let[i,o]=n[0];for(let l=1;l<n.length;l++){const[c,a]=n[l];c<=o?o=Math.max(o,a):(r.push([i,o]),i=c,o=a)}return r.push([i,o]),this._intervals=r,this.collapsed=!0,this}subtract(t){this.collapse(),t.collapse();const{_intervals:n}=this,{_intervals:s}=t;if(!n.length||!s.length)return this;let r=[...n];for(const[i,o]of s){const l=[];for(const[c,a]of r){if(o<=c||i>=a){l.push([c,a]);continue}i>c&&l.push([c,i]),o<a&&l.push([o,a])}r=l}return this._intervals=r,this.collapse(!0),this}test(t,n="head"){const{_intervals:s}=this;switch(n){case"head":return s.some(([r,i])=>t>=r&&t<i);case"tail":return s.some(([r,i])=>t>r&&t<=i);case"both":return s.some(([r,i])=>t>=r&&t<=i);case"none":return s.some(([r,i])=>t>r&&t<i)}}slice(t){this.collapse();const n=[];for(const[s,r]of this._intervals)n.push(t.slice(s,r));return n.filter(Boolean).join("")}offset(t){for(const n of this._intervals)n[0]+=t,n[1]+=t;return this}}const q=e=>{const t=/(\/\/[^\n]*|\/\*[\s\S]*?\*\/|<!--[\s\S]*?-->)/gm,n=[];let s;for(;(s=t.exec(e))!==null;){const r=[s.index,s.index+s[0].length],i=Le(s[0]).trim();n.push({range:r,value:i})}return n},Le=e=>e.startsWith("//")?e.slice(2):e.startsWith("/*")?e.slice(2,-2):e.startsWith("<!--")?e.slice(4,-3):e,Ue=["(pd)","(p↓)","(parkdown)","pd:","p↓:","parkdown:"],ne=(e,t)=>e.range[0]-t.range[0],F=(e,t,n)=>(n??q(e)).filter(({value:s})=>s.split(" ").includes(t)).sort(ne),se=e=>({isSpace:e===" ",isNewline:e===`
10
+ `||e===void 0}),re=e=>Ue.flatMap(t=>F(e,t)).sort((t,n)=>t.range[0]-n.range[0]).reverse().reduce((t,{range:[n,s],value:r})=>{const i=(S,k)=>t.slice(0,S)+t.slice(k),o=se(t[n-1]),l=se(t[s]),c=s===t.length;let a=n;for(;a>0&&t[a-1]!==`
11
+ `;)a--;let m=s;for(;m<t.length&&t[m]!==`
12
+ `;)m++;const p=t.slice(a,n).trim()===""&&t.slice(s,m).trim()==="";return o.isNewline&&l.isNewline?i(n-(c?1:0),s+1):o.isNewline||p?i(a,s+(l.isSpace||l.isNewline?1:0)):i(n-(o.isSpace?1:0),s)},e),Be=I(["extract(id: string, 0?: string, 1?: string, 2?: string)","remove(id: string, 0?: string, 1?: string, 2?: string)","replace(id: string, with?: string, space?: string)","remap(id?: string, from: string, to?: string, space?: string)","single-line(id: string, includeBoundaries?: boolean)","trim(id: string, inside?: boolean, outside?: boolean)","trim-start(id: string, left?: boolean, right?: boolean)","trim-end(id: string, left?: boolean, right?: boolean)","splice-start(id: string, deleteCount?: number, insert?: string, space?: string)","splice-end(id: string, deleteCount?: number, insert?: string, space?: string)","debug()"]),C=(e,t,n)=>{const s=F(e,t,n),r=[];for(let i=0;i<s.length-1;i+=2)r.push([s[i],s[i+1]]);return r},Fe=(e,t,n)=>new w(...F(e,t,n).map(({range:s})=>s)),R=e=>Ee.dedent(e).trim(),ie=(e,t)=>e[t]!==void 0&&/[^\S\r\n]/.test(e[t]),oe=(e,t)=>Math.max(0,Math.min(e.length,t)),He=(e,...t)=>{if(t.length===0)return e;const n=q(e);return R(new w(...t.flatMap(s=>C(e,s,n).map(([{range:[r]},{range:[,i]}])=>{for(;ie(e,r);)r--;for(;ie(e,i);)i++;return[oe(e,r),oe(e,i)]}))).slice(e))},Qe=(e,...t)=>{if(t.length===0)return e;const n=q(e);return R(new w([0,e.length]).subtract(new w(...t.flatMap(s=>C(e,s,n).map(([{range:[,r]},{range:[i]}])=>[r,i])))).slice(e))},Ge=(e,t,n,s)=>{if(!t)return e;let r="",i=0;for(const[o,l]of C(e,t))r+=e.slice(i,o.range[1]),r+=O(n??o.value,s),i=l.range[0];return r+=e.slice(i),R(new w([0,r.length]).subtract(Fe(r,t)).slice(r))},ze=(e,t,n,s,r,i)=>{if(!t)return e;const o=!(s===void 0||s<0),l=r?O(r,i):"",c=s??0,a=q(e),m=new w(...a.map(({range:p})=>p));return C(e,t,a).map(p=>p[n==="start"?0:1]).sort(ne).reverse().reduce((p,{range:S})=>{const k=new w,P=o?1:-1;let M=Math.abs(c),g=o?S[1]:S[0];for(;M>0&&g<p.length&&g>=0;)m.test(g)||(k.push(g),M--),g+=P;return g=Math.min(Math.max(0,g),p.length),p=p.slice(0,g)+l+p.slice(g),new w([0,p.length]).subtract(k.offset(o?0:l.length)).slice(p)},e)},Ve=(e,t,n,s,r)=>{let i="",o=0;[n,s]=[n,s??""].map(c=>O(c,r));const l=t?C(e,t):[[{range:[0,0]},{range:[e.length,e.length]}]];for(const[c,a]of l)i+=e.slice(o,c.range[1]),i+=e.slice(c.range[1],a.range[0]).replaceAll(n,s),o=a.range[0];return i+=e.slice(o),i},Xe=(e,t)=>{if(!t)return e;let n="",s=0;for(const[r,i]of C(e,t))n+=e.slice(s,r.range[1]),n+=e.slice(r.range[1],i.range[0]).replaceAll(/[\s\n]+/g," "),s=i.range[0];return n+=e.slice(s),n},le=(e,t,n)=>{var i,o,l,c;if(!t)return e;const s=a=>/\s/.test(e[a]),r=new w;for(const[a,m]of C(e,t)){if((i=n.start)!=null&&i.left){let p=a.range[0]-1;for(;s(p);)r.push(p--)}if((o=n.start)!=null&&o.right){let p=a.range[1];for(;s(p);)r.push(p++)}if((l=n.end)!=null&&l.left){let p=m.range[0]-1;for(;s(p);)r.push(p--)}if((c=n.end)!=null&&c.right){let p=m.range[1];for(;s(p);)r.push(p++)}}return new w([0,e.length]).subtract(r).slice(e)},Je=(e,t,n)=>{if(!t)return re(e);const s=Be(t);switch(s.name){case"extract":e=He(e,s.id,...U(s));break;case"remove":e=Qe(e,s.id,...U(s));break;case"replace":e=Ge(e,s.id,s.with,s.space);break;case"splice-start":case"splice-end":{const{deleteCount:r,insert:i,space:o,id:l}=s,c=s.name==="splice-start"?"start":"end";e=ze(e,l,c,r,i,o);break}case"trim-start":case"trim-end":{const{left:r,right:i,id:o}=s,l=s.name==="trim-start"?"start":"end";e=le(e,o,{[l]:{left:r??!0,right:i??!0}});break}case"trim":{const{inside:r,outside:i,id:o}=s;e=le(e,o,{start:{left:i??!0,right:r??!0},end:{left:r??!0,right:i??!0}});break}case"single-line":e=Xe(e,s.id);break;case"remap":e=Ve(e,s.id,s.from,s.to,s.space);break}return s.name==="debug"&&console.log("debug!!!",s),e=n&&s.name!=="debug"?re(e):e,n?R(e):e},Ze=["recipe(id: string)"],Ke=["recipe(id: string, 0?: string, 1?: string, 2?: string)"],ae={register:I(Ze),apply:I(Ke)},N=class N{constructor(){T(this,"recipes",new Map)}tryStore(t){const n=N.Entries(this.apply(t));for(let s=0;s<n.length;s++){const[r,i]=n[s];if(r!=="register")continue;const o=ae.register(i);switch(o.name){case"recipe":const l=n.slice(s+1).filter(([c])=>c!=="register").map(([c,a])=>`${c}=${a}`).join("&");this.recipes.set(o.id,l);break;default:throw new Error(`Unknown registration: ${o.name}`)}}}apply(t){const n=N.Entries(t),s=[];for(let r=0;r<n.length;r++){const[i,o]=n[r];if(i!=="apply"){s.push(`${i}=${o}`);continue}const l=ae.apply(o);switch(l.name){case"recipe":s.push(this.recipes.get(l.id)),s.push(...U(l).map(c=>this.recipes.get(c)));break;default:throw new Error(`Unknown registration: ${l.name}`)}}return s.join("&")}};T(N,"Entries",t=>Array.from(new URLSearchParams(t).entries()));let H=N;const Ye=["http","./","../","?"],et=({url:e})=>Ye.some(t=>e.startsWith(t)),tt=e=>D(e)&&ke(e)&&et(e),ce=({url:e},t)=>`[](${t?h.join(t,e):e})`,f={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return A(f._open,f._flag,"BEGIN",f._close)},get end(){return A(f._open,f._flag,"END",f._close)},lengthOf(e){const t=`lines: ${e.split(`
13
+ `).length}`,n=`chars: ${e.length}`;return A(f._open,f._flag,"length",t,n,f._close)}},pe=e=>t=>D(t)&&t.value===f[e],nt=({position:e,url:t,siblingCount:n},s)=>({position:e,url:t,headingDepth:s,inline:n>=1}),st=({position:{start:e},url:t,siblingCount:n},{position:{end:s}},r)=>({position:{start:e,end:s},url:t,headingDepth:r,inline:n>=1}),rt=(e,t,n)=>(e.inline?A:Ne)(ce(e,n),f.begin,f.lengthOf(t),t,f.end),it=e=>{const t=j(e,"heading").reduce((n,{position:s,depth:r})=>n.set(s.start.line,r),new Map);return n=>{for(let s=n.position.start.line;s>=1;s--){const r=t.get(s);if(r)return r}return 0}},W={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`)},ot=(e,t)=>{const n=[],s=[...e.map(i=>({node:i,type:"open"})),...t.map(i=>({node:i,type:"close"}))].sort((i,o)=>y(i.node,o.node)),r=[];for(const i of s)if(i.type==="open")r.push(i);else{const o=i.node;if(r.length===0)throw W.closingCommentNotMatchedToOpening(o);const l=r.pop().node;if(r.length>0)continue;n.push({open:l,close:o})}if(r.length>0)throw W.openingCommentNotClosed(r[0].node);return n},lt=(e,t,n)=>{const s=[...t].sort(y),r=[];return[...n].sort((i,o)=>y.reverse(i.open,o.open)).forEach(i=>{for(;s.length>0;){const o=s.pop();if(o.position.start.offset<i.open.position.start.offset){if(xe(e,o,i.open).trim()!=="")throw W.openingCommentDoesNotFollowLink(i.open);return r.push([o,i])}r.push(o)}throw W.openingCommentDoesNotFollowLink(i.open)}),r.push(...s.reverse()),r.reverse()},ue=(e,t)=>{t??(t=L.md(e));const n=it(t),s=j(t,"link").filter(tt),r=j(t,"html").sort(y),i=r.filter(pe("begin")),o=r.filter(pe("end")),l=ot(i,o);return lt(e,s,l).map(a=>Array.isArray(a)?st(a[0],a[1].close,n(a[0])):nt(a,n(a)))},at=(e,{url:t})=>n=>e(h.join(h.dirname(t),n)),me=(...e)=>{const t=e.reduce((n,s)=>n+s,0);return Math.min(Math.max(t,1),6)},ct=(e,t,n)=>{if(t===0)return e;n??(n=L.md(e));const s=j(n,"heading"),r=e.split(`
14
+ `);for(const i of s){const{depth:o,position:{start:l,end:c}}=i,a=me(o,t),m=r[l.line-1].slice(o,c.column),p="#".repeat(a)+m;r[l.line-1]=p,i.depth=a}return r.join(`
15
+ `)},de=e=>ue(e).reverse().sort(y.reverse).reduce((t,n)=>J(t,ce(n),n),e),he=(e,t,n,s,r)=>{try{e=de(e),e=ct(e,t);const i=L.md(e),o=new H,l=ue(e,i).sort(y);return l.filter(({url:c})=>c.startsWith("?")).forEach(({url:c})=>o.tryStore(c)),l.reverse().map(c=>{var P,M;const{url:a,headingDepth:m}=c,[p,...S]=h.basename(a).split("?"),k=o.apply(S.join("?"));if(!a.startsWith("?"))if(a.startsWith("./")||a.startsWith("../")){const g=p.split(".").pop()??"",Q=h.dirname(a),we=h.join(Q,p);let v=n(we);const _=new ye.URLSearchParams(k),G=(P=(b=>{const $=Array.from(_.entries()).filter(([x])=>x===b).map(([x,X])=>X);return $.length>=1?$.join(","):void 0})("region"))==null?void 0:P.split(Z);v=(G==null?void 0:G.reduce((b,$,x,{length:X})=>Je(b,$,x===X-1),v))??v;const ut=_.has("skip"),mt=_.get("heading")??0,dt=_.has("inline");let{inline:z}=c;if(dt&&(z=!0),!ut)if(g==="md"){const b=at(n,c),$=r?h.join(r,Q):Q,x=me(m,Number(mt));v=he(v,x,b,we,$)}else/^(js|ts)x?|svelte$/i.test(g)&&(v=te(v,"code",{extension:g,inline:z}));const V=(M=_.get("wrap"))==null?void 0:M.split(Z);return v=(V==null?void 0:V.reduce((b,$)=>te(b,$,{extension:g,inline:z}),v))??v,{target:c,content:rt(c,v,r)}}else throw a.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${a}`)}).filter(Boolean).reduce((c,{target:a,content:m})=>J(c,m,a),e)}catch(i){throw new Error(`Error populating inclusions in file ${s??"(unknown)"}: ${i}`)}},ge=e=>d.readFileSync(e,"utf-8"),fe=e=>{const t=h.resolve(e),n=h.dirname(t);return{content:ge(t),dir:n,path:t}},ve=(e,t=!0,n)=>{const{dir:s,path:r,content:i}=fe(e);n==null||n.unwatch(r),n==null||n.once("change",(c,a)=>{console.log(`Change detected in "${c}". Regenerating "${r}"...`),ve(e,t,n)});const l=he(i,0,c=>{const a=h.resolve(s,_e(c));return n==null||n.add(a),ge(a)},r);return t&&d.writeFileSync(r,l),n==null||n.add(r),l},pt=(e,t=!0,n)=>{const{path:s,content:r}=fe(e),i=de(r);return t&&d.writeFileSync(s,i),i};u.depopulateMarkdownInclusions=pt,u.populateMarkdownInclusions=ve,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@p-buddy/parkdown",
3
3
  "type": "module",
4
4
  "private": false,
5
- "version": "0.0.25",
5
+ "version": "0.0.27",
6
6
  "main": "./dist/index.js",
7
7
  "bin": "./dist/cli.js",
8
8
  "files": [