@dogsbay/site-files 0.2.0-beta.154 → 0.2.0-beta.156
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 +1 -0
- package/dist/index.js +1 -0
- package/dist/markdown-links.d.ts +6 -1
- package/dist/markdown-links.js +21 -11
- package/dist/tree-links.d.ts +16 -0
- package/dist/tree-links.js +130 -0
- package/package.json +1 -1
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";
|
package/dist/markdown-links.d.ts
CHANGED
|
@@ -10,8 +10,13 @@
|
|
|
10
10
|
* Rewrites each root-relative destination (`/…`, not `//host`) in Markdown text with `fn`:
|
|
11
11
|
* inline links and images `[text](/x "title")` / `[text](</x>)`, reference definitions
|
|
12
12
|
* `[id]: /x`, and `<a href="/x">`. Code spans and fenced code are left as written.
|
|
13
|
+
* `{ local: true }` takes every destination on this site, relative ones too (`b`, `../b`): not
|
|
14
|
+
* a scheme (`https:`, `mailto:`), not `//host`, not a bare `#fragment` (a title's links, which
|
|
15
|
+
* the copies resolve as an `href` is).
|
|
13
16
|
*/
|
|
14
|
-
export declare function rewriteMarkdownLinks(md: string, fn: (url: string) => string
|
|
17
|
+
export declare function rewriteMarkdownLinks(md: string, fn: (url: string) => string, options?: {
|
|
18
|
+
local?: boolean;
|
|
19
|
+
}): string;
|
|
15
20
|
/**
|
|
16
21
|
* The Markdown an `endpoint` node holds, rewritten with `fn`: the operation's `description`, its
|
|
17
22
|
* parameters', request fields' (nested through `properties`/`items`) and responses' and their
|
package/dist/markdown-links.js
CHANGED
|
@@ -10,32 +10,42 @@
|
|
|
10
10
|
* Rewrites each root-relative destination (`/…`, not `//host`) in Markdown text with `fn`:
|
|
11
11
|
* inline links and images `[text](/x "title")` / `[text](</x>)`, reference definitions
|
|
12
12
|
* `[id]: /x`, and `<a href="/x">`. Code spans and fenced code are left as written.
|
|
13
|
+
* `{ local: true }` takes every destination on this site, relative ones too (`b`, `../b`): not
|
|
14
|
+
* a scheme (`https:`, `mailto:`), not `//host`, not a bare `#fragment` (a title's links, which
|
|
15
|
+
* the copies resolve as an `href` is).
|
|
13
16
|
*/
|
|
14
|
-
export function rewriteMarkdownLinks(md, fn) {
|
|
15
|
-
|
|
17
|
+
export function rewriteMarkdownLinks(md, fn, options = {}) {
|
|
18
|
+
const local = options.local === true;
|
|
19
|
+
if (local ? !/\]\(|\]:|href/i.test(md) : !md.includes("/"))
|
|
16
20
|
return md;
|
|
21
|
+
const re = local ? LOCAL_LINK_REF : LINK_REF;
|
|
17
22
|
let out = "";
|
|
18
23
|
let last = 0;
|
|
19
24
|
for (const [start, end] of codeRanges(md)) {
|
|
20
|
-
out += rewriteText(md.slice(last, start), fn) + md.slice(start, end);
|
|
25
|
+
out += rewriteText(md.slice(last, start), fn, re) + md.slice(start, end);
|
|
21
26
|
last = end;
|
|
22
27
|
}
|
|
23
|
-
return out + rewriteText(md.slice(last), fn);
|
|
28
|
+
return out + rewriteText(md.slice(last), fn, re);
|
|
24
29
|
}
|
|
25
30
|
const ROOTED = String.raw `\/(?!\/)`;
|
|
31
|
+
// On this site: not a scheme, not `//host`, not a bare fragment, and starting with a character
|
|
32
|
+
// that can begin a path (the destination patterns below take the rest)
|
|
33
|
+
const LOCAL = String.raw `(?![a-zA-Z][a-zA-Z0-9+.-]*:|\/\/|#)(?=[^\s)>"'<])`;
|
|
26
34
|
// Each alternative is bounded or anchored, so a hostile description cannot make it backtrack
|
|
27
|
-
const
|
|
35
|
+
const linkRef = (start) => new RegExp([
|
|
28
36
|
// An inline link or image destination
|
|
29
|
-
String.raw `(?<lh>(?<!\\)\]\([ \t]*(?:\n[ \t]*)?<?)(?<lu>${
|
|
37
|
+
String.raw `(?<lh>(?<!\\)\]\([ \t]*(?:\n[ \t]*)?<?)(?<lu>${start}[^\s)>]*)`,
|
|
30
38
|
// <a href="/x">
|
|
31
|
-
String.raw `(?<ah><a\b[^>]{0,200}?\bhref[ \t\n]*=[ \t\n]*)(?:(?<aq>["'])(?<au>${
|
|
39
|
+
String.raw `(?<ah><a\b[^>]{0,200}?\bhref[ \t\n]*=[ \t\n]*)(?:(?<aq>["'])(?<au>${start}[^"'\n]*)\k<aq>|(?<bu>${start}[^\s"'<>=\x60]*))`,
|
|
32
40
|
// A reference definition
|
|
33
|
-
String.raw `(?<dh>^ {0,3}\[[^\]\n]{1,999}\]:[ \t]*<?)(?<du>${
|
|
41
|
+
String.raw `(?<dh>^ {0,3}\[[^\]\n]{1,999}\]:[ \t]*<?)(?<du>${start}[^\s>]*)`,
|
|
34
42
|
].join("|"), "gim");
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
const LINK_REF = linkRef(ROOTED);
|
|
44
|
+
const LOCAL_LINK_REF = linkRef(LOCAL);
|
|
45
|
+
function rewriteText(text, fn, re) {
|
|
46
|
+
if (re === LINK_REF && !text.includes("/"))
|
|
37
47
|
return text;
|
|
38
|
-
return text.replace(
|
|
48
|
+
return text.replace(re, (match, ...args) => {
|
|
39
49
|
const g = args[args.length - 1];
|
|
40
50
|
if (g.lh !== undefined)
|
|
41
51
|
return `${g.lh}${fn(g.lu)}`;
|
|
@@ -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,130 @@
|
|
|
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, rewriteMarkdownLinks } from "./markdown-links.js";
|
|
9
|
+
const IMAGE_FILE = /\.(svg|png|jpe?g|gif|webp|avif)$/i;
|
|
10
|
+
/**
|
|
11
|
+
* Props a renderer reads as inline Markdown (@dogsbay/render-html `titleMarkup`, format-astro
|
|
12
|
+
* `titleInline`): a callout's or details' title, a table's or figure's caption. Their links are
|
|
13
|
+
* rewritten in the copies as an `href` is (SSG review, 2026-11-02). A card's, link card's or
|
|
14
|
+
* accordion's title is plain text in both renderers: no link to rewrite.
|
|
15
|
+
*/
|
|
16
|
+
const MARKDOWN_PROPS = {
|
|
17
|
+
callout: ["title"],
|
|
18
|
+
details: ["title", "summary"],
|
|
19
|
+
table: ["caption"],
|
|
20
|
+
figure: ["caption"],
|
|
21
|
+
};
|
|
22
|
+
/** Node types whose `html` is text (code, math, a diagram's source), not markup. */
|
|
23
|
+
const TEXT_HTML = new Set(["code", "math", "math-block", "diagram", "mermaid"]);
|
|
24
|
+
/**
|
|
25
|
+
* Rewrites, in place, the links and file references a page tree carries, where the renderer resolves
|
|
26
|
+
* them (@dogsbay/render-html: `ctx.link`, `ctx.asset`): `href` fields (links, buttons, cards), `src`
|
|
27
|
+
* and `poster` (images, videos), an image `icon`, `href`/`src` in raw HTML, and links in an OpenAPI
|
|
28
|
+
* operation's Markdown descriptions (its other props are the API's data). For the `.md` copies, so
|
|
29
|
+
* their links name what the site serves (plans/md-copy-links.md). Returns a function that puts every
|
|
30
|
+
* changed value back, so the page renders from the same tree without a copy of it.
|
|
31
|
+
*/
|
|
32
|
+
export function rewriteTreeLinks(tree, fn) {
|
|
33
|
+
const undo = [];
|
|
34
|
+
const set = (o, k, v) => {
|
|
35
|
+
if (o[k] === v)
|
|
36
|
+
return;
|
|
37
|
+
undo.push([o, k, o[k]]);
|
|
38
|
+
o[k] = v;
|
|
39
|
+
};
|
|
40
|
+
// An attribute's value unescaped, resolved and escaped again, as render-html's rewriteHtml does
|
|
41
|
+
// A comment is copied as written: `<!-- <a href="/g/b"> -->` is not a link (SSG review)
|
|
42
|
+
const html = (h) =>
|
|
43
|
+
// `<!-->` and `<!--->` close at once, as a browser reads them (review)
|
|
44
|
+
h.replace(/<!--(?:-?>|[\s\S]*?(?:-->|$))|<[a-zA-Z][^<>]*>/g, (tag) => tag.startsWith("<!--") ? tag :
|
|
45
|
+
tag.replace(/(\s)(href|src)(\s*=\s*)("([^"]*)"|'([^']*)')/gi, (m, sp, attr, eq, _q, dq, sq) => {
|
|
46
|
+
const v = unescAttr(dq ?? sq ?? "");
|
|
47
|
+
if (!v || v.startsWith("#") || /^[a-z][a-z0-9+.-]*:|^\/\//i.test(v))
|
|
48
|
+
return m;
|
|
49
|
+
const out = attr.toLowerCase() === "href" ? fn.link(v, true) : fn.asset(v, true);
|
|
50
|
+
return `${sp}${attr}${eq}"${escAttr(out)}"`;
|
|
51
|
+
}));
|
|
52
|
+
// A node reached twice (shared by a cache) is rewritten once
|
|
53
|
+
const seen = new WeakSet();
|
|
54
|
+
const seenProps = new WeakSet();
|
|
55
|
+
const walk = (node) => {
|
|
56
|
+
if (node && typeof node === "object") {
|
|
57
|
+
if (seen.has(node))
|
|
58
|
+
return;
|
|
59
|
+
seen.add(node);
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(node)) {
|
|
62
|
+
for (const n of node)
|
|
63
|
+
walk(n);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!node || typeof node !== "object")
|
|
67
|
+
return;
|
|
68
|
+
const o = node;
|
|
69
|
+
if (o.type === "endpoint") {
|
|
70
|
+
// Its Markdown descriptions are rewritten in a copy of its props
|
|
71
|
+
const props = structuredClone(o.props);
|
|
72
|
+
rewriteEndpointMarkdown(props, fn.link);
|
|
73
|
+
set(o, "props", props);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (typeof o.href === "string")
|
|
77
|
+
set(o, "href", fn.link(o.href));
|
|
78
|
+
if (typeof o.src === "string")
|
|
79
|
+
set(o, "src", fn.asset(o.src));
|
|
80
|
+
if (typeof o.poster === "string")
|
|
81
|
+
set(o, "poster", fn.asset(o.poster));
|
|
82
|
+
if (typeof o.icon === "string" && IMAGE_FILE.test(o.icon))
|
|
83
|
+
set(o, "icon", fn.asset(o.icon));
|
|
84
|
+
// A code block keeps its text in `html` (the dogsbay-md parser), as math and diagram sources
|
|
85
|
+
// do: it is code shown, never markup, so its `href="…"` stays as written (review: every
|
|
86
|
+
// copy's HTML samples were rewritten)
|
|
87
|
+
if (typeof o.html === "string" && !TEXT_HTML.has(String(o.type)))
|
|
88
|
+
set(o, "html", html(o.html));
|
|
89
|
+
const mdProps = typeof o.type === "string" && Object.hasOwn(MARKDOWN_PROPS, o.type) ? MARKDOWN_PROPS[o.type] : undefined;
|
|
90
|
+
// a props object two nodes share is rewritten once (it was twice: `/x//x/b`, review)
|
|
91
|
+
if (mdProps && o.props && typeof o.props === "object" && !seenProps.has(o.props)) {
|
|
92
|
+
seenProps.add(o.props);
|
|
93
|
+
const props = o.props;
|
|
94
|
+
for (const key of mdProps) {
|
|
95
|
+
const v = props[key];
|
|
96
|
+
if (typeof v === "string")
|
|
97
|
+
set(props, key, rewriteMarkdownLinks(v, (u) => (IMAGE_FILE.test(u.split(/[?#]/)[0]) ? fn.asset(u) : fn.link(u)), { local: true }));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const v of Object.values(o))
|
|
101
|
+
if (v && typeof v === "object")
|
|
102
|
+
walk(v);
|
|
103
|
+
};
|
|
104
|
+
const restore = () => {
|
|
105
|
+
for (let i = undo.length - 1; i >= 0; i--)
|
|
106
|
+
undo[i][0][undo[i][1]] = undo[i][2];
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
walk(tree);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
restore();
|
|
113
|
+
throw e;
|
|
114
|
+
}
|
|
115
|
+
return restore;
|
|
116
|
+
}
|
|
117
|
+
function unescAttr(v) {
|
|
118
|
+
// Numeric entities too, so `&` is not escaped again into a literal `&` (review)
|
|
119
|
+
return v.replace(/&(amp|quot|lt|gt|#(\d{1,7})|#x([0-9a-f]{1,6}));/gi, (m, e, dec, hex) => {
|
|
120
|
+
if (dec !== undefined || hex !== undefined) {
|
|
121
|
+
const cp = dec !== undefined ? Number(dec) : parseInt(hex, 16);
|
|
122
|
+
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m;
|
|
123
|
+
}
|
|
124
|
+
return { amp: "&", quot: '"', lt: "<", gt: ">" }[e.toLowerCase()];
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/** A value for a raw HTML attribute in double quotes. */
|
|
128
|
+
export function escAttr(v) {
|
|
129
|
+
return v.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/site-files",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.156",
|
|
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",
|