@iyulab/canopy-page 0.4.0 → 0.5.0
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/CHANGELOG.md +22 -0
- package/dist/assets/scrollspy.css +10 -0
- package/dist/assets/scrollspy.js +98 -0
- package/dist/assets/search.css +53 -0
- package/dist/assets/search.js +220 -0
- package/dist/assets/theme-toggle.js +87 -0
- package/dist/assets-bundle.d.ts +8 -0
- package/dist/assets-bundle.js +42 -0
- package/dist/build.d.ts +10 -3
- package/dist/build.js +34 -17
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ Notable changes to canopy-page. The format follows
|
|
|
7
7
|
The `settings.json` contract is what consuming projects plan their upgrades around, so changes
|
|
8
8
|
to it — its fields, its validation, and what the checks reject — are what this file is about.
|
|
9
9
|
|
|
10
|
+
## [0.5.0] — 2026-08-09
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **Search, wired up.** Every build now carries a search box in the top bar, a `Ctrl+K` /
|
|
15
|
+
`Cmd+K` shortcut to jump to it, and an on-page outline that highlights the section currently
|
|
16
|
+
in view while scrolling — all with no `settings.json` field to turn on, and all inert if a
|
|
17
|
+
reader's browser has scripts disabled.
|
|
18
|
+
- **A dark/light toggle**, riding in the same script bundle. Remembers a reader's choice across
|
|
19
|
+
visits; without a stored choice, follows the system preference exactly as before.
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- **Upgraded to canopy 0.6.0.** The current page's sidebar entry is now highlighted, a reader
|
|
24
|
+
landing partway down a page can flip `data-theme` by hand, mobile navigation opens as a
|
|
25
|
+
full-screen panel instead of pushing the page down, an unlabeled or unrecognized code fence
|
|
26
|
+
highlights as plain text instead of falling back unstyled, and every page gains previous/next
|
|
27
|
+
links to its neighbors in the sidebar order — all on the next build, no `settings.json` field
|
|
28
|
+
changed. See
|
|
29
|
+
[canopy's changelog](https://github.com/iyulab/canopy/blob/main/CHANGELOG.md#060--2026-08-09)
|
|
30
|
+
for the underlying markup and CSS selector changes.
|
|
31
|
+
|
|
10
32
|
## [0.4.0] — 2026-08-08
|
|
11
33
|
|
|
12
34
|
### Changed
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* Scrollspy — pairs with assets/scrollspy.js. Styles the `.canopy-outline`
|
|
2
|
+
link whose section is currently in view. Reuses canopy's own tokens
|
|
3
|
+
(--accent, --font-weight-semibold) rather than introducing new ones, and
|
|
4
|
+
targets the standard `aria-current` attribute rather than a new class so
|
|
5
|
+
the same rule serves any script that sets it, not just this one. */
|
|
6
|
+
|
|
7
|
+
.canopy-outline a[aria-current="location"] {
|
|
8
|
+
color: var(--accent);
|
|
9
|
+
font-weight: var(--font-weight-semibold);
|
|
10
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canopy-page's on-page outline scrollspy — vanilla JS, no dependencies.
|
|
3
|
+
* Marks whichever `.canopy-outline` link points at the heading currently in
|
|
4
|
+
* view with `aria-current="location"`, the same attribute browsers already
|
|
5
|
+
* use for "current place in a set" (MDN: aria-current). No new class name,
|
|
6
|
+
* so a caller who wants to restyle it only needs `[aria-current="location"]`.
|
|
7
|
+
*
|
|
8
|
+
* `pickActive` is exposed for tests: it is the one piece of this file with
|
|
9
|
+
* real logic (choosing among several simultaneously-visible headings), and
|
|
10
|
+
* it needs no DOM to run.
|
|
11
|
+
*/
|
|
12
|
+
var CanopyScrollspy = (function () {
|
|
13
|
+
"use strict";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Given the outline's heading ids in document order and the subset
|
|
17
|
+
* currently intersecting the viewport, pick the one to mark current.
|
|
18
|
+
*
|
|
19
|
+
* The topmost visible heading wins — not the one with the largest
|
|
20
|
+
* intersection ratio, since a short section near the top of the viewport
|
|
21
|
+
* can be fully visible while a long section just below it is only
|
|
22
|
+
* fractionally visible, and a reader who just scrolled to that short
|
|
23
|
+
* section expects it, not its taller neighbor, to light up.
|
|
24
|
+
*/
|
|
25
|
+
function pickActive(orderedIds, visibleIds) {
|
|
26
|
+
if (visibleIds.length === 0) return null;
|
|
27
|
+
var visible = new Set(visibleIds);
|
|
28
|
+
for (var i = 0; i < orderedIds.length; i++) {
|
|
29
|
+
if (visible.has(orderedIds[i])) return orderedIds[i];
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function main() {
|
|
35
|
+
var outline = document.querySelector(".canopy-outline");
|
|
36
|
+
if (!outline) return;
|
|
37
|
+
var links = outline.querySelectorAll("a[href^='#']");
|
|
38
|
+
if (links.length === 0) return;
|
|
39
|
+
|
|
40
|
+
var orderedIds = [];
|
|
41
|
+
var linkById = {};
|
|
42
|
+
links.forEach(function (link) {
|
|
43
|
+
var id = link.getAttribute("href").slice(1);
|
|
44
|
+
orderedIds.push(id);
|
|
45
|
+
linkById[id] = link;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
var headings = orderedIds
|
|
49
|
+
.map(function (id) {
|
|
50
|
+
return document.getElementById(id);
|
|
51
|
+
})
|
|
52
|
+
.filter(Boolean);
|
|
53
|
+
if (headings.length === 0) return;
|
|
54
|
+
|
|
55
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
56
|
+
|
|
57
|
+
var current = null;
|
|
58
|
+
function setCurrent(id) {
|
|
59
|
+
if (id === current) return;
|
|
60
|
+
if (current !== null && linkById[current]) {
|
|
61
|
+
linkById[current].removeAttribute("aria-current");
|
|
62
|
+
}
|
|
63
|
+
current = id;
|
|
64
|
+
if (current !== null && linkById[current]) {
|
|
65
|
+
linkById[current].setAttribute("aria-current", "location");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var visible = [];
|
|
70
|
+
var observer = new IntersectionObserver(
|
|
71
|
+
function (entries) {
|
|
72
|
+
entries.forEach(function (entry) {
|
|
73
|
+
var id = entry.target.id;
|
|
74
|
+
var at = visible.indexOf(id);
|
|
75
|
+
if (entry.isIntersecting) {
|
|
76
|
+
if (at === -1) visible.push(id);
|
|
77
|
+
} else if (at !== -1) {
|
|
78
|
+
visible.splice(at, 1);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
setCurrent(pickActive(orderedIds, visible));
|
|
82
|
+
},
|
|
83
|
+
// A heading counts as "in view" while it sits in a band from the
|
|
84
|
+
// upper tenth to the upper third of the viewport — narrow enough that
|
|
85
|
+
// a reader scrolling past a short section still sees it light up, but
|
|
86
|
+
// wide enough to actually be a non-empty band (top + bottom margins
|
|
87
|
+
// here must sum to under 100%, or the observed strip is zero-height).
|
|
88
|
+
{ rootMargin: "-10% 0px -70% 0px" },
|
|
89
|
+
);
|
|
90
|
+
headings.forEach(function (heading) {
|
|
91
|
+
observer.observe(heading);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (typeof document !== "undefined") main();
|
|
96
|
+
|
|
97
|
+
return { pickActive: pickActive };
|
|
98
|
+
})();
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/* Search UI — pairs with assets/search.js and the `.canopy-search` form
|
|
2
|
+
canopy's shell emits. Loaded only when a build carries both, so a site
|
|
3
|
+
with no search never pays for this either. Reuses canopy's own tokens
|
|
4
|
+
(--sp-*, --border, --radius-m, --bg-primary, --text-*) rather than
|
|
5
|
+
introducing new ones. */
|
|
6
|
+
|
|
7
|
+
.canopy-search {
|
|
8
|
+
position: relative;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.canopy-search-results {
|
|
12
|
+
position: absolute;
|
|
13
|
+
top: 100%;
|
|
14
|
+
right: 0;
|
|
15
|
+
z-index: 10;
|
|
16
|
+
margin: var(--sp-2) 0 0;
|
|
17
|
+
padding: var(--sp-2) 0;
|
|
18
|
+
width: min(24rem, 90vw);
|
|
19
|
+
max-height: 60vh;
|
|
20
|
+
overflow-y: auto;
|
|
21
|
+
list-style: none;
|
|
22
|
+
background: var(--bg-primary);
|
|
23
|
+
border: 1px solid var(--border);
|
|
24
|
+
border-radius: var(--radius-m);
|
|
25
|
+
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.canopy-search-results li {
|
|
29
|
+
padding: var(--sp-2) var(--sp-3);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.canopy-search-results li.is-active,
|
|
33
|
+
.canopy-search-results li:hover {
|
|
34
|
+
background: var(--bg-secondary);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.canopy-search-results a {
|
|
38
|
+
display: block;
|
|
39
|
+
font-weight: var(--font-weight-semibold);
|
|
40
|
+
color: var(--text-normal);
|
|
41
|
+
text-decoration: none;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.canopy-search-snippet {
|
|
45
|
+
margin: var(--sp-1, 4px) 0 0;
|
|
46
|
+
font-size: 0.85em;
|
|
47
|
+
color: var(--text-muted);
|
|
48
|
+
overflow: hidden;
|
|
49
|
+
text-overflow: ellipsis;
|
|
50
|
+
display: -webkit-box;
|
|
51
|
+
-webkit-line-clamp: 2;
|
|
52
|
+
-webkit-box-orient: vertical;
|
|
53
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canopy-page's client search — vanilla JS, no dependencies. Wires the
|
|
3
|
+
* `.canopy-search` form canopy's shell emits when `--search-index` and
|
|
4
|
+
* `--script` are both given.
|
|
5
|
+
*
|
|
6
|
+
* Substring matching, not stemming: Korean attaches particles to a word stem
|
|
7
|
+
* (주문 -> 주문을/주문이), so a substring search already finds the stem
|
|
8
|
+
* inside the inflected form — English queries use the same code path.
|
|
9
|
+
*
|
|
10
|
+
* One global (`CanopySearch`), so this adds exactly one name to whatever
|
|
11
|
+
* page carries it. `searchIndex` is exposed for tests, which load and
|
|
12
|
+
* evaluate this exact file rather than reimplementing it (search-ui.test.ts).
|
|
13
|
+
*/
|
|
14
|
+
var CanopySearch = (function () {
|
|
15
|
+
"use strict";
|
|
16
|
+
|
|
17
|
+
function normalize(s) {
|
|
18
|
+
return s.normalize("NFC").toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Score one entry against a query's terms. Every term has to appear
|
|
23
|
+
* somewhere (title, a heading, or the body) or the entry is dropped — an
|
|
24
|
+
* OR across terms would surface pages matching none of what was typed.
|
|
25
|
+
*/
|
|
26
|
+
function scoreEntry(entry, terms) {
|
|
27
|
+
var title = normalize(entry.t);
|
|
28
|
+
var headings = entry.h.map(normalize);
|
|
29
|
+
var body = normalize(entry.b);
|
|
30
|
+
var score = 0;
|
|
31
|
+
for (var i = 0; i < terms.length; i++) {
|
|
32
|
+
var term = terms[i];
|
|
33
|
+
var inTitle = title.indexOf(term) !== -1;
|
|
34
|
+
var inHeading = headings.some(function (h) {
|
|
35
|
+
return h.indexOf(term) !== -1;
|
|
36
|
+
});
|
|
37
|
+
var inBody = body.indexOf(term) !== -1;
|
|
38
|
+
if (!inTitle && !inHeading && !inBody) return 0;
|
|
39
|
+
if (inTitle) score += 3;
|
|
40
|
+
if (inHeading) score += 2;
|
|
41
|
+
if (inBody) score += 1;
|
|
42
|
+
}
|
|
43
|
+
return score;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A short excerpt around the first matched term, for the result list. */
|
|
47
|
+
function snippet(entry, terms) {
|
|
48
|
+
var body = entry.b;
|
|
49
|
+
var lower = normalize(body);
|
|
50
|
+
var at = -1;
|
|
51
|
+
for (var i = 0; i < terms.length; i++) {
|
|
52
|
+
at = lower.indexOf(terms[i]);
|
|
53
|
+
if (at !== -1) break;
|
|
54
|
+
}
|
|
55
|
+
if (at === -1) return body.slice(0, 100);
|
|
56
|
+
var start = Math.max(0, at - 40);
|
|
57
|
+
var end = Math.min(body.length, at + 60);
|
|
58
|
+
return (start > 0 ? "…" : "") + body.slice(start, end) + (end < body.length ? "…" : "");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Search `entries` (the parsed search-index.json) for `query`. */
|
|
62
|
+
function searchIndex(entries, query, limit) {
|
|
63
|
+
var terms = normalize(query).split(/\s+/).filter(Boolean);
|
|
64
|
+
if (terms.length === 0) return [];
|
|
65
|
+
var results = [];
|
|
66
|
+
for (var i = 0; i < entries.length; i++) {
|
|
67
|
+
var score = scoreEntry(entries[i], terms);
|
|
68
|
+
if (score > 0) {
|
|
69
|
+
results.push({ entry: entries[i], score: score, snippet: snippet(entries[i], terms) });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
results.sort(function (a, b) {
|
|
73
|
+
return b.score - a.score;
|
|
74
|
+
});
|
|
75
|
+
return results.slice(0, limit || 10);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// DOM wiring below is skipped outside a browser, so this file stays
|
|
79
|
+
// loadable in a plain Node sandbox for the test above.
|
|
80
|
+
|
|
81
|
+
var SCRIPT_SRC =
|
|
82
|
+
typeof document !== "undefined" && document.currentScript ? document.currentScript.src : "";
|
|
83
|
+
|
|
84
|
+
function main() {
|
|
85
|
+
var form = document.querySelector(".canopy-search");
|
|
86
|
+
var input = form ? form.querySelector("input[type=search]") : null;
|
|
87
|
+
if (!form || !input) return;
|
|
88
|
+
|
|
89
|
+
// canopy always writes this script to assets/script.js at the site root
|
|
90
|
+
// and links it relatively, so the resolved URL minus that suffix is the
|
|
91
|
+
// site root — the same root the index and every result link resolve
|
|
92
|
+
// against.
|
|
93
|
+
var root = SCRIPT_SRC.replace(/assets\/script\.js(\?.*)?$/, "");
|
|
94
|
+
var indexUrl = root + "search-index.json";
|
|
95
|
+
|
|
96
|
+
var list = document.createElement("ul");
|
|
97
|
+
list.className = "canopy-search-results";
|
|
98
|
+
list.hidden = true;
|
|
99
|
+
list.setAttribute("role", "listbox");
|
|
100
|
+
form.appendChild(list);
|
|
101
|
+
|
|
102
|
+
var indexPromise = null;
|
|
103
|
+
function loadIndex() {
|
|
104
|
+
if (!indexPromise) {
|
|
105
|
+
indexPromise = fetch(indexUrl).then(function (response) {
|
|
106
|
+
if (!response.ok) throw new Error("search index request failed");
|
|
107
|
+
return response.json();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return indexPromise;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var active = -1;
|
|
114
|
+
|
|
115
|
+
function render(results) {
|
|
116
|
+
active = -1;
|
|
117
|
+
list.textContent = "";
|
|
118
|
+
if (results.length === 0) {
|
|
119
|
+
list.hidden = true;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
results.forEach(function (result) {
|
|
123
|
+
var item = document.createElement("li");
|
|
124
|
+
item.setAttribute("role", "option");
|
|
125
|
+
var link = document.createElement("a");
|
|
126
|
+
link.href = root + result.entry.p;
|
|
127
|
+
link.textContent = result.entry.t;
|
|
128
|
+
var preview = document.createElement("p");
|
|
129
|
+
preview.className = "canopy-search-snippet";
|
|
130
|
+
preview.textContent = result.snippet;
|
|
131
|
+
item.appendChild(link);
|
|
132
|
+
item.appendChild(preview);
|
|
133
|
+
list.appendChild(item);
|
|
134
|
+
});
|
|
135
|
+
list.hidden = false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
var pending;
|
|
139
|
+
function handleInput() {
|
|
140
|
+
var query = input.value;
|
|
141
|
+
clearTimeout(pending);
|
|
142
|
+
pending = setTimeout(function () {
|
|
143
|
+
if (query.trim() === "") {
|
|
144
|
+
render([]);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
loadIndex()
|
|
148
|
+
.then(function (entries) {
|
|
149
|
+
render(searchIndex(entries, query, 10));
|
|
150
|
+
})
|
|
151
|
+
.catch(function () {
|
|
152
|
+
list.textContent = "";
|
|
153
|
+
var item = document.createElement("li");
|
|
154
|
+
item.textContent = "Search failed to load.";
|
|
155
|
+
list.appendChild(item);
|
|
156
|
+
list.hidden = false;
|
|
157
|
+
});
|
|
158
|
+
}, 120);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function setActive(index) {
|
|
162
|
+
var items = list.children;
|
|
163
|
+
if (active >= 0 && items[active]) items[active].classList.remove("is-active");
|
|
164
|
+
active = index;
|
|
165
|
+
if (active >= 0 && items[active]) {
|
|
166
|
+
items[active].classList.add("is-active");
|
|
167
|
+
items[active].scrollIntoView({ block: "nearest" });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function handleKeydown(event) {
|
|
172
|
+
if (list.hidden || list.children.length === 0) return;
|
|
173
|
+
if (event.key === "ArrowDown") {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
setActive((active + 1) % list.children.length);
|
|
176
|
+
} else if (event.key === "ArrowUp") {
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
setActive((active - 1 + list.children.length) % list.children.length);
|
|
179
|
+
} else if (event.key === "Enter") {
|
|
180
|
+
var target = active >= 0 ? list.children[active] : list.children[0];
|
|
181
|
+
var link = target && target.querySelector("a");
|
|
182
|
+
if (link) {
|
|
183
|
+
event.preventDefault();
|
|
184
|
+
location.href = link.href;
|
|
185
|
+
}
|
|
186
|
+
} else if (event.key === "Escape") {
|
|
187
|
+
render([]);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
input.addEventListener("focus", loadIndex, { once: true });
|
|
192
|
+
input.addEventListener("input", handleInput);
|
|
193
|
+
input.addEventListener("keydown", handleKeydown);
|
|
194
|
+
document.addEventListener("click", function (event) {
|
|
195
|
+
if (!form.contains(event.target)) list.hidden = true;
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Ctrl+K / Cmd+K jumps to search from anywhere on the page — the
|
|
199
|
+
// convention readers already know from editors and other doc sites.
|
|
200
|
+
// Global rather than scoped to the form, since the point is not having
|
|
201
|
+
// to click the form first.
|
|
202
|
+
document.addEventListener("keydown", function (event) {
|
|
203
|
+
var key = (event.key || "").toLowerCase();
|
|
204
|
+
if ((event.ctrlKey || event.metaKey) && key === "k") {
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
input.focus();
|
|
207
|
+
input.select();
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// A script that ran this far is a script that can wire the form up —
|
|
212
|
+
// reveal it now, and not before, so a build with no script attached
|
|
213
|
+
// (or one that throws before this point) never shows a dead control.
|
|
214
|
+
form.hidden = false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (typeof document !== "undefined") main();
|
|
218
|
+
|
|
219
|
+
return { searchIndex: searchIndex };
|
|
220
|
+
})();
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canopy-page's dark/light toggle — vanilla JS, no dependencies. Wires the
|
|
3
|
+
* `.canopy-theme-toggle` hidden button canopy's shell always carries when a
|
|
4
|
+
* top bar exists, and flips `data-theme` on `<html>`, the attribute canopy's
|
|
5
|
+
* own stylesheet reads to override `prefers-color-scheme` (see canopy's
|
|
6
|
+
* tokens.ts).
|
|
7
|
+
*
|
|
8
|
+
* `effectiveTheme` and `nextTheme` are exposed for tests, which is also why
|
|
9
|
+
* the resolution logic is split out as pure functions taking primitives
|
|
10
|
+
* rather than reading `matchMedia`/`data-theme` inline — the same shape
|
|
11
|
+
* search.js's `searchIndex` and scrollspy.js's `pickActive` already use.
|
|
12
|
+
*/
|
|
13
|
+
var CanopyThemeToggle = (function () {
|
|
14
|
+
"use strict";
|
|
15
|
+
|
|
16
|
+
var STORAGE_KEY = "canopy-theme";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The theme actually in effect right now: an explicit data-theme
|
|
20
|
+
* attribute if one is set, otherwise the system preference. This is the
|
|
21
|
+
* "current" a click toggles away from — computing it fresh (rather than
|
|
22
|
+
* trusting a stored value) is what makes a first click always flip the
|
|
23
|
+
* theme the reader is actually looking at, even if that reader never
|
|
24
|
+
* clicked before and nothing was ever stored.
|
|
25
|
+
*/
|
|
26
|
+
function effectiveTheme(dataThemeAttr, systemPrefersDark) {
|
|
27
|
+
if (dataThemeAttr === "dark" || dataThemeAttr === "light") return dataThemeAttr;
|
|
28
|
+
return systemPrefersDark ? "dark" : "light";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The theme a click moves to: simply the other one. */
|
|
32
|
+
function nextTheme(current) {
|
|
33
|
+
return current === "dark" ? "light" : "dark";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function systemPrefersDark() {
|
|
37
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Storage access can throw (private browsing with storage disabled, a
|
|
41
|
+
// strict cookie/storage policy) — every call here is wrapped so that a
|
|
42
|
+
// reader who cannot persist a preference still gets a fully working
|
|
43
|
+
// toggle for the rest of the session, just not a remembered one.
|
|
44
|
+
function readStored() {
|
|
45
|
+
try {
|
|
46
|
+
return localStorage.getItem(STORAGE_KEY);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function writeStored(theme) {
|
|
53
|
+
try {
|
|
54
|
+
localStorage.setItem(STORAGE_KEY, theme);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
// Nothing to recover: the theme is still applied for this page view.
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function main() {
|
|
61
|
+
var button = document.querySelector(".canopy-theme-toggle");
|
|
62
|
+
if (!button) return;
|
|
63
|
+
|
|
64
|
+
var stored = readStored();
|
|
65
|
+
if (stored === "dark" || stored === "light") {
|
|
66
|
+
document.documentElement.setAttribute("data-theme", stored);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
button.addEventListener("click", function () {
|
|
70
|
+
var current = effectiveTheme(
|
|
71
|
+
document.documentElement.getAttribute("data-theme"),
|
|
72
|
+
systemPrefersDark(),
|
|
73
|
+
);
|
|
74
|
+
var theme = nextTheme(current);
|
|
75
|
+
document.documentElement.setAttribute("data-theme", theme);
|
|
76
|
+
writeStored(theme);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// A script that ran this far can wire the button up — reveal it now,
|
|
80
|
+
// and not before, the same reasoning search.js's form reveal uses.
|
|
81
|
+
button.hidden = false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (typeof document !== "undefined") main();
|
|
85
|
+
|
|
86
|
+
return { effectiveTheme: effectiveTheme, nextTheme: nextTheme };
|
|
87
|
+
})();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The single script every canopy-page site carries via canopy's `--script`. */
|
|
2
|
+
export declare function assembleScript(): Promise<string>;
|
|
3
|
+
/**
|
|
4
|
+
* CSS canopy-page contributes on top of a site's own tokens, carried via
|
|
5
|
+
* canopy's `--tokens-css` — the same channel a site's own `settings.tokens`
|
|
6
|
+
* already uses, so no new canopy surface is needed for this either.
|
|
7
|
+
*/
|
|
8
|
+
export declare function assembleTokensCss(userTokensCss: string | undefined): Promise<string>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* Assembling canopy-page's own client-side surface for a build.
|
|
6
|
+
*
|
|
7
|
+
* canopy carries at most one `--script` and one `--tokens-css`, so every UI
|
|
8
|
+
* feature canopy-page ships (search, the outline scrollspy, and whatever
|
|
9
|
+
* follows) has to land in those two files rather than one each. Reading the
|
|
10
|
+
* pieces here — rather than at each call site — keeps the list of what
|
|
11
|
+
* ships in one place: adding a feature means adding one line below, not
|
|
12
|
+
* hunting for every place a script or stylesheet gets assembled.
|
|
13
|
+
*
|
|
14
|
+
* Resolved relative to this module rather than `process.cwd()`, so it finds
|
|
15
|
+
* `assets/` next to itself whether it is running as `src/assets-bundle.ts`
|
|
16
|
+
* (tests, dev) or the compiled `dist/assets-bundle.js` (published package) —
|
|
17
|
+
* `copy-assets.mjs` copies `src/assets` to `dist/assets` in the same
|
|
18
|
+
* position relative to the compiled output.
|
|
19
|
+
*/
|
|
20
|
+
const ASSETS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "assets");
|
|
21
|
+
async function readAsset(name) {
|
|
22
|
+
return readFile(path.join(ASSETS_DIR, name), "utf8");
|
|
23
|
+
}
|
|
24
|
+
/** The single script every canopy-page site carries via canopy's `--script`. */
|
|
25
|
+
export async function assembleScript() {
|
|
26
|
+
const [search, scrollspy, themeToggle] = await Promise.all([
|
|
27
|
+
readAsset("search.js"),
|
|
28
|
+
readAsset("scrollspy.js"),
|
|
29
|
+
readAsset("theme-toggle.js"),
|
|
30
|
+
]);
|
|
31
|
+
return `${search}\n${scrollspy}\n${themeToggle}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* CSS canopy-page contributes on top of a site's own tokens, carried via
|
|
35
|
+
* canopy's `--tokens-css` — the same channel a site's own `settings.tokens`
|
|
36
|
+
* already uses, so no new canopy surface is needed for this either.
|
|
37
|
+
*/
|
|
38
|
+
export async function assembleTokensCss(userTokensCss) {
|
|
39
|
+
const [search, scrollspy] = await Promise.all([readAsset("search.css"), readAsset("scrollspy.css")]);
|
|
40
|
+
const own = `${search}\n${scrollspy}`;
|
|
41
|
+
return userTokensCss === undefined ? own : `${userTokensCss}\n${own}`;
|
|
42
|
+
}
|
package/dist/build.d.ts
CHANGED
|
@@ -14,14 +14,21 @@ export interface BuildOptions {
|
|
|
14
14
|
/** Directory to write the site into. */
|
|
15
15
|
out: string;
|
|
16
16
|
}
|
|
17
|
+
/** Assembled search/UI assets, written to real files so canopy's CLI can read them. */
|
|
18
|
+
export interface SearchAssets {
|
|
19
|
+
/** Absolute path to the assembled tokens CSS (a site's own tokens, plus canopy-page's). */
|
|
20
|
+
tokensCssPath: string;
|
|
21
|
+
/** Absolute path to the assembled client script (search, scrollspy, ...). */
|
|
22
|
+
scriptPath: string;
|
|
23
|
+
}
|
|
17
24
|
/**
|
|
18
25
|
* Translate settings into canopy's arguments.
|
|
19
26
|
*
|
|
20
27
|
* Everything a settings file says about the site itself is already something
|
|
21
28
|
* canopy takes: this is a translation, not a layer of behaviour of its own. The
|
|
22
|
-
* navigation spec
|
|
23
|
-
* reads
|
|
29
|
+
* navigation spec and `searchAssets` are the things that have to be materialized
|
|
30
|
+
* first, since canopy reads all three from files.
|
|
24
31
|
*/
|
|
25
|
-
export declare function canopyArgs(site: Awaited<ReturnType<typeof loadSite>>, out: string, navPath: string | undefined): string[];
|
|
32
|
+
export declare function canopyArgs(site: Awaited<ReturnType<typeof loadSite>>, out: string, navPath: string | undefined, searchAssets: SearchAssets): string[];
|
|
26
33
|
/** Build the site in `dir` into `out`, returning the exit code to leave with. */
|
|
27
34
|
export declare function buildSite({ dir, out }: BuildOptions): Promise<number>;
|
package/dist/build.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
1
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { assembleScript, assembleTokensCss } from "./assets-bundle.js";
|
|
4
5
|
import { runCanopy } from "./canopy.js";
|
|
5
6
|
import { siteFindings } from "./check.js";
|
|
6
7
|
import { listHtmlFiles, robotsTxt, sitemapXml } from "./sitemap.js";
|
|
@@ -10,10 +11,10 @@ import { loadSite, reportFindings } from "./site.js";
|
|
|
10
11
|
*
|
|
11
12
|
* Everything a settings file says about the site itself is already something
|
|
12
13
|
* canopy takes: this is a translation, not a layer of behaviour of its own. The
|
|
13
|
-
* navigation spec
|
|
14
|
-
* reads
|
|
14
|
+
* navigation spec and `searchAssets` are the things that have to be materialized
|
|
15
|
+
* first, since canopy reads all three from files.
|
|
15
16
|
*/
|
|
16
|
-
export function canopyArgs(site, out, navPath) {
|
|
17
|
+
export function canopyArgs(site, out, navPath, searchAssets) {
|
|
17
18
|
const { settings } = site;
|
|
18
19
|
return [
|
|
19
20
|
"build",
|
|
@@ -23,21 +24,30 @@ export function canopyArgs(site, out, navPath) {
|
|
|
23
24
|
...(settings.description === undefined ? [] : ["--site-description", settings.description]),
|
|
24
25
|
...(settings.lang === undefined ? [] : ["--lang", settings.lang]),
|
|
25
26
|
...(settings.icon === undefined ? [] : ["--site-icon", settings.icon]),
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
// Always present: canopy-page's own CSS (search, scrollspy) rides here
|
|
28
|
+
// whether or not the site names a tokens file of its own (assembleTokensCss
|
|
29
|
+
// folds one into the other before this ever runs) — no settings field for
|
|
30
|
+
// this, matching the minimal-configuration principle Wave 2 already set.
|
|
31
|
+
"--tokens-css",
|
|
32
|
+
searchAssets.tokensCssPath,
|
|
31
33
|
...(settings.logo === undefined ? [] : ["--site-logo", settings.logo]),
|
|
32
34
|
...(settings.home === undefined
|
|
33
35
|
? []
|
|
34
36
|
: ["--home-url", settings.home.url, "--home-label", settings.home.label]),
|
|
35
37
|
...(navPath === undefined ? [] : ["--nav", navPath]),
|
|
38
|
+
// Always on, same reasoning as --tokens-css above: a search index and the
|
|
39
|
+
// script that searches it are canopy-page's own contribution, not a site
|
|
40
|
+
// author's choice to make.
|
|
41
|
+
"--search-index",
|
|
42
|
+
"search-index.json",
|
|
43
|
+
"--script",
|
|
44
|
+
searchAssets.scriptPath,
|
|
36
45
|
// The settings file is configuration rather than content, and canopy has no
|
|
37
46
|
// reason to know it exists; excluding it keeps it off the published site.
|
|
38
47
|
...["--exclude", "settings.json"],
|
|
39
48
|
// Configuration, not content — the same reason settings.json is excluded.
|
|
40
|
-
// Without this the same CSS ships twice: once
|
|
49
|
+
// Without this the same CSS ships twice: once folded into tokens.css by
|
|
50
|
+
// assembleTokensCss, once copied as a plain asset.
|
|
41
51
|
...(settings.tokens === undefined ? [] : ["--exclude", settings.tokens]),
|
|
42
52
|
...(settings.exclude ?? []).flatMap((pattern) => ["--exclude", pattern]),
|
|
43
53
|
];
|
|
@@ -52,16 +62,24 @@ export async function buildSite({ dir, out }) {
|
|
|
52
62
|
// The spec is derived from settings and means nothing on its own, so it lives
|
|
53
63
|
// in a temporary file rather than in the site or its output: writing it beside
|
|
54
64
|
// the source would leave a generated file for someone to edit by hand, and
|
|
55
|
-
// writing it into the output would ship it.
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
// writing it into the output would ship it. The assembled script/CSS are
|
|
66
|
+
// temporary for the same reason — they are canopy-page's own contribution,
|
|
67
|
+
// not something a site author edits or that belongs in the output tree.
|
|
68
|
+
const workDir = await mkdtemp(path.join(tmpdir(), "canopy-page-"));
|
|
58
69
|
try {
|
|
70
|
+
let navPath;
|
|
59
71
|
if (site.nav.spec !== undefined) {
|
|
60
|
-
workDir = await mkdtemp(path.join(tmpdir(), "canopy-page-"));
|
|
61
72
|
navPath = path.join(workDir, "nav.json");
|
|
62
73
|
await writeFile(navPath, JSON.stringify(site.nav.spec, null, 2), "utf8");
|
|
63
74
|
}
|
|
64
|
-
const
|
|
75
|
+
const userTokensCss = site.settings.tokens === undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: await readFile(path.join(site.root, site.settings.tokens), "utf8");
|
|
78
|
+
const tokensCssPath = path.join(workDir, "tokens.css");
|
|
79
|
+
await writeFile(tokensCssPath, await assembleTokensCss(userTokensCss), "utf8");
|
|
80
|
+
const scriptPath = path.join(workDir, "script.js");
|
|
81
|
+
await writeFile(scriptPath, await assembleScript(), "utf8");
|
|
82
|
+
const code = await runCanopy(canopyArgs(site, path.resolve(out), navPath, { tokensCssPath, scriptPath }));
|
|
65
83
|
// Only after canopy succeeded, and only over what it actually wrote: a
|
|
66
84
|
// sitemap listing pages a failed build never produced would be a lie a
|
|
67
85
|
// crawler acts on.
|
|
@@ -75,7 +93,6 @@ export async function buildSite({ dir, out }) {
|
|
|
75
93
|
return code;
|
|
76
94
|
}
|
|
77
95
|
finally {
|
|
78
|
-
|
|
79
|
-
await rm(workDir, { recursive: true, force: true });
|
|
96
|
+
await rm(workDir, { recursive: true, force: true });
|
|
80
97
|
}
|
|
81
98
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iyulab/canopy-page",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Authoring pipeline for documentation sites: one settings file, integrity checks, and a build.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"check": "tsc --noEmit",
|
|
40
40
|
"lint": "biome lint ./src",
|
|
41
41
|
"test": "vitest run",
|
|
42
|
-
"build": "tsc -p tsconfig.build.json",
|
|
42
|
+
"build": "tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
|
|
43
43
|
"prepublishOnly": "npm run build"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
@@ -49,6 +49,6 @@
|
|
|
49
49
|
"vitest": "^4.1.9"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@iyulab/canopy": "^0.
|
|
52
|
+
"@iyulab/canopy": "^0.6.0"
|
|
53
53
|
}
|
|
54
54
|
}
|