@dogsbay/commands 0.2.0-beta.182 → 0.2.0-beta.184
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/commands/menu-edit.d.ts +2 -2
- package/dist/commands/refactor-files.d.ts +6 -0
- package/dist/commands/refactor-files.js +67 -138
- package/dist/commands/validate.js +1 -84
- package/dist/edits.d.ts +16 -1
- package/dist/edits.js +91 -29
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -2
- package/dist/indexes.d.ts +10 -0
- package/dist/indexes.js +71 -7
- package/dist/node-storage.d.ts +7 -0
- package/dist/node-storage.js +28 -0
- package/dist/refactor-entry.d.ts +13 -0
- package/dist/refactor-entry.js +13 -0
- package/dist/refactor.d.ts +64 -0
- package/dist/refactor.js +428 -0
- package/dist/site-layout.d.ts +24 -0
- package/dist/site-layout.js +91 -0
- package/dist/storage.d.ts +6 -0
- package/dist/storage.js +7 -0
- package/package.json +10 -6
package/dist/indexes.d.ts
CHANGED
|
@@ -142,6 +142,8 @@ export interface InboundRef {
|
|
|
142
142
|
raw: string;
|
|
143
143
|
/** Fragment the reference targets, when present. */
|
|
144
144
|
fragment?: string;
|
|
145
|
+
/** 1-based line in the referencing file, when known. */
|
|
146
|
+
line?: number;
|
|
145
147
|
}
|
|
146
148
|
export interface WorkspaceIndex {
|
|
147
149
|
/** Every file in the workspace (posix-relative) — assets included; what a site-absolute link may land on. */
|
|
@@ -186,6 +188,14 @@ export interface WorkspaceIndex {
|
|
|
186
188
|
* so a directive inside one is real and must be visible to validation.
|
|
187
189
|
*/
|
|
188
190
|
export declare const blankCodeRegions: (source: string) => string;
|
|
191
|
+
/**
|
|
192
|
+
* The props of a directive that name a page or a file: `href` / `link` (a card, a link card, a
|
|
193
|
+
* badge), `src` / `poster` (a video, a file), and `icon` / `image` when they are an image path (a
|
|
194
|
+
* card's picture; `lucide:rocket` is a name). Not an include's `src` (indexed as an include), an
|
|
195
|
+
* example's `src` (the examples root), a data directive's (the data folder) or an API's `spec`:
|
|
196
|
+
* those are not relative to the page.
|
|
197
|
+
*/
|
|
198
|
+
export declare const LINK_PROPS: readonly ["href", "link", "src", "poster", "icon", "image"];
|
|
189
199
|
/** A menu file by its name (`nav.yml`, `<name>.nav.yml`; format-topicmap's rule, re-exported). */
|
|
190
200
|
export { isMenuFilePath };
|
|
191
201
|
/**
|
package/dist/indexes.js
CHANGED
|
@@ -42,7 +42,10 @@ const inlineText = (nodes) => {
|
|
|
42
42
|
return node.text;
|
|
43
43
|
case "link":
|
|
44
44
|
case "highlight":
|
|
45
|
+
case "when":
|
|
45
46
|
return inlineText(node.children);
|
|
47
|
+
case "choice-value":
|
|
48
|
+
return node.form === "link" && node.children?.length ? inlineText(node.children) : (node.text ?? "");
|
|
46
49
|
default:
|
|
47
50
|
return "";
|
|
48
51
|
}
|
|
@@ -92,6 +95,26 @@ const findHtmlIdSpan = (state, id) => {
|
|
|
92
95
|
}
|
|
93
96
|
return undefined;
|
|
94
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* The props of a directive that name a page or a file: `href` / `link` (a card, a link card, a
|
|
100
|
+
* badge), `src` / `poster` (a video, a file), and `icon` / `image` when they are an image path (a
|
|
101
|
+
* card's picture; `lucide:rocket` is a name). Not an include's `src` (indexed as an include), an
|
|
102
|
+
* example's `src` (the examples root), a data directive's (the data folder) or an API's `spec`:
|
|
103
|
+
* those are not relative to the page.
|
|
104
|
+
*/
|
|
105
|
+
export const LINK_PROPS = ["href", "link", "src", "poster", "icon", "image"];
|
|
106
|
+
const PROPS_NOT_LINKS = new Set(["include", "example", "openapi", "data-table", "data-cards"]);
|
|
107
|
+
const IMAGE_PATH = /\.(?:png|jpe?g|gif|svg|webp|avif|ico|bmp)(?:[?#].*)?$/i;
|
|
108
|
+
const recordPropHrefs = (state, props) => {
|
|
109
|
+
for (const key of LINK_PROPS) {
|
|
110
|
+
const value = props[key];
|
|
111
|
+
if (typeof value !== "string" || !value.trim() || value.includes("{{"))
|
|
112
|
+
continue;
|
|
113
|
+
if ((key === "icon" || key === "image") && !IMAGE_PATH.test(value))
|
|
114
|
+
continue;
|
|
115
|
+
recordHref(state, value, key === "href" || key === "link" ? "link" : "image");
|
|
116
|
+
}
|
|
117
|
+
};
|
|
95
118
|
const recordHref = (state, href, kind) => {
|
|
96
119
|
if (href.startsWith("key:")) {
|
|
97
120
|
const name = href.slice("key:".length).split("#")[0];
|
|
@@ -164,7 +187,16 @@ const scanInline = (state, nodes) => {
|
|
|
164
187
|
case "html-inline":
|
|
165
188
|
scanHtmlAnchors(state, node.html);
|
|
166
189
|
break;
|
|
190
|
+
case "badge":
|
|
191
|
+
case "icon": {
|
|
192
|
+
// an inline directive's `{href=…}` (`:badge[New]{href="x.md"}`)
|
|
193
|
+
const attrs = node.attrs;
|
|
194
|
+
if (attrs)
|
|
195
|
+
recordPropHrefs(state, attrs);
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
167
198
|
case "highlight":
|
|
199
|
+
case "when":
|
|
168
200
|
scanInline(state, node.children);
|
|
169
201
|
break;
|
|
170
202
|
default:
|
|
@@ -210,6 +242,9 @@ const scanTree = (state, nodes) => {
|
|
|
210
242
|
if (typeof src === "string" && src.length > 0)
|
|
211
243
|
recordHref(state, src, "image");
|
|
212
244
|
}
|
|
245
|
+
else if (node.props && !PROPS_NOT_LINKS.has(node.type)) {
|
|
246
|
+
recordPropHrefs(state, node.props);
|
|
247
|
+
}
|
|
213
248
|
if ((node.type === "html" || node.type === "html-container") && node.html) {
|
|
214
249
|
scanHtmlAnchors(state, node.html);
|
|
215
250
|
}
|
|
@@ -259,7 +294,18 @@ const scanFrontmatter = (source) => {
|
|
|
259
294
|
const TEMPLATE_PAGE = /^template:[ \t]*["']?jinja["']?[ \t]*$/m;
|
|
260
295
|
const NOT_TEMPLATE = /^template:[ \t]*false[ \t]*$/m;
|
|
261
296
|
const NO_PREPROCESS = /^preprocess:[ \t]*false[ \t]*$/m;
|
|
262
|
-
|
|
297
|
+
/** The config attributes of each build a page is in: the variants of the deepest source folder holding it. */
|
|
298
|
+
const buildAttributes = (variants, path) => {
|
|
299
|
+
const holding = variants.filter((v) => inFolder(path, v.path));
|
|
300
|
+
const deepest = Math.max(-1, ...holding.map((v) => v.path.length));
|
|
301
|
+
return holding.filter((v) => v.path.length === deepest).map((v) => v.attributes);
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* `attributeSets`: the config `attributes:` of each build the page is in (its content source's,
|
|
305
|
+
* over the site's). A migrated AsciiDoc heading whose id comes from an attribute's value
|
|
306
|
+
* (`asciidoc-title`) gets the id the first build gives it; the other builds' ids are anchors too.
|
|
307
|
+
*/
|
|
308
|
+
const indexDoc = (path, source, templateSource = false, attributeSets = []) => {
|
|
263
309
|
const front = scanFrontmatter(source);
|
|
264
310
|
// where the body starts: past the closing `---` line of a terminated block
|
|
265
311
|
let bodyStart = 0;
|
|
@@ -346,8 +392,14 @@ const indexDoc = (path, source, templateSource = false) => {
|
|
|
346
392
|
pushDest(m[1] ?? m[2] ?? "", m.index ?? 0);
|
|
347
393
|
for (const m of masked.matchAll(/^[ \t]{0,3}\[[^\]]+\]:[ \t]+(?:<([^>]*)>|(\S+))/gm))
|
|
348
394
|
pushDest(m[1] ?? m[2] ?? "", m.index ?? 0);
|
|
349
|
-
|
|
350
|
-
|
|
395
|
+
// `href="x"` in raw HTML and a directive's `{href="x"}` / `{href=x}`; a `---` props block's `href: x`
|
|
396
|
+
// (in the body: front matter's `image: cover.png` is no link)
|
|
397
|
+
for (const m of masked.matchAll(/(?<![\w-])(?:href|link|src|poster|icon|image)\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s}"'>]+))/g))
|
|
398
|
+
if ((m.index ?? 0) >= bodyStart)
|
|
399
|
+
pushDest(m[1] ?? m[2] ?? m[3] ?? "", m.index ?? 0);
|
|
400
|
+
for (const m of masked.matchAll(/^[ \t]*(?:href|link|src|poster|icon|image)[ \t]*:[ \t]*(?:"([^"]+)"|'([^']+)'|([^\s"'#]+))/gm))
|
|
401
|
+
if ((m.index ?? 0) >= bodyStart)
|
|
402
|
+
pushDest(m[1] ?? m[2] ?? m[3] ?? "", m.index ?? 0);
|
|
351
403
|
// per-href lists were filled regex-by-regex (inline, then reference definitions, then HTML):
|
|
352
404
|
// document order across kinds needs a sort, or a `[ref]: x` + `[q](x)` pair swaps lines
|
|
353
405
|
for (const list of destOffsets.values())
|
|
@@ -372,7 +424,7 @@ const indexDoc = (path, source, templateSource = false) => {
|
|
|
372
424
|
if (typeof frontmatter.title === "string")
|
|
373
425
|
entry.title = frontmatter.title;
|
|
374
426
|
// THE slug pass, read without changing the tree (render-html's ids, the Astro build's)
|
|
375
|
-
state.pageIds = headingIds(tree);
|
|
427
|
+
state.pageIds = headingIds(tree, { attributes: attributeSets[0] });
|
|
376
428
|
scanTree(state, tree);
|
|
377
429
|
// Lines for links: each href's successive occurrences in the code-masked source, in
|
|
378
430
|
// document order (the tree carries no positions) — a repeated href maps to the next
|
|
@@ -406,6 +458,15 @@ const indexDoc = (path, source, templateSource = false) => {
|
|
|
406
458
|
}
|
|
407
459
|
};
|
|
408
460
|
walkSlugs(tree);
|
|
461
|
+
// another build with other attribute values gives such a heading another id: a link may use it
|
|
462
|
+
if (attributeSets.length > 1 && source.includes("asciidoc-title=")) {
|
|
463
|
+
for (const attributes of attributeSets.slice(1)) {
|
|
464
|
+
for (const [node, given] of headingIds(tree, { attributes })) {
|
|
465
|
+
if (node.type === "heading" && given.generated && given.id)
|
|
466
|
+
entry.anchors.push(given.id);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
409
470
|
entry.headings.forEach((h, i) => { const slug = slugs[i]; if (slug && !h.id)
|
|
410
471
|
h.slug = slug; });
|
|
411
472
|
// headings with no `{#id}`: the slug the site gives them, after the authored targets
|
|
@@ -605,7 +666,9 @@ export async function buildWorkspaceIndex(storage, options = {}) {
|
|
|
605
666
|
}
|
|
606
667
|
else if (isDocPath(path)) {
|
|
607
668
|
if (!(reuse && docs.has(path)))
|
|
608
|
-
docs.set(path, indexDoc(path, await storage.read(path), config.templateSources.some((dir) => inFolder(path, dir))
|
|
669
|
+
docs.set(path, indexDoc(path, await storage.read(path), config.templateSources.some((dir) => inFolder(path, dir)),
|
|
670
|
+
// the builds this page is in: its content source's (the deepest folder holding it)
|
|
671
|
+
buildAttributes(config.variants, path)));
|
|
609
672
|
}
|
|
610
673
|
}
|
|
611
674
|
// menu files reached only by an include (`api/part.yml`): indexed too, to a fixpoint. A "/"-rooted
|
|
@@ -650,12 +713,13 @@ export async function buildWorkspaceIndex(storage, options = {}) {
|
|
|
650
713
|
kind: link.kind,
|
|
651
714
|
raw: link.href,
|
|
652
715
|
...(link.fragment ? { fragment: link.fragment } : {}),
|
|
716
|
+
...(link.line !== undefined ? { line: link.line } : {}),
|
|
653
717
|
});
|
|
654
718
|
}
|
|
655
719
|
}
|
|
656
720
|
for (const include of doc.includes) {
|
|
657
721
|
includeCount++;
|
|
658
|
-
addInbound(include.target, { from: doc.path, kind: "include", raw: include.raw });
|
|
722
|
+
addInbound(include.target, { from: doc.path, kind: "include", raw: include.raw, ...(include.line !== undefined ? { line: include.line } : {}) });
|
|
659
723
|
}
|
|
660
724
|
for (const name of doc.keyUses) {
|
|
661
725
|
const users = keyUses.get(name) ?? [];
|
|
@@ -668,7 +732,7 @@ export async function buildWorkspaceIndex(storage, options = {}) {
|
|
|
668
732
|
for (const ref of map.refs) {
|
|
669
733
|
if (ref.target) {
|
|
670
734
|
const hash = ref.href.indexOf("#");
|
|
671
|
-
addInbound(ref.target, { from: map.path, kind: "menu", raw: ref.href, ...(hash >= 0 && hash < ref.href.length - 1 ? { fragment: ref.href.slice(hash + 1) } : {}) });
|
|
735
|
+
addInbound(ref.target, { from: map.path, kind: "menu", raw: ref.href, ...(hash >= 0 && hash < ref.href.length - 1 ? { fragment: ref.href.slice(hash + 1) } : {}), ...(ref.line !== undefined ? { line: ref.line } : {}) });
|
|
672
736
|
}
|
|
673
737
|
}
|
|
674
738
|
}
|
package/dist/node-storage.d.ts
CHANGED
|
@@ -38,5 +38,12 @@ export declare class NodeStorage implements Storage {
|
|
|
38
38
|
}): Promise<Uint8Array>;
|
|
39
39
|
write(path: string, content: string): Promise<void>;
|
|
40
40
|
delete(path: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Move a file as it is. Always contained, whatever `containSymlinks` says: a move takes the file
|
|
43
|
+
* away from where it is, so one reached through a symlink to outside the root is refused (a
|
|
44
|
+
* folder move would otherwise empty the link's target). Never replaces a file, and copies then
|
|
45
|
+
* deletes across filesystems (EXDEV).
|
|
46
|
+
*/
|
|
47
|
+
move(from: string, to: string): Promise<void>;
|
|
41
48
|
exists(path: string): Promise<boolean>;
|
|
42
49
|
}
|
package/dist/node-storage.js
CHANGED
|
@@ -141,6 +141,34 @@ export class NodeStorage {
|
|
|
141
141
|
const fs = await this.fs();
|
|
142
142
|
await fs.unlink(await this.safeAbs(path));
|
|
143
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Move a file as it is. Always contained, whatever `containSymlinks` says: a move takes the file
|
|
146
|
+
* away from where it is, so one reached through a symlink to outside the root is refused (a
|
|
147
|
+
* folder move would otherwise empty the link's target). Never replaces a file, and copies then
|
|
148
|
+
* deletes across filesystems (EXDEV).
|
|
149
|
+
*/
|
|
150
|
+
async move(from, to) {
|
|
151
|
+
const fs = await this.fs();
|
|
152
|
+
const src = this.abs(from);
|
|
153
|
+
const dst = this.abs(to);
|
|
154
|
+
if (!(await this.isInsideRoot(src)) || !(await this.isInsideRoot(dst)))
|
|
155
|
+
throw new Error(`Path resolves outside the workspace root: ${from} → ${to}`);
|
|
156
|
+
await fs.mkdir(dst.slice(0, dst.lastIndexOf("/")), { recursive: true });
|
|
157
|
+
// COPYFILE_EXCL / link: fail rather than replace a file that appeared after the plan
|
|
158
|
+
try {
|
|
159
|
+
await fs.link(src, dst);
|
|
160
|
+
await fs.unlink(src);
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
const code = err.code;
|
|
164
|
+
if (code === "EEXIST")
|
|
165
|
+
throw new Error(`${to} already exists`);
|
|
166
|
+
if (code !== "EXDEV" && code !== "EPERM" && code !== "ENOTSUP" && code !== "EOPNOTSUPP")
|
|
167
|
+
throw err;
|
|
168
|
+
await fs.copyFile(src, dst, (await import("node:fs")).constants.COPYFILE_EXCL);
|
|
169
|
+
await fs.unlink(src);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
144
172
|
async exists(path) {
|
|
145
173
|
const fs = await this.fs();
|
|
146
174
|
try {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@dogsbay/commands/refactor` — rename, move and delete as PLANS, for a browser or Worker bundle
|
|
3
|
+
* (the playground's item 36; plans/browser-refactor.md): what would change is computed first, the
|
|
4
|
+
* host shows it and applies it with `applyPlan` as one step.
|
|
5
|
+
*
|
|
6
|
+
* Like `./indexes` it reaches no zod, no command engine, no @dogsbay/render and no Node built-in:
|
|
7
|
+
* `tests/refactor-entry.test.ts` bundles it and fails on any. The main entry exports the same
|
|
8
|
+
* functions, and the `rename-file`, `move-folder` and `safe-delete` verbs run them.
|
|
9
|
+
*/
|
|
10
|
+
export { planMove, planFolderMove, planDelete, applyPlan, type FileMove, type RefactorPlan, type RefactorRefusal, type RefactorRef, type DeletePlan, type PlanOptions, } from "./refactor.js";
|
|
11
|
+
export { type Storage, MemoryStorage, normalizeStoragePath } from "./storage.js";
|
|
12
|
+
export { Workspace } from "./workspace.js";
|
|
13
|
+
export { buildWorkspaceIndex, type WorkspaceIndex, type InboundRef, type RefKind } from "./indexes.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@dogsbay/commands/refactor` — rename, move and delete as PLANS, for a browser or Worker bundle
|
|
3
|
+
* (the playground's item 36; plans/browser-refactor.md): what would change is computed first, the
|
|
4
|
+
* host shows it and applies it with `applyPlan` as one step.
|
|
5
|
+
*
|
|
6
|
+
* Like `./indexes` it reaches no zod, no command engine, no @dogsbay/render and no Node built-in:
|
|
7
|
+
* `tests/refactor-entry.test.ts` bundles it and fails on any. The main entry exports the same
|
|
8
|
+
* functions, and the `rename-file`, `move-folder` and `safe-delete` verbs run them.
|
|
9
|
+
*/
|
|
10
|
+
export { planMove, planFolderMove, planDelete, applyPlan, } from "./refactor.js";
|
|
11
|
+
export { MemoryStorage, normalizeStoragePath } from "./storage.js";
|
|
12
|
+
export { Workspace } from "./workspace.js";
|
|
13
|
+
export { buildWorkspaceIndex } from "./indexes.js";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type RefKind, type WorkspaceIndex } from "./indexes.js";
|
|
2
|
+
import { type Storage } from "./storage.js";
|
|
3
|
+
import { type SiteLayout } from "./site-layout.js";
|
|
4
|
+
export interface FileMove {
|
|
5
|
+
from: string;
|
|
6
|
+
to: string;
|
|
7
|
+
/** The moved file's new text, when its own references changed; else it moves as it is (any bytes). */
|
|
8
|
+
text?: string;
|
|
9
|
+
}
|
|
10
|
+
/** A reference the plan could not rewrite, or (for a delete) one that would break. */
|
|
11
|
+
export interface RefactorRef {
|
|
12
|
+
/** The file holding it. */
|
|
13
|
+
file: string;
|
|
14
|
+
/** 1-based line, when known. */
|
|
15
|
+
line?: number;
|
|
16
|
+
kind: RefKind | "site";
|
|
17
|
+
/** As written. */
|
|
18
|
+
raw: string;
|
|
19
|
+
/** Why it is listed (a miss: why it could not be rewritten). */
|
|
20
|
+
reason?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface RefactorPlan {
|
|
23
|
+
ok: true;
|
|
24
|
+
moves: FileMove[];
|
|
25
|
+
/** Files that stay where they are, with their new text. */
|
|
26
|
+
edits: Map<string, string>;
|
|
27
|
+
/** The files in `edits` (sorted): the referrers that change. */
|
|
28
|
+
changed: string[];
|
|
29
|
+
/** How many references are rewritten, in all. */
|
|
30
|
+
rewritten: number;
|
|
31
|
+
misses: RefactorRef[];
|
|
32
|
+
}
|
|
33
|
+
export interface RefactorRefusal {
|
|
34
|
+
ok: false;
|
|
35
|
+
code: string;
|
|
36
|
+
message: string;
|
|
37
|
+
file?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface PlanOptions {
|
|
40
|
+
/** The workspace index, when the host has one (else it is built from `storage`). */
|
|
41
|
+
index?: WorkspaceIndex;
|
|
42
|
+
}
|
|
43
|
+
/** Plan moving files (`moves`: from → to, workspace paths). Nothing is written. */
|
|
44
|
+
export declare function planMove(storage: Storage, moves: Array<{
|
|
45
|
+
from: string;
|
|
46
|
+
to: string;
|
|
47
|
+
}>, options?: PlanOptions): Promise<RefactorPlan | RefactorRefusal>;
|
|
48
|
+
/** Plan moving a folder and everything in it (`guide` → `guides`). */
|
|
49
|
+
export declare function planFolderMove(storage: Storage, from: string, to: string, options?: PlanOptions): Promise<RefactorPlan | RefactorRefusal>;
|
|
50
|
+
/**
|
|
51
|
+
* Write a plan: the referrers' new text, then each move (a file that did not change moves as it
|
|
52
|
+
* is, through `Storage.move`). Without `Storage.move` a file that is not text (an image) is refused
|
|
53
|
+
* BEFORE anything is written: read and written as text, it would be corrupted.
|
|
54
|
+
*/
|
|
55
|
+
export declare function applyPlan(storage: Storage, plan: RefactorPlan): Promise<void>;
|
|
56
|
+
export interface DeletePlan {
|
|
57
|
+
ok: true;
|
|
58
|
+
file: string;
|
|
59
|
+
/** Every reference that would break, with its file and line: links, props, includes, menus, keys, site paths. */
|
|
60
|
+
referrers: RefactorRef[];
|
|
61
|
+
}
|
|
62
|
+
/** What deleting `file` would break. Nothing is deleted. */
|
|
63
|
+
export declare function planDelete(storage: Storage, file: string, options?: PlanOptions): Promise<DeletePlan | RefactorRefusal>;
|
|
64
|
+
export type { SiteLayout };
|