@dogsbay/corpus 0.2.0-beta.155 → 0.2.0-beta.157

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -73,3 +73,4 @@ export declare function withPages<E extends {
73
73
  file?: string;
74
74
  page?: string;
75
75
  }>(ledger: E[], pageOf: ReadonlyMap<string, string>): E[];
76
+ export { MEDIA_EXTS, mountCopies, assetsReferenced, assetCopies, type AssetMount } from "./mounts.js";
package/dist/index.js CHANGED
@@ -69,3 +69,4 @@ export function withPages(ledger, pageOf) {
69
69
  return page === undefined ? e : { ...e, page };
70
70
  });
71
71
  }
72
+ export { MEDIA_EXTS, mountCopies, assetsReferenced, assetCopies } from "./mounts.js";
@@ -0,0 +1,63 @@
1
+ /**
2
+ * An importer's asset mounts and what of them an exporter copies (moved from format-dogsbay-md,
3
+ * 2026-11-02, so format-obsidian shares the same rules). Node-free.
4
+ */
5
+ export declare const MEDIA_EXTS: Set<string>;
6
+ /** An importer's asset directory, as `ImportResult.assetMounts` declares it, relative to the corpus. */
7
+ export interface AssetMount {
8
+ dir: string;
9
+ /** A subdirectory of the root to land in (`pages` → `_assets/pages/`). */
10
+ into?: string;
11
+ /** Media files only (`MEDIA_EXTS`): the mount is the docs tree itself. */
12
+ mediaOnly?: boolean;
13
+ /**
14
+ * Directory names to leave behind, matched at ANY depth in the mount.
15
+ *
16
+ * `mediaOnly` is the wrong tool when a mount is mostly wanted: it also drops scripts, YAML
17
+ * and text a corpus may legitimately carry. This skips named subtrees instead — e.g.
18
+ * Calico's `licenses/`, ~35 MB of third-party attribution HTML for versions the corpus does
19
+ * not even import, which nothing references. Matched at any depth on purpose: the first
20
+ * version compared only the FIRST path segment and silently did nothing, because the real
21
+ * tree is `_assets/calico/3.32/licenses/…`.
22
+ */
23
+ exclude?: string[];
24
+ /**
25
+ * Folders of the mount, as paths relative to it (`public`, `guide/img`), left out whole.
26
+ * Unlike `exclude` it names one folder, not a name at any depth, and holds in `mediaOnly`
27
+ * mounts too: VitePress's pages mount leaves out `public/`, which is a mount of its own.
28
+ */
29
+ skip?: string[];
30
+ /** Public root the mount lands in; `_assets` unless the source used another. */
31
+ root?: string;
32
+ }
33
+ /**
34
+ * The files the exporter copies for `mounts`, from a corpus's `paths`: corpus path → path in
35
+ * the content root. The same rules as the exporter's copy on disk: the whole mount minus
36
+ * excluded names, or its media only.
37
+ */
38
+ export declare function mountCopies(paths: string[], mounts: AssetMount[]): {
39
+ from: string;
40
+ to: string;
41
+ }[];
42
+ /**
43
+ * The asset files (`<root>/…` paths, of `known`) some written text refers to — the pages and nav of
44
+ * a Docusaurus `/migrate`, the notes of an Obsidian vault (moved here from format-docusaurus,
45
+ * 2026-11-02, so both use one rule). A path is read however it was written — a Markdown
46
+ * destination with spaces or parentheses, an encoded name (`%20`, `%23`), an attribute with
47
+ * `&amp;` or any entity, `//` and `./` — by testing what follows each `<root>/` against the files
48
+ * there are: every prefix that ends where a path can end, raw and decoded. Guessing where a
49
+ * reference ends from its characters missed `Screenshot (1).png` and `c%233.png`. A name that
50
+ * matches no file exactly matches one that differs only in case or Unicode normalization (a vault
51
+ * opened on macOS or Windows found it).
52
+ */
53
+ export declare function assetsReferenced(texts: Iterable<string>, known: ReadonlySet<string>, roots?: string[]): Set<string>;
54
+ /**
55
+ * The files to copy, one per output path: the last mount to name a path wins, as when an exporter
56
+ * copies them in turn. With `used`, only the files used, and with an HTML file the rest of its
57
+ * folder (a javadoc or an API viewer under static/ loads its own CSS and pages) — never a whole
58
+ * mount's root (`wholeMounts`, default each root). A page at the same path (`pages`) wins over all.
59
+ */
60
+ export declare function assetCopies<T extends {
61
+ from: string;
62
+ to: string;
63
+ }>(all: T[], pages: ReadonlySet<string>, used?: ReadonlySet<string>, wholeMounts?: string[]): T[];
package/dist/mounts.js ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * An importer's asset mounts and what of them an exporter copies (moved from format-dogsbay-md,
3
+ * 2026-11-02, so format-obsidian shares the same rules). Node-free.
4
+ */
5
+ export const MEDIA_EXTS = new Set([
6
+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif",
7
+ ".svg", ".ico", ".pdf", ".mp4", ".webm",
8
+ ]);
9
+ /**
10
+ * The files the exporter copies for `mounts`, from a corpus's `paths`: corpus path → path in
11
+ * the content root. The same rules as the exporter's copy on disk: the whole mount minus
12
+ * excluded names, or its media only.
13
+ */
14
+ export function mountCopies(paths, mounts) {
15
+ const out = [];
16
+ for (const mount of mounts) {
17
+ const dir = mount.dir.replace(/\/+$/, "");
18
+ const dest = [mount.root ?? "_assets", mount.into].filter(Boolean).join("/");
19
+ const skip = new Set(mount.exclude ?? []);
20
+ const folders = (mount.skip ?? []).map((f) => f.replace(/^\/+|\/+$/g, "")).filter(Boolean);
21
+ for (const path of paths) {
22
+ if (dir && !path.startsWith(dir + "/"))
23
+ continue;
24
+ const rel = dir ? path.slice(dir.length + 1) : path;
25
+ if (folders.some((f) => rel.startsWith(f + "/")))
26
+ continue;
27
+ const segs = rel.split("/");
28
+ if (mount.mediaOnly) {
29
+ const name = segs[segs.length - 1];
30
+ const dot = name.lastIndexOf(".");
31
+ if (dot === -1 || !MEDIA_EXTS.has(name.slice(dot).toLowerCase()))
32
+ continue;
33
+ }
34
+ else if (segs.some((s) => skip.has(s)))
35
+ continue;
36
+ out.push({ from: path, to: `${dest}/${rel}` });
37
+ }
38
+ }
39
+ return out;
40
+ }
41
+ /** The most characters of a reference read after `<root>/`. */
42
+ const MAX_REFERENCE = 1024;
43
+ /** Where a path can end inside a reference: what may follow a file name in Markdown or HTML. */
44
+ const PATH_END = new Set([" ", "\t", ")", "(", "?", "#", ",", ".", ";", ":", "!", "*", "]", "|"]);
45
+ const ENTITIES = { amp: "&", quot: '"', apos: "'", lt: "<", gt: ">", nbsp: "\u00a0" };
46
+ const decodeEntities = (text) => text.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (whole, e) => {
47
+ if (e[0] !== "#")
48
+ return ENTITIES[e.toLowerCase()] ?? whole;
49
+ const code = e[1] === "x" || e[1] === "X" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
50
+ return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : whole;
51
+ });
52
+ /**
53
+ * The asset files (`<root>/…` paths, of `known`) some written text refers to — the pages and nav of
54
+ * a Docusaurus `/migrate`, the notes of an Obsidian vault (moved here from format-docusaurus,
55
+ * 2026-11-02, so both use one rule). A path is read however it was written — a Markdown
56
+ * destination with spaces or parentheses, an encoded name (`%20`, `%23`), an attribute with
57
+ * `&amp;` or any entity, `//` and `./` — by testing what follows each `<root>/` against the files
58
+ * there are: every prefix that ends where a path can end, raw and decoded. Guessing where a
59
+ * reference ends from its characters missed `Screenshot (1).png` and `c%233.png`. A name that
60
+ * matches no file exactly matches one that differs only in case or Unicode normalization (a vault
61
+ * opened on macOS or Windows found it).
62
+ */
63
+ export function assetsReferenced(texts, known, roots = ["_assets"]) {
64
+ const used = new Set();
65
+ const folded = new Map();
66
+ for (const k of known)
67
+ folded.set(k.normalize("NFC").toLowerCase(), k);
68
+ const norm = (p) => p.replace(/\/{2,}/g, "/").replace(/\/(?:\.\/)+/g, "/");
69
+ const decodings = (text) => {
70
+ const forms = new Set([text, decodeEntities(text)]);
71
+ for (const form of [...forms]) {
72
+ try {
73
+ forms.add(decodeURIComponent(form));
74
+ }
75
+ catch {
76
+ // a malformed escape: each segment that can be decoded is
77
+ forms.add(form.split("/").map((seg) => { try {
78
+ return decodeURIComponent(seg);
79
+ }
80
+ catch {
81
+ return seg;
82
+ } }).join("/"));
83
+ }
84
+ }
85
+ for (const form of [...forms])
86
+ forms.add(norm(form));
87
+ return [...forms];
88
+ };
89
+ const prefixes = [...new Set(roots.map((r) => r.replace(/^\/+|\/+$/g, "")).filter(Boolean))].map((r) => `${r}/`);
90
+ for (const text of texts) {
91
+ for (const prefix of prefixes) {
92
+ for (let at = text.indexOf(prefix); at !== -1; at = text.indexOf(prefix, at + prefix.length)) {
93
+ // The reference runs to a quote, angle bracket, backtick or line end
94
+ let end = at;
95
+ const limit = Math.min(text.length, at + MAX_REFERENCE);
96
+ while (end < limit && !`"'<>\`\n\r`.includes(text[end]))
97
+ end++;
98
+ for (const form of decodings(text.slice(at, end))) {
99
+ for (let k = prefix.length; k <= form.length; k++) {
100
+ if (k !== form.length && !PATH_END.has(form[k]))
101
+ continue;
102
+ const candidate = form.slice(0, k);
103
+ if (known.has(candidate))
104
+ used.add(candidate);
105
+ else {
106
+ const other = folded.get(candidate.normalize("NFC").toLowerCase());
107
+ if (other !== undefined)
108
+ used.add(other);
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+ }
115
+ return used;
116
+ }
117
+ /**
118
+ * The files to copy, one per output path: the last mount to name a path wins, as when an exporter
119
+ * copies them in turn. With `used`, only the files used, and with an HTML file the rest of its
120
+ * folder (a javadoc or an API viewer under static/ loads its own CSS and pages) — never a whole
121
+ * mount's root (`wholeMounts`, default each root). A page at the same path (`pages`) wins over all.
122
+ */
123
+ export function assetCopies(all, pages, used, wholeMounts = ["_assets/", "_assets/pages/"]) {
124
+ const winners = new Map();
125
+ for (const copy of all)
126
+ winners.set(copy.to, copy);
127
+ let keep = (to) => !pages.has(to);
128
+ if (used) {
129
+ const roots = new Set(wholeMounts.map((d) => (d.endsWith("/") ? d : `${d}/`)));
130
+ const folders = [...used]
131
+ .filter((to) => /\.html?$/i.test(to))
132
+ .map((to) => to.slice(0, to.lastIndexOf("/") + 1))
133
+ // Not a whole mount: an HTML file in static/ itself (pure-html.html) brings only itself
134
+ .filter((dir) => !roots.has(dir));
135
+ keep = (to) => !pages.has(to) && (used.has(to) || folders.some((dir) => to.startsWith(dir)));
136
+ }
137
+ return [...winners].filter(([to]) => keep(to)).map(([, copy]) => copy);
138
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/corpus",
3
- "version": "0.2.0-beta.155",
3
+ "version": "0.2.0-beta.157",
4
4
  "description": "A documentation repository as its importers read it, in the browser or in Node: plain paths plus text on demand, from a map of files or from a folder whose symlinks are kept inside it",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "README.md"
21
21
  ],
22
22
  "dependencies": {
23
- "@dogsbay/detect": "0.2.0-beta.155"
23
+ "@dogsbay/detect": "0.2.0-beta.157"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^22.20.1",