@dogsbay/site-files 0.2.0-beta.169 → 0.2.0-beta.171

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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * A blog's posts in llms.txt (and llms-full.txt): newest first, as the blog's index lists them,
3
+ * when the site has no nav file and no post sets its own order. The ONE rule, for the Astro build
4
+ * (format-astro) and dogsbay-ssg (plans/ssg-astro-parity.md there): an auto nav lists posts by
5
+ * title or file name, which is no blog's order. Browser/Worker-safe.
6
+ */
7
+ /**
8
+ * The nav with its posts reordered: each post takes the place of the one before it in `rank`
9
+ * order (lower first), so other entries keep their places, and each folder is ordered on its own.
10
+ * `rank` names a post's place in the post list (newest first) and is undefined for anything else.
11
+ * Each host matches its own nav entries to posts (format-astro by href, dogsbay-ssg by slug).
12
+ */
13
+ export declare function postsNewestFirst<N extends {
14
+ children?: readonly N[];
15
+ }>(nav: readonly N[], rank: (entry: N) => number | undefined): N[];
16
+ /**
17
+ * Whether an author ordered the pages in front matter, by a key an auto nav sorts by: then the nav's
18
+ * order is theirs and stands. `sidebar.order` (format-dogsbay-md's auto nav, the key docs/dogsbay-md.md
19
+ * documents) and `order` / `weight` (dogsbay-ssg's). Not `sidebar_position` (Docusaurus): no auto nav
20
+ * sorts by it, so counting it would keep a nav in file-name or title order, which is neither the
21
+ * author's order nor newest first.
22
+ */
23
+ export declare function authorOrdered(frontmatters: Iterable<Record<string, unknown> | undefined>): boolean;
24
+ /** Whether llms.txt lists the posts newest first: a blog, with no nav file, whose posts set no order. */
25
+ export declare function postsInDateOrder(o: {
26
+ blog: boolean;
27
+ navFromFile: boolean;
28
+ frontmatters: Iterable<Record<string, unknown> | undefined>;
29
+ }): boolean;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * A blog's posts in llms.txt (and llms-full.txt): newest first, as the blog's index lists them,
3
+ * when the site has no nav file and no post sets its own order. The ONE rule, for the Astro build
4
+ * (format-astro) and dogsbay-ssg (plans/ssg-astro-parity.md there): an auto nav lists posts by
5
+ * title or file name, which is no blog's order. Browser/Worker-safe.
6
+ */
7
+ /**
8
+ * The nav with its posts reordered: each post takes the place of the one before it in `rank`
9
+ * order (lower first), so other entries keep their places, and each folder is ordered on its own.
10
+ * `rank` names a post's place in the post list (newest first) and is undefined for anything else.
11
+ * Each host matches its own nav entries to posts (format-astro by href, dogsbay-ssg by slug).
12
+ */
13
+ export function postsNewestFirst(nav, rank) {
14
+ const walk = (items) => {
15
+ const posts = items.filter((n) => rank(n) !== undefined).sort((a, b) => rank(a) - rank(b));
16
+ let i = 0;
17
+ return items.map((n) => {
18
+ const placed = rank(n) !== undefined ? posts[i++] : n;
19
+ return placed.children?.length ? { ...placed, children: walk(placed.children) } : placed;
20
+ });
21
+ };
22
+ return walk(nav);
23
+ }
24
+ /**
25
+ * Whether an author ordered the pages in front matter, by a key an auto nav sorts by: then the nav's
26
+ * order is theirs and stands. `sidebar.order` (format-dogsbay-md's auto nav, the key docs/dogsbay-md.md
27
+ * documents) and `order` / `weight` (dogsbay-ssg's). Not `sidebar_position` (Docusaurus): no auto nav
28
+ * sorts by it, so counting it would keep a nav in file-name or title order, which is neither the
29
+ * author's order nor newest first.
30
+ */
31
+ export function authorOrdered(frontmatters) {
32
+ for (const fm of frontmatters) {
33
+ if (!fm)
34
+ continue;
35
+ const sidebar = fm.sidebar;
36
+ if (fm.order != null || fm.weight != null)
37
+ return true;
38
+ if (sidebar && typeof sidebar === "object" && sidebar.order != null)
39
+ return true;
40
+ }
41
+ return false;
42
+ }
43
+ /** Whether llms.txt lists the posts newest first: a blog, with no nav file, whose posts set no order. */
44
+ export function postsInDateOrder(o) {
45
+ return o.blog && !o.navFromFile && !authorOrdered(o.frontmatters);
46
+ }
package/dist/index.d.ts CHANGED
@@ -23,3 +23,5 @@ export * from "./host-files.js";
23
23
  export * from "./inline-markdown.js";
