@p-buddy/parkdown 0.0.4 → 0.0.6

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,163 +1,198 @@
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) => {
17
- 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 });
1
+ var ue = Object.defineProperty;
2
+ var me = (e, t, n) => t in e ? ue(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var q = (e, t, n) => me(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { writeFileSync as J, readFileSync as he } from "node:fs";
5
+ import { basename as fe, dirname as O, join as x, resolve as V } from "node:path";
6
+ import { URLSearchParams as ge } from "node:url";
7
+ import { unified as de } from "unified";
8
+ import ve from "remark-parse";
9
+ import { visit as we } from "unist-util-visit";
10
+ import $e from "stable-hash";
11
+ import { dedent as D } from "ts-dedent";
12
+ import Ce from "extract-comments";
13
+ const f = (e, t) => e.position.start.offset - t.position.start.offset;
14
+ f.reverse = (e, t) => f(t, e);
15
+ const I = (e) => e.position !== void 0 && e.position.start.offset !== void 0 && e.position.end.offset !== void 0, ye = de().use(ve), j = {
16
+ md: (e) => ye.parse(e)
17
+ }, S = (e, t) => {
18
+ const n = [];
19
+ return we(e, (o, s, r) => {
20
+ if (o.type !== "root") {
21
+ if (t && o.type !== t) return;
22
+ if (I(o)) {
23
+ const i = $e(r), c = ((r == null ? void 0 : r.children.length) ?? 0) - 1;
24
+ n.push({ ...o, parentID: i, siblingIndex: s, 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);
32
- 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];
27
+ }), n;
28
+ }, Ee = (e) => e.children.length === 0, Z = (e, t, ...n) => {
29
+ if (n.length === 0) throw new Error("No nodes to replace content from");
30
+ n.sort(f);
31
+ const o = n.at(0), s = n.at(-1);
32
+ return e.slice(0, o.position.start.offset) + t + e.slice(s.position.end.offset);
33
+ }, xe = (e, t, n) => {
34
+ const o = Math.min(t.position.end.offset, n.position.end.offset), s = Math.max(t.position.start.offset, n.position.start.offset);
35
+ return e.slice(o, s);
36
+ }, E = (...e) => e.join(" "), Se = (...e) => e.join(`
37
+ `), be = (e) => {
38
+ const t = e.split("?");
39
+ return t.length > 1 ? [t.slice(0, -1).join("?"), t.at(-1)] : [e, ""];
40
+ }, ke = (e) => be(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 [o, s] = t[0];
57
+ for (let r = 1; r < t.length; r++) {
58
+ const [i, c] = t[r];
59
+ i <= s ? s = Math.max(s, c) : (n.push([o, s]), o = i, s = c);
57
60
  }
58
- return t.push([s, o]), this.intervals = t;
61
+ return n.push([o, s]), 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];
64
- for (const [r, i] of s) {
63
+ subtract(t) {
64
+ const { intervals: n } = this, { intervals: o } = t;
65
+ if (!n.length || !o.length) return n;
66
+ let s = [...n];
67
+ for (const [r, i] of o) {
65
68
  const c = [];
66
- for (const [l, a] of o) {
69
+ for (const [l, a] of s) {
67
70
  if (i <= l || r >= a) {
68
71
  c.push([l, a]);
69
72
  continue;
70
73
  }
71
74
  r > l && c.push([l, r]), i < a && c.push([i, a]);
72
75
  }
73
- o = c;
76
+ s = c;
74
77
  }
75
- return this.intervals = o;
78
+ return this.intervals = s;
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 W = /,\s*(?![^()]*\))/, B = {
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 = "-", F = {
108
+ static: [
109
+ ["'''", '"'],
110
+ ["''", "'"],
111
+ [/parkdown:\s+/g, ""],
112
+ [/p↓:\s+/g, ""]
113
+ ],
114
+ url: [
115
+ ...Object.entries(B.unsafe),
116
+ ...Object.entries(B.reserved)
117
+ ]
118
+ }, U = (e, [t, n]) => e.replaceAll(t, n), K = (e, t = Ne) => {
119
+ const n = F.static.reduce(U, e);
120
+ return F.url.map(([o, s]) => [t + o + t, s]).reduce(U, n).replaceAll(t, " ");
121
+ }, Me = ["string", "number", "boolean"], _e = (e) => Me.includes(e), Te = /^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/, Oe = (e) => {
122
+ const t = e.match(Te);
123
+ if (!t) throw new Error(`Invalid invocation: ${e}`);
124
+ const [, n, o = ""] = t;
125
+ if (!o.trim()) return { name: n, parameters: [] };
126
+ const s = Ie(o);
127
+ return { name: n, parameters: s };
128
+ }, De = (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;
100
- for (let o = 0; o < e.length; o++) {
101
- const r = e[o], i = r === "'";
102
- if (i && e.at(o + 1) === "'") {
103
- 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;
132
+ }, Ie = (e) => {
133
+ const t = [];
134
+ let n = "", o = !1;
135
+ for (let s = 0; s < e.length; s++) {
136
+ const r = e[s], i = r === "'";
137
+ if (i && e.at(s + 1) === "'") {
138
+ const c = e.at(s + 2) === "'";
139
+ o = !o, n += c ? "'''" : "''", s += c ? 2 : 1;
140
+ } else i ? (o = !o, n += r) : r === "," && !o ? (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;
115
- if (!o) return { name: s };
142
+ return t.push(n.trim()), t.map((s) => s === "" ? void 0 : s ? je(s) : void 0).filter((s, r, i) => s !== void 0 || !De(i, r));
143
+ }, je = (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
+ }, Le = (e) => {
147
+ const t = /^(\w+)(?:\(([^)]*)\))?/, n = e.match(t);
148
+ if (!n) return { name: e };
149
+ const [, o, s] = n;
150
+ if (!s) return { name: o };
116
151
  const r = /(\w+)(\?)?:\s*([^,]+)/g, i = [];
117
152
  let c;
118
- for (; (c = r.exec(o)) !== null; ) {
119
- const [, l, a, p] = c, m = p.trim();
120
- if (!ve(m)) throw new Error(`Unsupported type: ${m}`);
121
- i.push({ name: l, optional: a === "?", type: m });
153
+ for (; (c = r.exec(s)) !== null; ) {
154
+ const [, l, a, p] = c, u = p.trim();
155
+ if (!_e(u)) throw new Error(`Unsupported type: ${u}`);
156
+ i.push({ name: l, optional: a === "?", type: u });
122
157
  }
123
- 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),
158
+ return { name: o, parameters: i };
159
+ }, Y = (e) => {
160
+ const t = e.map(Le).reduce(
161
+ (n, { name: o, parameters: s }) => n.set(o, s),
127
162
  /* @__PURE__ */ new Map()
128
163
  );
129
- return (t) => {
130
- const { name: s, parameters: o } = Ce(t);
131
- if (!n.has(s))
132
- throw new Error(`Unknown method: ${s}`);
133
- const r = n.get(s);
164
+ return (n) => {
165
+ const { name: o, parameters: s } = Oe(n);
166
+ if (!t.has(o))
167
+ throw new Error(`Unknown method: ${o}`);
168
+ const r = t.get(o);
134
169
  if (r === void 0)
135
- return { name: s };
136
- if (o.length > r.length) {
170
+ return { name: o };
171
+ if (s.length > r.length) {
137
172
  const i = r.filter(({ optional: l }) => !l).length, c = i === r.length ? i.toString() : `${i} - ${r.length}`;
138
- throw new Error(`Too many parameters: ${o.length} for method '${s}' (expected: ${c})`);
173
+ throw new Error(`Too many parameters: ${s.length} for method '${o}' (expected: ${c})`);
139
174
  }
140
175
  return r.reduce((i, { name: c, optional: l, type: a }, p) => {
141
- const m = o[p];
142
- if (m === void 0) {
176
+ const u = s[p];
177
+ if (u === void 0) {
143
178
  if (l) return i;
144
- throw new Error(`Missing required parameter: ${c} for method '${s}'`);
179
+ throw new Error(`Missing required parameter: ${c} for method '${o}'`);
145
180
  }
146
181
  switch (a) {
147
182
  case "string":
148
- i[c] = m;
183
+ i[c] = u;
149
184
  break;
150
185
  case "number":
151
186
  case "boolean":
152
- i[c] = JSON.parse(m);
187
+ i[c] = JSON.parse(u);
153
188
  break;
154
189
  }
155
190
  if (typeof i[c] !== a)
156
- throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${s}'`);
191
+ throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${o}'`);
157
192
  return i;
158
- }, { name: s });
193
+ }, { name: o });
159
194
  };
160
- }, R = (e) => Object.entries(e).filter(([n]) => n !== "name" && !isNaN(Number(n))).map(([n, t]) => t), xe = [
195
+ }, G = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Ae = [
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,264 @@ 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
- `);
194
- switch (s.name) {
225
+ ], Pe = Y(Ae), g = (e, t = 1) => `
226
+ `.repeat(t) + e + `
227
+ `.repeat(t), T = (e, t, n) => `<${t}${n ? ` ${n}` : ""}>${g(e)}</${t}>`, H = (e, t, n) => {
228
+ const o = Pe(t);
229
+ switch (o.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 (!o.lang && !o.meta && (n == null ? void 0 : n.inline) && !e.includes(`
232
+ `)) return `\`${e}\``;
233
+ const r = o.lang ?? (n == null ? void 0 : n.extension) ?? "", i = o.meta ? ` ${o.meta}` : "";
234
+ return g(`\`\`\`${r}${i}${g(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}` : g(T(g(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 = T(K(o.summary), "summary");
241
+ return g(T([c, e].join(`
242
+ `), "details", o.open ? "open" : void 0));
211
243
  }
212
- }, _e = [
244
+ }, Re = [
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) {
220
- const c = s.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
275
+ ], qe = Y(Re), L = (e) => Ce(e), b = (e, t) => L(e).filter(({ value: n }) => n.includes(t)).sort((n, o) => n.range[0] - o.range[0]), We = (e, ...t) => {
276
+ if (t.length === 0) return e;
277
+ const n = ([i, c]) => e.slice(i, c), o = L(e), s = new d(), r = new d();
278
+ for (const i of t) {
279
+ const c = o.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
- 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));
282
+ s.push(a.range[1], p.range[0]);
283
+ const [u, ...y] = n([a.range[1], p.range[0]]), v = y[y.length - 1];
284
+ r.push(a.range[0], a.range[1] + (u.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 s.collapse(), r.collapse(), D(
288
+ s.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) {
235
- const c = s.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
290
+ }, Be = (e, ...t) => {
291
+ if (t.length === 0) return e;
292
+ const n = ([i, c]) => e.slice(i, c), o = L(e), s = new d();
293
+ for (const i of t) {
294
+ const c = o.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);
238
- o.push(a.range[0], m === `
296
+ const a = c[l], p = c[l + 1], u = n([p.range[1], p.range[1] + 1]).at(-1);
297
+ s.push(a.range[0], u === `
239
298
  ` ? p.range[1] + 1 : p.range[1]);
240
299
  }
241
300
  }
242
- o.collapse();
301
+ s.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(s), D(
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);
250
- if (o.length < 2) return e;
306
+ }, Fe = (e, t, n, o) => {
307
+ if (!t) return e;
308
+ const s = b(e, t);
309
+ if (s.length < 2) return e;
251
310
  let r = "", i = 0;
252
- for (let l = 0; l < o.length - 1; l += 2) {
253
- 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];
311
+ for (let l = 0; l < s.length - 1; l += 2) {
312
+ const a = s[l], p = s[l + 1];
313
+ r += e.slice(i, a.range[1]), r += K(n ?? a.value, o), 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
+ b(r, t).reduce((l, { range: a }) => (l.push(...a), l), new d())
319
+ ), D(
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
+ }, Q = (e) => ({ space: e === " ", newline: e === `
323
+ ` }), z = (e) => [
324
+ ...b(e, "p↓:"),
325
+ ...b(e, "parkdown:")
326
+ ].sort((t, n) => t.range[0] - n.range[0]).reverse().reduce((t, { range: [n, o], loc: { start: { column: s } } }) => {
327
+ const r = (l, a) => t.slice(0, l) + t.slice(a), i = {
328
+ prev: Q(t[n - 1]),
329
+ next: Q(t[o]),
330
+ startLine: s === 0,
331
+ final: o === t.length
332
+ };
333
+ return i.startLine && (i.next.newline || i.final) ? r(n - (i.final ? 1 : 0), o + 1) : i.startLine ? r(n, o + (i.next.space ? 1 : 0)) : r(n - (i.prev.space ? 1 : 0), o);
334
+ }, e), Ue = (e, t) => {
335
+ if (!t) return z(e);
336
+ const n = qe(t);
337
+ switch (n.name) {
266
338
  case "extract":
267
- return Te(e, t.id, ...R(t));
339
+ e = We(e, n.id, ...G(n));
340
+ break;
268
341
  case "remove":
269
- return Ie(e, t.id, ...R(t));
342
+ e = Be(e, n.id, ...G(n));
343
+ break;
270
344
  case "replace":
271
- return De(e, t.id, t.with, t.space);
345
+ e = Fe(e, n.id, n.with, n.space);
346
+ break;
272
347
  }
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 = {
348
+ return z(e);
349
+ }, Ge = ["http", "./", "../"], He = ({ url: e }) => Ge.some((t) => e.startsWith(t)), Qe = (e) => I(e) && Ee(e) && He(e), ee = ({ url: e }, t) => `[](${t ? x(t, e) : e})`, m = {
274
350
  _open: "<!--",
275
351
  _close: "-->",
276
- _flag: "p",
352
+ _flag: "p",
277
353
  get begin() {
278
- return L(h._open, h._flag, "BEGIN", h._close);
354
+ return E(m._open, m._flag, "BEGIN", m._close);
279
355
  },
280
356
  get end() {
281
- return L(h._open, h._flag, "END", h._close);
357
+ return E(m._open, m._flag, "END", m._close);
358
+ },
359
+ lengthOf(e) {
360
+ const t = `lines: ${e.split(`
361
+ `).length}`, n = `chars: ${e.length}`;
362
+ return E(m._open, m._flag, "length", t, n, m._close);
282
363
  }
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);
288
- if (o) return o;
364
+ }, X = (e) => (t) => I(t) && t.value === m[e], ze = ({ position: e, url: t, siblingCount: n }, o) => ({ position: e, url: t, headingDepth: o, inline: n >= 1 }), Xe = ({ position: { start: e }, url: t, siblingCount: n }, { position: { end: o } }, s) => ({ position: { start: e, end: o }, url: t, headingDepth: s, inline: n >= 1 }), Je = (e, t, n) => (e.inline ? E : Se)(
365
+ ee(e, n),
366
+ m.begin,
367
+ m.lengthOf(t),
368
+ t,
369
+ m.end
370
+ ), Ve = (e) => {
371
+ const t = S(e, "heading").reduce((n, { position: o, depth: s }) => n.set(o.start.line, s), /* @__PURE__ */ new Map());
372
+ return (n) => {
373
+ for (let o = n.position.start.line; o >= 1; o--) {
374
+ const s = t.get(o);
375
+ if (s) return s;
289
376
  }
290
377
  return 0;
291
378
  };
292
- }, E = {
379
+ }, k = {
293
380
  openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
294
381
  closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
295
382
  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 = [
383
+ }, Ze = (e, t) => {
384
+ const n = [], o = [
298
385
  ...e.map((r) => ({ node: r, type: "open" })),
299
- ...n.map((r) => ({ node: r, type: "close" }))
300
- ].sort((r, i) => g(r.node, i.node)), o = [];
301
- for (const r of s)
302
- if (r.type === "open") o.push(r);
386
+ ...t.map((r) => ({ node: r, type: "close" }))
387
+ ].sort((r, i) => f(r.node, i.node)), s = [];
388
+ for (const r of o)
389
+ if (r.type === "open") s.push(r);
303
390
  else {
304
391
  const i = r.node;
305
- if (o.length === 0)
306
- throw E.closingCommentNotMatchedToOpening(i);
307
- const c = o.pop().node;
308
- if (o.length > 0) continue;
309
- t.push({ open: c, close: i });
392
+ if (s.length === 0)
393
+ throw k.closingCommentNotMatchedToOpening(i);
394
+ const c = s.pop().node;
395
+ if (s.length > 0) continue;
396
+ n.push({ open: c, close: i });
310
397
  }
311
- 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) => {
317
- for (; s.length > 0; ) {
318
- const i = s.pop();
398
+ if (s.length > 0)
399
+ throw k.openingCommentNotClosed(s[0].node);
400
+ return n;
401
+ }, Ke = (e, t, n) => {
402
+ const o = [...t].sort(f), s = [];
403
+ return [...n].sort((r, i) => f.reverse(r.open, i.open)).forEach((r) => {
404
+ for (; o.length > 0; ) {
405
+ const i = o.pop();
319
406
  if (i.position.start.offset < r.open.position.start.offset) {
320
- if (me(e, i, r.open).trim() !== "")
321
- throw E.openingCommentDoesNotFollowLink(r.open);
322
- return o.push([i, r]);
407
+ if (xe(e, i, r.open).trim() !== "")
408
+ throw k.openingCommentDoesNotFollowLink(r.open);
409
+ return s.push([i, r]);
323
410
  }
324
- o.push(i);
411
+ s.push(i);
325
412
  }
326
- throw E.openingCommentDoesNotFollowLink(r.open);
327
- }), 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;
337
- t ?? (t = I.md(e));
338
- const s = y(t, "heading"), o = e.split(`
413
+ throw k.openingCommentDoesNotFollowLink(r.open);
414
+ }), s.push(...o.reverse()), s.reverse();
415
+ }, te = (e, t) => {
416
+ t ?? (t = j.md(e));
417
+ const n = Ve(t), o = S(t, "link").filter(Qe), s = S(t, "html").sort(f), r = s.filter(X("begin")), i = s.filter(X("end")), c = Ze(r, i);
418
+ return Ke(e, o, c).map((a) => Array.isArray(a) ? Xe(a[0], a[1].close, n(a[0])) : ze(a, n(a)));
419
+ }, Ye = (e, { url: t }) => (n) => e(x(O(t), n)), ne = (...e) => {
420
+ const t = e.reduce((n, o) => n + o, 0);
421
+ return Math.min(Math.max(t, 1), 6);
422
+ }, et = (e, t, n) => {
423
+ if (t === 0) return e;
424
+ n ?? (n = j.md(e));
425
+ const o = S(n, "heading"), s = e.split(`
339
426
  `);
340
- 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;
342
- o[c.line - 1] = m, r.depth = a;
427
+ for (const r of o) {
428
+ const { depth: i, position: { start: c, end: l } } = r, a = ne(i, t), p = s[c.line - 1].slice(i, l.column), u = "#".repeat(a) + p;
429
+ s[c.line - 1] = u, r.depth = a;
343
430
  }
344
- return o.join(`
431
+ return s.join(`
345
432
  `);
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);
433
+ }, oe = (e) => te(e).reverse().sort(f.reverse).reduce((t, n) => Z(t, ee(n), n), e), se = (e, t, n, o, s) => {
434
+ try {
435
+ e = oe(e), e = et(e, t);
436
+ const r = j.md(e);
437
+ return te(e, r).sort(f).reverse().map((i) => {
438
+ var P, R;
439
+ const { url: c, headingDepth: l } = i, [a, ...p] = fe(c).split("?"), u = a.split(".").pop() ?? "", y = p.join("?"), v = O(c), A = x(v, a);
440
+ if (c.startsWith("./") || c.startsWith("../")) {
441
+ let h = n(A);
442
+ const w = new ge(y), N = (P = w.get("region")) == null ? void 0 : P.split(W);
443
+ h = (N == null ? void 0 : N.reduce(($, C) => Ue($, C), h)) ?? h;
444
+ const ce = w.has("skip"), le = w.get("heading") ?? 0, ae = w.has("inline");
445
+ let { inline: M } = i;
446
+ if (ae && (M = !0), !ce)
447
+ if (u === "md") {
448
+ const $ = Ye(n, i), C = s ? x(s, v) : v, pe = ne(l, Number(le));
449
+ h = se(
450
+ h,
451
+ /** p↓: ... */
452
+ pe,
453
+ $,
454
+ A,
455
+ C
456
+ /** p↓: ... */
457
+ );
458
+ } else /^(js|ts)x?|svelte$/i.test(u) && (h = H(
459
+ h,
460
+ "code",
461
+ /** p↓: ... */
462
+ { extension: u, inline: M }
463
+ /** p↓: ... */
464
+ ));
465
+ const _ = (R = w.get("wrap")) == null ? void 0 : R.split(W);
466
+ return h = (_ == null ? void 0 : _.reduce(($, C) => H($, C, { extension: u, inline: M }), h)) ?? h, { target: i, content: Je(i, h, s) };
467
+ } else throw c.startsWith("http") ? new Error("External web links are not implemented yet") : new Error(`Unsupported link type: ${c}`);
468
+ }).reduce((i, { target: c, content: l }) => Z(i, l, c), e);
469
+ } catch (r) {
470
+ throw new Error(`Error populating inclusions in file ${o}: ${r}`);
471
+ }
472
+ }, re = (e) => he(e, "utf-8"), ie = (e) => {
473
+ const t = V(e), n = O(t);
474
+ return { markdown: re(t), dir: n, path: t };
475
+ }, ut = (e, t = !0) => {
476
+ const { dir: n, path: o, markdown: s } = ie(e), i = se(s, 0, (c) => re(V(n, ke(c))), o);
477
+ return t && J(o, i), i;
478
+ }, mt = (e, t = !0) => {
479
+ const { path: n, markdown: o } = ie(e), s = oe(o);
480
+ return t && J(n, s), s;
391
481
  };
392
482
  export {
393
- tt as depopulateMarkdownInclusions,
394
- et as populateMarkdownInclusions,
395
- nt as remapImportSpecifiers
483
+ mt as depopulateMarkdownInclusions,
484
+ ut as populateMarkdownInclusions
396
485
  };