@dogsbay/site-files 0.2.0-beta.158 → 0.2.0-beta.160

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.
@@ -50,7 +50,35 @@ export declare function prefixSlug(prefix: readonly string[], slug: string): str
50
50
  */
51
51
  export declare function prefixNavHrefs<T extends PrefixableNavItem>(items: readonly T[], prefix: readonly string[], basePath: string,
52
52
  /** A file of `content.static` (a site path without the base path): its link is not this source's. */
53
- isSiteFile?: (path: string) => boolean): T[];
53
+ isSiteFile?: (path: string) => boolean,
54
+ /** The page's language and version: a menu entry into another language (`localeLink`). */
55
+ locales?: LocaleLinkAxes): T[];
56
+ /**
57
+ * A page's language and version, as its URL segments carry them (product, LANGUAGE, VERSION: the
58
+ * last two, when present), and the site's: what a root link starting with a language id means.
59
+ */
60
+ export interface LocaleLinkAxes {
61
+ /** The page's language id: its URL segment. Unset for a page in no language (flat URLs). */
62
+ locale?: string;
63
+ /** Every language id of the site (of the product). */
64
+ localeIds: ReadonlySet<string>;
65
+ /** The page's version segment; unset for the root version (no segment). */
66
+ versionSegment?: string;
67
+ /** Every version URL segment of the site (of the product). */
68
+ versionSegments: ReadonlySet<string>;
69
+ /** A version written by its id → its URL segment ("" for the root version), where they differ. */
70
+ versionPaths?: ReadonlyMap<string, string>;
71
+ }
72
+ /**
73
+ * A root link into a language, as both pipelines read it (agreed with dogsbay-ssg, 2026-11-03):
74
+ * `/fr/x` on a German page is the same page in French, in the page's own product and VERSION —
75
+ * on `/calico/de/1/x`, `/fr/x` is `/calico/fr/1/x` — as `/x` is a page of the link's own version.
76
+ * A version after the language is the link's own and kept (`/fr/2/x`; one written by its id gets
77
+ * its URL segment, the root version none). A page in no language puts the language before its
78
+ * version. `/fr` alone gets its trailing slash. Undefined when the link does not start with a
79
+ * language id (or the page's segments do not end in its language).
80
+ */
81
+ export declare function localeLink(u: string, prefixSegs: readonly string[], axes: LocaleLinkAxes): string | undefined;
54
82
  /**
55
83
  * The URL segments that name a product: every source's `name:` when namespaces are active,
56
84
  * else none. A root-relative link starting with one already names its product
@@ -103,4 +131,6 @@ export declare function staticFileMatcher(files: ReadonlySet<string>): (path: st
103
131
  */
104
132
  export declare function rewriteContentRefs(tree: unknown, prefixSegs: readonly string[], globalPrefixes: ReadonlySet<string>,
105
133
  /** A file of `content.static`, served as it is at the site root: a ref to it is not this source's. */
106
- isSiteFile?: (path: string) => boolean): void;
134
+ isSiteFile?: (path: string) => boolean,
135
+ /** The page's language and version, for links into another language (`localeLink`). */
136
+ locales?: LocaleLinkAxes): void;
@@ -62,7 +62,9 @@ export function prefixSlug(prefix, slug) {
62
62
  */
63
63
  export function prefixNavHrefs(items, prefix, basePath,
64
64
  /** A file of `content.static` (a site path without the base path): its link is not this source's. */
65
- isSiteFile) {
65
+ isSiteFile,
66
+ /** The page's language and version: a menu entry into another language (`localeLink`). */
67
+ locales) {
66
68
  if (prefix.length === 0)
67
69
  return items.map((item) => ({ ...item }));
68
70
  const match = basePath ? `${basePath}/` : "/";
@@ -73,6 +75,9 @@ isSiteFile) {
73
75
  return h;
74
76
  if (isSiteFile?.(`/${h.slice(match.length).split(/[#?]/)[0]}`))
75
77
  return h;
78
+ const other = locales ? localeLink(`/${h.slice(match.length)}`, prefix, locales) : undefined;
79
+ if (other !== undefined)
80
+ return `${basePath}${other}`;
76
81
  return `${match}${prefix.join("/")}/${h.slice(match.length)}`;
77
82
  };
78
83
  return items.map((item) => {
@@ -80,11 +85,58 @@ isSiteFile) {
80
85
  if (item.href !== undefined)
81
86
  next.href = href(item.href);
82
87
  if (item.children && item.children.length > 0) {
83
- next.children = prefixNavHrefs(item.children, prefix, basePath, isSiteFile);
88
+ next.children = prefixNavHrefs(item.children, prefix, basePath, isSiteFile, locales);
84
89
  }
85
90
  return next;
86
91
  });
87
92
  }
