@savvy-web/silk-effects 5.7.2 → 5.8.0

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.
@@ -0,0 +1,105 @@
1
+ //#region src/pr-body/region.ts
2
+ /**
3
+ * The generic marker-delimited region grammar every silk-managed document
4
+ * uses: `<!-- token:start -->` … `<!-- token:end -->`.
5
+ *
6
+ * @remarks
7
+ * Extracted from `silk-release-action` (its `pr-body.ts` and
8
+ * `managed-sections.ts` both carried a private copy) so the grammar has one
9
+ * owner. **Every marker is a pair.** A lone opening marker can only be located
10
+ * by scanning forward to whatever happens to follow it, which makes the
11
+ * region's extent a function of its neighbours rather than of itself — moving
12
+ * anything nearby silently redefines it. The token is free-form; `:start` and
13
+ * `:end` are the whole contract, so pairs nest and a region can contain
14
+ * sub-regions without either needing to know about the other.
15
+ */
16
+ /**
17
+ * Pure helpers over the `<!-- token:start -->` / `<!-- token:end -->` region
18
+ * grammar.
19
+ *
20
+ * @remarks
21
+ * Every operation is total: a body with no region (or a broken pair) degrades
22
+ * to the documented fail-safe result rather than failing, because the callers
23
+ * are regenerating actions that must still produce a body when the prior one
24
+ * is malformed. Use `PrBodyDiagnostic.scan` when a caller wants to be told
25
+ * about a broken pair instead of silently tolerating it.
26
+ *
27
+ * @public
28
+ */
29
+ var Region = class Region {
30
+ constructor() {}
31
+ /**
32
+ * The opening delimiter for a named region.
33
+ *
34
+ * @public
35
+ */
36
+ static start(token) {
37
+ return `<!-- ${token}:start -->`;
38
+ }
39
+ /**
40
+ * The closing delimiter for a named region.
41
+ *
42
+ * @public
43
+ */
44
+ static end(token) {
45
+ return `<!-- ${token}:end -->`;
46
+ }
47
+ /**
48
+ * The content between a region's delimiters, or `undefined` when absent.
49
+ *
50
+ * @remarks
51
+ * Finds the FIRST opening marker and the matching close after it, so a
52
+ * nested region of a different token is returned as part of the content
53
+ * rather than truncating it.
54
+ *
55
+ * @public
56
+ */
57
+ static read(body, token) {
58
+ const from = body.indexOf(Region.start(token));
59
+ const to = body.indexOf(Region.end(token), from === -1 ? 0 : from);
60
+ if (from === -1 || to === -1 || to < from) return void 0;
61
+ return body.slice(from + Region.start(token).length, to);
62
+ }
63
+ /**
64
+ * Everything outside a region, with the region and its delimiters removed.
65
+ *
66
+ * @remarks
67
+ * A body without the region comes back unchanged — removal of an absent
68
+ * region is a no-op, not an error.
69
+ *
70
+ * @public
71
+ */
72
+ static strip(body, token) {
73
+ const from = body.indexOf(Region.start(token));
74
+ const to = body.indexOf(Region.end(token), from === -1 ? 0 : from);
75
+ if (from === -1 || to === -1 || to < from) return body;
76
+ return `${body.slice(0, from)}${body.slice(to + Region.end(token).length)}`;
77
+ }
78
+ /**
79
+ * Put `rendered` (a fully rendered region, its own markers included) into
80
+ * `body`, replacing a previous region of the same token and leaving
81
+ * everything else alone.
82
+ *
83
+ * @remarks
84
+ * **Human edits outside the markers survive.** A predecessor spliced on a
85
+ * markdown heading, which silently ate any content a human happened to put
86
+ * under a heading of that name and could not tell generated text from
87
+ * theirs. An explicit marker pair can.
88
+ *
89
+ * A body with no markers keeps its content and gains the region **below**
90
+ * it, so an existing hand-written document is not displaced. The result is
91
+ * trimmed.
92
+ *
93
+ * @public
94
+ */
95
+ static upsert(body, token, rendered) {
96
+ const start = body.indexOf(Region.start(token));
97
+ const end = body.indexOf(Region.end(token));
98
+ if (start !== -1 && end !== -1 && end > start) return `${body.slice(0, start)}${rendered}${body.slice(end + Region.end(token).length)}`.trim();
99
+ const trimmed = body.trim();
100
+ return trimmed === "" ? rendered : `${trimmed}\n\n${rendered}`;
101
+ }
102
+ };
103
+
104
+ //#endregion
105
+ export { Region };