@dogsbay/site-files 0.2.0-beta.155 → 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/markdown-links.d.ts +6 -1
- package/dist/markdown-links.js +21 -11
- package/dist/tree-links.js +36 -8
- package/package.json +1 -1
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)}`;
|
package/dist/tree-links.js
CHANGED
|
@@ -5,8 +5,20 @@
|
|
|
5
5
|
* llms-full.txt), so their links name what the site serves: moved here from dogsbay-ssg
|
|
6
6
|
* (src/core/links.ts, 7737a0d) on 2026-11-02; plans/md-mirror-links.md.
|
|
7
7
|
*/
|
|
8
|
-
import { rewriteEndpointMarkdown } from "./markdown-links.js";
|
|
8
|
+
import { rewriteEndpointMarkdown, rewriteMarkdownLinks } from "./markdown-links.js";
|
|
9
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
|
+
};
|
|
10
22
|
/** Node types whose `html` is text (code, math, a diagram's source), not markup. */
|
|
11
23
|
const TEXT_HTML = new Set(["code", "math", "math-block", "diagram", "mermaid"]);
|
|
12
24
|
/**
|
|
@@ -26,15 +38,20 @@ export function rewriteTreeLinks(tree, fn) {
|
|
|
26
38
|
o[k] = v;
|
|
27
39
|
};
|
|
28
40
|
// An attribute's value unescaped, resolved and escaped again, as render-html's rewriteHtml does
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
}));
|
|
36
52
|
// A node reached twice (shared by a cache) is rewritten once
|
|
37
53
|
const seen = new WeakSet();
|
|
54
|
+
const seenProps = new WeakSet();
|
|
38
55
|
const walk = (node) => {
|
|
39
56
|
if (node && typeof node === "object") {
|
|
40
57
|
if (seen.has(node))
|
|
@@ -69,6 +86,17 @@ export function rewriteTreeLinks(tree, fn) {
|
|
|
69
86
|
// copy's HTML samples were rewritten)
|
|
70
87
|
if (typeof o.html === "string" && !TEXT_HTML.has(String(o.type)))
|
|
71
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
|
+
}
|
|
72
100
|
for (const v of Object.values(o))
|
|
73
101
|
if (v && typeof v === "object")
|
|
74
102
|
walk(v);
|
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",
|