93
+ /**
94
+ * A root link into a language, as both pipelines read it (agreed with dogsbay-ssg, 2026-11-03):
95
+ * `/fr/x` on a German page is the same page in French, in the page's own product and VERSION —
96
+ * on `/calico/de/1/x`, `/fr/x` is `/calico/fr/1/x` — as `/x` is a page of the link's own version.
97
+ * A version after the language is the link's own and kept (`/fr/2/x`; one written by its id gets
98
+ * its URL segment, the root version none). A page in no language puts the language before its
99
+ * version. `/fr` alone gets its trailing slash. Undefined when the link does not start with a
100
+ * language id (or the page's segments do not end in its language).
101
+ */
102
+ export function localeLink(u, prefixSegs, axes) {
103
+ if (!u.startsWith("/") || u.startsWith("//"))
104
+ return undefined;
105
+ const first = u.slice(1).split(/[/#?]/)[0];
106
+ if (!first || !axes.localeIds.has(first))
107
+ return undefined;
108
+ const hasVersion = axes.versionSegment !== undefined;
109
+ const out = [...prefixSegs];
110
+ let versionAt = hasVersion ? out.length - 1 : -1;
111
+ if (axes.locale !== undefined) {
112
+ const localeAt = out.length - (hasVersion ? 2 : 1);
113
+ if (localeAt < 0 || out[localeAt] !== axes.locale)
114
+ return undefined;
115
+ out[localeAt] = first;
116
+ }
117
+ else {
118
+ // a page in no language: the language goes where it would stand, before the version
119
+ const at = hasVersion ? out.length - 1 : out.length;
120
+ out.splice(at, 0, first);
121
+ if (hasVersion)
122
+ versionAt = at + 1;
123
+ }
124
+ let rest = u.slice(1 + first.length);
125
+ const second = rest.startsWith("/") ? rest.slice(1).split(/[/#?]/)[0] : "";
126
+ const path = second ? axes.versionPaths?.get(second) : undefined;
127
+ if (second && (axes.versionSegments.has(second) || path !== undefined)) {
128
+ // the link names its version: the page's is not added
129
+ if (versionAt >= 0)
130
+ out.splice(versionAt, 1);
131
+ if (path !== undefined && path !== second) {
132
+ const after = rest.slice(1 + second.length);
133
+ rest = path ? `/${path}${after}` : after;
134
+ }
135
+ }
136
+ if (rest === "" || rest[0] === "?" || rest[0] === "#")
137
+ rest = `/${rest}`;
138
+ return `/${out.join("/")}${rest}`;
139
+ }
88
140
  /**
89
141
  * The URL segments that name a product: every source's `name:` when namespaces are active,
90
142
  * else none. A root-relative link starting with one already names its product
@@ -152,7 +204,9 @@ export function staticFileMatcher(files) {
152
204
  */
153
205
  export function rewriteContentRefs(tree, prefixSegs, globalPrefixes,
154
206
  /** A file of `content.static`, served as it is at the site root: a ref to it is not this source's. */
155
- isSiteFile) {
207
+ isSiteFile,
208
+ /** The page's language and version, for links into another language (`localeLink`). */
209
+ locales) {
156
210
  if (prefixSegs.length === 0)
157
211
  return;
158
212
  const seg = prefixSegs.join("/");
@@ -174,6 +228,10 @@ isSiteFile) {
174
228
  const firstSeg = u.slice(1).split(/[/#?]/)[0];
175
229
  if (globalPrefixes.has(firstSeg))
176
230
  return u;
231
+ // A link into a language (`/fr/x`): the same place in that language
232
+ const other = locales ? localeLink(u, prefixSegs, locales) : undefined;
233
+ if (other !== undefined)
234
+ return other;
177
235
  if (isSiteFile?.(u.split(/[#?]/)[0]))
178
236
  return u;
179
237
  return `/${seg}${u}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.158",
3
+ "version": "0.2.0-beta.160",
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",