@p-buddy/parkdown 0.0.4 → 0.0.5

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/index.js CHANGED
@@ -1,66 +1,69 @@
1
- import { writeFileSync as q, readFileSync as te } from "node:fs";
2
- import { basename as ne, dirname as _, join as C, resolve as U } from "node:path";
3
- import { URLSearchParams as oe } from "node:url";
4
- import { unified as se } from "unified";
5
- import re from "remark-parse";
6
- import { visit as ie } from "unist-util-visit";
7
- import ce from "stable-hash";
8
- import { dedent as k } from "ts-dedent";
9
- import le from "extract-comments";
10
- const g = (e, n) => e.position.start.offset - n.position.start.offset;
11
- g.reverse = (e, n) => g(n, e);
12
- const T = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, ae = se().use(re), I = {
13
- md: (e) => ae.parse(e)
14
- }, y = (e, n) => {
15
- const t = [];
16
- return ie(e, (s, o, r) => {
1
+ var ae = Object.defineProperty;
2
+ var pe = (e, t, n) => t in e ? ae(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var q = (e, t, n) => pe(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { writeFileSync as z, readFileSync as me } from "node:fs";
5
+ import { basename as ue, dirname as O, join as S, resolve as X } from "node:path";
6
+ import { URLSearchParams as he } from "node:url";
7
+ import { unified as ge } from "unified";
8
+ import fe from "remark-parse";
9
+ import { visit as de } from "unist-util-visit";
10
+ import ve from "stable-hash";
11
+ import { dedent as T } from "ts-dedent";
12
+ import we from "extract-comments";
13
+ const g = (e, t) => e.position.start.offset - t.position.start.offset;
14
+ g.reverse = (e, t) => g(t, e);
15
+ const D = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, $e = ge().use(fe), I = {
16
+ md: (e) => $e.parse(e)
17
+ }, b = (e, t) => {
18
+ const n = [];
19
+ return de(e, (s, o, r) => {
17
20
  if (s.type !== "root") {
18
- if (n && s.type !== n) return;
19
- if (T(s)) {
20
- const i = ce(r), c = ((r == null ? void 0 : r.children.length) ?? 0) - 1;
21
- t.push({ ...s, parentID: i, siblingIndex: o, siblingCount: c });
21
+ if (t && s.type !== t) return;
22
+ if (D(s)) {
23
+ const i = ve(r), c = ((r == null ? void 0 : r.children.length) ?? 0) - 1;
24
+ n.push({ ...s, parentID: i, siblingIndex: o, siblingCount: c });
22
25
  }
23
26
  }
24
- }), t;
25
- }, pe = (e) => e.children.length === 0, G = (e, n, ...t) => {
26
- if (t.length === 0) throw new Error("No nodes to replace content from");
27
- t.sort(g);
28
- const s = t.at(0), o = t.at(-1);
29
- return e.slice(0, s.position.start.offset) + n + e.slice(o.position.end.offset);
30
- }, me = (e, n, t) => {
31
- const s = Math.min(n.position.end.offset, t.position.end.offset), o = Math.max(n.position.start.offset, t.position.start.offset);
27
+ }), n;
28
+ }, Ce = (e) => e.children.length === 0, J = (e, t, ...n) => {
29
+ if (n.length === 0) throw new Error("No nodes to replace content from");
30
+ n.sort(g);
31
+ const s = n.at(0), o = n.at(-1);
32
+ return e.slice(0, s.position.start.offset) + t + e.slice(o.position.end.offset);
33
+ }, ye = (e, t, n) => {
34
+ const s = Math.min(t.position.end.offset, n.position.end.offset), o = Math.max(t.position.start.offset, n.position.start.offset);
32
35
  return e.slice(s, o);
33
- }, L = (...e) => e.join(" "), ue = (...e) => e.join(`
34
- `), he = (e) => {
35
- const n = e.split("?");
36
- return n.length > 1 ? [n.slice(0, -1).join("?"), n.at(-1)] : [e, ""];
37
- }, ge = (e) => he(e)[0];
36
+ }, E = (...e) => e.join(" "), Ee = (...e) => e.join(`
37
+ `), Se = (e) => {
38
+ const t = e.split("?");
39
+ return t.length > 1 ? [t.slice(0, -1).join("?"), t.at(-1)] : [e, ""];
40
+ }, be = (e) => Se(e)[0];
38
41
  class d {
39
42
  constructor() {
40
- this.intervals = [];
43
+ q(this, "intervals", []);
41
44
  }
42
- push(n, t) {
43
- this.intervals.push([Math.min(n, t), Math.max(n, t)]);
45
+ push(t, n) {
46
+ this.intervals.push([Math.min(t, n), Math.max(t, n)]);
44
47
  }
45
- combine(n) {
46
- this.intervals.push(...n.intervals);
48
+ combine(t) {
49
+ this.intervals.push(...t.intervals);
47
50
  }
48
51
  collapse() {
49
- const { intervals: n } = this;
50
- if (!n.length) return this.intervals = [];
51
- n.sort((r, i) => r[0] - i[0]);
52
- const t = [];
53
- let [s, o] = n[0];
54
- for (let r = 1; r < n.length; r++) {
55
- const [i, c] = n[r];
56
- i <= o ? o = Math.max(o, c) : (t.push([s, o]), s = i, o = c);
52
+ const { intervals: t } = this;
53
+ if (!t.length) return this.intervals = [];
54
+ t.sort((r, i) => r[0] - i[0]);
55
+ const n = [];
56
+ let [s, o] = t[0];
57
+ for (let r = 1; r < t.length; r++) {
58
+ const [i, c] = t[r];
59
+ i <= o ? o = Math.max(o, c) : (n.push([s, o]), s = i, o = c);
57
60
  }
58
- return t.push([s, o]), this.intervals = t;
61
+ return n.push([s, o]), this.intervals = n;
59
62
  }
60
- subtract(n) {
61
- const { intervals: t } = this, { intervals: s } = n;
62
- if (!t.length || !s.length) return t;
63
- let o = [...t];
63
+ subtract(t) {
64
+ const { intervals: n } = this, { intervals: s } = t;
65
+ if (!n.length || !s.length) return n;
66
+ let o = [...n];
64
67
  for (const [r, i] of s) {
65
68
  const c = [];
66
69
  for (const [l, a] of o) {
@@ -75,62 +78,94 @@ class d {
75
78
  return this.intervals = o;
76
79
  }
77
80
  }
78
- const P = /,\s*(?![^()]*\))/, fe = [
79
- [/'''/g, '"'],
80
- [/''/g, "'"],
81
- [/parkdown:\s+/g, ""],
82
- [/p▼:\s+/g, ""]
83
- ], de = (e, n = "-") => {
84
- const t = fe.reduce((s, [o, r]) => s.replaceAll(o, r), e);
85
- return n ? t.replaceAll(n, " ") : t;
86
- }, we = ["string", "number", "boolean"], ve = (e) => we.includes(e), $e = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, Ce = (e) => {
87
- const n = e.match($e);
88
- if (!n) throw new Error(`Invalid invocation: ${e}`);
89
- const [, t, s = ""] = n;
90
- if (!s.trim()) return { name: t, parameters: [] };
91
- const o = Ee(s);
92
- return { name: t, parameters: o };
93
- }, ye = (e, n) => {
94
- for (let t = n + 1; t < e.length; t++)
95
- if (e[t] !== void 0) return !1;
81
+ const L = /,\s*(?![^()]*\))/, W = {
82
+ reserved: {
83
+ semi: ";",
84
+ slash: "/",
85
+ question: "?",
86
+ colon: ":",
87
+ at: "@",
88
+ equal: "=",
89
+ and: "&"
90
+ },
91
+ unsafe: {
92
+ quote: '"',
93
+ angle: "<",
94
+ unangle: ">",
95
+ hash: "#",
96
+ percent: "%",
97
+ curly: "{",
98
+ uncurly: "}",
99
+ pipe: "|",
100
+ back: "\\",
101
+ carrot: "^",
102
+ tilde: "~",
103
+ square: "[",
104
+ unsquare: "]",
105
+ tick: "`"
106
+ }
107
+ }, Ne = "-", B = {
108
+ static: [
109
+ ["'''", '"'],
110
+ ["''", "'"],
111
+ [/parkdown:\s+/g, ""],
112
+ [/p↓:\s+/g, ""]
113
+ ],
114
+ url: [
115
+ ...Object.entries(W.unsafe),
116
+ ...Object.entries(W.reserved)
117
+ ]
118
+ }, F = (e, [t, n]) => e.replaceAll(t, n), V = (e, t = Ne) => {
119
+ const n = B.static.reduce(F, e);
120
+ return B.url.map(([s, o]) => [t + s + t, o]).reduce(F, n).replaceAll(t, " ");
121
+ }, ke = ["string", "number", "boolean"], xe = (e) => ke.includes(e), Me = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, _e = (e) => {
122
+ const t = e.match(Me);
123
+ if (!t) throw new Error(`Invalid invocation: ${e}`);
124
+ const [, n, s = ""] = t;
125
+ if (!s.trim()) return { name: n, parameters: [] };
126
+ const o = Te(s);
127
+ return { name: n, parameters: o };
128
+ }, Oe = (e, t) => {
129
+ for (let n = t + 1; n < e.length; n++)
130
+ if (e[n] !== void 0) return !1;
96
131
  return !0;
97
- }, Ee = (e) => {
98
- const n = [];
99
- let t = "", s = !1;
132
+ }, Te = (e) => {
133
+ const t = [];
134
+ let n = "", s = !1;
100
135
  for (let o = 0; o < e.length; o++) {
101
136
  const r = e[o], i = r === "'";
102
137
  if (i && e.at(o + 1) === "'") {
103
138
  const c = e.at(o + 2) === "'";
104
- s = !s, t += c ? "'''" : "''", o += c ? 2 : 1;
105
- } else i ? (s = !s, t += r) : r === "," && !s ? (n.push(t.trim()), t = "") : t += r;
139
+ s = !s, n += c ? "'''" : "''", o += c ? 2 : 1;
140
+ } else i ? (s = !s, n += r) : r === "," && !s ? (t.push(n.trim()), n = "") : n += r;
106
141
  }
107
- return n.push(t.trim()), n.map((o) => o === "" ? void 0 : o ? Se(o) : void 0).filter((o, r, i) => o !== void 0 || !ye(i, r));
108
- }, Se = (e) => {
109
- const n = e.length >= 2 && e[0] === "'" && e.at(-1) === "'", t = n && e.length >= 4 && e[1] === "'" && e.at(-2) === "'";
110
- return !n || t ? e : e.slice(1, -1);
111
- }, Ne = (e) => {
112
- const n = /^(\w+)(?:\(([^)]*)\))?/, t = e.match(n);
113
- if (!t) return { name: e };
114
- const [, s, o] = t;
142
+ return t.push(n.trim()), t.map((o) => o === "" ? void 0 : o ? De(o) : void 0).filter((o, r, i) => o !== void 0 || !Oe(i, r));
143
+ }, De = (e) => {
144
+ const t = e.length >= 2 && e[0] === "'" && e.at(-1) === "'", n = t && e.length >= 4 && e[1] === "'" && e.at(-2) === "'";
145
+ return !t || n ? e : e.slice(1, -1);
146
+ }, Ie = (e) => {
147
+ const t = /^(\w+)(?:\(([^)]*)\))?/, n = e.match(t);
148
+ if (!n) return { name: e };
149
+ const [, s, o] = n;
115
150
  if (!o) return { name: s };
116
151
  const r = /(\w+)(\?)?:\s*([^,]+)/g, i = [];
117
152
  let c;
118
153
  for (; (c = r.exec(o)) !== null; ) {
119
154
  const [, l, a, p] = c, m = p.trim();
120
- if (!ve(m)) throw new Error(`Unsupported type: ${m}`);
155
+ if (!xe(m)) throw new Error(`Unsupported type: ${m}`);
121
156
  i.push({ name: l, optional: a === "?", type: m });
122
157
  }
123
158
  return { name: s, parameters: i };
124
- }, H = (e) => {
125
- const n = e.map(Ne).reduce(
126
- (t, { name: s, parameters: o }) => t.set(s, o),
159
+ }, Z = (e) => {
160
+ const t = e.map(Ie).reduce(
161
+ (n, { name: s, parameters: o }) => n.set(s, o),
127
162
  /* @__PURE__ */ new Map()
128
163
  );
129
- return (t) => {
130
- const { name: s, parameters: o } = Ce(t);
131
- if (!n.has(s))
164
+ return (n) => {
165
+ const { name: s, parameters: o } = _e(n);
166
+ if (!t.has(s))
132
167
  throw new Error(`Unknown method: ${s}`);
133
- const r = n.get(s);
168
+ const r = t.get(s);
134
169
  if (r === void 0)
135
170
  return { name: s };
136
171
  if (o.length > r.length) {
@@ -157,7 +192,7 @@ const P = /,\s*(?![^()]*\))/, fe = [
157
192
  return i;
158
193
  }, { name: s });
159
194
  };
160
- }, R = (e) => Object.entries(e).filter(([n]) => n !== "name" && !isNaN(Number(n))).map(([n, t]) => t), xe = [
195
+ }, U = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), je = [
161
196
  /**
162
197
  * Wraps the content in a markdown-formatted code block.
163
198
  * @param lang The language of the code block (defaults to the file extension).
@@ -187,210 +222,251 @@ const P = /,\s*(?![^()]*\))/, fe = [
187
222
  * @example [](<url>?wrap=dropdown(hello_world,,_))
188
223
  */
189
224
  "dropdown(summary: string, open?: boolean, space?: string)"
190
- ], Me = "-", be = H(xe), W = (e, n, t) => {
191
- const s = be(n), o = (t == null ? void 0 : t.inline) && !e.includes(`
192
-
193
- `);
225
+ ], Re = Z(je), f = (e, t = 1) => `
226
+ `.repeat(t) + e + `
227
+ `.repeat(t), _ = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${f(e)}</${t}>`, G = (e, t, n) => {
228
+ const s = Re(t);
194
229
  switch (s.name) {
195
230
  case "code":
196
- const r = s.lang ?? (t == null ? void 0 : t.extension) ?? "", i = s.meta ? ` ${s.meta}` : "";
197
- return `\`\`\`${r}${i}
198
- ${e}
199
- \`\`\``;
231
+ if (!s.lang && !s.meta && (n == null ? void 0 : n.inline) && !e.includes(`
232
+ `)) return `\`${e}\``;
233
+ const r = s.lang ?? (n == null ? void 0 : n.extension) ?? "", i = s.meta ? ` ${s.meta}` : "";
234
+ return f(`\`\`\`${r}${i}${f(e)}\`\`\``);
200
235
  case "quote":
201
- return o ? `> ${e}` : `<blockquote>
236
+ return n != null && n.inline && !e.includes(`
202
237
 
203
- ${e}
204
-
205
- </blockquote>
206
- `;
238
+ `) ? `> ${e}` : f(_(f(e), "blockquote"));
207
239
  case "dropdown":
208
- const c = `<details${s.open ? " open" : ""}>`, l = `<summary>${s.summary.split(s.space ?? Me).join(" ")}</summary>`;
209
- return ["", c, l, "", e, "</details>", ""].join(`
210
- `);
240
+ const c = _(V(s.summary), "summary");
241
+ return f(_([c, e].join(`
242
+ `), "details", s.open ? "open" : void 0));
211
243
  }
212
- }, _e = [
244
+ }, Ae = [
245
+ /**
246
+ * Extract regions from the retrieved content between comments that INCLUDE the specified ids.
247
+ * @param id The id of the comment to extract.
248
+ * @param 0 An optional additional id to extract.
249
+ * @param 1 An optional additional id to extract.
250
+ * @param 2 An optional additional id to extract.
251
+ * @example [](<url>?region=extract(specifier))
252
+ * @example [](<url>?region=extract(specifier,other-specifier,some-other-specifier))
253
+ */
213
254
  "extract(id: string, 0?: string, 1?: string, 2?: string)",
255
+ /**
256
+ * Remove regions from the retrieved content between comments that INCLUDE the specified ids.
257
+ * @param id The id of the comment to remove.
258
+ * @param 0 An optional additional id to remove.
259
+ * @param 1 An optional additional id to remove.
260
+ * @param 2 An optional additional id to remove.
261
+ * @example [](<url>?region=remove(specifier))
262
+ * @example [](<url>?region=remove(specifier,other-specifier,some-other-specifier))
263
+ */
214
264
  "remove(id: string, 0?: string, 1?: string, 2?: string)",
265
+ /**
266
+ * Replace regions from the retrieved content between comments that INCLUDE the specified ids.
267
+ * @param id The id of the comment to replace.
268
+ * @param with The replacement content (if ommitted, the content of the detected comment will be used).
269
+ * @param space The space character to use between words in the replacement content (defaults to `-`).
270
+ * @example [](<url>?region=replace(specifier))
271
+ * @example [](<url>?region=replace(specifier,new-content))
272
+ * @example [](<url>?region=replace(specifier,new_content,_)
273
+ */
215
274
  "replace(id: string, with?: string, space?: string)"
216
- ], ke = H(_e), D = (e) => le(e), B = (e, n) => D(e).filter(({ value: t }) => t.includes(n)).sort((t, s) => t.range[0] - s.range[0]), Te = (e, ...n) => {
217
- if (n.length === 0) return e;
218
- const t = ([i, c]) => e.slice(i, c), s = D(e), o = new d(), r = new d();
219
- for (const i of n) {
275
+ ], Pe = Z(Ae), j = (e) => we(e), H = (e, t) => j(e).filter(({ value: n }) => n.includes(t)).sort((n, s) => n.range[0] - s.range[0]), qe = (e, ...t) => {
276
+ if (t.length === 0) return e;
277
+ const n = ([i, c]) => e.slice(i, c), s = j(e), o = new d(), r = new d();
278
+ for (const i of t) {
220
279
  const c = s.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
221
280
  for (let l = 0; l < c.length - 1; l += 2) {
222
281
  const a = c[l], p = c[l + 1];
223
282
  o.push(a.range[1], p.range[0]);
224
- const [m, ...f] = t([a.range[1], p.range[0]]), S = f[f.length - 1];
225
- r.push(a.range[0], a.range[1] + (m.trim() ? 0 : 1)), r.push(p.range[0], p.range[1] + (S.trim() ? 0 : 1));
283
+ const [m, ...y] = n([a.range[1], p.range[0]]), v = y[y.length - 1];
284
+ r.push(a.range[0], a.range[1] + (m.trim() ? 0 : 1)), r.push(p.range[0], p.range[1] + (v.trim() ? 0 : 1));
226
285
  }
227
286
  }
228
- return o.collapse(), r.collapse(), k(
229
- o.subtract(r).map(t).filter(Boolean).join("")
287
+ return o.collapse(), r.collapse(), T(
288
+ o.subtract(r).map(n).filter(Boolean).join("")
230
289
  ).trim();
231
- }, Ie = (e, ...n) => {
232
- if (n.length === 0) return e;
233
- const t = ([i, c]) => e.slice(i, c), s = D(e), o = new d();
234
- for (const i of n) {
290
+ }, Le = (e, ...t) => {
291
+ if (t.length === 0) return e;
292
+ const n = ([i, c]) => e.slice(i, c), s = j(e), o = new d();
293
+ for (const i of t) {
235
294
  const c = s.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
236
295
  for (let l = 0; l < c.length - 1; l += 2) {
237
- const a = c[l], p = c[l + 1], m = t([p.range[1], p.range[1] + 1]).at(-1);
296
+ const a = c[l], p = c[l + 1], m = n([p.range[1], p.range[1] + 1]).at(-1);
238
297
  o.push(a.range[0], m === `
239
298
  ` ? p.range[1] + 1 : p.range[1]);
240
299
  }
241
300
  }
242
301
  o.collapse();
243
302
  const r = new d();
244
- return r.push(0, e.length), r.subtract(o), k(
245
- r.collapse().map(t).filter(Boolean).join("")
303
+ return r.push(0, e.length), r.subtract(o), T(
304
+ r.collapse().map(n).filter(Boolean).join("")
246
305
  ).trim();
247
- }, De = (e, n, t, s) => {
248
- if (!n) return e;
249
- const o = B(e, n);
306
+ }, We = (e, t, n, s) => {
307
+ if (!t) return e;
308
+ const o = H(e, t);
250
309
  if (o.length < 2) return e;
251
310
  let r = "", i = 0;
252
311
  for (let l = 0; l < o.length - 1; l += 2) {
253
312
  const a = o[l], p = o[l + 1];
254
- r += e.slice(i, a.range[1]), r += de(t ?? a.value, s), i = p.range[0];
313
+ r += e.slice(i, a.range[1]), r += V(n ?? a.value, s), i = p.range[0];
255
314
  }
256
315
  r += e.slice(i);
257
316
  const c = new d();
258
317
  return c.push(0, r.length), c.subtract(
259
- B(r, n).reduce((l, { range: a }) => (l.push(...a), l), new d())
260
- ), k(
318
+ H(r, t).reduce((l, { range: a }) => (l.push(...a), l), new d())
319
+ ), T(
261
320
  c.collapse().map(([l, a]) => r.slice(l, a)).filter(Boolean).join("")
262
321
  ).trim();
263
- }, je = (e, n) => {
264
- const t = ke(n);
265
- switch (t.name) {
322
+ }, Be = (e, t) => {
323
+ const n = Pe(t);
324
+ switch (n.name) {
266
325
  case "extract":
267
- return Te(e, t.id, ...R(t));
326
+ e = qe(e, n.id, ...U(n));
327
+ break;
268
328
  case "remove":
269
- return Ie(e, t.id, ...R(t));
329
+ e = Le(e, n.id, ...U(n));
330
+ break;
270
331
  case "replace":
271
- return De(e, t.id, t.with, t.space);
332
+ e = We(e, n.id, n.with, n.space);
333
+ break;
272
334
  }
273
- }, Oe = ["http", "./", "../"], Ae = ({ url: e }) => Oe.some((n) => e.startsWith(n)), Le = (e) => T(e) && pe(e) && Ae(e), b = ({ url: e }, n) => `[](${n ? C(n, e) : e})`, h = {
335
+ return e;
336
+ }, Fe = ["http", "./", "../"], Ue = ({ url: e }) => Fe.some((t) => e.startsWith(t)), Ge = (e) => D(e) && Ce(e) && Ue(e), K = ({ url: e }, t) => `[](${t ? S(t, e) : e})`, u = {
274
337
  _open: "<!--",
275
338
  _close: "-->",
276
- _flag: "p",
339
+ _flag: "p",
277
340
  get begin() {
278
- return L(h._open, h._flag, "BEGIN", h._close);
341
+ return E(u._open, u._flag, "BEGIN", u._close);
279
342
  },
280
343
  get end() {
281
- return L(h._open, h._flag, "END", h._close);
344
+ return E(u._open, u._flag, "END", u._close);
345
+ },
346
+ lengthOf(e) {
347
+ const t = `lines: ${e.split(`
348
+ `).length}`, n = `chars: ${e.length}`;
349
+ return E(u._open, u._flag, "length", t, n, u._close);
282
350
  }
283
- }, F = (e) => (n) => T(n) && n.value === h[e], Pe = ({ position: e, url: n, siblingCount: t }, s) => ({ position: e, url: n, headingDepth: s, inline: t >= 1 }), Re = ({ position: { start: e }, url: n, siblingCount: t }, { position: { end: s } }, o) => ({ position: { start: e, end: s }, url: n, headingDepth: o, inline: t >= 1 }), We = (e, n, t) => e.inline ? `${b(e, t)} ${h.begin} ${n} ${h.end}` : ue(b(e, t), h.begin, n, h.end), Be = (e) => {
284
- const n = y(e, "heading").reduce((t, { position: s, depth: o }) => t.set(s.start.line, o), /* @__PURE__ */ new Map());
285
- return (t) => {
286
- for (let s = t.position.start.line; s >= 1; s--) {
287
- const o = n.get(s);
351
+ }, Q = (e) => (t) => D(t) && t.value === u[e], He = ({ position: e, url: t, siblingCount: n }, s) => ({ position: e, url: t, headingDepth: s, inline: n >= 1 }), Qe = ({ position: { start: e }, url: t, siblingCount: n }, { position: { end: s } }, o) => ({ position: { start: e, end: s }, url: t, headingDepth: o, inline: n >= 1 }), ze = (e, t, n) => (e.inline ? E : Ee)(
352
+ K(e, n),
353
+ u.begin,
354
+ u.lengthOf(t),
355
+ t,
356
+ u.end
357
+ ), Xe = (e) => {
358
+ const t = b(e, "heading").reduce((n, { position: s, depth: o }) => n.set(s.start.line, o), /* @__PURE__ */ new Map());
359
+ return (n) => {
360
+ for (let s = n.position.start.line; s >= 1; s--) {
361
+ const o = t.get(s);
288
362
  if (o) return o;
289
363
  }
290
364
  return 0;
291
365
  };
292
- }, E = {
366
+ }, N = {
293
367
  openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
294
368
  closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
295
369
  openingCommentNotClosed: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)
296
- }, Fe = (e, n) => {
297
- const t = [], s = [
370
+ }, Je = (e, t) => {
371
+ const n = [], s = [
298
372
  ...e.map((r) => ({ node: r, type: "open" })),
299
- ...n.map((r) => ({ node: r, type: "close" }))
373
+ ...t.map((r) => ({ node: r, type: "close" }))
300
374
  ].sort((r, i) => g(r.node, i.node)), o = [];
301
375
  for (const r of s)
302
376
  if (r.type === "open") o.push(r);
303
377
  else {
304
378
  const i = r.node;
305
379
  if (o.length === 0)
306
- throw E.closingCommentNotMatchedToOpening(i);
380
+ throw N.closingCommentNotMatchedToOpening(i);
307
381
  const c = o.pop().node;
308
382
  if (o.length > 0) continue;
309
- t.push({ open: c, close: i });
383
+ n.push({ open: c, close: i });
310
384
  }
311
385
  if (o.length > 0)
312
- throw E.openingCommentNotClosed(o[0].node);
313
- return t;
314
- }, qe = (e, n, t) => {
315
- const s = [...n].sort(g), o = [];
316
- return [...t].sort((r, i) => g.reverse(r.open, i.open)).forEach((r) => {
386
+ throw N.openingCommentNotClosed(o[0].node);
387
+ return n;
388
+ }, Ve = (e, t, n) => {
389
+ const s = [...t].sort(g), o = [];
390
+ return [...n].sort((r, i) => g.reverse(r.open, i.open)).forEach((r) => {
317
391
  for (; s.length > 0; ) {
318
392
  const i = s.pop();
319
393
  if (i.position.start.offset < r.open.position.start.offset) {
320
- if (me(e, i, r.open).trim() !== "")
321
- throw E.openingCommentDoesNotFollowLink(r.open);
394
+ if (ye(e, i, r.open).trim() !== "")
395
+ throw N.openingCommentDoesNotFollowLink(r.open);
322
396
  return o.push([i, r]);
323
397
  }
324
398
  o.push(i);
325
399
  }
326
- throw E.openingCommentDoesNotFollowLink(r.open);
400
+ throw N.openingCommentDoesNotFollowLink(r.open);
327
401
  }), o.push(...s.reverse()), o.reverse();
328
- }, Q = (e, n) => {
329
- n ?? (n = I.md(e));
330
- const t = Be(n), s = y(n, "link").filter(Le), o = y(n, "html").sort(g), r = o.filter(F("begin")), i = o.filter(F("end")), c = Fe(r, i);
331
- return qe(e, s, c).map((a) => Array.isArray(a) ? Re(a[0], a[1].close, t(a[0])) : Pe(a, t(a)));
332
- }, Ue = (e, { url: n }) => (t) => e(C(_(n), t)), z = (...e) => {
333
- const n = e.reduce((t, s) => t + s, 0);
334
- return Math.min(Math.max(n, 1), 6);
335
- }, Ge = (e, n, t) => {
336
- if (n === 0) return e;
402
+ }, Y = (e, t) => {
337
403
  t ?? (t = I.md(e));
338
- const s = y(t, "heading"), o = e.split(`
404
+ const n = Xe(t), s = b(t, "link").filter(Ge), o = b(t, "html").sort(g), r = o.filter(Q("begin")), i = o.filter(Q("end")), c = Je(r, i);
405
+ return Ve(e, s, c).map((a) => Array.isArray(a) ? Qe(a[0], a[1].close, n(a[0])) : He(a, n(a)));
406
+ }, Ze = (e, { url: t }) => (n) => e(S(O(t), n)), ee = (...e) => {
407
+ const t = e.reduce((n, s) => n + s, 0);
408
+ return Math.min(Math.max(t, 1), 6);
409
+ }, Ke = (e, t, n) => {
410
+ if (t === 0) return e;
411
+ n ?? (n = I.md(e));
412
+ const s = b(n, "heading"), o = e.split(`
339
413
  `);
340
414
  for (const r of s) {
341
- const { depth: i, position: { start: c, end: l } } = r, a = z(i, n), p = o[c.line - 1].slice(i, l.column), m = "#".repeat(a) + p;
415
+ const { depth: i, position: { start: c, end: l } } = r, a = ee(i, t), p = o[c.line - 1].slice(i, l.column), m = "#".repeat(a) + p;
342
416
  o[c.line - 1] = m, r.depth = a;
343
417
  }
344
418
  return o.join(`
345
419
  `);
346
- }, X = (e) => Q(e).reverse().sort(g.reverse).reduce((n, t) => G(n, b(t), t), e), J = (e, n, t, s) => {
347
- e = X(e), e = Ge(e, n);
348
- const o = I.md(e);
349
- return Q(e, o).sort(g).reverse().map((r) => {
350
- var O, A;
351
- const { url: i, headingDepth: c } = r, [l, ...a] = ne(i).split("?"), p = l.split(".").pop() ?? "", m = a.join("?"), f = _(i), S = C(f, l);
352
- if (i.startsWith("./") || i.startsWith("../")) {
353
- let u = t(S);
354
- const w = new oe(m), N = (O = w.get("region")) == null ? void 0 : O.split(P);
355
- u = (N == null ? void 0 : N.reduce((v, $) => je(v, $), u)) ?? u;
356
- const Z = w.has("skip"), K = w.get("heading") ?? 0, Y = w.has("inline");
357
- let { inline: x } = r;
358
- if (Y && (x = !0), !Z)
359
- if (p === "md") {
360
- const v = Ue(t, r), $ = s ? C(s, f) : f, ee = z(c, Number(K));
361
- u = J(
362
- u,
363
- /** p▼: ... */
364
- ee,
365
- v,
366
- $
367
- /** p▼: ... */
368
- );
369
- } else /^(js|ts)x?|svelte$/i.test(p) && (u = W(
370
- u,
371
- "code",
372
- /** p▼: ... */
373
- { extension: p, inline: x }
374
- /** p▼: ... */
375
- ));
376
- const M = (A = w.get("wrap")) == null ? void 0 : A.split(P);
377
- return u = (M == null ? void 0 : M.reduce((v, $) => W(v, $, { extension: p, inline: x }), u)) ?? u, { target: r, content: We(r, u, s) };
378
- } else throw i.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${i}`);
379
- }).reduce((r, { target: i, content: c }) => G(r, c, i), e);
380
- }, V = (e) => te(e, "utf-8"), j = (e) => {
381
- const n = U(e), t = _(n);
382
- return { markdown: V(n), dir: t, path: n };
383
- }, et = (e, n = !0) => {
384
- const { dir: t, path: s, markdown: o } = j(e), i = J(o, 0, (c) => V(U(t, ge(c))));
385
- return n && q(s, i), i;
386
- }, tt = (e, n = !0) => {
387
- const { path: t, markdown: s } = j(e), o = X(s);
388
- return n && q(t, o), o;
389
- }, nt = (e, n = !0) => {
390
- j(e);
420
+ }, te = (e) => Y(e).reverse().sort(g.reverse).reduce((t, n) => J(t, K(n), n), e), ne = (e, t, n, s, o) => {
421
+ try {
422
+ e = te(e), e = Ke(e, t);
423
+ const r = I.md(e);
424
+ return Y(e, r).sort(g).reverse().map((i) => {
425
+ var A, P;
426
+ const { url: c, headingDepth: l } = i, [a, ...p] = ue(c).split("?"), m = a.split(".").pop() ?? "", y = p.join("?"), v = O(c), R = S(v, a);
427
+ if (c.startsWith("./") || c.startsWith("../")) {
428
+ let h = n(R);
429
+ const w = new he(y), k = (A = w.get("region")) == null ? void 0 : A.split(L);
430
+ h = (k == null ? void 0 : k.reduce(($, C) => Be($, C), h)) ?? h;
431
+ const re = w.has("skip"), ie = w.get("heading") ?? 0, ce = w.has("inline");
432
+ let { inline: x } = i;
433
+ if (ce && (x = !0), !re)
434
+ if (m === "md") {
435
+ const $ = Ze(n, i), C = o ? S(o, v) : v, le = ee(l, Number(ie));
436
+ h = ne(
437
+ h,
438
+ /** p↓: ... */
439
+ le,
440
+ $,
441
+ R,
442
+ C
443
+ /** p↓: ... */
444
+ );
445
+ } else /^(js|ts)x?|svelte$/i.test(m) && (h = G(
446
+ h,
447
+ "code",
448
+ /** p↓: ... */
449
+ { extension: m, inline: x }
450
+ /** p↓: ... */
451
+ ));
452
+ const M = (P = w.get("wrap")) == null ? void 0 : P.split(L);
453
+ return h = (M == null ? void 0 : M.reduce(($, C) => G($, C, { extension: m, inline: x }), h)) ?? h, { target: i, content: ze(i, h, o) };
454
+ } else throw c.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${c}`);
455
+ }).reduce((i, { target: c, content: l }) => J(i, l, c), e);
456
+ } catch (r) {
457
+ throw new Error(`Error populating inclusions in file ${s}: ${r}`);
458
+ }
459
+ }, oe = (e) => me(e, "utf-8"), se = (e) => {
460
+ const t = X(e), n = O(t);
461
+ return { markdown: oe(t), dir: n, path: t };
462
+ }, at = (e, t = !0) => {
463
+ const { dir: n, path: s, markdown: o } = se(e), i = ne(o, 0, (c) => oe(X(n, be(c))), s);
464
+ return t && z(s, i), i;
465
+ }, pt = (e, t = !0) => {
466
+ const { path: n, markdown: s } = se(e), o = te(s);
467
+ return t && z(n, o), o;
391
468
  };
392
469
  export {
393
- tt as depopulateMarkdownInclusions,
394
- et as populateMarkdownInclusions,
395
- nt as remapImportSpecifiers
470
+ pt as depopulateMarkdownInclusions,
471
+ at as populateMarkdownInclusions
396
472
  };