@dogsbay/site-files 0.2.0-beta.170 → 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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/nav-sections.d.ts +71 -0
- package/dist/nav-sections.js +134 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -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.
|
|
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",
|