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