24
24
  export * from "./site-chrome.js";
25
25
  export * from "./facets.js";
26
+ export * from "./blog-order.js";
27
+ export * from "./nav-sections.js";
package/dist/index.js CHANGED
@@ -23,3 +23,5 @@ export * from "./host-files.js";
23
23
  export * from "./inline-markdown.js";
24
24
  export * from "./site-chrome.js";
25
25
  export * from "./facets.js";
26
+ export * from "./blog-order.js";
27
+ export * from "./nav-sections.js";
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Section-scoped navigation (`build.navScope: section`): each page's sidebar
3
+ * shows only its own section, as Nimbus's `sidebar.scope: "section"` does for
4
+ * Cloudflare's docs. One implementation, so the Astro pipeline and dogsbay-ssg
5
+ * pick the same section, sidebar entries and Previous / Next for a page.
6
+ *
7
+ * - The SECTIONS are the top-level entries of the nav the page is shown with:
8
+ * the nav AFTER the version / language / product filter, so they are worked
9
+ * out per unit. A nav that is one group (a product's nav, wrapped in its
10
+ * product's entry) is opened: its children are the sections.
11
+ * - A page's section is the first section that holds it (its own link or any
12
+ * link below it).
13
+ * - The home page lists the sections instead, as does any page in no
14
+ * section. Home is `home` (the site's), and a top-level page with no
15
+ * children whose path holds every other entry's link: the landing page of
16
+ * a version or language (`/v1/` beside `/v1/guide/…`). Neither is a section.
17
+ * - Previous / Next walk the scoped entries, so they never cross a section.
18
+ * The home page's list holds no link to itself, so it has neither.
19
+ *
20
+ * Hrefs are compared after `normalizeNavHref` (or the caller's own rule):
21
+ * the Astro pipeline links `/zaraz/`, dogsbay-ssg `zaraz.html`; each passes
22
+ * the current page in the same form as its nav. See plans/nav-scope.md.
23
+ */
24
+ export type NavScope = "site" | "section";
25
+ /** The nav shape both pipelines read: label, optional link, children. */
26
+ export interface SectionNavItem {
27
+ label: string;
28
+ href?: string;
29
+ children?: SectionNavItem[];
30
+ }
31
+ export interface NavScopeOptions {
32
+ /** Compare hrefs in this form. Default: `normalizeNavHref`. */
33
+ normalize?: (href: string) => string;
34
+ /** The site's home page. It lists the sections even when the nav links it. */
35
+ home?: string;
36
+ }
37
+ /** Drop a query or fragment and a trailing slash; the root stays `/`. */
38
+ export declare function normalizeNavHref(href: string): string;
39
+ /** The index of the section holding `current` (in the opened level), or -1. */
40
+ export declare function sectionIndex(nav: readonly SectionNavItem[], current: string, opts?: NavScopeOptions): number;
41
+ /**
42
+ * A section's sidebar entries: its children, with the section's own page
43
+ * first when no child links it. A top-level page with no children is a
44
+ * section of one page.
45
+ */
46
+ export declare function sectionItems<N extends SectionNavItem>(section: N, opts?: NavScopeOptions): N[];
47
+ /** One entry per section that has a page to open: its own, else its first. */
48
+ export interface SectionEntry {
49
+ label: string;
50
+ href: string;
51
+ /** Position in the opened level, as `sectionIndex` gives it. */
52
+ index: number;
53
+ }
54
+ export declare function sectionEntries(nav: readonly SectionNavItem[], opts?: NavScopeOptions): SectionEntry[];
55
+ export interface ScopedNav<N extends SectionNavItem> {
56
+ /** What the sidebar shows, and what Previous / Next walk. */
57
+ items: N[];
58
+ /** The page's section (`sectionIndex`); -1 with scope `site` or for a page in none. */
59
+ section: number;
60
+ }
61
+ /**
62
+ * The sidebar for a page. Scope `site` (or unset) is the whole nav; scope
63
+ * `section` is the page's section, or the list of sections for a page in none.
64
+ */
65
+ export declare function scopeNav<N extends SectionNavItem>(nav: readonly N[], current: string, scope: NavScope | undefined, opts?: NavScopeOptions): ScopedNav<N>;
66
+ /** The section menu's data: every section, and the page's own when it has one. */
67
+ export interface SectionMenu {
68
+ entries: SectionEntry[];
69
+ current?: SectionEntry;
70
+ }
71
+ export declare function sectionMenu(nav: readonly SectionNavItem[], current: string, opts?: NavScopeOptions): SectionMenu;
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Section-scoped navigation (`build.navScope: section`): each page's sidebar
3
+ * shows only its own section, as Nimbus's `sidebar.scope: "section"` does for
4
+ * Cloudflare's docs. One implementation, so the Astro pipeline and dogsbay-ssg
5
+ * pick the same section, sidebar entries and Previous / Next for a page.
6
+ *
7
+ * - The SECTIONS are the top-level entries of the nav the page is shown with:
8
+ * the nav AFTER the version / language / product filter, so they are worked
9
+ * out per unit. A nav that is one group (a product's nav, wrapped in its
10
+ * product's entry) is opened: its children are the sections.
11
+ * - A page's section is the first section that holds it (its own link or any
12
+ * link below it).
13
+ * - The home page lists the sections instead, as does any page in no
14
+ * section. Home is `home` (the site's), and a top-level page with no
15
+ * children whose path holds every other entry's link: the landing page of
16
+ * a version or language (`/v1/` beside `/v1/guide/…`). Neither is a section.
17
+ * - Previous / Next walk the scoped entries, so they never cross a section.
18
+ * The home page's list holds no link to itself, so it has neither.
19
+ *
20
+ * Hrefs are compared after `normalizeNavHref` (or the caller's own rule):
21
+ * the Astro pipeline links `/zaraz/`, dogsbay-ssg `zaraz.html`; each passes
22
+ * the current page in the same form as its nav. See plans/nav-scope.md.
23
+ */
24
+ /** Drop a query or fragment and a trailing slash; the root stays `/`. */
25
+ export function normalizeNavHref(href) {
26
+ const bare = href.replace(/[?#].*$/, "");
27
+ return bare.replace(/\/+$/, "") || (bare.startsWith("/") ? "/" : "");
28
+ }
29
+ function holds(item, current, norm) {
30
+ if (item.href !== undefined && norm(item.href) === current)
31
+ return true;
32
+ return item.children?.some((c) => holds(c, current, norm)) ?? false;
33
+ }
34
+ function firstHref(item) {
35
+ if (item.href)
36
+ return item.href;
37
+ for (const c of item.children ?? []) {
38
+ const h = firstHref(c);
39
+ if (h)
40
+ return h;
41
+ }
42
+ return undefined;
43
+ }
44
+ /** The level whose entries are the sections: a nav of one group is opened. */
45
+ function sectionLevel(nav) {
46
+ let level = nav;
47
+ while (level.length === 1 && level[0].children?.length)
48
+ level = level[0].children;
49
+ return level;
50
+ }
51
+ /** The home pages of this level: `home`, and a unit's landing page (see the header). */
52
+ function homesOf(level, opts) {
53
+ const norm = opts.normalize ?? normalizeNavHref;
54
+ const homes = new Set();
55
+ if (opts.home !== undefined)
56
+ homes.add(norm(opts.home));
57
+ for (const item of level) {
58
+ if (item.children?.length || item.href === undefined)
59
+ continue;
60
+ const h = norm(item.href);
61
+ if (!h.startsWith("/") || h === "/")
62
+ continue;
63
+ // stops at the first link outside it: on most navs, the first one
64
+ const under = (o) => (o.href === undefined || norm(o.href).startsWith(`${h}/`)) && (o.children ?? []).every(under);
65
+ const others = level.filter((o) => o !== item);
66
+ if (others.some((o) => firstHref(o) !== undefined) && others.every(under))
67
+ homes.add(h);
68
+ }
69
+ return homes;
70
+ }
71
+ function isSection(item, homes, norm) {
72
+ return Boolean(item.children?.length) || item.href === undefined || !homes.has(norm(item.href));
73
+ }
74
+ function locate(nav, current, opts) {
75
+ const norm = opts.normalize ?? normalizeNavHref;
76
+ const level = sectionLevel(nav);
77
+ const homes = homesOf(level, opts);
78
+ const cur = norm(current);
79
+ const index = homes.has(cur) ? -1 : level.findIndex((item) => isSection(item, homes, norm) && holds(item, cur, norm));
80
+ return { level, index, homes, norm };
81
+ }
82
+ /** The index of the section holding `current` (in the opened level), or -1. */
83
+ export function sectionIndex(nav, current, opts = {}) {
84
+ return locate(nav, current, opts).index;
85
+ }
86
+ /**
87
+ * A section's sidebar entries: its children, with the section's own page
88
+ * first when no child links it. A top-level page with no children is a
89
+ * section of one page.
90
+ */
91
+ export function sectionItems(section, opts = {}) {
92
+ const norm = opts.normalize ?? normalizeNavHref;
93
+ const children = (section.children ?? []);
94
+ if (!children.length)
95
+ return [section];
96
+ if (section.href === undefined)
97
+ return children;
98
+ const own = norm(section.href);
99
+ if (children.some((c) => holds(c, own, norm)))
100
+ return children;
101
+ return [{ label: section.label, href: section.href }, ...children];
102
+ }
103
+ function entriesOf(l) {
104
+ const out = [];
105
+ l.level.forEach((item, index) => {
106
+ if (!isSection(item, l.homes, l.norm))
107
+ return;
108
+ const href = firstHref(item);
109
+ if (href)
110
+ out.push({ label: item.label, href, index });
111
+ });
112
+ return out;
113
+ }
114
+ export function sectionEntries(nav, opts = {}) {
115
+ return entriesOf(locate(nav, "", opts));
116
+ }
117
+ /**
118
+ * The sidebar for a page. Scope `site` (or unset) is the whole nav; scope
119
+ * `section` is the page's section, or the list of sections for a page in none.
120
+ */
121
+ export function scopeNav(nav, current, scope, opts = {}) {
122
+ if (scope !== "section")
123
+ return { items: [...nav], section: -1 };
124
+ const l = locate(nav, current, opts);
125
+ if (l.index >= 0)
126
+ return { items: sectionItems(l.level[l.index], opts), section: l.index };
127
+ return { items: entriesOf(l).map((e) => ({ label: e.label, href: e.href })), section: -1 };
128
+ }
129
+ export function sectionMenu(nav, current, opts = {}) {
130
+ const l = locate(nav, current, opts);
131
+ const entries = entriesOf(l);
132
+ const own = entries.find((e) => e.index === l.index);
133
+ return own ? { entries, current: own } : { entries };
134
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.169",
3
+ "version": "0.2.0-beta.171",
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",