@p-buddy/parkdown 0.0.5 → 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/README.md +24 -10
- package/dist/cli.js +1 -1
- package/dist/index.js +231 -218
- package/dist/index.umd.cjs +12 -11
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ populateMarkdownInclusions(file, writeFile);
|
|
|
44
44
|
|
|
45
45
|
[](./.assets/authoring.md)
|
|
46
46
|
<!-- p↓ BEGIN -->
|
|
47
|
-
<!-- p↓ length lines:
|
|
47
|
+
<!-- p↓ length lines: 572 chars: 17752 -->
|
|
48
48
|
## Authoring
|
|
49
49
|
|
|
50
50
|
You author inclusions in your markdown files using a link with no text i.e. `[](<url>)`, where `<url>` points to some local or remote text resource (e.g.`./other.md`, `https://example.com/remote.md`).
|
|
@@ -254,7 +254,7 @@ Before...
|
|
|
254
254
|
|
|
255
255
|
[](.assets/query.md?heading=-1)
|
|
256
256
|
<!-- p↓ BEGIN -->
|
|
257
|
-
<!-- p↓ length lines:
|
|
257
|
+
<!-- p↓ length lines: 361 chars: 13249 -->
|
|
258
258
|
### Query parameters
|
|
259
259
|
|
|
260
260
|
You can pass query parameters to your inclusion links to control how their content is processed and included within your markdown.
|
|
@@ -294,6 +294,8 @@ Below is the currently supported API for the `region` query parameter, where eac
|
|
|
294
294
|
- `[](<url>?region=remove(some-specifier))`
|
|
295
295
|
- `[](<url>?region=replace(some-specifier,replacement-content))`
|
|
296
296
|
|
|
297
|
+
If no value(s) are included (e.g. `[](<url>?region)`), then simply all comments that contain `parkdown:` or `p↓:` will be stripped (as is done as a post-processing step for all other `region` functionality).
|
|
298
|
+
|
|
297
299
|
[](.assets/api-note.md?wrap=quote)
|
|
298
300
|
<!-- p↓ BEGIN -->
|
|
299
301
|
<!-- p↓ length lines: 9 chars: 352 -->
|
|
@@ -355,7 +357,7 @@ Skip the default processing behavior for the given type of file.
|
|
|
355
357
|
|
|
356
358
|
[](src/include.ts?wrap=dropdown(See-default-processing-behavior.)®ion=extract(Default-Behavior),replace(...))
|
|
357
359
|
<!-- p↓ BEGIN -->
|
|
358
|
-
<!-- p↓ length lines:
|
|
360
|
+
<!-- p↓ length lines: 21 chars: 525 -->
|
|
359
361
|
|
|
360
362
|
<details>
|
|
361
363
|
<summary>
|
|
@@ -364,11 +366,15 @@ See default processing behavior.
|
|
|
364
366
|
|
|
365
367
|
```ts
|
|
366
368
|
if (extension === "md") {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
+
|
|
370
|
+
const getContent = extendGetRelativePathContent(getRelativePathContent, target);
|
|
371
|
+
const relative = basePath ? join(basePath, dir) : dir;
|
|
372
|
+
const depth = clampHeadingSum(headingDepth, Number(headingModfiier));
|
|
373
|
+
|
|
374
|
+
content = recursivelyPopulateInclusions(content, depth, getContent, path, relative);
|
|
369
375
|
}
|
|
370
376
|
else if (/^(js|ts)x?|svelte$/i.test(extension))
|
|
371
|
-
content = wrap(content, "code",
|
|
377
|
+
content = wrap(content, "code", { extension, inline });
|
|
372
378
|
```
|
|
373
379
|
|
|
374
380
|
</details>
|
|
@@ -422,6 +428,14 @@ If we instead wanted the included heading to remain a `h1` / `#` heading, we'd m
|
|
|
422
428
|
[](./to-be-included.md?heading=-1)
|
|
423
429
|
```
|
|
424
430
|
|
|
431
|
+
which would result in the following:
|
|
432
|
+
|
|
433
|
+
```md
|
|
434
|
+
# Heading
|
|
435
|
+
|
|
436
|
+
# Included Heading
|
|
437
|
+
```
|
|
438
|
+
|
|
425
439
|
</details>
|
|
426
440
|
|
|
427
441
|
#### `inline` (Advanced)
|
|
@@ -496,7 +510,7 @@ const definitions = [
|
|
|
496
510
|
|
|
497
511
|
[](.assets/api.md?heading=-1)
|
|
498
512
|
<!-- p↓ BEGIN -->
|
|
499
|
-
<!-- p↓ length lines: 104 chars:
|
|
513
|
+
<!-- p↓ length lines: 104 chars: 4973 -->
|
|
500
514
|
#### Query Parameters with Function-like APIs
|
|
501
515
|
|
|
502
516
|
Some query parameters have more complex APIs, which are defined by a collection of typescript function singatures (limited to only `string`, `boolean`, and `number` arguments), like:
|
|
@@ -570,7 +584,7 @@ const urlCharacters = {
|
|
|
570
584
|
|
|
571
585
|
[](src/utils.ts?region=extract(sanitize))
|
|
572
586
|
<!-- p↓ BEGIN -->
|
|
573
|
-
<!-- p↓ length lines: 29 chars:
|
|
587
|
+
<!-- p↓ length lines: 29 chars: 771 -->
|
|
574
588
|
|
|
575
589
|
```ts
|
|
576
590
|
type Replacement = [from: RegExp | string, to: string];
|
|
@@ -591,8 +605,8 @@ const replacements: Record<string, Replacement[]> = {
|
|
|
591
605
|
const applyReplacement = (accumulator: string, [from, to]: Replacement) =>
|
|
592
606
|
accumulator.replaceAll(from, to);
|
|
593
607
|
|
|
594
|
-
export const sanitize = (
|
|
595
|
-
const sanitized = replacements.static.reduce(applyReplacement,
|
|
608
|
+
export const sanitize = (content: string, space: string = DEFAULT_SPACE) => {
|
|
609
|
+
const sanitized = replacements.static.reduce(applyReplacement, content)
|
|
596
610
|
return replacements.url
|
|
597
611
|
.map(([key, to]) => ([space + key + space, to] satisfies Replacement))
|
|
598
612
|
.reduce(applyReplacement, sanitized)
|
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 r, depopulateMarkdownInclusions as l } from "@p-buddy/parkdown";
|
|
4
|
-
const p = "0.0.
|
|
4
|
+
const p = "0.0.6", f = new t().version(p).option("--nw, --no-write", "Do NOT write result to file (defaults to false)", !1).option("--ni, --no-inclusions", "Do NOT process file inclusions (defaults to false)", !1).option("-d, --depopulate", "Remove populated inclusions from the file", !1).option("-f, --file <flag>", "The file(s) to process", (e, o) => (o.push(e), o), new Array()).option("-r, --remap-imports", "Remap import specifiers in code blocks from one destination to another").parse(), { inclusions: a, depopulate: c, file: n, write: i } = f.opts();
|
|
5
5
|
n.length === 0 && n.push("README.md");
|
|
6
6
|
const u = [
|
|
7
7
|
[r, !a],
|
package/dist/index.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var q = (e, t, n) =>
|
|
4
|
-
import { writeFileSync as
|
|
5
|
-
import { basename as
|
|
6
|
-
import { URLSearchParams as
|
|
7
|
-
import { unified as
|
|
8
|
-
import
|
|
9
|
-
import { visit as
|
|
10
|
-
import
|
|
11
|
-
import { dedent as
|
|
12
|
-
import
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
md: (e) =>
|
|
17
|
-
},
|
|
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
18
|
const n = [];
|
|
19
|
-
return
|
|
20
|
-
if (
|
|
21
|
-
if (t &&
|
|
22
|
-
if (
|
|
23
|
-
const i =
|
|
24
|
-
n.push({ ...
|
|
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 });
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}), n;
|
|
28
|
-
},
|
|
28
|
+
}, Ee = (e) => e.children.length === 0, Z = (e, t, ...n) => {
|
|
29
29
|
if (n.length === 0) throw new Error("No nodes to replace content from");
|
|
30
|
-
n.sort(
|
|
31
|
-
const
|
|
32
|
-
return e.slice(0,
|
|
33
|
-
},
|
|
34
|
-
const
|
|
35
|
-
return e.slice(
|
|
36
|
-
}, E = (...e) => e.join(" "),
|
|
37
|
-
`),
|
|
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
38
|
const t = e.split("?");
|
|
39
39
|
return t.length > 1 ? [t.slice(0, -1).join("?"), t.at(-1)] : [e, ""];
|
|
40
|
-
},
|
|
40
|
+
}, ke = (e) => be(e)[0];
|
|
41
41
|
class d {
|
|
42
42
|
constructor() {
|
|
43
43
|
q(this, "intervals", []);
|
|
@@ -53,32 +53,32 @@ class d {
|
|
|
53
53
|
if (!t.length) return this.intervals = [];
|
|
54
54
|
t.sort((r, i) => r[0] - i[0]);
|
|
55
55
|
const n = [];
|
|
56
|
-
let [
|
|
56
|
+
let [o, s] = t[0];
|
|
57
57
|
for (let r = 1; r < t.length; r++) {
|
|
58
58
|
const [i, c] = t[r];
|
|
59
|
-
i <=
|
|
59
|
+
i <= s ? s = Math.max(s, c) : (n.push([o, s]), o = i, s = c);
|
|
60
60
|
}
|
|
61
|
-
return n.push([
|
|
61
|
+
return n.push([o, s]), this.intervals = n;
|
|
62
62
|
}
|
|
63
63
|
subtract(t) {
|
|
64
|
-
const { intervals: n } = this, { intervals:
|
|
65
|
-
if (!n.length || !
|
|
66
|
-
let
|
|
67
|
-
for (const [r, i] of
|
|
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) {
|
|
68
68
|
const c = [];
|
|
69
|
-
for (const [l, a] of
|
|
69
|
+
for (const [l, a] of s) {
|
|
70
70
|
if (i <= l || r >= a) {
|
|
71
71
|
c.push([l, a]);
|
|
72
72
|
continue;
|
|
73
73
|
}
|
|
74
74
|
r > l && c.push([l, r]), i < a && c.push([i, a]);
|
|
75
75
|
}
|
|
76
|
-
|
|
76
|
+
s = c;
|
|
77
77
|
}
|
|
78
|
-
return this.intervals =
|
|
78
|
+
return this.intervals = s;
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
const
|
|
81
|
+
const W = /,\s*(?![^()]*\))/, B = {
|
|
82
82
|
reserved: {
|
|
83
83
|
semi: ";",
|
|
84
84
|
slash: "/",
|
|
@@ -104,7 +104,7 @@ const L = /,\s*(?![^()]*\))/, W = {
|
|
|
104
104
|
unsquare: "]",
|
|
105
105
|
tick: "`"
|
|
106
106
|
}
|
|
107
|
-
}, Ne = "-",
|
|
107
|
+
}, Ne = "-", F = {
|
|
108
108
|
static: [
|
|
109
109
|
["'''", '"'],
|
|
110
110
|
["''", "'"],
|
|
@@ -112,87 +112,87 @@ const L = /,\s*(?![^()]*\))/, W = {
|
|
|
112
112
|
[/p↓:\s+/g, ""]
|
|
113
113
|
],
|
|
114
114
|
url: [
|
|
115
|
-
...Object.entries(
|
|
116
|
-
...Object.entries(
|
|
115
|
+
...Object.entries(B.unsafe),
|
|
116
|
+
...Object.entries(B.reserved)
|
|
117
117
|
]
|
|
118
|
-
},
|
|
119
|
-
const n =
|
|
120
|
-
return
|
|
121
|
-
},
|
|
122
|
-
const t = e.match(
|
|
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
123
|
if (!t) throw new Error(`Invalid invocation: ${e}`);
|
|
124
|
-
const [, n,
|
|
125
|
-
if (!
|
|
126
|
-
const
|
|
127
|
-
return { name: n, parameters:
|
|
128
|
-
},
|
|
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
129
|
for (let n = t + 1; n < e.length; n++)
|
|
130
130
|
if (e[n] !== void 0) return !1;
|
|
131
131
|
return !0;
|
|
132
|
-
},
|
|
132
|
+
}, Ie = (e) => {
|
|
133
133
|
const t = [];
|
|
134
|
-
let n = "",
|
|
135
|
-
for (let
|
|
136
|
-
const r = e[
|
|
137
|
-
if (i && e.at(
|
|
138
|
-
const c = e.at(
|
|
139
|
-
|
|
140
|
-
} else i ? (
|
|
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;
|
|
141
141
|
}
|
|
142
|
-
return t.push(n.trim()), t.map((
|
|
143
|
-
},
|
|
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
144
|
const t = e.length >= 2 && e[0] === "'" && e.at(-1) === "'", n = t && e.length >= 4 && e[1] === "'" && e.at(-2) === "'";
|
|
145
145
|
return !t || n ? e : e.slice(1, -1);
|
|
146
|
-
},
|
|
146
|
+
}, Le = (e) => {
|
|
147
147
|
const t = /^(\w+)(?:\(([^)]*)\))?/, n = e.match(t);
|
|
148
148
|
if (!n) return { name: e };
|
|
149
|
-
const [,
|
|
150
|
-
if (!
|
|
149
|
+
const [, o, s] = n;
|
|
150
|
+
if (!s) return { name: o };
|
|
151
151
|
const r = /(\w+)(\?)?:\s*([^,]+)/g, i = [];
|
|
152
152
|
let c;
|
|
153
|
-
for (; (c = r.exec(
|
|
154
|
-
const [, l, a, p] = c,
|
|
155
|
-
if (!
|
|
156
|
-
i.push({ name: l, optional: a === "?", type:
|
|
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 });
|
|
157
157
|
}
|
|
158
|
-
return { name:
|
|
159
|
-
},
|
|
160
|
-
const t = e.map(
|
|
161
|
-
(n, { name:
|
|
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),
|
|
162
162
|
/* @__PURE__ */ new Map()
|
|
163
163
|
);
|
|
164
164
|
return (n) => {
|
|
165
|
-
const { name:
|
|
166
|
-
if (!t.has(
|
|
167
|
-
throw new Error(`Unknown method: ${
|
|
168
|
-
const r = t.get(
|
|
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);
|
|
169
169
|
if (r === void 0)
|
|
170
|
-
return { name:
|
|
171
|
-
if (
|
|
170
|
+
return { name: o };
|
|
171
|
+
if (s.length > r.length) {
|
|
172
172
|
const i = r.filter(({ optional: l }) => !l).length, c = i === r.length ? i.toString() : `${i} - ${r.length}`;
|
|
173
|
-
throw new Error(`Too many parameters: ${
|
|
173
|
+
throw new Error(`Too many parameters: ${s.length} for method '${o}' (expected: ${c})`);
|
|
174
174
|
}
|
|
175
175
|
return r.reduce((i, { name: c, optional: l, type: a }, p) => {
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
176
|
+
const u = s[p];
|
|
177
|
+
if (u === void 0) {
|
|
178
178
|
if (l) return i;
|
|
179
|
-
throw new Error(`Missing required parameter: ${c} for method '${
|
|
179
|
+
throw new Error(`Missing required parameter: ${c} for method '${o}'`);
|
|
180
180
|
}
|
|
181
181
|
switch (a) {
|
|
182
182
|
case "string":
|
|
183
|
-
i[c] =
|
|
183
|
+
i[c] = u;
|
|
184
184
|
break;
|
|
185
185
|
case "number":
|
|
186
186
|
case "boolean":
|
|
187
|
-
i[c] = JSON.parse(
|
|
187
|
+
i[c] = JSON.parse(u);
|
|
188
188
|
break;
|
|
189
189
|
}
|
|
190
190
|
if (typeof i[c] !== a)
|
|
191
|
-
throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${
|
|
191
|
+
throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${o}'`);
|
|
192
192
|
return i;
|
|
193
|
-
}, { name:
|
|
193
|
+
}, { name: o });
|
|
194
194
|
};
|
|
195
|
-
},
|
|
195
|
+
}, G = (e) => Object.entries(e).filter(([t]) => t !== "name" && !isNaN(Number(t))).map(([t, n]) => n), Ae = [
|
|
196
196
|
/**
|
|
197
197
|
* Wraps the content in a markdown-formatted code block.
|
|
198
198
|
* @param lang The language of the code block (defaults to the file extension).
|
|
@@ -222,26 +222,26 @@ const L = /,\s*(?![^()]*\))/, W = {
|
|
|
222
222
|
* @example [](<url>?wrap=dropdown(hello_world,,_))
|
|
223
223
|
*/
|
|
224
224
|
"dropdown(summary: string, open?: boolean, space?: string)"
|
|
225
|
-
],
|
|
225
|
+
], Pe = Y(Ae), g = (e, t = 1) => `
|
|
226
226
|
`.repeat(t) + e + `
|
|
227
|
-
`.repeat(t),
|
|
228
|
-
const
|
|
229
|
-
switch (
|
|
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) {
|
|
230
230
|
case "code":
|
|
231
|
-
if (!
|
|
231
|
+
if (!o.lang && !o.meta && (n == null ? void 0 : n.inline) && !e.includes(`
|
|
232
232
|
`)) return `\`${e}\``;
|
|
233
|
-
const r =
|
|
234
|
-
return
|
|
233
|
+
const r = o.lang ?? (n == null ? void 0 : n.extension) ?? "", i = o.meta ? ` ${o.meta}` : "";
|
|
234
|
+
return g(`\`\`\`${r}${i}${g(e)}\`\`\``);
|
|
235
235
|
case "quote":
|
|
236
236
|
return n != null && n.inline && !e.includes(`
|
|
237
237
|
|
|
238
|
-
`) ? `> ${e}` :
|
|
238
|
+
`) ? `> ${e}` : g(T(g(e), "blockquote"));
|
|
239
239
|
case "dropdown":
|
|
240
|
-
const c =
|
|
241
|
-
return
|
|
242
|
-
`), "details",
|
|
240
|
+
const c = T(K(o.summary), "summary");
|
|
241
|
+
return g(T([c, e].join(`
|
|
242
|
+
`), "details", o.open ? "open" : void 0));
|
|
243
243
|
}
|
|
244
|
-
},
|
|
244
|
+
}, Re = [
|
|
245
245
|
/**
|
|
246
246
|
* Extract regions from the retrieved content between comments that INCLUDE the specified ids.
|
|
247
247
|
* @param id The id of the comment to extract.
|
|
@@ -272,201 +272,214 @@ const L = /,\s*(?![^()]*\))/, W = {
|
|
|
272
272
|
* @example [](<url>?region=replace(specifier,new_content,_)
|
|
273
273
|
*/
|
|
274
274
|
"replace(id: string, with?: string, space?: string)"
|
|
275
|
-
],
|
|
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
276
|
if (t.length === 0) return e;
|
|
277
|
-
const n = ([i, c]) => e.slice(i, c),
|
|
277
|
+
const n = ([i, c]) => e.slice(i, c), o = L(e), s = new d(), r = new d();
|
|
278
278
|
for (const i of t) {
|
|
279
|
-
const c =
|
|
279
|
+
const c = o.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
|
|
280
280
|
for (let l = 0; l < c.length - 1; l += 2) {
|
|
281
281
|
const a = c[l], p = c[l + 1];
|
|
282
|
-
|
|
283
|
-
const [
|
|
284
|
-
r.push(a.range[0], a.range[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));
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
|
-
return
|
|
288
|
-
|
|
287
|
+
return s.collapse(), r.collapse(), D(
|
|
288
|
+
s.subtract(r).map(n).filter(Boolean).join("")
|
|
289
289
|
).trim();
|
|
290
|
-
},
|
|
290
|
+
}, Be = (e, ...t) => {
|
|
291
291
|
if (t.length === 0) return e;
|
|
292
|
-
const n = ([i, c]) => e.slice(i, c),
|
|
292
|
+
const n = ([i, c]) => e.slice(i, c), o = L(e), s = new d();
|
|
293
293
|
for (const i of t) {
|
|
294
|
-
const c =
|
|
294
|
+
const c = o.filter(({ value: l }) => l.includes(i)).sort((l, a) => l.range[0] - a.range[0]);
|
|
295
295
|
for (let l = 0; l < c.length - 1; l += 2) {
|
|
296
|
-
const a = c[l], p = c[l + 1],
|
|
297
|
-
|
|
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 === `
|
|
298
298
|
` ? p.range[1] + 1 : p.range[1]);
|
|
299
299
|
}
|
|
300
300
|
}
|
|
301
|
-
|
|
301
|
+
s.collapse();
|
|
302
302
|
const r = new d();
|
|
303
|
-
return r.push(0, e.length), r.subtract(
|
|
303
|
+
return r.push(0, e.length), r.subtract(s), D(
|
|
304
304
|
r.collapse().map(n).filter(Boolean).join("")
|
|
305
305
|
).trim();
|
|
306
|
-
},
|
|
306
|
+
}, Fe = (e, t, n, o) => {
|
|
307
307
|
if (!t) return e;
|
|
308
|
-
const
|
|
309
|
-
if (
|
|
308
|
+
const s = b(e, t);
|
|
309
|
+
if (s.length < 2) return e;
|
|
310
310
|
let r = "", i = 0;
|
|
311
|
-
for (let l = 0; l <
|
|
312
|
-
const a =
|
|
313
|
-
r += e.slice(i, a.range[1]), r +=
|
|
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];
|
|
314
314
|
}
|
|
315
315
|
r += e.slice(i);
|
|
316
316
|
const c = new d();
|
|
317
317
|
return c.push(0, r.length), c.subtract(
|
|
318
|
-
|
|
319
|
-
),
|
|
318
|
+
b(r, t).reduce((l, { range: a }) => (l.push(...a), l), new d())
|
|
319
|
+
), D(
|
|
320
320
|
c.collapse().map(([l, a]) => r.slice(l, a)).filter(Boolean).join("")
|
|
321
321
|
).trim();
|
|
322
|
-
},
|
|
323
|
-
|
|
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);
|
|
324
337
|
switch (n.name) {
|
|
325
338
|
case "extract":
|
|
326
|
-
e =
|
|
339
|
+
e = We(e, n.id, ...G(n));
|
|
327
340
|
break;
|
|
328
341
|
case "remove":
|
|
329
|
-
e =
|
|
342
|
+
e = Be(e, n.id, ...G(n));
|
|
330
343
|
break;
|
|
331
344
|
case "replace":
|
|
332
|
-
e =
|
|
345
|
+
e = Fe(e, n.id, n.with, n.space);
|
|
333
346
|
break;
|
|
334
347
|
}
|
|
335
|
-
return e;
|
|
336
|
-
},
|
|
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 = {
|
|
337
350
|
_open: "<!--",
|
|
338
351
|
_close: "-->",
|
|
339
352
|
_flag: "p↓",
|
|
340
353
|
get begin() {
|
|
341
|
-
return E(
|
|
354
|
+
return E(m._open, m._flag, "BEGIN", m._close);
|
|
342
355
|
},
|
|
343
356
|
get end() {
|
|
344
|
-
return E(
|
|
357
|
+
return E(m._open, m._flag, "END", m._close);
|
|
345
358
|
},
|
|
346
359
|
lengthOf(e) {
|
|
347
360
|
const t = `lines: ${e.split(`
|
|
348
361
|
`).length}`, n = `chars: ${e.length}`;
|
|
349
|
-
return E(
|
|
362
|
+
return E(m._open, m._flag, "length", t, n, m._close);
|
|
350
363
|
}
|
|
351
|
-
},
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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),
|
|
355
368
|
t,
|
|
356
|
-
|
|
357
|
-
),
|
|
358
|
-
const 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());
|
|
359
372
|
return (n) => {
|
|
360
|
-
for (let
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
373
|
+
for (let o = n.position.start.line; o >= 1; o--) {
|
|
374
|
+
const s = t.get(o);
|
|
375
|
+
if (s) return s;
|
|
363
376
|
}
|
|
364
377
|
return 0;
|
|
365
378
|
};
|
|
366
|
-
},
|
|
379
|
+
}, k = {
|
|
367
380
|
openingCommentDoesNotFollowLink: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),
|
|
368
381
|
closingCommentNotMatchedToOpening: ({ position: { start: e } }) => new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),
|
|
369
382
|
openingCommentNotClosed: ({ position: { start: e } }) => new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)
|
|
370
|
-
},
|
|
371
|
-
const n = [],
|
|
383
|
+
}, Ze = (e, t) => {
|
|
384
|
+
const n = [], o = [
|
|
372
385
|
...e.map((r) => ({ node: r, type: "open" })),
|
|
373
386
|
...t.map((r) => ({ node: r, type: "close" }))
|
|
374
|
-
].sort((r, i) =>
|
|
375
|
-
for (const r of
|
|
376
|
-
if (r.type === "open")
|
|
387
|
+
].sort((r, i) => f(r.node, i.node)), s = [];
|
|
388
|
+
for (const r of o)
|
|
389
|
+
if (r.type === "open") s.push(r);
|
|
377
390
|
else {
|
|
378
391
|
const i = r.node;
|
|
379
|
-
if (
|
|
380
|
-
throw
|
|
381
|
-
const c =
|
|
382
|
-
if (
|
|
392
|
+
if (s.length === 0)
|
|
393
|
+
throw k.closingCommentNotMatchedToOpening(i);
|
|
394
|
+
const c = s.pop().node;
|
|
395
|
+
if (s.length > 0) continue;
|
|
383
396
|
n.push({ open: c, close: i });
|
|
384
397
|
}
|
|
385
|
-
if (
|
|
386
|
-
throw
|
|
398
|
+
if (s.length > 0)
|
|
399
|
+
throw k.openingCommentNotClosed(s[0].node);
|
|
387
400
|
return n;
|
|
388
|
-
},
|
|
389
|
-
const
|
|
390
|
-
return [...n].sort((r, i) =>
|
|
391
|
-
for (;
|
|
392
|
-
const i =
|
|
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();
|
|
393
406
|
if (i.position.start.offset < r.open.position.start.offset) {
|
|
394
|
-
if (
|
|
395
|
-
throw
|
|
396
|
-
return
|
|
407
|
+
if (xe(e, i, r.open).trim() !== "")
|
|
408
|
+
throw k.openingCommentDoesNotFollowLink(r.open);
|
|
409
|
+
return s.push([i, r]);
|
|
397
410
|
}
|
|
398
|
-
|
|
411
|
+
s.push(i);
|
|
399
412
|
}
|
|
400
|
-
throw
|
|
401
|
-
}),
|
|
402
|
-
},
|
|
403
|
-
t ?? (t =
|
|
404
|
-
const n =
|
|
405
|
-
return
|
|
406
|
-
},
|
|
407
|
-
const t = e.reduce((n,
|
|
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);
|
|
408
421
|
return Math.min(Math.max(t, 1), 6);
|
|
409
|
-
},
|
|
422
|
+
}, et = (e, t, n) => {
|
|
410
423
|
if (t === 0) return e;
|
|
411
|
-
n ?? (n =
|
|
412
|
-
const
|
|
424
|
+
n ?? (n = j.md(e));
|
|
425
|
+
const o = S(n, "heading"), s = e.split(`
|
|
413
426
|
`);
|
|
414
|
-
for (const r of
|
|
415
|
-
const { depth: i, position: { start: c, end: l } } = r, a =
|
|
416
|
-
|
|
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;
|
|
417
430
|
}
|
|
418
|
-
return
|
|
431
|
+
return s.join(`
|
|
419
432
|
`);
|
|
420
|
-
},
|
|
433
|
+
}, oe = (e) => te(e).reverse().sort(f.reverse).reduce((t, n) => Z(t, ee(n), n), e), se = (e, t, n, o, s) => {
|
|
421
434
|
try {
|
|
422
|
-
e =
|
|
423
|
-
const r =
|
|
424
|
-
return
|
|
425
|
-
var
|
|
426
|
-
const { url: c, headingDepth: l } = i, [a, ...p] =
|
|
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);
|
|
427
440
|
if (c.startsWith("./") || c.startsWith("../")) {
|
|
428
|
-
let h = n(
|
|
429
|
-
const w = new
|
|
430
|
-
h = (
|
|
431
|
-
const
|
|
432
|
-
let { inline:
|
|
433
|
-
if (
|
|
434
|
-
if (
|
|
435
|
-
const $ =
|
|
436
|
-
h =
|
|
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(
|
|
437
450
|
h,
|
|
438
451
|
/** p↓: ... */
|
|
439
|
-
|
|
452
|
+
pe,
|
|
440
453
|
$,
|
|
441
|
-
|
|
454
|
+
A,
|
|
442
455
|
C
|
|
443
456
|
/** p↓: ... */
|
|
444
457
|
);
|
|
445
|
-
} else /^(js|ts)x?|svelte$/i.test(
|
|
458
|
+
} else /^(js|ts)x?|svelte$/i.test(u) && (h = H(
|
|
446
459
|
h,
|
|
447
460
|
"code",
|
|
448
461
|
/** p↓: ... */
|
|
449
|
-
{ extension:
|
|
462
|
+
{ extension: u, inline: M }
|
|
450
463
|
/** p↓: ... */
|
|
451
464
|
));
|
|
452
|
-
const
|
|
453
|
-
return h = (
|
|
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) };
|
|
454
467
|
} 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 }) =>
|
|
468
|
+
}).reduce((i, { target: c, content: l }) => Z(i, l, c), e);
|
|
456
469
|
} catch (r) {
|
|
457
|
-
throw new Error(`Error populating inclusions in file ${
|
|
470
|
+
throw new Error(`Error populating inclusions in file ${o}: ${r}`);
|
|
458
471
|
}
|
|
459
|
-
},
|
|
460
|
-
const t =
|
|
461
|
-
return { markdown:
|
|
462
|
-
},
|
|
463
|
-
const { dir: n, path:
|
|
464
|
-
return t &&
|
|
465
|
-
},
|
|
466
|
-
const { path: n, markdown:
|
|
467
|
-
return t &&
|
|
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;
|
|
468
481
|
};
|
|
469
482
|
export {
|
|
470
|
-
|
|
471
|
-
|
|
483
|
+
mt as depopulateMarkdownInclusions,
|
|
484
|
+
ut as populateMarkdownInclusions
|
|
472
485
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
(function(p,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("stable-hash"),require("ts-dedent"),require("extract-comments")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","stable-hash","ts-dedent","extract-comments"],h):(p=typeof globalThis<"u"?globalThis:p||self,h(p.index={},p.node_fs,p.node_path,p.node_url,p.unified,p.remarkParse,p.unistUtilVisit,p.hash,p.tsDedent,p._extractComments))})(this,function(p,h,
|
|
2
|
-
`),
|
|
3
|
-
`.repeat(
|
|
4
|
-
`.repeat(
|
|
5
|
-
`))return`\`${e}\``;const r=
|
|
1
|
+
(function(p,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("node:fs"),require("node:path"),require("node:url"),require("unified"),require("remark-parse"),require("unist-util-visit"),require("stable-hash"),require("ts-dedent"),require("extract-comments")):typeof define=="function"&&define.amd?define(["exports","node:fs","node:path","node:url","unified","remark-parse","unist-util-visit","stable-hash","ts-dedent","extract-comments"],h):(p=typeof globalThis<"u"?globalThis:p||self,h(p.index={},p.node_fs,p.node_path,p.node_url,p.unified,p.remarkParse,p.unistUtilVisit,p.hash,p.tsDedent,p._extractComments))})(this,function(p,h,f,ie,ce,le,ae,pe,b,ue){"use strict";var Je=Object.defineProperty;var Ze=(p,h,f)=>h in p?Je(p,h,{enumerable:!0,configurable:!0,writable:!0,value:f}):p[h]=f;var re=(p,h,f)=>Ze(p,typeof h!="symbol"?h+"":h,f);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,me=ce.unified().use(le),q={md:e=>me.parse(e)},x=(e,n)=>{const t=[];return ae.visit(e,(s,o,r)=>{if(s.type!=="root"){if(n&&s.type!==n)return;if(j(s)){const i=pe(r),c=((r==null?void 0:r.children.length)??0)-1;t.push({...s,parentID:i,siblingIndex:o,siblingCount:c})}}}),t},he=e=>e.children.length===0,A=(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)},fe=(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)},S=(...e)=>e.join(" "),de=(...e)=>e.join(`
|
|
2
|
+
`),ge=e=>{const n=e.split("?");return n.length>1?[n.slice(0,-1).join("?"),n.at(-1)]:[e,""]},ve=e=>ge(e)[0];class w{constructor(){re(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 R=/,\s*(?![^()]*\))/,_={reserved:{semi:";",slash:"/",question:"?",colon:":",at:"@",equal:"=",and:"&"},unsafe:{quote:'"',angle:"<",unangle:">",hash:"#",percent:"%",curly:"{",uncurly:"}",pipe:"|",back:"\\",carrot:"^",tilde:"~",square:"[",unsquare:"]",tick:"`"}},we="-",W={static:[["'''",'"'],["''","'"],[/parkdown:\s+/g,""],[/p↓:\s+/g,""]],url:[...Object.entries(_.unsafe),...Object.entries(_.reserved)]},B=(e,[n,t])=>e.replaceAll(n,t),F=(e,n=we)=>{const t=W.static.reduce(B,e);return W.url.map(([s,o])=>[n+s+n,o]).reduce(B,t).replaceAll(n," ")},$e=["string","number","boolean"],Ce=e=>$e.includes(e),ye=/^([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?$/,Ee=e=>{const n=e.match(ye);if(!n)throw new Error(`Invalid invocation: ${e}`);const[,t,s=""]=n;if(!s.trim())return{name:t,parameters:[]};const o=xe(s);return{name:t,parameters:o}},ke=(e,n)=>{for(let t=n+1;t<e.length;t++)if(e[t]!==void 0)return!1;return!0},xe=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?Se(o):void 0).filter((o,r,i)=>o!==void 0||!ke(i,r))},Se=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)},Me=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,u]=c,m=u.trim();if(!Ce(m))throw new Error(`Unsupported type: ${m}`);i.push({name:l,optional:a==="?",type:m})}return{name:s,parameters:i}},U=e=>{const n=e.map(Me).reduce((t,{name:s,parameters:o})=>t.set(s,o),new Map);return t=>{const{name:s,parameters:o}=Ee(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},u)=>{const m=o[u];if(m===void 0){if(l)return i;throw new Error(`Missing required parameter: ${c} for method '${s}'`)}switch(a){case"string":i[c]=m;break;case"number":case"boolean":i[c]=JSON.parse(m);break}if(typeof i[c]!==a)throw new Error(`Invalid type: ${c} must be ${a}, got ${typeof i[c]} for method '${s}'`);return i},{name:s})}},G=e=>Object.entries(e).filter(([n])=>n!=="name"&&!isNaN(Number(n))).map(([n,t])=>t),Ne=U(["code(lang?: string, meta?: string)","quote()","dropdown(summary: string, open?: boolean, space?: string)"]),$=(e,n=1)=>`
|
|
3
|
+
`.repeat(n)+e+`
|
|
4
|
+
`.repeat(n),O=(e,n,t)=>`<${n}${t?` ${t}`:""}>${$(e)}</${n}>`,H=(e,n,t)=>{const s=Ne(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 $(`\`\`\`${r}${i}${$(e)}\`\`\``);case"quote":return t!=null&&t.inline&&!e.includes(`
|
|
6
6
|
|
|
7
|
-
`)?`> ${e}`:$(
|
|
8
|
-
`),"details",
|
|
9
|
-
`?u.range[1]+1:u.range[1])}}
|
|
10
|
-
`
|
|
11
|
-
`);for(const
|
|
12
|
-
`)
|
|
7
|
+
`)?`> ${e}`:$(O($(e),"blockquote"));case"dropdown":const c=O(F(s.summary),"summary");return $(O([c,e].join(`
|
|
8
|
+
`),"details",s.open?"open":void 0))}},Te=U(["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)"]),I=e=>ue(e),M=(e,n)=>I(e).filter(({value:t})=>t.includes(n)).sort((t,s)=>t.range[0]-s.range[0]),be=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),s=I(e),o=new w,r=new w;for(const i of n){const c=s.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],u=c[l+1];o.push(a.range[1],u.range[0]);const[m,...T]=t([a.range[1],u.range[0]]),C=T[T.length-1];r.push(a.range[0],a.range[1]+(m.trim()?0:1)),r.push(u.range[0],u.range[1]+(C.trim()?0:1))}}return o.collapse(),r.collapse(),b.dedent(o.subtract(r).map(t).filter(Boolean).join("")).trim()},je=(e,...n)=>{if(n.length===0)return e;const t=([i,c])=>e.slice(i,c),s=I(e),o=new w;for(const i of n){const c=s.filter(({value:l})=>l.includes(i)).sort((l,a)=>l.range[0]-a.range[0]);for(let l=0;l<c.length-1;l+=2){const a=c[l],u=c[l+1],m=t([u.range[1],u.range[1]+1]).at(-1);o.push(a.range[0],m===`
|
|
9
|
+
`?u.range[1]+1:u.range[1])}}o.collapse();const r=new w;return r.push(0,e.length),r.subtract(o),b.dedent(r.collapse().map(t).filter(Boolean).join("")).trim()},qe=(e,n,t,s)=>{if(!n)return e;const o=M(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],u=o[l+1];r+=e.slice(i,a.range[1]),r+=F(t??a.value,s),i=u.range[0]}r+=e.slice(i);const c=new w;return c.push(0,r.length),c.subtract(M(r,n).reduce((l,{range:a})=>(l.push(...a),l),new w)),b.dedent(c.collapse().map(([l,a])=>r.slice(l,a)).filter(Boolean).join("")).trim()},Q=e=>({space:e===" ",newline:e===`
|
|
10
|
+
`}),z=e=>[...M(e,"p↓:"),...M(e,"parkdown:")].sort((n,t)=>n.range[0]-t.range[0]).reverse().reduce((n,{range:[t,s],loc:{start:{column:o}}})=>{const r=(l,a)=>n.slice(0,l)+n.slice(a),i={prev:Q(n[t-1]),next:Q(n[s]),startLine:o===0,final:s===n.length};return i.startLine&&(i.next.newline||i.final)?r(t-(i.final?1:0),s+1):i.startLine?r(t,s+(i.next.space?1:0)):r(t-(i.prev.space?1:0),s)},e),Oe=(e,n)=>{if(!n)return z(e);const t=Te(n);switch(t.name){case"extract":e=be(e,t.id,...G(t));break;case"remove":e=je(e,t.id,...G(t));break;case"replace":e=qe(e,t.id,t.with,t.space);break}return z(e)},Ie=["http","./","../"],De=({url:e})=>Ie.some(n=>e.startsWith(n)),Le=e=>j(e)&&he(e)&&De(e),X=({url:e},n)=>`[](${n?f.join(n,e):e})`,d={_open:"<!--",_close:"-->",_flag:"p↓",get begin(){return S(d._open,d._flag,"BEGIN",d._close)},get end(){return S(d._open,d._flag,"END",d._close)},lengthOf(e){const n=`lines: ${e.split(`
|
|
11
|
+
`).length}`,t=`chars: ${e.length}`;return S(d._open,d._flag,"length",n,t,d._close)}},V=e=>n=>j(n)&&n.value===d[e],Pe=({position:e,url:n,siblingCount:t},s)=>({position:e,url:n,headingDepth:s,inline:t>=1}),Ae=({position:{start:e},url:n,siblingCount:t},{position:{end:s}},o)=>({position:{start:e,end:s},url:n,headingDepth:o,inline:t>=1}),Re=(e,n,t)=>(e.inline?S:de)(X(e,t),d.begin,d.lengthOf(n),n,d.end),_e=e=>{const n=x(e,"heading").reduce((t,{position:s,depth:o})=>t.set(s.start.line,o),new Map);return t=>{for(let s=t.position.start.line;s>=1;s--){const o=n.get(s);if(o)return o}return 0}},N={openingCommentDoesNotFollowLink:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) does not follow link`),closingCommentNotMatchedToOpening:({position:{start:e}})=>new Error(`Closing comment (@${e.line}:${e.column}) does not match to opening comment`),openingCommentNotClosed:({position:{start:e}})=>new Error(`Opening comment (@${e.line}:${e.column}) is not followed by a closing comment`)},We=(e,n)=>{const t=[],s=[...e.map(r=>({node:r,type:"open"})),...n.map(r=>({node:r,type:"close"}))].sort((r,i)=>v(r.node,i.node)),o=[];for(const r of s)if(r.type==="open")o.push(r);else{const i=r.node;if(o.length===0)throw N.closingCommentNotMatchedToOpening(i);const c=o.pop().node;if(o.length>0)continue;t.push({open:c,close:i})}if(o.length>0)throw N.openingCommentNotClosed(o[0].node);return t},Be=(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(fe(e,i,r.open).trim()!=="")throw N.openingCommentDoesNotFollowLink(r.open);return o.push([i,r])}o.push(i)}throw N.openingCommentDoesNotFollowLink(r.open)}),o.push(...s.reverse()),o.reverse()},J=(e,n)=>{n??(n=q.md(e));const t=_e(n),s=x(n,"link").filter(Le),o=x(n,"html").sort(v),r=o.filter(V("begin")),i=o.filter(V("end")),c=We(r,i);return Be(e,s,c).map(a=>Array.isArray(a)?Ae(a[0],a[1].close,t(a[0])):Pe(a,t(a)))},Fe=(e,{url:n})=>t=>e(f.join(f.dirname(n),t)),Z=(...e)=>{const n=e.reduce((t,s)=>t+s,0);return Math.min(Math.max(n,1),6)},Ue=(e,n,t)=>{if(n===0)return e;t??(t=q.md(e));const s=x(t,"heading"),o=e.split(`
|
|
12
|
+
`);for(const r of s){const{depth:i,position:{start:c,end:l}}=r,a=Z(i,n),u=o[c.line-1].slice(i,l.column),m="#".repeat(a)+u;o[c.line-1]=m,r.depth=a}return o.join(`
|
|
13
|
+
`)},K=e=>J(e).reverse().sort(v.reverse).reduce((n,t)=>A(n,X(t),t),e),Y=(e,n,t,s,o)=>{try{e=K(e),e=Ue(e,n);const r=q.md(e);return J(e,r).sort(v).reverse().map(i=>{var se,oe;const{url:c,headingDepth:l}=i,[a,...u]=f.basename(c).split("?"),m=a.split(".").pop()??"",T=u.join("?"),C=f.dirname(c),te=f.join(C,a);if(c.startsWith("./")||c.startsWith("../")){let g=t(te);const y=new ie.URLSearchParams(T),D=(se=y.get("region"))==null?void 0:se.split(R);g=(D==null?void 0:D.reduce((E,k)=>Oe(E,k),g))??g;const Qe=y.has("skip"),ze=y.get("heading")??0,Xe=y.has("inline");let{inline:L}=i;if(Xe&&(L=!0),!Qe)if(m==="md"){const E=Fe(t,i),k=o?f.join(o,C):C,Ve=Z(l,Number(ze));g=Y(g,Ve,E,te,k)}else/^(js|ts)x?|svelte$/i.test(m)&&(g=H(g,"code",{extension:m,inline:L}));const P=(oe=y.get("wrap"))==null?void 0:oe.split(R);return g=(P==null?void 0:P.reduce((E,k)=>H(E,k,{extension:m,inline:L}),g))??g,{target:i,content:Re(i,g,o)}}else throw c.startsWith("http")?new Error("External web links are not implemented yet"):new Error(`Unsupported link type: ${c}`)}).reduce((i,{target:c,content:l})=>A(i,l,c),e)}catch(r){throw new Error(`Error populating inclusions in file ${s}: ${r}`)}},ee=e=>h.readFileSync(e,"utf-8"),ne=e=>{const n=f.resolve(e),t=f.dirname(n);return{markdown:ee(n),dir:t,path:n}},Ge=(e,n=!0)=>{const{dir:t,path:s,markdown:o}=ne(e),i=Y(o,0,c=>ee(f.resolve(t,ve(c))),s);return n&&h.writeFileSync(s,i),i},He=(e,n=!0)=>{const{path:t,markdown:s}=ne(e),o=K(s);return n&&h.writeFileSync(t,o),o};p.depopulateMarkdownInclusions=He,p.populateMarkdownInclusions=Ge,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@p-buddy/parkdown",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.6",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"bin": "./dist/cli.js",
|
|
8
8
|
"files": [
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"build:cli": "vite build --config vite.cli.config.ts",
|
|
38
38
|
"build": "pnpm run build:lib && pnpm run build:cli",
|
|
39
39
|
"test": "vitest",
|
|
40
|
-
"test:run": "vitest run"
|
|
40
|
+
"test:run": "vitest run",
|
|
41
|
+
"cli": "npx tsx src/cli.ts"
|
|
41
42
|
},
|
|
42
43
|
"typings": "./dist/index.d.ts",
|
|
43
44
|
"exports": {
|