@clarusjs/theme-docs 0.7.0 → 0.8.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/package.json +6 -3
- package/src/mermaid.tscl +5 -2
- package/src/nav.ts +21 -1
- package/src/page-head.tscl +9 -0
- package/src/pages.d.ts +6 -0
- package/src/parts/sidebar.tscl +2 -0
- package/src/parts/version-menu.tscl +33 -0
- package/src/theme.css +38 -0
- package/src/version-list.tscl +25 -0
- package/src/versions-cli.js +229 -0
- package/src/versions.d.ts +163 -0
- package/src/versions.js +209 -0
- package/src/vite.d.ts +27 -0
- package/src/vite.js +38 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/theme-docs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A docs theme for clarusjs: layout with header, sidebar, content and on-this-page, design tokens, dark mode, copy buttons and callouts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,11 +12,14 @@
|
|
|
12
12
|
"exports": {
|
|
13
13
|
"./*": "./src/*"
|
|
14
14
|
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"clarusjs-docs-versions": "./src/versions-cli.js"
|
|
17
|
+
},
|
|
15
18
|
"publishConfig": {
|
|
16
19
|
"access": "public"
|
|
17
20
|
},
|
|
18
21
|
"peerDependencies": {
|
|
19
|
-
"clarusjs": "^0.
|
|
22
|
+
"clarusjs": "^0.21.0",
|
|
20
23
|
"mermaid": ">=11"
|
|
21
24
|
},
|
|
22
25
|
"engines": {
|
|
@@ -41,7 +44,7 @@
|
|
|
41
44
|
"devDependencies": {
|
|
42
45
|
"@tailwindcss/cli": "4.3.5",
|
|
43
46
|
"@types/node": "^26.6.11",
|
|
44
|
-
"clarusjs": "^0.
|
|
47
|
+
"clarusjs": "^0.21.0",
|
|
45
48
|
"mermaid": "12.4.0",
|
|
46
49
|
"tailwindcss": "4.3.5",
|
|
47
50
|
"typescript": "^7.0.2",
|
package/src/mermaid.tscl
CHANGED
|
@@ -20,8 +20,11 @@
|
|
|
20
20
|
async function draw() {
|
|
21
21
|
const run = ++latest
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
// Once more if it fails: in dev, an import can fail while Vite re-optimizes dependencies
|
|
24
|
+
const { default: mermaid } = await import('mermaid').catch(() => new Promise((r) => setTimeout(r, 500)).then(() => import('mermaid')))
|
|
25
|
+
mermaid.initialize({ startOnLoad: false, securityLevel: 'strict', theme: dark() ? 'dark' : 'default',
|
|
26
|
+
// Dark edge labels: Mermaid's #ccc on #585858 is 4.4:1, under WCAG AA's 4.5; on #333 it's 7.9:1
|
|
27
|
+
...(dark() && { themeVariables: { edgeLabelBackground: '#333333' } }) })
|
|
25
28
|
const { svg } = await mermaid.render('cl-mermaid-' + Math.random().toString(36).slice(2), source)
|
|
26
29
|
if (gone || run !== latest) return
|
|
27
30
|
let out = box.querySelector('.mermaid-svg')
|
package/src/nav.ts
CHANGED
|
@@ -25,7 +25,7 @@ export type Heading = { id: string; text: string; level: number }
|
|
|
25
25
|
* What a site's layout passes to a theme's `layout.tscl`: the contract a theme implements (#362). `config` and
|
|
26
26
|
* `title` always; the rest optional, with the defaults the docs theme uses. A theme's layout also has the slots
|
|
27
27
|
* `sidebar-top`, `sidebar-bottom`, `content-top`, `content-bottom` and `footer`, beside its default one.
|
|
28
|
-
* @see https://clarusjs.com/
|
|
28
|
+
* @see https://clarusjs.com/docs-sites/writing-a-theme
|
|
29
29
|
*/
|
|
30
30
|
export type LayoutInputs = {
|
|
31
31
|
config: ThemeConfig
|
|
@@ -82,6 +82,26 @@ export type ThemeConfig = {
|
|
|
82
82
|
viewTransitions?: boolean
|
|
83
83
|
/** The site footer's text. */
|
|
84
84
|
footer?: string
|
|
85
|
+
/**
|
|
86
|
+
* Versioned docs: a version menu at the top of the sidebar, and links into archived versions load their pages. Pass
|
|
87
|
+
* `import versions from 'virtual:theme-docs/versions'` (themeDocs({ versions }) serves it; null without the option).
|
|
88
|
+
* @see https://clarusjs.com/docs-sites/versioned-docs
|
|
89
|
+
*/
|
|
90
|
+
versions?: Versions | null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The version menu's data, as themeDocs({ versions }) serves it. */
|
|
94
|
+
export type Versions = {
|
|
95
|
+
/** The live docs' version, as shown ('v0.20.0'). */
|
|
96
|
+
current: string
|
|
97
|
+
/** The archived versions served, newest first (version '0.19' is at /v0.19/), with the tag each was built from. */
|
|
98
|
+
archived: readonly { version: string; tag: string }[]
|
|
99
|
+
/** More rows at the end of the menu. */
|
|
100
|
+
links: readonly { label: string; href: string }[]
|
|
101
|
+
/** At most this many archived versions in the menu; the rest through `all`. */
|
|
102
|
+
menu?: number
|
|
103
|
+
/** A page listing every version: an "All versions" row in the menu. */
|
|
104
|
+
all?: string
|
|
85
105
|
}
|
|
86
106
|
|
|
87
107
|
const none: Item = { title: '', href: '' }
|
package/src/page-head.tscl
CHANGED
|
@@ -19,6 +19,15 @@
|
|
|
19
19
|
</script>
|
|
20
20
|
<fragment>
|
|
21
21
|
<meta charset="utf-8" />
|
|
22
|
+
{{--
|
|
23
|
+
Versioned docs: each version is its own build, so a link into another version loads the page instead of the
|
|
24
|
+
site's navigator swapping it in (LATEST_GUARD in versions.js; a test keeps the two the same). Before the module scripts.
|
|
25
|
+
--}}
|
|
26
|
+
@if (props.config.versions) {
|
|
27
|
+
<script inline>
|
|
28
|
+
window.navigation&&navigation.addEventListener('navigate',function(e){var p=new URL(e.destination.url).pathname;var v=function(x){var r=/\/(v\d+(?:\.\d+)?)(?:\/|$)/.exec(x);return r?r[1]:''};if(v(p)!==v(location.pathname))e.stopImmediatePropagation()})
|
|
29
|
+
</script>
|
|
30
|
+
}
|
|
22
31
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
23
32
|
@if (props.description) {
|
|
24
33
|
<meta name="description" content="{{ props.description }}" />
|
package/src/pages.d.ts
CHANGED
|
@@ -4,3 +4,9 @@ declare module 'virtual:theme-docs/pages' {
|
|
|
4
4
|
const pages: import('./nav.ts').PageInfo[]
|
|
5
5
|
export default pages
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
// Versioned docs: the version menu's data (themeDocs({ versions })), for the theme config's `versions`.
|
|
9
|
+
declare module 'virtual:theme-docs/versions' {
|
|
10
|
+
const versions: import('./nav.ts').Versions | null
|
|
11
|
+
export default versions
|
|
12
|
+
}
|
package/src/parts/sidebar.tscl
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
or the search box: the header has one on phones), and a row of links at the bottom.
|
|
10
10
|
--}}
|
|
11
11
|
@import groupIcon from './icon.tscl'
|
|
12
|
+
@import versionMenu from './version-menu.tscl'
|
|
12
13
|
<script lang="ts">
|
|
13
14
|
import { groupsOf, sectionKey } from '../nav.ts'
|
|
14
15
|
import type { ThemeConfig } from '../nav.ts'
|
|
@@ -73,6 +74,7 @@
|
|
|
73
74
|
>
|
|
74
75
|
</button>
|
|
75
76
|
}
|
|
77
|
+
@if (props.config.versions) {<versionMenu [versions]="props.config.versions" [path]="props.path" />}
|
|
76
78
|
<slot name="top"></slot>
|
|
77
79
|
@for (group of groupsOf(props.config.sections, props.path); track group.title) {
|
|
78
80
|
<details
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
The version menu (versioned docs), at the top of the sidebar when the theme config has `versions`: the live docs'
|
|
3
|
+
version, the same page in each archived one, and the site's own links. A <details>: it opens without JavaScript.
|
|
4
|
+
Archived versions are other builds: a link to one loads the page (the guard in page-head.tscl).
|
|
5
|
+
--}}
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
import type { Versions } from '../nav.ts'
|
|
8
|
+
const { versions, path = '/' } = props<{
|
|
9
|
+
/** The menu's data: themeDocs({ versions }) serves it as virtual:theme-docs/versions. */
|
|
10
|
+
versions: Versions
|
|
11
|
+
/** The page being read: each version's link goes to it. */
|
|
12
|
+
path?: string
|
|
13
|
+
}>()
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<details class="version-menu group relative mb-3 text-[13px]">
|
|
17
|
+
<summary
|
|
18
|
+
class="inline-flex cursor-pointer list-none items-center gap-1.5 rounded-full border border-rule px-2.5 py-1 font-semibold text-muted hover:border-accent hover:text-fg group-open:border-accent group-open:text-fg after:text-[10px] after:content-['▾'] [&::-webkit-details-marker]:hidden"
|
|
19
|
+
>{{ versions.current }}</summary
|
|
20
|
+
>
|
|
21
|
+
<ul class="m-0 mt-1.5 list-none rounded-lg border border-rule bg-bg p-1">
|
|
22
|
+
<li><a [href]="path" aria-current="true" class="block rounded-md px-2 py-1.5 font-semibold text-accent no-underline hover:bg-surface">{{ versions.current }} (latest)</a></li>
|
|
23
|
+
@for (v of versions.archived.slice(0, versions.menu ?? Infinity); track v.version) {
|
|
24
|
+
<li><a [href]="`/v${v.version}${path === '/' ? '/' : path}`" class="block rounded-md px-2 py-1.5 text-fg no-underline hover:bg-surface">v{{ v.version }}</a></li>
|
|
25
|
+
}
|
|
26
|
+
@if (versions.all) {
|
|
27
|
+
<li><a [href]="versions.all" class="block rounded-md px-2 py-1.5 text-fg no-underline hover:bg-surface">All versions</a></li>
|
|
28
|
+
}
|
|
29
|
+
@for (l of versions.links; track l.href) {
|
|
30
|
+
<li><a [href]="l.href" class="block rounded-md px-2 py-1.5 text-fg no-underline hover:bg-surface">{{ l.label }}</a></li>
|
|
31
|
+
}
|
|
32
|
+
</ul>
|
|
33
|
+
</details>
|
package/src/theme.css
CHANGED
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
--default-transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
+
.relative {
|
|
24
|
+
position: relative;
|
|
25
|
+
}
|
|
23
26
|
.m-0 {
|
|
24
27
|
margin: 0px;
|
|
25
28
|
}
|
|
@@ -38,6 +41,9 @@
|
|
|
38
41
|
.mt-0\.5 {
|
|
39
42
|
margin-top: calc(var(--spacing) * 0.5);
|
|
40
43
|
}
|
|
44
|
+
.mt-1\.5 {
|
|
45
|
+
margin-top: calc(var(--spacing) * 1.5);
|
|
46
|
+
}
|
|
41
47
|
.mt-3 {
|
|
42
48
|
margin-top: calc(var(--spacing) * 3);
|
|
43
49
|
}
|
|
@@ -74,6 +80,9 @@
|
|
|
74
80
|
.grid {
|
|
75
81
|
display: grid;
|
|
76
82
|
}
|
|
83
|
+
.inline-flex {
|
|
84
|
+
display: inline-flex;
|
|
85
|
+
}
|
|
77
86
|
.size-2 {
|
|
78
87
|
width: calc(var(--spacing) * 2);
|
|
79
88
|
height: calc(var(--spacing) * 2);
|
|
@@ -131,6 +140,9 @@
|
|
|
131
140
|
.gap-1 {
|
|
132
141
|
gap: var(--spacing);
|
|
133
142
|
}
|
|
143
|
+
.gap-1\.5 {
|
|
144
|
+
gap: calc(var(--spacing) * 1.5);
|
|
145
|
+
}
|
|
134
146
|
.gap-2 {
|
|
135
147
|
gap: calc(var(--spacing) * 2);
|
|
136
148
|
}
|
|
@@ -222,6 +234,9 @@
|
|
|
222
234
|
.px-1\.5 {
|
|
223
235
|
padding-inline: calc(var(--spacing) * 1.5);
|
|
224
236
|
}
|
|
237
|
+
.px-2 {
|
|
238
|
+
padding-inline: calc(var(--spacing) * 2);
|
|
239
|
+
}
|
|
225
240
|
.px-2\.5 {
|
|
226
241
|
padding-inline: calc(var(--spacing) * 2.5);
|
|
227
242
|
}
|
|
@@ -364,10 +379,27 @@
|
|
|
364
379
|
:is(:where(.group):is([open], :popover-open, :open) .group-open\:rotate-90) {
|
|
365
380
|
rotate: 90deg;
|
|
366
381
|
}
|
|
382
|
+
:is(:where(.group):is([open], :popover-open, :open) .group-open\:border-accent) {
|
|
383
|
+
border-color: var(--accent);
|
|
384
|
+
}
|
|
385
|
+
:is(:where(.group):is([open], :popover-open, :open) .group-open\:text-fg) {
|
|
386
|
+
color: var(--fg);
|
|
387
|
+
}
|
|
388
|
+
.after\:text-\[10px\]::after {
|
|
389
|
+
content: var(--tw-content);
|
|
390
|
+
font-size: 10px;
|
|
391
|
+
}
|
|
392
|
+
.after\:content-\[\'▾\'\]::after {
|
|
393
|
+
--tw-content: '▾';
|
|
394
|
+
content: var(--tw-content);
|
|
395
|
+
}
|
|
367
396
|
@media (hover: hover) {
|
|
368
397
|
.hover\:border-accent:hover {
|
|
369
398
|
border-color: var(--accent);
|
|
370
399
|
}
|
|
400
|
+
.hover\:bg-surface:hover {
|
|
401
|
+
background-color: var(--surface);
|
|
402
|
+
}
|
|
371
403
|
.hover\:text-fg:hover {
|
|
372
404
|
color: var(--fg);
|
|
373
405
|
}
|
|
@@ -1651,6 +1683,11 @@ html[data-cl-navigating]::before {
|
|
|
1651
1683
|
syntax: "*";
|
|
1652
1684
|
inherits: false;
|
|
1653
1685
|
}
|
|
1686
|
+
@property --tw-content {
|
|
1687
|
+
syntax: "*";
|
|
1688
|
+
initial-value: "";
|
|
1689
|
+
inherits: false;
|
|
1690
|
+
}
|
|
1654
1691
|
@property --tw-shadow {
|
|
1655
1692
|
syntax: "*";
|
|
1656
1693
|
inherits: false;
|
|
@@ -1723,6 +1760,7 @@ html[data-cl-navigating]::before {
|
|
|
1723
1760
|
--tw-leading: initial;
|
|
1724
1761
|
--tw-font-weight: initial;
|
|
1725
1762
|
--tw-tracking: initial;
|
|
1763
|
+
--tw-content: "";
|
|
1726
1764
|
--tw-shadow: 0 0 #0000;
|
|
1727
1765
|
--tw-shadow-color: initial;
|
|
1728
1766
|
--tw-shadow-alpha: 100%;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Every version of the docs, for a page of its own (versioned docs; themeDocs({ versions: { all: '/versions' } }) links
|
|
3
|
+
it from the menu): the live docs, then each archived version served, with the release it was built from.
|
|
4
|
+
|
|
5
|
+
<version-list [versions]="versions" /> (import versions from 'virtual:theme-docs/versions')
|
|
6
|
+
--}}
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import type { Versions } from './nav.ts'
|
|
9
|
+
const { versions } = props<{
|
|
10
|
+
/** The versions, as themeDocs({ versions }) serves them (virtual:theme-docs/versions). */
|
|
11
|
+
versions: Versions
|
|
12
|
+
}>()
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<table class="version-list">
|
|
16
|
+
<thead>
|
|
17
|
+
<tr><th scope="col">Version</th><th scope="col">Release</th></tr>
|
|
18
|
+
</thead>
|
|
19
|
+
<tbody>
|
|
20
|
+
<tr><td><a href="/">{{ versions.current }}</a> (latest)</td><td>{{ versions.current }}</td></tr>
|
|
21
|
+
@for (v of versions.archived; track v.version) {
|
|
22
|
+
<tr><td><a [href]="`/v${v.version}/`">v{{ v.version }}</a></td><td>{{ v.tag }}</td></tr>
|
|
23
|
+
}
|
|
24
|
+
</tbody>
|
|
25
|
+
</table>
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// clarusjs-docs-versions: versioned docs (versions.js). Run from the docs site's folder (where versions.json is).
|
|
3
|
+
//
|
|
4
|
+
// clarusjs-docs-versions snapshot <tag> build that release's docs under <base>v<version>/, marked and
|
|
5
|
+
// checked, packed into .versions/docs-v<version>.tar.gz
|
|
6
|
+
// clarusjs-docs-versions snapshot --missing every release not archived yet (the release workflow's step)
|
|
7
|
+
// --upload attach each to its GitHub release (gh) and list it in versions.json
|
|
8
|
+
// --base /docs/ the site's base path, if it has one
|
|
9
|
+
// clarusjs-docs-versions fetch [--from <dir>] after the site's build: unpack the served archives into it, and
|
|
10
|
+
// write the _redirects lines (each version's 404, retired ones)
|
|
11
|
+
// clarusjs-docs-versions list each archived version, its tag, and whether it's served
|
|
12
|
+
// @see https://clarusjs.com/docs-sites/versioned-docs
|
|
13
|
+
import { execFileSync } from 'node:child_process'
|
|
14
|
+
import { appendFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
15
|
+
import { tmpdir } from 'node:os'
|
|
16
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
17
|
+
import { assetName, downloadUrl, lineOf, markSnapshot, newestOf, pagePath, plan, settingsOf, toArchive } from './versions.js'
|
|
18
|
+
|
|
19
|
+
const site = process.cwd()
|
|
20
|
+
const listFile = join(site, 'versions.json')
|
|
21
|
+
const args = process.argv.slice(2)
|
|
22
|
+
/** @param {string} name */
|
|
23
|
+
const flag = (name) => args.includes(name)
|
|
24
|
+
/** @param {string} name */
|
|
25
|
+
const value = (name) => (flag(name) ? args[args.indexOf(name) + 1] : undefined)
|
|
26
|
+
|
|
27
|
+
/** @param {string} cmd @param {string[]} a @param {string} [cwd] */
|
|
28
|
+
const run = (cmd, a, cwd = site) => execFileSync(cmd, a, { cwd, stdio: 'inherit' })
|
|
29
|
+
/** @param {string} cmd @param {string[]} a */
|
|
30
|
+
const read = (cmd, a) => execFileSync(cmd, a, { cwd: site, encoding: 'utf8' }).trim()
|
|
31
|
+
|
|
32
|
+
if (!existsSync(listFile)) {
|
|
33
|
+
console.error(`clarusjs-docs-versions: no versions.json in ${site}: run this from the docs site's folder`)
|
|
34
|
+
process.exit(1)
|
|
35
|
+
}
|
|
36
|
+
/** @type {import('./versions.js').VersionsFile} */
|
|
37
|
+
const file = JSON.parse(readFileSync(listFile, 'utf8'))
|
|
38
|
+
const settings = settingsOf(file, repositoryOf(site))
|
|
39
|
+
const listed = file.versions ?? []
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
if (args[0] === 'snapshot') snapshotCommand()
|
|
43
|
+
else if (args[0] === 'fetch') await fetchCommand()
|
|
44
|
+
else if (args[0] === 'list') listCommand()
|
|
45
|
+
else fail('usage: clarusjs-docs-versions snapshot <tag> | snapshot --missing [--upload] [--base /x/] | fetch [--from <dir>] | list')
|
|
46
|
+
} catch (e) {
|
|
47
|
+
// Thrown, not exited where it happened, so a snapshot's finally removes its worktree first
|
|
48
|
+
console.error(`clarusjs-docs-versions: ${e instanceof Error ? e.message : e}`)
|
|
49
|
+
process.exitCode = 1
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function listCommand() {
|
|
53
|
+
const { served, retired } = plan(file)
|
|
54
|
+
console.log(`live: ${file.latest ?? '(unknown until snapshot --missing --upload)'}`)
|
|
55
|
+
for (const a of listed) {
|
|
56
|
+
const r = retired.find((x) => x.version === a.version)
|
|
57
|
+
const status = served.includes(a.version) ? 'served' : `retired${a.retired ? '' : ' (by lines, minors or keep)'}, redirects to ${r?.to ? `/v${r.to}/` : 'the latest docs'}`
|
|
58
|
+
console.log(`v${a.version.padEnd(8)} ${a.tag.padEnd(24)} ${status}`)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function snapshotCommand() {
|
|
63
|
+
const tag = args.slice(1).find((a, i, all) => !a.startsWith('--') && all[i - 1] !== '--base')
|
|
64
|
+
const [before, after] = settings.tag.split('{version}')
|
|
65
|
+
const tags = read('git', ['tag', '-l']).split('\n')
|
|
66
|
+
const newest = newestOf(tags, settings)
|
|
67
|
+
const todo = flag('--missing')
|
|
68
|
+
? toArchive(tags, settings, listed)
|
|
69
|
+
: tag
|
|
70
|
+
? [{ version: lineOf(tag.slice(before?.length, tag.length - (after?.length ?? 0)), settings, newest[0]), tag }]
|
|
71
|
+
: fail('snapshot: name a tag, or --missing')
|
|
72
|
+
// The newest release, so deploys apply lines, minors and keep without git tags
|
|
73
|
+
if (flag('--upload') && newest.length && file.latest !== newest.join('.')) {
|
|
74
|
+
file.latest = newest.join('.')
|
|
75
|
+
writeFileSync(listFile, JSON.stringify({ ...file, versions: listed }, null, 2) + '\n')
|
|
76
|
+
}
|
|
77
|
+
if (!todo.length) console.log('Nothing to archive.')
|
|
78
|
+
for (const v of todo) {
|
|
79
|
+
const archive = snapshot(v, value('--base') ?? '/')
|
|
80
|
+
console.log(`${v.tag}: ${relative(site, archive)}`)
|
|
81
|
+
if (!flag('--upload')) continue
|
|
82
|
+
run('gh', ['release', 'upload', v.tag, archive, '--clobber'])
|
|
83
|
+
const versions = [v, ...listed.filter((a) => a.version !== v.version)]
|
|
84
|
+
versions.sort((a, b) => b.version.localeCompare(a.version, 'en', { numeric: true }))
|
|
85
|
+
listed.splice(0, listed.length, ...versions)
|
|
86
|
+
writeFileSync(listFile, JSON.stringify({ ...file, versions: listed }, null, 2) + '\n')
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Builds one release's docs in a worktree, marks every page, checks no link leaves it, packs it.
|
|
92
|
+
* @param {import('./versions.js').Archived} v
|
|
93
|
+
* @param {string} siteBase
|
|
94
|
+
*/
|
|
95
|
+
function snapshot({ version, tag }, siteBase) {
|
|
96
|
+
const base = `${siteBase}v${version}/`
|
|
97
|
+
const repo = read('git', ['rev-parse', '--show-toplevel'])
|
|
98
|
+
const tree = mkdtempSync(join(tmpdir(), 'docs-version-'))
|
|
99
|
+
try {
|
|
100
|
+
run('git', ['worktree', 'add', '--detach', tree, tag], repo)
|
|
101
|
+
const dir = join(tree, relative(repo, site))
|
|
102
|
+
const pm = packageManager(tree)
|
|
103
|
+
run('sh', ['-c', file.install ?? pm.install], tree)
|
|
104
|
+
run('sh', ['-c', (file.build ?? pm.build).replace('{base}', base)], dir)
|
|
105
|
+
const out = join(dir, settings.out)
|
|
106
|
+
/** @type {Set<string>} */
|
|
107
|
+
const escaping = new Set()
|
|
108
|
+
for (const f of readdirSync(out, { recursive: true, encoding: 'utf8' })) {
|
|
109
|
+
if (!f.endsWith('.html')) continue
|
|
110
|
+
const rel = f.replaceAll('\\', '/')
|
|
111
|
+
const html = markSnapshot(readFileSync(join(out, f), 'utf8'), { version, path: pagePath(rel), base: siteBase, name: file.name })
|
|
112
|
+
// Every root-relative link stays in the snapshot, except the banner's (to the latest docs) and a canonical link
|
|
113
|
+
// (for search engines, not followed)
|
|
114
|
+
const followed = html.replace(/<div class="old-version-banner"[\s\S]*?<\/div>/, '').replace(/<link rel="canonical"[^>]*>/g, '')
|
|
115
|
+
for (const [, url] of followed.matchAll(/(?:href|src)="(\/[^"]*)"/g)) {
|
|
116
|
+
if (!url?.startsWith(base) && !url?.startsWith('//')) escaping.add(`${rel}: ${url}`)
|
|
117
|
+
}
|
|
118
|
+
writeFileSync(join(out, f), html)
|
|
119
|
+
}
|
|
120
|
+
if (escaping.size) {
|
|
121
|
+
fail(`${tag}: ${escaping.size} links leave ${base}, so its docs don't build under a base; archive later releases ("from" in versions.json). E.g.\n ${[...escaping].slice(0, 5).join('\n ')}`)
|
|
122
|
+
}
|
|
123
|
+
mkdirSync(join(site, '.versions'), { recursive: true })
|
|
124
|
+
const archive = join(site, '.versions', assetName(version))
|
|
125
|
+
run('tar', ['-czf', archive, '-C', out, '.'])
|
|
126
|
+
return archive
|
|
127
|
+
} finally {
|
|
128
|
+
rmSync(tree, { recursive: true, force: true })
|
|
129
|
+
run('git', ['worktree', 'prune'], repo)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function fetchCommand() {
|
|
134
|
+
const from = value('--from')
|
|
135
|
+
const siteBase = value('--base') ?? '/'
|
|
136
|
+
// Vercel (clarusjs's adapter: 'vercel') reads .vercel/output, made from the build: _redirects lines and clean URLs
|
|
137
|
+
// are routes and overrides in its config.json, so they go there. Any other host: the built folder and _redirects.
|
|
138
|
+
const vercel = join(site, '.vercel/output/config.json')
|
|
139
|
+
const onVercel = existsSync(vercel)
|
|
140
|
+
const out = onVercel ? join(site, '.vercel/output/static', `.${siteBase}`) : join(site, settings.out)
|
|
141
|
+
if (!existsSync(out)) fail(`${out} doesn't exist: build the site first`)
|
|
142
|
+
/** @type {{ routes: object[], overrides: Record<string, { path: string }> }} */
|
|
143
|
+
const config = onVercel ? JSON.parse(readFileSync(vercel, 'utf8')) : { routes: [], overrides: {} }
|
|
144
|
+
/** @type {object[]} */ const redirects = []
|
|
145
|
+
/** @type {object[]} */ const notFound = []
|
|
146
|
+
/** @param {string} v */
|
|
147
|
+
const re = (v) => `^${siteBase}v${v.replaceAll('.', '\\.')}`
|
|
148
|
+
const { served, retired } = plan(file)
|
|
149
|
+
for (const { version, to } of retired) {
|
|
150
|
+
// Old links and bookmarks go on to the same page: in its major's archive if served (/v2.3/x → /v2/x), else the
|
|
151
|
+
// latest docs
|
|
152
|
+
const target = to ? `${siteBase}v${to}/` : siteBase
|
|
153
|
+
if (onVercel) redirects.push({ src: `${re(version)}(?:/(.*))?$`, status: 301, headers: { Location: `${target}$1` } })
|
|
154
|
+
else appendFileSync(join(out, '_redirects'), `${siteBase}v${version}/* ${target}:splat 301\n`)
|
|
155
|
+
console.log(`v${version}: retired, redirected to ${to ? `/v${to}/` : 'the latest docs'}`)
|
|
156
|
+
}
|
|
157
|
+
for (const entry of listed.filter((a) => served.includes(a.version))) {
|
|
158
|
+
const { version } = entry
|
|
159
|
+
const archive = from ? resolve(from, assetName(version)) : join(site, '.versions', assetName(version))
|
|
160
|
+
if (!from) {
|
|
161
|
+
const url = downloadUrl(settings, entry)
|
|
162
|
+
const bytes = await download(url)
|
|
163
|
+
mkdirSync(dirname(archive), { recursive: true })
|
|
164
|
+
writeFileSync(archive, bytes)
|
|
165
|
+
}
|
|
166
|
+
const dir = join(out, `v${version}`)
|
|
167
|
+
rmSync(dir, { recursive: true, force: true })
|
|
168
|
+
mkdirSync(dir, { recursive: true })
|
|
169
|
+
run('tar', ['-xzf', archive, '-C', dir])
|
|
170
|
+
// A page that didn't exist yet in that version: its own 404 (hosts serve files first, so only missing ones)
|
|
171
|
+
if (onVercel) {
|
|
172
|
+
notFound.push({ src: `${re(version)}/.*`, status: 404, dest: `${siteBase}v${version}/404.html` })
|
|
173
|
+
for (const f of readdirSync(dir, { recursive: true, encoding: 'utf8' })) {
|
|
174
|
+
const rel = `${siteBase.slice(1)}v${version}/${f.replaceAll('\\', '/')}`
|
|
175
|
+
if (rel.endsWith('/index.html')) config.overrides[rel] = { path: rel.replace(/\/index\.html$/, '') }
|
|
176
|
+
}
|
|
177
|
+
} else appendFileSync(join(out, '_redirects'), `${siteBase}v${version}/* ${siteBase}v${version}/404.html 404\n`)
|
|
178
|
+
console.log(`v${version} (${entry.tag})`)
|
|
179
|
+
}
|
|
180
|
+
if (!onVercel) return
|
|
181
|
+
// Retired versions first (before the filesystem); each version's 404 before the site's own
|
|
182
|
+
const error = config.routes.findIndex((r) => 'handle' in r && r.handle === 'error')
|
|
183
|
+
config.routes.splice(error < 0 ? config.routes.length : error + 1, 0, ...notFound)
|
|
184
|
+
config.routes.unshift(...redirects)
|
|
185
|
+
writeFileSync(vercel, JSON.stringify(config, null, 2))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The install and build commands, from the lockfile at the repository's root. @param {string} root */
|
|
189
|
+
function packageManager(root) {
|
|
190
|
+
if (existsSync(join(root, 'pnpm-lock.yaml'))) return { install: 'pnpm install --frozen-lockfile', build: 'pnpm run build --base {base}' }
|
|
191
|
+
if (existsSync(join(root, 'yarn.lock'))) return { install: 'yarn install --immutable', build: 'yarn run build --base {base}' }
|
|
192
|
+
if (existsSync(join(root, 'bun.lock')) || existsSync(join(root, 'bun.lockb'))) return { install: 'bun install --frozen-lockfile', build: 'bun run build --base {base}' }
|
|
193
|
+
return { install: 'npm ci', build: 'npm run build -- --base {base}' }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** package.json's repository, from the site's folder up. @param {string} dir @returns {string | undefined} */
|
|
197
|
+
function repositoryOf(dir) {
|
|
198
|
+
for (let d = dir; ; d = dirname(d)) {
|
|
199
|
+
const pkg = join(d, 'package.json')
|
|
200
|
+
if (existsSync(pkg)) {
|
|
201
|
+
const r = JSON.parse(readFileSync(pkg, 'utf8')).repository
|
|
202
|
+
if (r) return typeof r === 'string' ? r : r.url
|
|
203
|
+
}
|
|
204
|
+
if (dirname(d) === d) return undefined
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** @param {string} message @returns {never} */
|
|
209
|
+
function fail(message) {
|
|
210
|
+
throw new Error(message)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* An archive's bytes: 30 seconds per try, and one more try after a failure or a 5xx (a deploy shouldn't fail or hang
|
|
215
|
+
* on one bad response from the host).
|
|
216
|
+
* @param {string} url
|
|
217
|
+
*/
|
|
218
|
+
async function download(url) {
|
|
219
|
+
for (let attempt = 1; ; attempt++) {
|
|
220
|
+
try {
|
|
221
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(30_000) })
|
|
222
|
+
if (res.ok) return Buffer.from(await res.arrayBuffer())
|
|
223
|
+
if (res.status < 500 || attempt === 2) fail(`${url}: ${res.status}`)
|
|
224
|
+
} catch (e) {
|
|
225
|
+
if (attempt === 2) throw e
|
|
226
|
+
}
|
|
227
|
+
await new Promise((r) => setTimeout(r, 2000))
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
export type Archived = {
|
|
2
|
+
/**
|
|
3
|
+
* as shown and in the URL (/v0.19/): '0.19' before 1.0, '1' from it
|
|
4
|
+
*/
|
|
5
|
+
version: string;
|
|
6
|
+
/**
|
|
7
|
+
* the release tag it's built from
|
|
8
|
+
*/
|
|
9
|
+
tag: string;
|
|
10
|
+
/**
|
|
11
|
+
* taken down: not served or listed; its URLs redirect to the latest docs
|
|
12
|
+
*/
|
|
13
|
+
retired?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type VersionsFile = {
|
|
16
|
+
/**
|
|
17
|
+
* how releases are tagged, with {version}: 'v{version}' (the default), 'mylib@{version}'
|
|
18
|
+
*/
|
|
19
|
+
tag?: string;
|
|
20
|
+
/**
|
|
21
|
+
* the first release to archive ('0.19.0'): docs older than it may not build under a base
|
|
22
|
+
*/
|
|
23
|
+
from?: string;
|
|
24
|
+
/**
|
|
25
|
+
* how releases group into versions: 'auto' (the default) by minor before
|
|
26
|
+
* 1.0 and by major from it; 'major' or 'minor' always
|
|
27
|
+
*/
|
|
28
|
+
lines?: 'auto' | 'major' | 'minor';
|
|
29
|
+
/**
|
|
30
|
+
* by major: the newest major's latest minors are versions of their own too (2.4, 2.3 while
|
|
31
|
+
* 2.5 is live); they give way to the major's archive once the next major is out
|
|
32
|
+
*/
|
|
33
|
+
minors?: number;
|
|
34
|
+
/**
|
|
35
|
+
* serve at most this many archived versions, newest first; older ones are retired
|
|
36
|
+
*/
|
|
37
|
+
keep?: number;
|
|
38
|
+
/**
|
|
39
|
+
* the newest release, written by snapshot: deploys apply the rules above without git tags
|
|
40
|
+
*/
|
|
41
|
+
latest?: string;
|
|
42
|
+
/**
|
|
43
|
+
* where archives are, with {tag} and {asset}; default: the repository's GitHub releases
|
|
44
|
+
*/
|
|
45
|
+
download?: string;
|
|
46
|
+
/**
|
|
47
|
+
* the project's name, for the banner ("the docs for <name> 0.19")
|
|
48
|
+
*/
|
|
49
|
+
name?: string;
|
|
50
|
+
/**
|
|
51
|
+
* the built site, from the site's folder (default dist/client)
|
|
52
|
+
*/
|
|
53
|
+
out?: string;
|
|
54
|
+
/**
|
|
55
|
+
* the install command (default: from the lockfile)
|
|
56
|
+
*/
|
|
57
|
+
install?: string;
|
|
58
|
+
/**
|
|
59
|
+
* the build command; {base} is the version's base path (default: from the lockfile)
|
|
60
|
+
*/
|
|
61
|
+
build?: string;
|
|
62
|
+
/**
|
|
63
|
+
* the archived versions, newest first
|
|
64
|
+
*/
|
|
65
|
+
versions?: Archived[];
|
|
66
|
+
};
|
|
67
|
+
export type Settings = Required<Pick<VersionsFile, 'tag' | 'out'>> & VersionsFile & {
|
|
68
|
+
repository?: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {object} Archived
|
|
72
|
+
* @property {string} version as shown and in the URL (/v0.19/): '0.19' before 1.0, '1' from it
|
|
73
|
+
* @property {string} tag the release tag it's built from
|
|
74
|
+
* @property {boolean} [retired] taken down: not served or listed; its URLs redirect to the latest docs
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* @typedef {object} VersionsFile versions.json, next to the site's Vite config
|
|
78
|
+
* @property {string} [tag] how releases are tagged, with {version}: 'v{version}' (the default), 'mylib@{version}'
|
|
79
|
+
* @property {string} [from] the first release to archive ('0.19.0'): docs older than it may not build under a base
|
|
80
|
+
* @property {'auto' | 'major' | 'minor'} [lines] how releases group into versions: 'auto' (the default) by minor before
|
|
81
|
+
* 1.0 and by major from it; 'major' or 'minor' always
|
|
82
|
+
* @property {number} [minors] by major: the newest major's latest minors are versions of their own too (2.4, 2.3 while
|
|
83
|
+
* 2.5 is live); they give way to the major's archive once the next major is out
|
|
84
|
+
* @property {number} [keep] serve at most this many archived versions, newest first; older ones are retired
|
|
85
|
+
* @property {string} [latest] the newest release, written by snapshot: deploys apply the rules above without git tags
|
|
86
|
+
* @property {string} [download] where archives are, with {tag} and {asset}; default: the repository's GitHub releases
|
|
87
|
+
* @property {string} [name] the project's name, for the banner ("the docs for <name> 0.19")
|
|
88
|
+
* @property {string} [out] the built site, from the site's folder (default dist/client)
|
|
89
|
+
* @property {string} [install] the install command (default: from the lockfile)
|
|
90
|
+
* @property {string} [build] the build command; {base} is the version's base path (default: from the lockfile)
|
|
91
|
+
* @property {Archived[]} [versions] the archived versions, newest first
|
|
92
|
+
*/
|
|
93
|
+
/** @typedef {Required<Pick<VersionsFile, 'tag' | 'out'>> & VersionsFile & { repository?: string }} Settings */
|
|
94
|
+
/**
|
|
95
|
+
* The settings with their defaults; `repository` (package.json's) for the default download address.
|
|
96
|
+
* @param {VersionsFile} file
|
|
97
|
+
* @param {string} [repository]
|
|
98
|
+
* @returns {Settings}
|
|
99
|
+
*/
|
|
100
|
+
export declare function settingsOf(file: VersionsFile, repository?: string): Settings;
|
|
101
|
+
/** The archive's file name. @param {string} version */
|
|
102
|
+
export declare const assetName: (version: string) => string;
|
|
103
|
+
/**
|
|
104
|
+
* Where an archive downloads from.
|
|
105
|
+
* @param {Settings} s
|
|
106
|
+
* @param {Archived} entry
|
|
107
|
+
*/
|
|
108
|
+
export declare function downloadUrl(s: Settings, entry: Archived): string;
|
|
109
|
+
/**
|
|
110
|
+
* The line (archived version) a release belongs to: by `lines`, and the newest major's minors with `minors`.
|
|
111
|
+
* @param {string} v '2.3.1'
|
|
112
|
+
* @param {Partial<Settings>} [s]
|
|
113
|
+
* @param {number} [newestMajor]
|
|
114
|
+
*/
|
|
115
|
+
export declare function lineOf(v: string, s?: Partial<Settings>, newestMajor?: number): string;
|
|
116
|
+
/**
|
|
117
|
+
* The versions to archive that aren't listed yet, newest first: the latest patch of each line from `from`, except the
|
|
118
|
+
* newest release's line (the live docs).
|
|
119
|
+
* @param {string[]} tags the repository's tags
|
|
120
|
+
* @param {Settings} s
|
|
121
|
+
* @param {Archived[]} listed
|
|
122
|
+
* @returns {Archived[]}
|
|
123
|
+
*/
|
|
124
|
+
export declare function toArchive(tags: string[], s: Settings, listed: Archived[]): Archived[];
|
|
125
|
+
/** The newest release's version numbers ([] without any). @param {string[]} tags @param {Settings} s */
|
|
126
|
+
export declare function newestOf(tags: string[], s: Settings): number[];
|
|
127
|
+
/**
|
|
128
|
+
* What a deploy serves, from versions.json alone: the archived versions listed, less those retired by hand, minors
|
|
129
|
+
* past `minors` (or of an older major than `latest`'s), and those past `keep`. A retired minor whose major is served
|
|
130
|
+
* redirects there (/v2.3/x → /v2/x); others to the latest docs (`to` '').
|
|
131
|
+
* @param {VersionsFile} file
|
|
132
|
+
* @returns {{ served: string[], retired: { version: string, to: string }[] }}
|
|
133
|
+
*/
|
|
134
|
+
export declare function plan(file: VersionsFile): {
|
|
135
|
+
served: string[];
|
|
136
|
+
retired: {
|
|
137
|
+
version: string;
|
|
138
|
+
to: string;
|
|
139
|
+
}[];
|
|
140
|
+
};
|
|
141
|
+
/** An archived version's pages: navigations leaving its prefix (/v0.19/, under the base) load the page. @param {string} prefix */
|
|
142
|
+
export declare const archivedGuard: (prefix: string) => string;
|
|
143
|
+
/**
|
|
144
|
+
* Every page the theme renders (the live docs, and archives built with it): a navigation into another version (a
|
|
145
|
+
* /v0.19/ or /v1 segment, under any base) loads the page; within the same version it swaps as usual. A page of the
|
|
146
|
+
* site's own with such a segment would load instead of swap: slower, never wrong.
|
|
147
|
+
*/
|
|
148
|
+
export declare const LATEST_GUARD: string;
|
|
149
|
+
/**
|
|
150
|
+
* An archived page's HTML, marked: a banner after `<body…>` saying which version it is, with a link to the same page in
|
|
151
|
+
* the latest docs; in a full document, the navigation guard first in <head>, its canonical link pointed at the latest
|
|
152
|
+
* page, and noindex. Insertions only: built under its own base, its links already stay inside it.
|
|
153
|
+
* @param {string} html
|
|
154
|
+
* @param {{ version: string, path: string, base: string, name?: string }} o the page's path without the base
|
|
155
|
+
*/
|
|
156
|
+
export declare function markSnapshot(html: string, { version, path, base, name }: {
|
|
157
|
+
version: string;
|
|
158
|
+
path: string;
|
|
159
|
+
base: string;
|
|
160
|
+
name?: string;
|
|
161
|
+
}): string;
|
|
162
|
+
/** A page's path from its file in the built site: 'guide/lists/index.html' → '/guide/lists'. @param {string} file */
|
|
163
|
+
export declare function pagePath(file: string): string;
|
package/src/versions.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// Versioned docs: each released line's docs kept at <base>v<version>/, built from its release tag and frozen. This
|
|
2
|
+
// file is the part with no I/O: the settings, which versions to archive, how an archived page is marked, and the
|
|
3
|
+
// navigation guards. versions-cli.js builds and fetches the archives; vite.js lists them for the version menu.
|
|
4
|
+
// @see https://clarusjs.com/docs-sites/versioned-docs
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {object} Archived
|
|
8
|
+
* @property {string} version as shown and in the URL (/v0.19/): '0.19' before 1.0, '1' from it
|
|
9
|
+
* @property {string} tag the release tag it's built from
|
|
10
|
+
* @property {boolean} [retired] taken down: not served or listed; its URLs redirect to the latest docs
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} VersionsFile versions.json, next to the site's Vite config
|
|
14
|
+
* @property {string} [tag] how releases are tagged, with {version}: 'v{version}' (the default), 'mylib@{version}'
|
|
15
|
+
* @property {string} [from] the first release to archive ('0.19.0'): docs older than it may not build under a base
|
|
16
|
+
* @property {'auto' | 'major' | 'minor'} [lines] how releases group into versions: 'auto' (the default) by minor before
|
|
17
|
+
* 1.0 and by major from it; 'major' or 'minor' always
|
|
18
|
+
* @property {number} [minors] by major: the newest major's latest minors are versions of their own too (2.4, 2.3 while
|
|
19
|
+
* 2.5 is live); they give way to the major's archive once the next major is out
|
|
20
|
+
* @property {number} [keep] serve at most this many archived versions, newest first; older ones are retired
|
|
21
|
+
* @property {string} [latest] the newest release, written by snapshot: deploys apply the rules above without git tags
|
|
22
|
+
* @property {string} [download] where archives are, with {tag} and {asset}; default: the repository's GitHub releases
|
|
23
|
+
* @property {string} [name] the project's name, for the banner ("the docs for <name> 0.19")
|
|
24
|
+
* @property {string} [out] the built site, from the site's folder (default dist/client)
|
|
25
|
+
* @property {string} [install] the install command (default: from the lockfile)
|
|
26
|
+
* @property {string} [build] the build command; {base} is the version's base path (default: from the lockfile)
|
|
27
|
+
* @property {Archived[]} [versions] the archived versions, newest first
|
|
28
|
+
*/
|
|
29
|
+
/** @typedef {Required<Pick<VersionsFile, 'tag' | 'out'>> & VersionsFile & { repository?: string }} Settings */
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The settings with their defaults; `repository` (package.json's) for the default download address.
|
|
33
|
+
* @param {VersionsFile} file
|
|
34
|
+
* @param {string} [repository]
|
|
35
|
+
* @returns {Settings}
|
|
36
|
+
*/
|
|
37
|
+
export function settingsOf(file, repository) {
|
|
38
|
+
return { tag: 'v{version}', out: 'dist/client', ...file, repository }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The archive's file name. @param {string} version */
|
|
42
|
+
export const assetName = (version) => `docs-v${version}.tar.gz`
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Where an archive downloads from.
|
|
46
|
+
* @param {Settings} s
|
|
47
|
+
* @param {Archived} entry
|
|
48
|
+
*/
|
|
49
|
+
export function downloadUrl(s, entry) {
|
|
50
|
+
let pattern = s.download
|
|
51
|
+
if (!pattern) {
|
|
52
|
+
// https://github.com/o/r(.git), git+https…, git@github.com:o/r.git, github:o/r; dots are allowed in names
|
|
53
|
+
const repo = /github(?:\.com[/:]|:)([^/\s]+\/[^/\s#]+?)(?:\.git)?(?:[/#]|$)/.exec(s.repository ?? '')?.[1]
|
|
54
|
+
if (!repo) throw new Error('versions.json: set "download" (with {tag} and {asset}); no GitHub repository in package.json to default to')
|
|
55
|
+
pattern = `https://github.com/${repo}/releases/download/{tag}/{asset}`
|
|
56
|
+
}
|
|
57
|
+
return pattern.replace('{tag}', encodeURIComponent(entry.tag)).replace('{asset}', assetName(entry.version))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The line (archived version) a release belongs to: by `lines`, and the newest major's minors with `minors`.
|
|
62
|
+
* @param {string} v '2.3.1'
|
|
63
|
+
* @param {Partial<Settings>} [s]
|
|
64
|
+
* @param {number} [newestMajor]
|
|
65
|
+
*/
|
|
66
|
+
export function lineOf(v, s = {}, newestMajor) {
|
|
67
|
+
const [major, minor] = /** @type {[string, string]} */ (v.split('.'))
|
|
68
|
+
const lines = s.lines ?? 'auto'
|
|
69
|
+
if (lines === 'minor' || (lines === 'auto' && major === '0')) return `${major}.${minor}`
|
|
70
|
+
return s.minors && Number(major) === newestMajor ? `${major}.${minor}` : major
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A 1.0+ minor line under major grouping (`minors`), as opposed to a major or a 0.x minor. @param {string} line @param {Partial<Settings>} s */
|
|
74
|
+
const isMinorOfMajor = (line, s) => (s.lines ?? 'auto') !== 'minor' && line.includes('.') && !line.startsWith('0.')
|
|
75
|
+
|
|
76
|
+
/** @param {number[]} a @param {number[]} b */
|
|
77
|
+
const cmp = (a, b) => (a[0] ?? 0) - (b[0] ?? 0) || (a[1] ?? 0) - (b[1] ?? 0) || (a[2] ?? 0) - (b[2] ?? 0)
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The versions to archive that aren't listed yet, newest first: the latest patch of each line from `from`, except the
|
|
81
|
+
* newest release's line (the live docs).
|
|
82
|
+
* @param {string[]} tags the repository's tags
|
|
83
|
+
* @param {Settings} s
|
|
84
|
+
* @param {Archived[]} listed
|
|
85
|
+
* @returns {Archived[]}
|
|
86
|
+
*/
|
|
87
|
+
export function toArchive(tags, s, listed) {
|
|
88
|
+
const releases = releasesOf(tags, s)
|
|
89
|
+
const newest = newestOf(tags, s)
|
|
90
|
+
const from = (s.from ?? '0.0.0').split('.').map(Number)
|
|
91
|
+
/** @type {Map<string, { tag: string, v: number[] }>} */
|
|
92
|
+
const latest = new Map()
|
|
93
|
+
for (const { tag, v } of releases) {
|
|
94
|
+
if (cmp(v, from) < 0) continue
|
|
95
|
+
const line = lineOf(v.join('.'), s, newest[0])
|
|
96
|
+
const seen = latest.get(line)
|
|
97
|
+
if (!seen || cmp(v, seen.v) > 0) latest.set(line, { tag, v })
|
|
98
|
+
}
|
|
99
|
+
latest.delete(lineOf(newest.join('.'), s, newest[0]))
|
|
100
|
+
// The newest major's minors: only its latest `minors` (older ones would be retired at once)
|
|
101
|
+
const minors = [...latest].filter(([line]) => isMinorOfMajor(line, s)).sort(([, a], [, b]) => cmp(b.v, a.v))
|
|
102
|
+
for (const [line] of minors.slice(s.minors ?? 0)) latest.delete(line)
|
|
103
|
+
const skip = new Set(listed.map((a) => a.version))
|
|
104
|
+
return [...latest]
|
|
105
|
+
.filter(([line]) => !skip.has(line))
|
|
106
|
+
.sort(([, a], [, b]) => cmp(b.v, a.v))
|
|
107
|
+
.map(([version, { tag }]) => ({ version, tag }))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The releases among `tags` (by the `tag` pattern; no prereleases). @param {string[]} tags @param {Settings} s */
|
|
111
|
+
function releasesOf(tags, s) {
|
|
112
|
+
const [before = '', after = ''] = s.tag.split('{version}')
|
|
113
|
+
return tags.flatMap((tag) => {
|
|
114
|
+
if (!tag.startsWith(before) || !tag.endsWith(after)) return []
|
|
115
|
+
const version = tag.slice(before.length, tag.length - after.length)
|
|
116
|
+
return /^\d+\.\d+\.\d+$/.test(version) ? [{ tag, v: version.split('.').map(Number) }] : []
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The newest release's version numbers ([] without any). @param {string[]} tags @param {Settings} s */
|
|
121
|
+
export function newestOf(tags, s) {
|
|
122
|
+
/** @type {number[]} */
|
|
123
|
+
let newest = []
|
|
124
|
+
for (const { v } of releasesOf(tags, s)) if (cmp(v, newest) > 0) newest = v
|
|
125
|
+
return newest
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* What a deploy serves, from versions.json alone: the archived versions listed, less those retired by hand, minors
|
|
130
|
+
* past `minors` (or of an older major than `latest`'s), and those past `keep`. A retired minor whose major is served
|
|
131
|
+
* redirects there (/v2.3/x → /v2/x); others to the latest docs (`to` '').
|
|
132
|
+
* @param {VersionsFile} file
|
|
133
|
+
* @returns {{ served: string[], retired: { version: string, to: string }[] }}
|
|
134
|
+
*/
|
|
135
|
+
export function plan(file) {
|
|
136
|
+
const s = settingsOf(file)
|
|
137
|
+
const newestMajor = file.latest ? Number(file.latest.split('.')[0]) : undefined
|
|
138
|
+
/** @type {string[]} */ const served = []
|
|
139
|
+
/** @type {Archived[]} */ const retired = []
|
|
140
|
+
let minors = 0
|
|
141
|
+
for (const a of file.versions ?? []) {
|
|
142
|
+
let ok = !a.retired
|
|
143
|
+
if (ok && newestMajor !== undefined && isMinorOfMajor(a.version, s)) {
|
|
144
|
+
ok = Number(a.version.split('.')[0]) === newestMajor && ++minors <= (s.minors ?? 0)
|
|
145
|
+
}
|
|
146
|
+
if (ok && s.keep !== undefined && served.length >= s.keep) ok = false
|
|
147
|
+
if (ok) served.push(a.version)
|
|
148
|
+
else retired.push(a)
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
served,
|
|
152
|
+
retired: retired.map((a) => {
|
|
153
|
+
const major = /** @type {string} */ (a.version.split('.')[0])
|
|
154
|
+
return { version: a.version, to: a.version.includes('.') && served.includes(major) ? major : '' }
|
|
155
|
+
}),
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Each version is its own build with its own runtime: a navigation between two must load the page, not swap it in.
|
|
160
|
+
// clarusjs takes over same-origin navigations (the Navigation API's navigate event); a listener registered before it,
|
|
161
|
+
// by an inline script in <head> (module scripts run after), stops the event reaching it for these.
|
|
162
|
+
// ponytail: relies on listener order; a framework opt-out (a navigation scope) would replace it.
|
|
163
|
+
/** @param {string} leaves the condition, on `p` (the destination's path) and `v(path)` (its version segment, or '') */
|
|
164
|
+
const guard = (leaves) =>
|
|
165
|
+
`window.navigation&&navigation.addEventListener('navigate',function(e){var p=new URL(e.destination.url).pathname;` +
|
|
166
|
+
String.raw`var v=function(x){var r=/\/(v\d+(?:\.\d+)?)(?:\/|$)/.exec(x);return r?r[1]:''};` +
|
|
167
|
+
`if(${leaves})e.stopImmediatePropagation()})`
|
|
168
|
+
|
|
169
|
+
/** An archived version's pages: navigations leaving its prefix (/v0.19/, under the base) load the page. @param {string} prefix */
|
|
170
|
+
export const archivedGuard = (prefix) => guard(`p.indexOf(${JSON.stringify(prefix)})!==0`)
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Every page the theme renders (the live docs, and archives built with it): a navigation into another version (a
|
|
174
|
+
* /v0.19/ or /v1 segment, under any base) loads the page; within the same version it swaps as usual. A page of the
|
|
175
|
+
* site's own with such a segment would load instead of swap: slower, never wrong.
|
|
176
|
+
*/
|
|
177
|
+
export const LATEST_GUARD = guard('v(p)!==v(location.pathname)')
|
|
178
|
+
|
|
179
|
+
const BANNER = 'old-version-banner'
|
|
180
|
+
// Inline, so it doesn't depend on the archived site's stylesheet; its colour tokens when it has them
|
|
181
|
+
const STYLE =
|
|
182
|
+
'padding:8px 16px;text-align:center;font-size:14px;background:var(--surface,#f4f4f5);color:var(--fg,#18181b);' +
|
|
183
|
+
'border-bottom:1px solid var(--rule,#d4d4d8)'
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* An archived page's HTML, marked: a banner after `<body…>` saying which version it is, with a link to the same page in
|
|
187
|
+
* the latest docs; in a full document, the navigation guard first in <head>, its canonical link pointed at the latest
|
|
188
|
+
* page, and noindex. Insertions only: built under its own base, its links already stay inside it.
|
|
189
|
+
* @param {string} html
|
|
190
|
+
* @param {{ version: string, path: string, base: string, name?: string }} o the page's path without the base
|
|
191
|
+
*/
|
|
192
|
+
export function markSnapshot(html, { version, path, base, name }) {
|
|
193
|
+
if (html.includes(`class="${BANNER}"`)) return html
|
|
194
|
+
const latest = base.replace(/\/$/, '') + path || '/'
|
|
195
|
+
const banner =
|
|
196
|
+
`<div class="${BANNER}" role="note" style="${STYLE}">You're reading the docs for ${name ?? 'version'} ${version}. ` +
|
|
197
|
+
`<a href="${latest}">Same page in the latest docs</a></div>`
|
|
198
|
+
let out = html.replace(/<body\b[^>]*>/, (tag) => tag + banner)
|
|
199
|
+
if (!/<\/head>/.test(out)) return out
|
|
200
|
+
out = out.replace(/<head\b[^>]*>/, (tag) => `${tag}<script>${archivedGuard(`${base}v${version}/`)}</script>`)
|
|
201
|
+
out = out.replace(/<link rel="canonical" href="([^"]*)">/, (_, href) => `<link rel="canonical" href="${href.replace(`${base}v${version}/`, base).replace(/\/$/, path === '/' ? '/' : '')}">`)
|
|
202
|
+
return out.replace('</head>', '<meta name="robots" content="noindex"></head>')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A page's path from its file in the built site: 'guide/lists/index.html' → '/guide/lists'. @param {string} file */
|
|
206
|
+
export function pagePath(file) {
|
|
207
|
+
const p = file.replace(/(^|\/)index(\.frag)?\.html$/, '').replace(/\.html$/, '')
|
|
208
|
+
return /^(404|500)$/.test(p) ? '/' : '/' + p
|
|
209
|
+
}
|
package/src/vite.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ export type Options = {
|
|
|
11
11
|
* write llms.txt and llms-full.txt to the client output after the build (llmstxt.org)
|
|
12
12
|
*/
|
|
13
13
|
llms?: LlmsOptions;
|
|
14
|
+
/**
|
|
15
|
+
* versioned docs: the version menu, from versions.json next to the Vite config
|
|
16
|
+
* (`import versions from 'virtual:theme-docs/versions'`, passed as the theme config's `versions`); archived versions are
|
|
17
|
+
* built and added by the clarusjs-docs-versions command (https://clarusjs.com/docs-sites/versioned-docs)
|
|
18
|
+
*/
|
|
19
|
+
versions?: VersionsOptions;
|
|
14
20
|
/**
|
|
15
21
|
* parts of the layout replaced by files of the site's own, from
|
|
16
22
|
* the Vite root: `{ header: '/src/theme/header.tscl' }`. Each gets the inputs and slots the theme's part gets.
|
|
@@ -18,6 +24,27 @@ export type Options = {
|
|
|
18
24
|
components?: Partial<Record<PartName, string>>;
|
|
19
25
|
};
|
|
20
26
|
export type PartName = 'header' | 'sidebar' | 'contents' | 'pageFooter' | 'footer';
|
|
27
|
+
export type VersionsOptions = {
|
|
28
|
+
/**
|
|
29
|
+
* the live docs' version, as the menu shows it ('v0.20.0')
|
|
30
|
+
*/
|
|
31
|
+
current: string;
|
|
32
|
+
/**
|
|
33
|
+
* more rows at the end of the menu (release notes, a changelog)
|
|
34
|
+
*/
|
|
35
|
+
links?: {
|
|
36
|
+
label: string;
|
|
37
|
+
href: string;
|
|
38
|
+
}[];
|
|
39
|
+
/**
|
|
40
|
+
* list at most this many archived versions in the menu, newest first (the rest: `all`)
|
|
41
|
+
*/
|
|
42
|
+
menu?: number;
|
|
43
|
+
/**
|
|
44
|
+
* a page listing every version (with the theme's version-list.tscl): an "All versions" row
|
|
45
|
+
*/
|
|
46
|
+
all?: string;
|
|
47
|
+
};
|
|
21
48
|
export type LlmsOptions = {
|
|
22
49
|
/**
|
|
23
50
|
* the site's name, the file's heading
|
package/src/vite.js
CHANGED
|
@@ -8,19 +8,31 @@ import { fileURLToPath } from 'node:url'
|
|
|
8
8
|
import { parse } from 'clarusjs/compiler'
|
|
9
9
|
import { gitDates } from 'clarusjs/vite'
|
|
10
10
|
import { CHUNK, termFile, tokens } from './search.js'
|
|
11
|
+
import { plan } from './versions.js'
|
|
11
12
|
|
|
12
13
|
const ID = 'virtual:theme-docs/pages'
|
|
14
|
+
const VERSIONS_ID = 'virtual:theme-docs/versions'
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* @typedef {object} Options
|
|
16
18
|
* @property {string} [pages] the pages folder, from the Vite root (default src/pages)
|
|
17
19
|
* @property {boolean} [search] false: no search index (it's written to the client output's search/ after the build)
|
|
18
20
|
* @property {LlmsOptions} [llms] write llms.txt and llms-full.txt to the client output after the build (llmstxt.org)
|
|
21
|
+
* @property {VersionsOptions} [versions] versioned docs: the version menu, from versions.json next to the Vite config
|
|
22
|
+
* (`import versions from 'virtual:theme-docs/versions'`, passed as the theme config's `versions`); archived versions are
|
|
23
|
+
* built and added by the clarusjs-docs-versions command (https://clarusjs.com/docs-sites/versioned-docs)
|
|
19
24
|
* @property {Partial<Record<PartName, string>>} [components] parts of the layout replaced by files of the site's own, from
|
|
20
25
|
* the Vite root: `{ header: '/src/theme/header.tscl' }`. Each gets the inputs and slots the theme's part gets.
|
|
21
26
|
*/
|
|
22
27
|
|
|
23
28
|
/** @typedef {'header' | 'sidebar' | 'contents' | 'pageFooter' | 'footer'} PartName */
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} VersionsOptions
|
|
31
|
+
* @property {string} current the live docs' version, as the menu shows it ('v0.20.0')
|
|
32
|
+
* @property {{ label: string, href: string }[]} [links] more rows at the end of the menu (release notes, a changelog)
|
|
33
|
+
* @property {number} [menu] list at most this many archived versions in the menu, newest first (the rest: `all`)
|
|
34
|
+
* @property {string} [all] a page listing every version (with the theme's version-list.tscl): an "All versions" row
|
|
35
|
+
*/
|
|
24
36
|
|
|
25
37
|
/**
|
|
26
38
|
* @typedef {object} LlmsOptions
|
|
@@ -97,6 +109,8 @@ export function themeDocs(options = {}) {
|
|
|
97
109
|
let dir = resolve(options.pages ?? 'src/pages')
|
|
98
110
|
/** @type {Map<string, string>} */
|
|
99
111
|
let overrides = new Map()
|
|
112
|
+
/** @type {import('./versions.js').VersionsFile} */
|
|
113
|
+
let versionsFile = {}
|
|
100
114
|
return {
|
|
101
115
|
name: 'clarusjs:theme-docs',
|
|
102
116
|
// Before Vite resolves the layout's relative import of a part itself.
|
|
@@ -106,11 +120,26 @@ export function themeDocs(options = {}) {
|
|
|
106
120
|
api: {
|
|
107
121
|
/** @param {string} root */
|
|
108
122
|
clarusComponents: (root) => [...overridesOf(options.components ?? {}, root).values()],
|
|
123
|
+
// Archived versions are other builds, unpacked into this one after it (retired ones redirect): not missing pages.
|
|
124
|
+
clarusLinksIgnore: () => (versionsFile.versions ?? []).map((a) => `/v${a.version}/`),
|
|
125
|
+
},
|
|
126
|
+
// mermaid (an optional peer, for diagrams) pre-bundled when the site has it: otherwise dev finds it at the first
|
|
127
|
+
// diagram, re-optimizes and reloads the page, and a diagram whose import was in flight shows its source
|
|
128
|
+
/** @param {{ root?: string }} userConfig */
|
|
129
|
+
config(userConfig) {
|
|
130
|
+
// node_modules/mermaid from the root up, as Vite resolves it (not require.resolve: pnpm run's NODE_PATH would find
|
|
131
|
+
// it in the workspace's store for a site that doesn't depend on it)
|
|
132
|
+
for (let d = resolve(userConfig.root ?? process.cwd()); ; d = dirname(d)) {
|
|
133
|
+
if (existsSync(join(d, 'node_modules/mermaid/package.json'))) return { optimizeDeps: { include: ['mermaid'] } }
|
|
134
|
+
if (dirname(d) === d) return undefined
|
|
135
|
+
}
|
|
109
136
|
},
|
|
110
137
|
/** @param {{ root: string }} config */
|
|
111
138
|
configResolved(config) {
|
|
112
139
|
dir = resolve(config.root, options.pages ?? 'src/pages')
|
|
113
140
|
overrides = overridesOf(options.components ?? {}, config.root)
|
|
141
|
+
const list = join(config.root, 'versions.json')
|
|
142
|
+
if (options.versions && existsSync(list)) versionsFile = JSON.parse(readFileSync(list, 'utf8'))
|
|
114
143
|
},
|
|
115
144
|
/**
|
|
116
145
|
* @param {string} id
|
|
@@ -118,6 +147,7 @@ export function themeDocs(options = {}) {
|
|
|
118
147
|
*/
|
|
119
148
|
resolveId(id, importer) {
|
|
120
149
|
if (id === ID) return '\0' + ID
|
|
150
|
+
if (id === VERSIONS_ID) return '\0' + VERSIONS_ID
|
|
121
151
|
// The layout's import of a part the site replaced (./parts/x.tscl from the theme): the site's file.
|
|
122
152
|
// The query (?render: the layout rendered in the browser) stays on the site's file.
|
|
123
153
|
if (overrides.size && importer && id.startsWith('./parts/')) {
|
|
@@ -129,6 +159,14 @@ export function themeDocs(options = {}) {
|
|
|
129
159
|
},
|
|
130
160
|
/** @param {string} id */
|
|
131
161
|
load(id) {
|
|
162
|
+
if (id === '\0' + VERSIONS_ID) {
|
|
163
|
+
const v = options.versions
|
|
164
|
+
// The served versions only (versions.js plan(): retired by hand, by lines, minors or keep aren't)
|
|
165
|
+
const served = new Set(plan(versionsFile).served)
|
|
166
|
+
const archived = (versionsFile.versions ?? []).filter((a) => served.has(a.version)).map(({ version, tag }) => ({ version, tag }))
|
|
167
|
+
const data = v && { current: v.current, archived, links: v.links ?? [], ...(v.menu !== undefined && { menu: v.menu }), ...(v.all && { all: v.all }) }
|
|
168
|
+
return `export default ${JSON.stringify(data ?? null)}\n`
|
|
169
|
+
}
|
|
132
170
|
return id === '\0' + ID ? `export default ${JSON.stringify(readPages(dir))}\n` : undefined
|
|
133
171
|
},
|
|
134
172
|
// After clarusjs has prerendered the site: the search index, from the built pages.
|