@clarusjs/theme-docs 0.0.1 → 0.1.1
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/LICENSE +21 -0
- package/README.md +23 -1
- package/package.json +49 -5
- package/src/callout.tscl +19 -0
- package/src/copy.tscl +21 -0
- package/src/layout.tscl +124 -0
- package/src/learn-progress.tscl +7 -0
- package/src/learn-status.tscl +40 -0
- package/src/lesson-bar.tscl +126 -0
- package/src/lesson.css +72 -0
- package/src/lesson.tscl +43 -0
- package/src/nav.ts +142 -0
- package/src/page-head.tscl +14 -0
- package/src/pages.d.ts +6 -0
- package/src/progress-board.tscl +64 -0
- package/src/progress.ts +93 -0
- package/src/quiz-choices.tscl +44 -0
- package/src/quiz.tscl +16 -0
- package/src/search.d.ts +25 -0
- package/src/search.js +53 -0
- package/src/search.tscl +76 -0
- package/src/theme-toggle.tscl +23 -0
- package/src/theme.css +359 -0
- package/src/view-transitions.tscl +7 -0
- package/src/vite.d.ts +47 -0
- package/src/vite.js +203 -0
package/src/theme.css
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/* @clarusjs/theme-docs: a quiet reading layout. Every colour, font and size is a custom property on :root (tokens). */
|
|
2
|
+
:root {
|
|
3
|
+
--header-height: 64px;
|
|
4
|
+
--page-width: 1440px; /* the header, sidebar, content and contents columns together */
|
|
5
|
+
--sidebar-width: 260px;
|
|
6
|
+
--toc-width: 220px;
|
|
7
|
+
--bg: #fbfcfc;
|
|
8
|
+
--surface: #f1f5f5;
|
|
9
|
+
--fg: #1a2526;
|
|
10
|
+
--muted: #566768;
|
|
11
|
+
--rule: #dbe4e4;
|
|
12
|
+
--accent: #0b6e70;
|
|
13
|
+
--accent-soft: rgba(11, 110, 112, 0.1);
|
|
14
|
+
--code-bg: #f1f5f5;
|
|
15
|
+
/* Highlighted code: the theme's own background, and line tints light enough that every token keeps WCAG AA. */
|
|
16
|
+
--shiki-bg: #ffffff;
|
|
17
|
+
--line-highlight: rgba(11, 110, 112, 0.07);
|
|
18
|
+
--line-add: rgba(47, 158, 110, 0.1);
|
|
19
|
+
--line-remove: rgba(208, 69, 59, 0.08);
|
|
20
|
+
--sans: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Helvetica, Arial, sans-serif;
|
|
21
|
+
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
22
|
+
color-scheme: light;
|
|
23
|
+
}
|
|
24
|
+
/* Dark: when the system is dark and light wasn't chosen, or when dark was chosen (theme toggle). */
|
|
25
|
+
@media (prefers-color-scheme: dark) {
|
|
26
|
+
:root:not([data-theme='light']) {
|
|
27
|
+
--bg: #0c1213;
|
|
28
|
+
--surface: #121b1c;
|
|
29
|
+
--fg: #e3eded;
|
|
30
|
+
--muted: #93a6a7;
|
|
31
|
+
--rule: #223031;
|
|
32
|
+
--accent: #4cc9cb;
|
|
33
|
+
--accent-soft: rgba(76, 201, 203, 0.12);
|
|
34
|
+
--code-bg: #121b1c;
|
|
35
|
+
--shiki-bg: var(--code-bg);
|
|
36
|
+
--line-highlight: rgba(76, 201, 203, 0.12);
|
|
37
|
+
--line-add: rgba(47, 158, 110, 0.14);
|
|
38
|
+
--line-remove: rgba(208, 69, 59, 0.14);
|
|
39
|
+
color-scheme: dark;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
:root[data-theme='dark'] {
|
|
43
|
+
--bg: #0c1213;
|
|
44
|
+
--surface: #121b1c;
|
|
45
|
+
--fg: #e3eded;
|
|
46
|
+
--muted: #93a6a7;
|
|
47
|
+
--rule: #223031;
|
|
48
|
+
--accent: #4cc9cb;
|
|
49
|
+
--accent-soft: rgba(76, 201, 203, 0.12);
|
|
50
|
+
--code-bg: #121b1c;
|
|
51
|
+
--shiki-bg: var(--code-bg);
|
|
52
|
+
--line-highlight: rgba(76, 201, 203, 0.12);
|
|
53
|
+
--line-add: rgba(47, 158, 110, 0.14);
|
|
54
|
+
--line-remove: rgba(208, 69, 59, 0.14);
|
|
55
|
+
color-scheme: dark;
|
|
56
|
+
}
|
|
57
|
+
:root[data-theme='light'] { color-scheme: light; }
|
|
58
|
+
|
|
59
|
+
* { box-sizing: border-box; }
|
|
60
|
+
html { -webkit-text-size-adjust: 100%; }
|
|
61
|
+
body {
|
|
62
|
+
margin: 0;
|
|
63
|
+
background: var(--bg);
|
|
64
|
+
color: var(--fg);
|
|
65
|
+
font: 16px/1.65 var(--sans);
|
|
66
|
+
}
|
|
67
|
+
a { color: var(--accent); text-underline-offset: 3px; }
|
|
68
|
+
/* Skip link: off screen until it has keyboard focus. */
|
|
69
|
+
.skip-link { position: absolute; left: 12px; top: -48px; z-index: 100; padding: 8px 14px; border-radius: 8px; background: var(--accent); color: var(--bg); font-weight: 600; text-decoration: none; }
|
|
70
|
+
.skip-link:focus { top: 12px; }
|
|
71
|
+
/* Focused by the skip link and after navigation (the new page's heading or <main>), not controls: no ring after a
|
|
72
|
+
click; keyboard users keep it so they can see where they are. */
|
|
73
|
+
[tabindex="-1"]:focus:not(:focus-visible) { outline: none; } /* keyboard focus (the skip link) keeps its ring */
|
|
74
|
+
a:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
|
|
75
|
+
|
|
76
|
+
.site-header {
|
|
77
|
+
position: sticky;
|
|
78
|
+
top: 0;
|
|
79
|
+
z-index: 10;
|
|
80
|
+
display: flex;
|
|
81
|
+
align-items: center;
|
|
82
|
+
gap: 12px 24px;
|
|
83
|
+
background: var(--bg);
|
|
84
|
+
/* Its content lines up with the page below (.docs, at most --page-width wide), not the window's edges. */
|
|
85
|
+
box-sizing: border-box;
|
|
86
|
+
min-height: var(--header-height); /* what the sticky sidebar and contents sit under */
|
|
87
|
+
padding: 0 max(16px, env(safe-area-inset-right, 0px), calc((100% - var(--page-width)) / 2 + 16px)) 0 max(16px, env(safe-area-inset-left, 0px), calc((100% - var(--page-width)) / 2 + 16px));
|
|
88
|
+
border-bottom: 1px solid var(--rule);
|
|
89
|
+
}
|
|
90
|
+
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--fg); text-decoration: none; font-weight: 650; font-size: 18px; }
|
|
91
|
+
.js { color: var(--accent); font-weight: 500; }
|
|
92
|
+
.site-header nav { display: flex; flex-wrap: wrap; gap: 4px 18px; }
|
|
93
|
+
.site-header nav a { color: var(--muted); text-decoration: none; font-size: 15px; }
|
|
94
|
+
.site-header nav a:hover { color: var(--fg); }
|
|
95
|
+
.site-header nav a[aria-current] { color: var(--accent); font-weight: 600; }
|
|
96
|
+
|
|
97
|
+
.content {
|
|
98
|
+
max-width: 760px;
|
|
99
|
+
margin: 0 auto;
|
|
100
|
+
padding: 32px max(16px, env(safe-area-inset-right, 0px)) 56px max(16px, env(safe-area-inset-left, 0px));
|
|
101
|
+
}
|
|
102
|
+
h1, h2 { line-height: 1.2; text-wrap: balance; }
|
|
103
|
+
h1 { font-size: clamp(30px, 5vw, 40px); margin: 0 0 16px; letter-spacing: -0.01em; }
|
|
104
|
+
h2 { font-size: 22px; margin: 40px 0 12px; }
|
|
105
|
+
p, ul, ol { margin: 0 0 16px; }
|
|
106
|
+
li { margin-bottom: 6px; }
|
|
107
|
+
|
|
108
|
+
code { font: 0.9em var(--mono); background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 4px; }
|
|
109
|
+
pre {
|
|
110
|
+
background: var(--code-bg);
|
|
111
|
+
border: 1px solid var(--rule);
|
|
112
|
+
border-radius: 8px;
|
|
113
|
+
padding: 14px 16px;
|
|
114
|
+
overflow-x: auto;
|
|
115
|
+
margin: 0 0 20px;
|
|
116
|
+
}
|
|
117
|
+
pre code { background: none; padding: 0; font-size: 14px; line-height: 1.55; }
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
.site-footer {
|
|
121
|
+
border-top: 1px solid var(--rule);
|
|
122
|
+
color: var(--muted);
|
|
123
|
+
font-size: 14px;
|
|
124
|
+
text-align: center;
|
|
125
|
+
padding: 20px max(16px, env(safe-area-inset-right, 0px)) 28px max(16px, env(safe-area-inset-left, 0px));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.sample { position: relative; }
|
|
129
|
+
.sample pre { padding-right: 72px; }
|
|
130
|
+
.copy {
|
|
131
|
+
position: absolute; top: 8px; right: 8px;
|
|
132
|
+
font: 600 12px var(--sans); padding: 4px 10px; border-radius: 6px;
|
|
133
|
+
border: 1px solid var(--rule); background: var(--bg); color: var(--muted); cursor: pointer;
|
|
134
|
+
}
|
|
135
|
+
.copy:hover { color: var(--accent); border-color: var(--accent); }
|
|
136
|
+
.theme-toggle {
|
|
137
|
+
font-size: 16px; line-height: 1; width: 30px; height: 30px; border-radius: 8px;
|
|
138
|
+
border: 1px solid var(--rule); background: transparent; color: var(--fg); cursor: pointer;
|
|
139
|
+
}
|
|
140
|
+
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }
|
|
141
|
+
.site-header nav { align-items: center; }
|
|
142
|
+
|
|
143
|
+
/* Docs pages: sidebar, content and "On this page" (from 1280px a column, below that a box above the content). */
|
|
144
|
+
.docs { display: grid; grid-template-columns: var(--sidebar-width) minmax(0, 1fr) var(--toc-width); max-width: var(--page-width); margin: 0 auto; }
|
|
145
|
+
.docs .content { margin: 0; max-width: 760px; min-width: 0; }
|
|
146
|
+
.sidebar { position: sticky; top: 0; align-self: start; max-height: 100vh; overflow-y: auto; padding: 28px 16px 40px max(16px, env(safe-area-inset-left, 0px)); border-right: 1px solid var(--rule); }
|
|
147
|
+
.sidebar .group { margin: 20px 0 6px; font-size: 13px; font-weight: 650; color: var(--fg); }
|
|
148
|
+
.sidebar .group:first-child { margin-top: 0; }
|
|
149
|
+
.sidebar ul { list-style: none; padding: 0; margin: 0; }
|
|
150
|
+
.sidebar li { margin: 0; }
|
|
151
|
+
.sidebar a { display: block; padding: 4px 8px; border-radius: 6px; color: var(--muted); text-decoration: none; font-size: 14px; }
|
|
152
|
+
.sidebar a:hover { color: var(--fg); }
|
|
153
|
+
.sidebar a[aria-current='page'] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
|
|
154
|
+
.crumb { margin: 0 0 6px; font-size: 13px; font-weight: 600; letter-spacing: 0.04em; color: var(--accent); }
|
|
155
|
+
.lead { font-size: 18px; color: var(--muted); }
|
|
156
|
+
.page-footer { margin-top: 48px; padding-top: 16px; border-top: 1px solid var(--rule); }
|
|
157
|
+
.edit { font-size: 14px; }
|
|
158
|
+
.updated { margin: 4px 0 0; font-size: 14px; color: var(--muted); }
|
|
159
|
+
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 20px; }
|
|
160
|
+
.pager a { display: block; padding: 12px 14px; border: 1px solid var(--rule); border-radius: 8px; text-decoration: none; font-weight: 600; }
|
|
161
|
+
.pager a:hover { border-color: var(--accent); }
|
|
162
|
+
.pager small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
|
|
163
|
+
.pager .next { grid-column: 2; text-align: right; }
|
|
164
|
+
table { width: 100%; border-collapse: collapse; margin: 0 0 16px; font-size: 15px; }
|
|
165
|
+
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--rule); vertical-align: top; }
|
|
166
|
+
.content table { display: block; overflow-x: auto; }
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
.persist { display: contents; }
|
|
170
|
+
|
|
171
|
+
/* Header: section links, then (pushed right) social icons, the ⋯ overflow menu and the theme toggle. */
|
|
172
|
+
.top-nav { display: flex; flex-wrap: wrap; gap: 4px 18px; }
|
|
173
|
+
.header-end { margin-left: auto; display: flex; align-items: center; gap: 6px; }
|
|
174
|
+
.social { display: flex; align-items: center; gap: 2px; }
|
|
175
|
+
.social a, .more-button, .menu-button { display: inline-flex; align-items: center; justify-content: center; min-width: 40px; height: 40px; padding: 0 6px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; text-decoration: none; }
|
|
176
|
+
.social a:hover, .more-button:hover, .menu-button:hover { color: var(--fg); background: var(--accent-soft); }
|
|
177
|
+
.npm { padding: 1px 5px; border: 1.8px solid currentColor; border-radius: 4px; font: 700 11px/1.3 var(--mono); }
|
|
178
|
+
.more-button, .menu-button { display: none; }
|
|
179
|
+
.more { inset: auto; margin: 0; padding: 6px; min-width: 200px; border: 1px solid var(--rule); border-radius: 12px; background: var(--bg); color: var(--fg); box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18); position-area: bottom span-left; }
|
|
180
|
+
.more a { display: flex; align-items: center; min-height: 44px; padding: 0 12px; border-radius: 8px; color: var(--fg); text-decoration: none; }
|
|
181
|
+
.more a:hover { background: var(--accent-soft); }
|
|
182
|
+
|
|
183
|
+
/* Search: a button in the header, a modal dialog with the results. */
|
|
184
|
+
.search-button { display: inline-flex; align-items: center; gap: 8px; height: 40px; min-width: 40px; padding: 0 10px; border: 1px solid var(--rule); border-radius: 10px; background: var(--bg); color: var(--muted); font: inherit; font-size: 14px; cursor: pointer; }
|
|
185
|
+
.search-button:hover { color: var(--fg); border-color: var(--accent); }
|
|
186
|
+
.search-button kbd { padding: 1px 6px; border: 1px solid var(--rule); border-radius: 6px; font: 12px var(--mono); }
|
|
187
|
+
.search-dialog { width: min(640px, calc(100vw - 32px)); max-height: min(560px, calc(100vh - 96px)); margin: 72px auto auto; padding: 0; border: 1px solid var(--rule); border-radius: 14px; background: var(--bg); color: var(--fg); box-shadow: 0 24px 64px rgba(0, 0, 0, 0.3); }
|
|
188
|
+
.search-dialog::backdrop { background: rgba(15, 23, 25, 0.45); }
|
|
189
|
+
.search-box { display: flex; flex-direction: column; max-height: inherit; }
|
|
190
|
+
.search-box input { height: 52px; padding: 0 16px; border: 0; border-bottom: 1px solid var(--rule); background: transparent; color: inherit; font: inherit; font-size: 17px; }
|
|
191
|
+
.search-box input:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
|
|
192
|
+
.search-status { margin: 0; padding: 8px 16px 0; font-size: 13px; color: var(--muted); }
|
|
193
|
+
.search-status:empty { display: none; }
|
|
194
|
+
#search-results { list-style: none; margin: 0; padding: 8px; overflow-y: auto; }
|
|
195
|
+
#search-results a { display: grid; gap: 2px; padding: 10px 12px; border-radius: 10px; color: var(--fg); text-decoration: none; }
|
|
196
|
+
#search-results [aria-selected="true"] a, #search-results a:hover { background: var(--accent-soft); }
|
|
197
|
+
.search-heading { font-weight: 650; }
|
|
198
|
+
.search-page { font-size: 13px; color: var(--accent); }
|
|
199
|
+
.search-text { font-size: 13px; color: var(--muted); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
|
|
200
|
+
|
|
201
|
+
/* The sidebar is a popover (so it can be a drawer on small screens): shown in place from 1024px. */
|
|
202
|
+
/* inset first: it would reset top. */
|
|
203
|
+
.sidebar[popover] { display: block; position: sticky; inset: auto; top: var(--header-height); width: auto; height: auto; max-height: calc(100vh - var(--header-height)); margin: 0; border: 0; border-right: 1px solid var(--rule); background: transparent; color: inherit; overflow-y: auto; }
|
|
204
|
+
.sidebar-head, .sidebar-links { display: none; }
|
|
205
|
+
|
|
206
|
+
/* On this page */
|
|
207
|
+
.toc { position: sticky; top: var(--header-height); align-self: start; max-height: calc(100vh - var(--header-height)); overflow-y: auto; padding: 36px 16px; font-size: 14px; }
|
|
208
|
+
.toc p { margin: 0 0 8px; font-weight: 650; }
|
|
209
|
+
.toc ul, .toc-inline ul { list-style: none; margin: 0; padding: 0; }
|
|
210
|
+
.toc li, .toc-inline li { margin: 0 0 6px; }
|
|
211
|
+
.toc li.sub, .toc-inline li.sub { padding-left: 14px; }
|
|
212
|
+
.toc a, .toc-inline a { color: var(--muted); text-decoration: none; }
|
|
213
|
+
.toc a:hover, .toc-inline a:hover { color: var(--accent); }
|
|
214
|
+
.toc-inline { display: none; margin: 0 0 24px; border: 1px solid var(--rule); border-radius: 10px; }
|
|
215
|
+
.toc-inline > summary { min-height: 44px; display: flex; align-items: center; padding: 0 14px; font-size: 14px; font-weight: 650; cursor: pointer; }
|
|
216
|
+
.toc-inline ul { padding: 0 14px 12px; font-size: 14px; }
|
|
217
|
+
|
|
218
|
+
/* Below 1280px: no room for the column; "On this page" is a box above the content, social links fold into ⋯. */
|
|
219
|
+
@media (max-width: 1279px) {
|
|
220
|
+
.docs { grid-template-columns: var(--sidebar-width) minmax(0, 1fr); }
|
|
221
|
+
.toc { display: none; }
|
|
222
|
+
.toc-inline { display: block; }
|
|
223
|
+
.social { display: none; }
|
|
224
|
+
.more-button { display: inline-flex; }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/* Below 1024px: the sidebar is a drawer from the ☰ button; the page takes the full width. */
|
|
228
|
+
@media (max-width: 1023px) {
|
|
229
|
+
.docs { display: block; }
|
|
230
|
+
.menu-button { display: inline-flex; }
|
|
231
|
+
.sidebar[popover] { display: none; }
|
|
232
|
+
.sidebar[popover]:popover-open { display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; width: min(340px, 86vw); height: 100%; max-height: none; padding: 0 12px 16px; border-right: 1px solid var(--rule); background: var(--bg); box-shadow: 8px 0 32px rgba(0, 0, 0, 0.25); }
|
|
233
|
+
.sidebar[popover]::backdrop { background: rgba(15, 23, 25, 0.45); }
|
|
234
|
+
.sidebar-head { display: flex; align-items: center; justify-content: space-between; min-height: 60px; font-weight: 700; border-bottom: 1px solid var(--rule); margin: 0 -12px 8px; padding: 0 8px 0 16px; }
|
|
235
|
+
.sidebar-head button { width: 44px; height: 44px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; }
|
|
236
|
+
.sidebar a { padding: 9px 10px; font-size: 15px; }
|
|
237
|
+
.sidebar-links { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: auto; padding-top: 12px; border-top: 1px solid var(--rule); }
|
|
238
|
+
.more-button, .social { display: none; }
|
|
239
|
+
.search-label, .search-button kbd { display: none; }
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/* Phones: the section links move into the drawer. */
|
|
243
|
+
@media (max-width: 699px) {
|
|
244
|
+
.site-header .top-nav { display: none; }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/* ::: details blocks from Markdown: a native <details>. */
|
|
248
|
+
details.custom-block { border: 1px solid var(--rule); border-radius: 8px; padding: 10px 14px; margin: 20px 0; }
|
|
249
|
+
details.custom-block > summary { cursor: pointer; font-weight: 600; }
|
|
250
|
+
details.custom-block[open] > summary { margin-bottom: 8px; }
|
|
251
|
+
|
|
252
|
+
/* Highlighted code from Markdown (Shiki, build time). Tokens carry both themes as variables; the page picks one. */
|
|
253
|
+
.code-block { position: relative; margin: 0 0 20px; }
|
|
254
|
+
.code-block pre { margin: 0; padding-right: 72px; }
|
|
255
|
+
.code-block .lang { position: absolute; top: 8px; right: 64px; font: 12px var(--mono); color: var(--muted); pointer-events: none; }
|
|
256
|
+
.code-block .code-title { margin: 0; padding: 6px 12px; font: 600 13px/20px var(--mono); border: 1px solid var(--rule); border-bottom: 0; border-radius: 8px 8px 0 0; background: var(--code-bg); }
|
|
257
|
+
.code-block .code-title + .lang + pre { border-top-left-radius: 0; border-top-right-radius: 0; }
|
|
258
|
+
.shiki span { color: var(--shiki-light); }
|
|
259
|
+
.shiki { background: var(--shiki-bg); border: 1px solid var(--rule); }
|
|
260
|
+
@media (prefers-color-scheme: dark) { :root:not([data-theme='light']) .shiki span { color: var(--shiki-dark); } }
|
|
261
|
+
:root[data-theme='dark'] .shiki span { color: var(--shiki-dark); }
|
|
262
|
+
.shiki code { display: block; }
|
|
263
|
+
.shiki .line { display: inline-block; width: 100%; }
|
|
264
|
+
.shiki .line.highlighted { background: var(--line-highlight); }
|
|
265
|
+
.shiki .line.diff.add { background: var(--line-add); }
|
|
266
|
+
.shiki .line.diff.remove { background: var(--line-remove); }
|
|
267
|
+
.shiki .line.diff.add::before { content: '+ '; }
|
|
268
|
+
.shiki .line.diff.remove::before { content: '- '; }
|
|
269
|
+
.shiki.has-focused .line:not(.focused) { opacity: 0.45; filter: blur(0.5px); }
|
|
270
|
+
.shiki.has-focused:hover .line { opacity: 1; filter: none; }
|
|
271
|
+
.code-block.line-numbers pre { padding-left: 44px; }
|
|
272
|
+
.line-numbers-column { position: absolute; top: 14px; left: 0; width: 32px; display: flex; flex-direction: column; text-align: right; font: 14px/1.55 var(--mono); color: var(--muted); }
|
|
273
|
+
/* Below a title (6 + 20 + 6 px, and its 1px top border) the code, and so the numbers, start lower. */
|
|
274
|
+
.code-block .code-title ~ .line-numbers-column { top: calc(14px + 33px); }
|
|
275
|
+
|
|
276
|
+
/* ::: code-group: radio tabs, no JavaScript. The checked tab's label is followed by its block. */
|
|
277
|
+
.code-group { display: flex; flex-wrap: wrap; margin: 0 0 20px; }
|
|
278
|
+
.code-group > input { position: absolute; opacity: 0; pointer-events: none; }
|
|
279
|
+
.code-group > label { order: 0; font: 600 13px var(--mono); padding: 6px 12px; border: 1px solid var(--rule); border-bottom: 0; border-radius: 8px 8px 0 0; color: var(--muted); cursor: pointer; }
|
|
280
|
+
.code-group > input:checked + label { color: var(--accent); background: var(--code-bg); }
|
|
281
|
+
.code-group > input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }
|
|
282
|
+
.code-group > .code-block { order: 1; width: 100%; display: none; margin: 0; }
|
|
283
|
+
.code-group > input:checked + label + .code-block { display: block; }
|
|
284
|
+
.code-group > .code-block pre { border-top-left-radius: 0; }
|
|
285
|
+
|
|
286
|
+
/* Visitors who ask for less motion get none: transitions and animations end at once, scrolling jumps. */
|
|
287
|
+
@media (prefers-reduced-motion: reduce) {
|
|
288
|
+
*, *::before, *::after {
|
|
289
|
+
animation-duration: 0.01ms !important;
|
|
290
|
+
animation-iteration-count: 1 !important;
|
|
291
|
+
transition-duration: 0.01ms !important;
|
|
292
|
+
scroll-behavior: auto !important;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/* Right and wrong (quizzes, the lesson bar): green and red that keep their contrast in both themes. */
|
|
297
|
+
:root { --ok: #17703c; --ok-soft: rgba(23, 112, 60, 0.1); --bad: #b42318; --bad-soft: rgba(180, 35, 24, 0.08); }
|
|
298
|
+
@media (prefers-color-scheme: dark) {
|
|
299
|
+
:root:not([data-theme='light']) { --ok: #4ade80; --ok-soft: rgba(74, 222, 128, 0.12); --bad: #f87171; --bad-soft: rgba(248, 113, 113, 0.12); }
|
|
300
|
+
}
|
|
301
|
+
:root[data-theme='dark'] { --ok: #4ade80; --ok-soft: rgba(74, 222, 128, 0.12); --bad: #f87171; --bad-soft: rgba(248, 113, 113, 0.12); }
|
|
302
|
+
|
|
303
|
+
/* Quizzes: big options to tap, then the right one green and a wrong pick red. */
|
|
304
|
+
.quiz { margin: 24px 0; padding: 18px; border: 1px solid var(--rule); border-radius: 14px; background: var(--surface); }
|
|
305
|
+
.quiz-question { font-weight: 650; margin: 0 0 12px; }
|
|
306
|
+
.quiz-options { display: grid; gap: 8px; }
|
|
307
|
+
.quiz-options label { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 0 14px; border: 1.5px solid var(--rule); border-radius: 12px; background: var(--bg); cursor: pointer; }
|
|
308
|
+
.quiz-options label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
|
|
309
|
+
.quiz-options label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
|
|
310
|
+
.quiz .quiz-options label.right { border-color: var(--ok); background: var(--ok-soft); }
|
|
311
|
+
.quiz .quiz-options label.wrong { border-color: var(--bad); background: var(--bad-soft); }
|
|
312
|
+
.quiz-options .mark { margin-left: auto; font-size: 13px; font-weight: 700; }
|
|
313
|
+
.quiz-options .right .mark { color: var(--ok); }
|
|
314
|
+
.quiz-options .wrong .mark { color: var(--bad); }
|
|
315
|
+
.quiz-options input { accent-color: var(--accent); width: 18px; height: 18px; }
|
|
316
|
+
.quiz-check { margin-top: 12px; min-height: 44px; padding: 0 18px; border: 0; border-radius: 10px; background: var(--accent); color: var(--bg); font: inherit; font-weight: 700; cursor: pointer; }
|
|
317
|
+
.quiz-check:disabled { opacity: 0.5; cursor: default; }
|
|
318
|
+
.quiz-status { margin: 8px 0 0; font-size: 14px; }
|
|
319
|
+
.quiz-status:empty { display: none; }
|
|
320
|
+
.quiz-answer { margin-top: 12px; font-size: 15px; }
|
|
321
|
+
.quiz-answer > summary { cursor: pointer; color: var(--muted); }
|
|
322
|
+
.quiz-correct { font-weight: 650; margin: 8px 0; }
|
|
323
|
+
|
|
324
|
+
/* Learn: the XP chip, lesson states in the sidebar (a hint: every lesson stays open), the progress page. */
|
|
325
|
+
.xp-chip { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border: 1px solid var(--rule); border-radius: 999px; background: var(--bg); color: var(--fg); font-size: 14px; font-weight: 700; text-decoration: none; white-space: nowrap; }
|
|
326
|
+
.xp-chip[hidden] { display: none; }
|
|
327
|
+
.xp-chip svg { color: #c99400; }
|
|
328
|
+
.xp-chip:hover { border-color: var(--accent); }
|
|
329
|
+
.sidebar a[data-status]::before { content: ''; display: inline-block; width: 10px; height: 10px; margin-right: 8px; border-radius: 50%; border: 2px solid var(--rule); vertical-align: -1px; }
|
|
330
|
+
.sidebar a[data-status="done"]::before { background: var(--ok); border-color: var(--ok); }
|
|
331
|
+
.sidebar a[data-status="current"]::before { border-color: var(--accent); }
|
|
332
|
+
.sidebar a[data-status="locked"] { color: var(--muted); }
|
|
333
|
+
.status-note { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
334
|
+
.progress-board .status-note { position: static; width: auto; height: auto; clip-path: none; color: var(--muted); font-size: 14px; }
|
|
335
|
+
.progress-note { padding: 12px 14px; border: 1px solid var(--rule); border-radius: 10px; background: var(--surface); }
|
|
336
|
+
.progress-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 0 0 24px; }
|
|
337
|
+
.progress-stats div { padding: 14px 16px; border: 1px solid var(--rule); border-radius: 12px; background: var(--surface); }
|
|
338
|
+
.progress-stats dt { font-size: 13px; color: var(--muted); }
|
|
339
|
+
.progress-stats dd { margin: 4px 0 0; font-size: 22px; font-weight: 800; }
|
|
340
|
+
.badges { display: grid; gap: 8px; padding: 0; list-style: none; }
|
|
341
|
+
.badges li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin: 0; padding: 12px 14px; border: 1px dashed var(--rule); border-radius: 12px; color: var(--muted); }
|
|
342
|
+
.badges li.earned { border-style: solid; border-color: var(--ok); color: var(--fg); background: var(--ok-soft); }
|
|
343
|
+
.badges em { margin-left: auto; font-style: normal; font-size: 13px; font-weight: 700; }
|
|
344
|
+
.progress-lessons li[data-status="done"] a::after { content: ' ✓'; color: var(--ok); }
|
|
345
|
+
.progress-reset { margin-top: 16px; min-height: 44px; padding: 0 16px; border: 1px solid var(--bad); border-radius: 10px; background: transparent; color: var(--bad); font: inherit; font-weight: 600; cursor: pointer; }
|
|
346
|
+
|
|
347
|
+
/* While a page change runs (fragments or the client router set data-cl-navigating on <html>): a thin bar at the top.
|
|
348
|
+
Shown after a short delay, so fast swaps don't flash it. */
|
|
349
|
+
html[data-cl-navigating]::before {
|
|
350
|
+
content: ''; position: fixed; inset: 0 auto auto 0; z-index: 20; height: 3px; width: 100%; background: var(--accent);
|
|
351
|
+
transform-origin: left; animation: cl-navigating 8s cubic-bezier(0.1, 0.7, 0.2, 1) 150ms both;
|
|
352
|
+
}
|
|
353
|
+
@keyframes cl-navigating { from { transform: scaleX(0); } to { transform: scaleX(0.9); } }
|
|
354
|
+
@media (prefers-reduced-motion: reduce) { html[data-cl-navigating]::before { animation: none; transform: scaleX(1); opacity: 0.6; } }
|
|
355
|
+
|
|
356
|
+
/* View transitions (config.viewTransitions): the default cross-fade, none with reduced motion. */
|
|
357
|
+
@media (prefers-reduced-motion: reduce) {
|
|
358
|
+
::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none; }
|
|
359
|
+
}
|
package/src/vite.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type Options = {
|
|
2
|
+
/**
|
|
3
|
+
* the pages folder, from the Vite root (default src/pages)
|
|
4
|
+
*/
|
|
5
|
+
pages?: string;
|
|
6
|
+
/**
|
|
7
|
+
* false: no search index (it's written to the client output's search/ after the build)
|
|
8
|
+
*/
|
|
9
|
+
search?: boolean;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} Options
|
|
13
|
+
* @property {string} [pages] the pages folder, from the Vite root (default src/pages)
|
|
14
|
+
* @property {boolean} [search] false: no search index (it's written to the client output's search/ after the build)
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Reads the pages for the sidebar (served as `virtual:theme-docs/pages`) and writes the search index after the build.
|
|
18
|
+
* @param {Options} [options]
|
|
19
|
+
* @returns {import('vite').Plugin}
|
|
20
|
+
*/
|
|
21
|
+
export declare function themeDocs(options?: Options): import('vite').Plugin;
|
|
22
|
+
/**
|
|
23
|
+
* Every page under `dir` with its source `file`, its last commit time (`updated`, from one `git log`) and its sidebar
|
|
24
|
+
* entry: frontmatter `title` (else the first `# ` heading, or a .tscl page's `[title]="'…'"`, else the file name),
|
|
25
|
+
* `group` (else the subfolder inside its section), `order`. `hidden` (not in the sidebar): `sidebar: false` and
|
|
26
|
+
* dynamic routes (`[step]`). Files or folders starting with `_` aren't pages.
|
|
27
|
+
* @param {string} dir
|
|
28
|
+
* @returns {import('./nav.ts').PageInfo[]}
|
|
29
|
+
*/
|
|
30
|
+
export declare function readPages(dir: string): import('./nav.ts').PageInfo[];
|
|
31
|
+
/**
|
|
32
|
+
* A built page's search sections: its main content (main#content, without the theme's crumb, inline contents and
|
|
33
|
+
* footer) split at each h1-h3 with an id. None for pages without one, noindex pages and pages with
|
|
34
|
+
* data-search="false" in their content.
|
|
35
|
+
* ponytail: regular expressions over our own prerendered HTML, not an HTML parser; enough for what the theme emits.
|
|
36
|
+
* @param {string} html
|
|
37
|
+
* @param {string} url
|
|
38
|
+
* @returns {import('./search.js').Section[]}
|
|
39
|
+
*/
|
|
40
|
+
export declare function sectionsOf(html: string, url: string): import('./search.js').Section[];
|
|
41
|
+
/**
|
|
42
|
+
* The index files for some sections (name to JSON): words sharded by their first two letters, sections in chunks
|
|
43
|
+
* of CHUNK, their text cut to 240 characters (the words are indexed in full).
|
|
44
|
+
* @param {import('./search.js').Section[]} sections
|
|
45
|
+
* @returns {Map<string, string>}
|
|
46
|
+
*/
|
|
47
|
+
export declare function buildIndex(sections: import('./search.js').Section[]): Map<string, string>;
|
package/src/vite.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// themeDocs(): the Vite plugin that reads src/pages for the sidebar, in Node, and serves the result as
|
|
2
|
+
// `virtual:theme-docs/pages`, so the sidebar data (not the files) reaches pages rendered in the browser too.
|
|
3
|
+
// Plain JavaScript: vite.config loads it from node_modules, where Node doesn't strip types. Types are the JSDoc here;
|
|
4
|
+
// vite.d.ts is generated from it (pnpm types; typecheck and prepack run it).
|
|
5
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join, resolve } from 'node:path'
|
|
7
|
+
import { gitDates } from 'clarusjs/vite'
|
|
8
|
+
import { CHUNK, termFile, tokens } from './search.js'
|
|
9
|
+
|
|
10
|
+
const ID = 'virtual:theme-docs/pages'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} Options
|
|
14
|
+
* @property {string} [pages] the pages folder, from the Vite root (default src/pages)
|
|
15
|
+
* @property {boolean} [search] false: no search index (it's written to the client output's search/ after the build)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reads the pages for the sidebar (served as `virtual:theme-docs/pages`) and writes the search index after the build.
|
|
20
|
+
* @param {Options} [options]
|
|
21
|
+
* @returns {import('vite').Plugin}
|
|
22
|
+
*/
|
|
23
|
+
export function themeDocs(options = {}) {
|
|
24
|
+
let dir = resolve(options.pages ?? 'src/pages')
|
|
25
|
+
return {
|
|
26
|
+
name: 'clarusjs:theme-docs',
|
|
27
|
+
/** @param {{ root: string }} config */
|
|
28
|
+
configResolved(config) {
|
|
29
|
+
dir = resolve(config.root, options.pages ?? 'src/pages')
|
|
30
|
+
},
|
|
31
|
+
/** @param {string} id */
|
|
32
|
+
resolveId(id) {
|
|
33
|
+
return id === ID ? '\0' + ID : undefined
|
|
34
|
+
},
|
|
35
|
+
/** @param {string} id */
|
|
36
|
+
load(id) {
|
|
37
|
+
return id === '\0' + ID ? `export default ${JSON.stringify(readPages(dir))}\n` : undefined
|
|
38
|
+
},
|
|
39
|
+
// After clarusjs has prerendered the site: the search index, from the built pages.
|
|
40
|
+
buildApp: {
|
|
41
|
+
order: 'post',
|
|
42
|
+
/** @param {any} builder */
|
|
43
|
+
async handler(builder) {
|
|
44
|
+
if (options.search === false) return
|
|
45
|
+
const out = resolve(builder.config.root, builder.environments.client.config.build.outDir)
|
|
46
|
+
const base = builder.config.base.replace(/\/$/, '')
|
|
47
|
+
/** @type {import('./search.js').Section[]} */
|
|
48
|
+
const sections = []
|
|
49
|
+
for (const file of readdirSync(out, { recursive: true, encoding: 'utf8' }).map((f) => f.replaceAll('\\', '/')).sort()) {
|
|
50
|
+
if (!file.endsWith('.html') || file.endsWith('.frag.html') || /^404(\/index)?\.html$/.test(file)) continue
|
|
51
|
+
const url = base + ('/' + file.replace(/\.html$/, '')).replace(/\/index$/, '') || '/'
|
|
52
|
+
sections.push(...sectionsOf(readFileSync(join(out, file), 'utf8'), url || '/'))
|
|
53
|
+
}
|
|
54
|
+
rmSync(join(out, 'search'), { recursive: true, force: true })
|
|
55
|
+
mkdirSync(join(out, 'search'), { recursive: true })
|
|
56
|
+
for (const [name, json] of buildIndex(sections)) writeFileSync(join(out, 'search', name), json)
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
// A page added, removed or edited in dev: the sidebar is read again.
|
|
60
|
+
/** @param {any} server */
|
|
61
|
+
configureServer(server) {
|
|
62
|
+
server.watcher.on('all', (/** @type {string} */ _event, /** @type {string} */ file) => {
|
|
63
|
+
if (!file.startsWith(dir) || !/\.(md|tscl)$/.test(file)) return
|
|
64
|
+
for (const env of Object.values(server.environments)) {
|
|
65
|
+
const mod = env.moduleGraph.getModuleById('\0' + ID)
|
|
66
|
+
if (mod) env.reloadModule(mod)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Every page under `dir` with its source `file`, its last commit time (`updated`, from one `git log`) and its sidebar
|
|
75
|
+
* entry: frontmatter `title` (else the first `# ` heading, or a .tscl page's `[title]="'…'"`, else the file name),
|
|
76
|
+
* `group` (else the subfolder inside its section), `order`. `hidden` (not in the sidebar): `sidebar: false` and
|
|
77
|
+
* dynamic routes (`[step]`). Files or folders starting with `_` aren't pages.
|
|
78
|
+
* @param {string} dir
|
|
79
|
+
* @returns {import('./nav.ts').PageInfo[]}
|
|
80
|
+
*/
|
|
81
|
+
export function readPages(dir) {
|
|
82
|
+
/** @type {string[]} */
|
|
83
|
+
let files
|
|
84
|
+
try {
|
|
85
|
+
files = readdirSync(dir, { recursive: true, encoding: 'utf8' })
|
|
86
|
+
} catch {
|
|
87
|
+
return []
|
|
88
|
+
}
|
|
89
|
+
const dates = gitDates(dir, '.')
|
|
90
|
+
/** @type {import('./nav.ts').PageInfo[]} */
|
|
91
|
+
const pages = []
|
|
92
|
+
for (const file of files.map((f) => f.replaceAll('\\', '/')).sort()) {
|
|
93
|
+
const m = /^(.+)\.(md|tscl)$/.exec(file)
|
|
94
|
+
if (!m || m[1].split('/').some((part) => part.startsWith('_'))) continue
|
|
95
|
+
const text = readFileSync(join(dir, file), 'utf8')
|
|
96
|
+
const data = m[2] === 'md' ? frontmatter(text) : {}
|
|
97
|
+
const parts = m[1].split('/')
|
|
98
|
+
const href = ('/' + m[1]).replace(/\/index$/, '') || '/'
|
|
99
|
+
/** @type {import('./nav.ts').PageInfo} */
|
|
100
|
+
const page = { href, title: data.title ?? /^# (.+)$/m.exec(text)?.[1]?.trim() ?? /\[title\]="'([^']*)'"/.exec(text)?.[1] ?? words(parts.at(-1) ?? ''), file }
|
|
101
|
+
const group = data.group ?? (parts.length > 2 ? words(parts[1]) : undefined)
|
|
102
|
+
if (group) page.group = group
|
|
103
|
+
if (data.order !== undefined && Number.isFinite(Number(data.order))) page.order = Number(data.order)
|
|
104
|
+
const updated = dates.get(resolve(dir, file))
|
|
105
|
+
if (updated !== undefined) page.updated = updated
|
|
106
|
+
if (data.sidebar === 'false' || parts.some((part) => part.startsWith('['))) page.hidden = true
|
|
107
|
+
pages.push(page)
|
|
108
|
+
}
|
|
109
|
+
return pages
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const BLOCK = /^<\/?(p|div|li|ul|ol|h\d|br|tr|td|th|table|pre|blockquote|section|aside|details|summary|dl|dt|dd|figure|figcaption|hr)\b/i
|
|
113
|
+
const ENTITIES = /** @type {Record<string, string>} */ ({ amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' })
|
|
114
|
+
/** @param {string} html */
|
|
115
|
+
const plain = (html) => html.replace(/<[^>]*>/g, (tag) => (BLOCK.test(tag) ? ' ' : ''))
|
|
116
|
+
.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (all, e) => e[0] !== '#' ? ENTITIES[e.toLowerCase()] ?? all : String.fromCodePoint(e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : Number(e.slice(1))))
|
|
117
|
+
.replace(/\s+/g, ' ').trim()
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A built page's search sections: its main content (main#content, without the theme's crumb, inline contents and
|
|
121
|
+
* footer) split at each h1-h3 with an id. None for pages without one, noindex pages and pages with
|
|
122
|
+
* data-search="false" in their content.
|
|
123
|
+
* ponytail: regular expressions over our own prerendered HTML, not an HTML parser; enough for what the theme emits.
|
|
124
|
+
* @param {string} html
|
|
125
|
+
* @param {string} url
|
|
126
|
+
* @returns {import('./search.js').Section[]}
|
|
127
|
+
*/
|
|
128
|
+
export function sectionsOf(html, url) {
|
|
129
|
+
if (/<meta\b(?=[^>]*\bname=["']?robots\b)(?=[^>]*noindex)[^>]*>/i.test(html)) return []
|
|
130
|
+
const main = /<main\b[^>]*\bid="content"[^>]*>([\s\S]*)<\/main>/i.exec(html)?.[1]
|
|
131
|
+
if (main === undefined || /\bdata-search="false"/.test(main)) return []
|
|
132
|
+
const body = main.replace(/<!--[\s\S]*?-->/g, '')
|
|
133
|
+
.replace(/<(script|style|template)\b[\s\S]*?<\/\1>/gi, '')
|
|
134
|
+
.replace(/<p class="crumb">[\s\S]*?<\/p>/, '')
|
|
135
|
+
.replace(/<details class="toc-inline">[\s\S]*?<\/details>/, '')
|
|
136
|
+
.replace(/<footer class="page-footer">[\s\S]*?<\/footer>/, '')
|
|
137
|
+
.replace(/<a class="header-anchor"[\s\S]*?<\/a>/g, '')
|
|
138
|
+
const parts = body.split(/(<h[1-3]\b[^>]*\bid="[^"]*"[^>]*>[\s\S]*?<\/h[1-3]>)/i)
|
|
139
|
+
const page = plain(/<h1\b[^>]*>([\s\S]*?)<\/h1>/i.exec(body)?.[1] ?? /<title>([\s\S]*?)<\/title>/i.exec(html)?.[1] ?? '')
|
|
140
|
+
/** @type {import('./search.js').Section[]} */
|
|
141
|
+
const sections = []
|
|
142
|
+
if (plain(parts[0])) sections.push({ u: url, p: page, h: page, t: plain(parts[0]) })
|
|
143
|
+
for (let i = 1; i < parts.length; i += 2) {
|
|
144
|
+
const [, level, id] = /** @type {RegExpExecArray} */ (/<h([1-3])\b[^>]*\bid="([^"]*)"/i.exec(parts[i]))
|
|
145
|
+
sections.push({ u: level === '1' ? url : `${url}#${id}`, p: page, h: plain(parts[i]), t: plain(parts[i + 1] ?? '') })
|
|
146
|
+
}
|
|
147
|
+
return sections
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The index files for some sections (name to JSON): words sharded by their first two letters, sections in chunks
|
|
152
|
+
* of CHUNK, their text cut to 240 characters (the words are indexed in full).
|
|
153
|
+
* @param {import('./search.js').Section[]} sections
|
|
154
|
+
* @returns {Map<string, string>}
|
|
155
|
+
*/
|
|
156
|
+
export function buildIndex(sections) {
|
|
157
|
+
/** @type {Map<string, Record<string, number[]>>} */
|
|
158
|
+
const shards = new Map()
|
|
159
|
+
for (const [id, section] of sections.entries()) {
|
|
160
|
+
/** @type {Map<string, number>} */
|
|
161
|
+
const weights = new Map()
|
|
162
|
+
/** @type {Map<string, number>} */
|
|
163
|
+
const uses = new Map()
|
|
164
|
+
for (const word of tokens(section.t)) uses.set(word, (uses.get(word) ?? 0) + 1)
|
|
165
|
+
// In the text a word counts at most 3 times: a long page repeating it doesn't outrank a heading.
|
|
166
|
+
for (const [word, n] of uses) weights.set(word, Math.min(n, 3))
|
|
167
|
+
for (const word of tokens(section.h)) weights.set(word, (weights.get(word) ?? 0) + 10)
|
|
168
|
+
for (const [word, weight] of weights) {
|
|
169
|
+
const shard = shards.get(termFile(word)) ?? shards.set(termFile(word), {}).get(termFile(word))
|
|
170
|
+
;(/** @type {Record<string, number[]>} */ (shard)[word] ??= []).push(id, weight)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** @type {Map<string, string>} */
|
|
174
|
+
const files = new Map([...shards].map(([name, terms]) => [name, JSON.stringify(terms)]))
|
|
175
|
+
for (let n = 0; n * CHUNK < sections.length; n++) {
|
|
176
|
+
const chunk = sections.slice(n * CHUNK, (n + 1) * CHUNK).map((s) => ({ ...s, t: s.t.length > 240 ? s.t.slice(0, 239) + '…' : s.t }))
|
|
177
|
+
files.set(`s-${n}.json`, JSON.stringify(chunk))
|
|
178
|
+
}
|
|
179
|
+
return files
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ponytail: flat `key: value` lines only (all the sidebar needs); the page itself is compiled with a real YAML parser.
|
|
183
|
+
/** @param {string} text @returns {Record<string, string>} */
|
|
184
|
+
function frontmatter(text) {
|
|
185
|
+
const block = /^---\r?\n([\s\S]*?)\r?\n---/.exec(text)?.[1] ?? ''
|
|
186
|
+
/** @type {Record<string, string>} */
|
|
187
|
+
const data = {}
|
|
188
|
+
for (const line of block.split(/\r?\n/)) {
|
|
189
|
+
const m = /^(\w+):\s*(.*?)\s*$/.exec(line)
|
|
190
|
+
if (!m) continue
|
|
191
|
+
// A quoted value ends at its closing quote; otherwise a ` #` starts a comment.
|
|
192
|
+
const double = /^"((?:[^"\\]|\\.)*)"/.exec(m[2])
|
|
193
|
+
const single = /^'((?:[^']|'')*)'/.exec(m[2])
|
|
194
|
+
if (double) {
|
|
195
|
+
try { data[m[1]] = JSON.parse(double[0]) } catch { data[m[1]] = double[1] }
|
|
196
|
+
} else if (single) data[m[1]] = single[1].replaceAll("''", "'")
|
|
197
|
+
else data[m[1]] = m[2].replace(/\s+#.*$/, '')
|
|
198
|
+
}
|
|
199
|
+
return data
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** @param {string} name */
|
|
203
|
+
const words = (name) => name.replace(/[-_]+/g, ' ').replace(/^\w/, (c) => c.toUpperCase())
|