@dogsbay/site-files 0.2.0-beta.154 → 0.2.0-beta.155

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
@@ -15,6 +15,7 @@ export * from "./translations.js";
15
15
  export * from "./namespaces.js";
16
16
  export * from "./mounts.js";
17
17
  export * from "./markdown-links.js";
18
+ export * from "./tree-links.js";
18
19
  export * from "./products.js";
19
20
  export * from "./version-menu.js";
20
21
  export * from "./static-folder.js";
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export * from "./translations.js";
15
15
  export * from "./namespaces.js";
16
16
  export * from "./mounts.js";
17
17
  export * from "./markdown-links.js";
18
+ export * from "./tree-links.js";
18
19
  export * from "./products.js";
19
20
  export * from "./version-menu.js";
20
21
  export * from "./static-folder.js";
@@ -0,0 +1,16 @@
1
+ /** Where a link and a file reference go; `inHtml` when the value is a raw HTML attribute's. */
2
+ export interface TreeLinkFunctions {
3
+ link(href: string, inHtml?: boolean): string;
4
+ asset(src: string, inHtml?: boolean): string;
5
+ }
6
+ /**
7
+ * Rewrites, in place, the links and file references a page tree carries, where the renderer resolves
8
+ * them (@dogsbay/render-html: `ctx.link`, `ctx.asset`): `href` fields (links, buttons, cards), `src`
9
+ * and `poster` (images, videos), an image `icon`, `href`/`src` in raw HTML, and links in an OpenAPI
10
+ * operation's Markdown descriptions (its other props are the API's data). For the `.md` copies, so
11
+ * their links name what the site serves (plans/md-copy-links.md). Returns a function that puts every
12
+ * changed value back, so the page renders from the same tree without a copy of it.
13
+ */
14
+ export declare function rewriteTreeLinks(tree: unknown, fn: TreeLinkFunctions): () => void;
15
+ /** A value for a raw HTML attribute in double quotes. */
16
+ export declare function escAttr(v: string): string;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The links and file references a page tree carries, rewritten in place where the renderer
3
+ * resolves them (@dogsbay/render-html: `ctx.link`, `ctx.asset`), and put back by the function it
4
+ * returns. ONE walker for both builds' Markdown copies (the `.md` beside each page and
5
+ * llms-full.txt), so their links name what the site serves: moved here from dogsbay-ssg
6
+ * (src/core/links.ts, 7737a0d) on 2026-11-02; plans/md-mirror-links.md.
7
+ */
8
+ import { rewriteEndpointMarkdown } from "./markdown-links.js";
9
+ const IMAGE_FILE = /\.(svg|png|jpe?g|gif|webp|avif)$/i;
10
+ /** Node types whose `html` is text (code, math, a diagram's source), not markup. */
11
+ const TEXT_HTML = new Set(["code", "math", "math-block", "diagram", "mermaid"]);
12
+ /**
13
+ * Rewrites, in place, the links and file references a page tree carries, where the renderer resolves
14
+ * them (@dogsbay/render-html: `ctx.link`, `ctx.asset`): `href` fields (links, buttons, cards), `src`
15
+ * and `poster` (images, videos), an image `icon`, `href`/`src` in raw HTML, and links in an OpenAPI
16
+ * operation's Markdown descriptions (its other props are the API's data). For the `.md` copies, so
17
+ * their links name what the site serves (plans/md-copy-links.md). Returns a function that puts every
18
+ * changed value back, so the page renders from the same tree without a copy of it.
19
+ */
20
+ export function rewriteTreeLinks(tree, fn) {
21
+ const undo = [];
22
+ const set = (o, k, v) => {
23
+ if (o[k] === v)
24
+ return;
25
+ undo.push([o, k, o[k]]);
26
+ o[k] = v;
27
+ };
28
+ // An attribute's value unescaped, resolved and escaped again, as render-html's rewriteHtml does
29
+ const html = (h) => h.replace(/<[a-zA-Z][^<>]*>/g, (tag) => tag.replace(/(\s)(href|src)(\s*=\s*)("([^"]*)"|'([^']*)')/gi, (m, sp, attr, eq, _q, dq, sq) => {
30
+ const v = unescAttr(dq ?? sq ?? "");
31
+ if (!v || v.startsWith("#") || /^[a-z][a-z0-9+.-]*:|^\/\//i.test(v))
32
+ return m;
33
+ const out = attr.toLowerCase() === "href" ? fn.link(v, true) : fn.asset(v, true);
34
+ return `${sp}${attr}${eq}"${escAttr(out)}"`;
35
+ }));
36
+ // A node reached twice (shared by a cache) is rewritten once
37
+ const seen = new WeakSet();
38
+ const walk = (node) => {
39
+ if (node && typeof node === "object") {
40
+ if (seen.has(node))
41
+ return;
42
+ seen.add(node);
43
+ }
44
+ if (Array.isArray(node)) {
45
+ for (const n of node)
46
+ walk(n);
47
+ return;
48
+ }
49
+ if (!node || typeof node !== "object")
50
+ return;
51
+ const o = node;
52
+ if (o.type === "endpoint") {
53
+ // Its Markdown descriptions are rewritten in a copy of its props
54
+ const props = structuredClone(o.props);
55
+ rewriteEndpointMarkdown(props, fn.link);
56
+ set(o, "props", props);
57
+ return;
58
+ }
59
+ if (typeof o.href === "string")
60
+ set(o, "href", fn.link(o.href));
61
+ if (typeof o.src === "string")
62
+ set(o, "src", fn.asset(o.src));
63
+ if (typeof o.poster === "string")
64
+ set(o, "poster", fn.asset(o.poster));
65
+ if (typeof o.icon === "string" && IMAGE_FILE.test(o.icon))
66
+ set(o, "icon", fn.asset(o.icon));
67
+ // A code block keeps its text in `html` (the dogsbay-md parser), as math and diagram sources
68
+ // do: it is code shown, never markup, so its `href="…"` stays as written (review: every
69
+ // copy's HTML samples were rewritten)
70
+ if (typeof o.html === "string" && !TEXT_HTML.has(String(o.type)))
71
+ set(o, "html", html(o.html));
72
+ for (const v of Object.values(o))
73
+ if (v && typeof v === "object")
74
+ walk(v);
75
+ };
76
+ const restore = () => {
77
+ for (let i = undo.length - 1; i >= 0; i--)
78
+ undo[i][0][undo[i][1]] = undo[i][2];
79
+ };
80
+ try {
81
+ walk(tree);
82
+ }
83
+ catch (e) {
84
+ restore();
85
+ throw e;
86
+ }
87
+ return restore;
88
+ }
89
+ function unescAttr(v) {
90
+ // Numeric entities too, so `&#38;` is not escaped again into a literal `&#38;` (review)
91
+ return v.replace(/&(amp|quot|lt|gt|#(\d{1,7})|#x([0-9a-f]{1,6}));/gi, (m, e, dec, hex) => {
92
+ if (dec !== undefined || hex !== undefined) {
93
+ const cp = dec !== undefined ? Number(dec) : parseInt(hex, 16);
94
+ return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m;
95
+ }
96
+ return { amp: "&", quot: '"', lt: "<", gt: ">" }[e.toLowerCase()];
97
+ });
98
+ }
99
+ /** A value for a raw HTML attribute in double quotes. */
100
+ export function escAttr(v) {
101
+ return v.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
102
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.154",
3
+ "version": "0.2.0-beta.155",
4
4
  "description": "robots.txt, _headers, llms.txt, sitemaps, redirects and JSON-LD for docs sites; shared by the dogsbay Astro pipeline and dogsbay-ssg, Worker-safe",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",