@dogsbay/site-files 0.2.0-beta.142 → 0.2.0-beta.143

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
@@ -14,3 +14,4 @@ export * from "./hreflang.js";
14
14
  export * from "./translations.js";
15
15
  export * from "./namespaces.js";
16
16
  export * from "./mounts.js";
17
+ export * from "./markdown-links.js";
package/dist/index.js CHANGED
@@ -14,3 +14,4 @@ export * from "./hreflang.js";
14
14
  export * from "./translations.js";
15
15
  export * from "./namespaces.js";
16
16
  export * from "./mounts.js";
17
+ export * from "./markdown-links.js";
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Links inside Markdown TEXT that a tree carries as a string prop, not as nodes: an OpenAPI
3
+ * `endpoint` keeps its description, and its parameters', request and response fields' and
4
+ * responses' descriptions, as Markdown that is rendered later. The ref rewrites (`mountRefs`,
5
+ * `rewriteContentRefs`) walk `href` fields and raw HTML, so without this they never saw those
6
+ * links, and a link from one operation to another missed the mount, version and namespace
7
+ * prefixes (the SSG team, 2026-10-28).
8
+ */
9
+ /**
10
+ * Rewrites each root-relative destination (`/…`, not `//host`) in Markdown text with `fn`:
11
+ * inline links and images `[text](/x "title")` / `[text](</x>)`, reference definitions
12
+ * `[id]: /x`, and `<a href="/x">`. Code spans and fenced code are left as written.
13
+ */
14
+ export declare function rewriteMarkdownLinks(md: string, fn: (url: string) => string): string;
15
+ /**
16
+ * The Markdown an `endpoint` node holds, rewritten with `fn`: the operation's `description`, its
17
+ * parameters', request fields' (nested through `properties`/`items`) and responses' and their
18
+ * fields'. Only those: an example value is the API's data and is published as written, even
19
+ * where it has a `description` or `href` key of its own. Changes `props` in place.
20
+ */
21
+ export declare function rewriteEndpointMarkdown(props: unknown, fn: (url: string) => string): void;
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Links inside Markdown TEXT that a tree carries as a string prop, not as nodes: an OpenAPI
3
+ * `endpoint` keeps its description, and its parameters', request and response fields' and
4
+ * responses' descriptions, as Markdown that is rendered later. The ref rewrites (`mountRefs`,
5
+ * `rewriteContentRefs`) walk `href` fields and raw HTML, so without this they never saw those
6
+ * links, and a link from one operation to another missed the mount, version and namespace
7
+ * prefixes (the SSG team, 2026-10-28).
8
+ */
9
+ /**
10
+ * Rewrites each root-relative destination (`/…`, not `//host`) in Markdown text with `fn`:
11
+ * inline links and images `[text](/x "title")` / `[text](</x>)`, reference definitions
12
+ * `[id]: /x`, and `<a href="/x">`. Code spans and fenced code are left as written.
13
+ */
14
+ export function rewriteMarkdownLinks(md, fn) {
15
+ if (!md.includes("/"))
16
+ return md;
17
+ let out = "";
18
+ let last = 0;
19
+ for (const [start, end] of codeRanges(md)) {
20
+ out += rewriteText(md.slice(last, start), fn) + md.slice(start, end);
21
+ last = end;
22
+ }
23
+ return out + rewriteText(md.slice(last), fn);
24
+ }
25
+ const ROOTED = String.raw `\/(?!\/)`;
26
+ // Each alternative is bounded or anchored, so a hostile description cannot make it backtrack
27
+ const LINK_REF = new RegExp([
28
+ // An inline link or image destination
29
+ String.raw `(?<lh>(?<!\\)\]\([ \t]*(?:\n[ \t]*)?<?)(?<lu>${ROOTED}[^\s)>]*)`,
30
+ // <a href="/x">
31
+ String.raw `(?<ah><a\b[^>]{0,200}?\bhref[ \t\n]*=[ \t\n]*)(?:(?<aq>["'])(?<au>${ROOTED}[^"'\n]*)\k<aq>|(?<bu>${ROOTED}[^\s"'<>=\x60]*))`,
32
+ // A reference definition
33
+ String.raw `(?<dh>^ {0,3}\[[^\]\n]{1,999}\]:[ \t]*<?)(?<du>${ROOTED}[^\s>]*)`,
34
+ ].join("|"), "gim");
35
+ function rewriteText(text, fn) {
36
+ if (!text.includes("/"))
37
+ return text;
38
+ return text.replace(LINK_REF, (match, ...args) => {
39
+ const g = args[args.length - 1];
40
+ if (g.lh !== undefined)
41
+ return `${g.lh}${fn(g.lu)}`;
42
+ if (g.ah !== undefined)
43
+ return g.au !== undefined ? `${g.ah}${g.aq}${fn(g.au)}${g.aq}` : `${g.ah}${fn(g.bu)}`;
44
+ if (g.dh !== undefined)
45
+ return `${g.dh}${fn(g.du)}`;
46
+ return match;
47
+ });
48
+ }
49
+ /**
50
+ * The code in Markdown text, as [start, end) ranges in order: fenced blocks (to the closing fence
51
+ * of the same character, at least as long, or the end) and, outside them, code spans (a backtick
52
+ * run to the next run of the same length). One pass over lines and one over backtick runs.
53
+ */
54
+ function codeRanges(md) {
55
+ const ranges = [];
56
+ const lineRe = /[^\n]*(?:\n|$)/g;
57
+ let fence;
58
+ let textStart = 0;
59
+ for (let m = lineRe.exec(md); m && m[0] !== ""; m = lineRe.exec(md)) {
60
+ const line = m[0];
61
+ const open = /^ {0,3}(`{3,}|~{3,})/.exec(line);
62
+ if (fence) {
63
+ const close = open && open[1][0] === fence.char && open[1].length >= fence.len && /^ {0,3}[`~]+[ \t]*\r?\n?$/.test(line);
64
+ if (close) {
65
+ ranges.push([fence.start, m.index + line.length]);
66
+ fence = undefined;
67
+ textStart = m.index + line.length;
68
+ }
69
+ }
70
+ else if (open && !(open[1][0] === "`" && line.slice(open[0].length).includes("`"))) {
71
+ ranges.push(...spanRanges(md, textStart, m.index));
72
+ fence = { char: open[1][0], len: open[1].length, start: m.index };
73
+ }
74
+ }
75
+ if (fence)
76
+ ranges.push([fence.start, md.length]);
77
+ else
78
+ ranges.push(...spanRanges(md, textStart, md.length));
79
+ return ranges;
80
+ }
81
+ function spanRanges(md, from, to) {
82
+ const runs = [];
83
+ const re = /`+/g;
84
+ re.lastIndex = from;
85
+ for (let m = re.exec(md); m && m.index < to; m = re.exec(md))
86
+ runs.push({ at: m.index, len: Math.min(m[0].length, to - m.index) });
87
+ // For each run, the index of the next run of the same length, found from the end
88
+ const next = new Array(runs.length).fill(-1);
89
+ const seen = new Map();
90
+ for (let i = runs.length - 1; i >= 0; i--) {
91
+ next[i] = seen.get(runs[i].len) ?? -1;
92
+ seen.set(runs[i].len, i);
93
+ }
94
+ const ranges = [];
95
+ for (let i = 0; i < runs.length; i++) {
96
+ const j = next[i];
97
+ if (j === -1)
98
+ continue;
99
+ ranges.push([runs[i].at, runs[j].at + runs[j].len]);
100
+ i = j;
101
+ }
102
+ return ranges;
103
+ }
104
+ /**
105
+ * The Markdown an `endpoint` node holds, rewritten with `fn`: the operation's `description`, its
106
+ * parameters', request fields' (nested through `properties`/`items`) and responses' and their
107
+ * fields'. Only those: an example value is the API's data and is published as written, even
108
+ * where it has a `description` or `href` key of its own. Changes `props` in place.
109
+ */
110
+ export function rewriteEndpointMarkdown(props, fn) {
111
+ if (!props || typeof props !== "object")
112
+ return;
113
+ const p = props;
114
+ const desc = (o) => {
115
+ if (o && typeof o === "object" && typeof o.description === "string") {
116
+ o.description = rewriteMarkdownLinks(o.description, fn);
117
+ }
118
+ };
119
+ const fields = (list, depth = 0) => {
120
+ if (!Array.isArray(list) || depth > 64)
121
+ return;
122
+ for (const f of list) {
123
+ desc(f);
124
+ if (f && typeof f === "object") {
125
+ fields(f.properties, depth + 1);
126
+ fields(f.items, depth + 1);
127
+ }
128
+ }
129
+ };
130
+ desc(p);
131
+ if (Array.isArray(p.parameters))
132
+ p.parameters.forEach(desc);
133
+ fields(p.requestBody);
134
+ if (Array.isArray(p.responses)) {
135
+ for (const r of p.responses) {
136
+ desc(r);
137
+ if (r && typeof r === "object")
138
+ fields(r.schema);
139
+ }
140
+ }
141
+ }
package/dist/mounts.d.ts CHANGED
@@ -36,8 +36,8 @@ export declare function isMountRoot(slug: string): boolean;
36
36
  * is one of `ownSlugs` (the source's slugs before mounting; `index` or `""`
37
37
  * is the root, linked as `/`). Every other ref is left for the source's
38
38
  * prefix as usual: an image (`/_assets/x.png`) is the site's, and a link to a
39
- * site page (`/guide/x`) stays one. Covers `href` fields and `href="…"` in raw
40
- * HTML. Changes `tree` in place.
39
+ * site page (`/guide/x`) stays one. Covers `href` fields, `href="…"` in raw
40
+ * HTML and links in an endpoint's Markdown descriptions. Changes `tree` in place.
41
41
  */
42
42
  export declare function mountRefs(tree: unknown, segments: readonly string[], ownSlugs: ReadonlySet<string>): void;
43
43
  /** Where `placeMountedNav` put the API's section. */
@@ -48,10 +48,16 @@ export type MountPlacement = "at-page" | "in-section" | "top-level";
48
48
  *
49
49
  * 1. The menu has an entry for the mount page: the API's sections become
50
50
  * that entry's children, after any it has.
51
- * 2. Otherwise, at the end of the deepest section with an entry from the
52
- * mount's parent folder (`reference/` for `reference/rest-api`), the
53
- * first in menu order when several are as deep: an entry labelled
54
- * `label`, linking to the mount, holding the API's sections.
51
+ * 2. Otherwise, at the end of the deepest section holding the mount's parent
52
+ * folder (`reference/` for `reference/rest-api`), the first in menu order
53
+ * when several are as deep: an entry labelled `label`, linking to the
54
+ * mount, holding the API's sections. A section holds the folder when it
55
+ * is the folder's own page (`reference`) or has an entry DIRECTLY in it
56
+ * (`reference/cli`, not `reference/cli/datastore/migrate`: a section of
57
+ * sub-folder pages is the sub-folder's, and was chosen as the deepest,
58
+ * the playground's Calico 3.33). When no section holds it that way (a
59
+ * category with no page whose sections are all sub-folders), the
60
+ * shallowest section with pages under the folder.
55
61
  * 3. Otherwise (a one-segment mount, or no such section), the same entry at
56
62
  * the end of the menu.
57
63
  *
package/dist/mounts.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { rewriteEndpointMarkdown } from "./markdown-links.js";
1
2
  /** A mount path's segments, or why it cannot be one. */
2
3
  export function parseMount(mount) {
3
4
  if (typeof mount !== "string")
@@ -37,8 +38,8 @@ export function isMountRoot(slug) {
37
38
  * is one of `ownSlugs` (the source's slugs before mounting; `index` or `""`
38
39
  * is the root, linked as `/`). Every other ref is left for the source's
39
40
  * prefix as usual: an image (`/_assets/x.png`) is the site's, and a link to a
40
- * site page (`/guide/x`) stays one. Covers `href` fields and `href="…"` in raw
41
- * HTML. Changes `tree` in place.
41
+ * site page (`/guide/x`) stays one. Covers `href` fields, `href="…"` in raw
42
+ * HTML and links in an endpoint's Markdown descriptions. Changes `tree` in place.
42
43
  */
43
44
  export function mountRefs(tree, segments, ownSlugs) {
44
45
  const base = segments.join("/");
@@ -68,6 +69,12 @@ export function mountRefs(tree, segments, ownSlugs) {
68
69
  if (typeof obj.html === "string") {
69
70
  obj.html = obj.html.replace(/\bhref=("|')(.*?)\1/gi, (_m, q, val) => `href=${q}${move(val)}${q}`);
70
71
  }
72
+ // An operation's descriptions are Markdown text, rendered later
73
+ // (and its props are the API's data otherwise: an example's `href` is not a link of the site's)
74
+ if (obj.type === "endpoint") {
75
+ rewriteEndpointMarkdown(obj.props, move);
76
+ return;
77
+ }
71
78
  for (const key of Object.keys(obj)) {
72
79
  const val = obj[key];
73
80
  if (val && typeof val === "object")
@@ -89,10 +96,16 @@ function hrefKey(href) {
89
96
  *
90
97
  * 1. The menu has an entry for the mount page: the API's sections become
91
98
  * that entry's children, after any it has.
92
- * 2. Otherwise, at the end of the deepest section with an entry from the
93
- * mount's parent folder (`reference/` for `reference/rest-api`), the
94
- * first in menu order when several are as deep: an entry labelled
95
- * `label`, linking to the mount, holding the API's sections.
99
+ * 2. Otherwise, at the end of the deepest section holding the mount's parent
100
+ * folder (`reference/` for `reference/rest-api`), the first in menu order
101
+ * when several are as deep: an entry labelled `label`, linking to the
102
+ * mount, holding the API's sections. A section holds the folder when it
103
+ * is the folder's own page (`reference`) or has an entry DIRECTLY in it
104
+ * (`reference/cli`, not `reference/cli/datastore/migrate`: a section of
105
+ * sub-folder pages is the sub-folder's, and was chosen as the deepest,
106
+ * the playground's Calico 3.33). When no section holds it that way (a
107
+ * category with no page whose sections are all sub-folders), the
108
+ * shallowest section with pages under the folder.
96
109
  * 3. Otherwise (a one-segment mount, or no such section), the same entry at
97
110
  * the end of the menu.
98
111
  *
@@ -126,28 +139,37 @@ export function placeMountedNav(host, mounted, mountHref, label, mount) {
126
139
  return { nav, placement: "at-page" };
127
140
  }
128
141
  const entry = { label, href: mountHref, children: sections };
129
- // 2. The deepest section holding an entry from the parent folder: only a
142
+ // 2. The deepest section holding the parent folder (its page, or an entry
143
+ // directly in it, not in a sub-folder): only a
130
144
  // mount of two segments or more has one (`/3.32/rest-api`'s `/3.32` is the
131
145
  // version, not a folder of the mount's).
132
146
  const cut = mountKey.lastIndexOf("/");
133
147
  const parent = cut > 0 ? mountKey.slice(0, cut) : "";
134
148
  if (parent && mount.split("/").filter(Boolean).length > 1) {
135
149
  let best;
150
+ // A folder with no page of its own whose sections are all sub-folders
151
+ // (a Docusaurus category): the shallowest section with pages under it
152
+ let outer;
153
+ const under = (items) => items.some((c) => hrefKey(c.href)?.startsWith(`${parent}/`) || (c.children ? under(c.children) : false));
136
154
  const visit = (items, depth) => {
137
155
  for (const item of items) {
138
156
  const children = item.children;
139
157
  if (!children || children.length === 0)
140
158
  continue;
141
- const holds = children.some((c) => {
142
- const k = hrefKey(c.href);
143
- return k !== undefined && (k === parent || k.startsWith(`${parent}/`));
144
- });
159
+ const holds = hrefKey(item.href) === parent ||
160
+ children.some((c) => {
161
+ const k = hrefKey(c.href);
162
+ return k !== undefined && (k === parent || (k.startsWith(`${parent}/`) && !k.slice(parent.length + 1).includes("/")));
163
+ });
145
164
  if (holds && (!best || depth > best.depth))
146
165
  best = { items: children, depth };
166
+ if (!holds && (!outer || depth < outer.depth) && under(children))
167
+ outer = { items: children, depth };
147
168
  visit(children, depth + 1);
148
169
  }
149
170
  };
150
171
  visit(nav, 0);
172
+ best ??= outer;
151
173
  if (best) {
152
174
  best.items.push(entry);
153
175
  return { nav, placement: "in-section" };
@@ -64,7 +64,8 @@ export declare function namespaceNavGroup<T extends PrefixableNavItem>(ns: strin
64
64
  * links and images resolve under the same `/<segs>/...` prefix as the
65
65
  * pages themselves. Covers page links (`href`) and asset refs (`src`),
66
66
  * in both markdown nodes (`src`/`href` fields) and raw-HTML nodes
67
- * (`href="..."`/`src="..."` inside an `html` string).
67
+ * (`href="..."`/`src="..."` inside an `html` string), and links in an OpenAPI
68
+ * endpoint's Markdown descriptions (markdown-links.ts).
68
69
  *
69
70
  * Refs at this pipeline stage are ROOT-RELATIVE and carry NO basePath
70
71
  * (importers resolve to `/foo`; basePath is composed downstream), so the
@@ -11,6 +11,7 @@
11
11
  * Naming one source never moves another's URLs: an unnamed source stays flat
12
12
  * beside named siblings. A single source, named or not, gets no segment.
13
13
  */
14
+ import { rewriteEndpointMarkdown } from "./markdown-links.js";
14
15
  /** A source's namespace: its `name:`, or undefined (flat URLs). */
15
16
  export function sourceNamespace(source) {
16
17
  return source.name;
@@ -94,7 +95,8 @@ export function namespaceNavGroup(ns, nav) {
94
95
  * links and images resolve under the same `/<segs>/...` prefix as the
95
96
  * pages themselves. Covers page links (`href`) and asset refs (`src`),
96
97
  * in both markdown nodes (`src`/`href` fields) and raw-HTML nodes
97
- * (`href="..."`/`src="..."` inside an `html` string).
98
+ * (`href="..."`/`src="..."` inside an `html` string), and links in an OpenAPI
99
+ * endpoint's Markdown descriptions (markdown-links.ts).
98
100
  *
99
101
  * Refs at this pipeline stage are ROOT-RELATIVE and carry NO basePath
100
102
  * (importers resolve to `/foo`; basePath is composed downstream), so the
@@ -154,6 +156,12 @@ export function rewriteContentRefs(tree, prefixSegs, globalPrefixes) {
154
156
  obj.href = prefix(obj.href);
155
157
  if (typeof obj.html === "string")
156
158
  obj.html = rewriteHtml(obj.html);
159
+ // An operation's descriptions are Markdown text, rendered later
160
+ // (and its props are the API's data otherwise: an example's `href` is not a link of the site's)
161
+ if (obj.type === "endpoint") {
162
+ rewriteEndpointMarkdown(obj.props, prefix);
163
+ return;
164
+ }
157
165
  // `::child-links{folder=…}` is a site path too: on a versioned or
158
166
  // localized site it names a folder in THIS source's namespace.
159
167
  if (obj.type === "child-links" && obj.props && typeof obj.props.folder === "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.142",
3
+ "version": "0.2.0-beta.143",
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",