@raystack/chronicle 0.9.0 → 0.10.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/dist/cli/index.js +19 -4
- package/package.json +2 -1
- package/src/lib/folder-utils.ts +26 -0
- package/src/lib/route-resolver.test.ts +3 -3
- package/src/lib/route-resolver.ts +12 -2
- package/src/lib/source-utils.test.ts +85 -0
- package/src/lib/source.ts +9 -20
- package/src/lib/tree-utils.test.ts +113 -0
- package/src/lib/tree-utils.ts +57 -0
- package/src/pages/DocsPage.tsx +5 -36
- package/src/server/entry-server.tsx +28 -1
- package/src/server/vite-config.ts +14 -4
- package/src/types/config.ts +8 -0
package/dist/cli/index.js
CHANGED
|
@@ -312,6 +312,18 @@ import { nitro } from "nitro/vite";
|
|
|
312
312
|
import fs3 from "node:fs/promises";
|
|
313
313
|
import path6 from "node:path";
|
|
314
314
|
import remarkDirective from "remark-directive";
|
|
315
|
+
function getDatabaseConnector(preset) {
|
|
316
|
+
switch (preset) {
|
|
317
|
+
case "bun":
|
|
318
|
+
return { connector: "bun-sqlite", options: { name: "chronicle-search" } };
|
|
319
|
+
case "cloudflare":
|
|
320
|
+
case "cloudflare-pages":
|
|
321
|
+
case "cloudflare-module":
|
|
322
|
+
return { connector: "cloudflare-d1", options: { bindingName: "CHRONICLE_DB" } };
|
|
323
|
+
default:
|
|
324
|
+
return { connector: "sqlite", options: { name: "chronicle-search" } };
|
|
325
|
+
}
|
|
326
|
+
}
|
|
315
327
|
function resolveOutputDir(projectRoot, preset) {
|
|
316
328
|
if (preset === "vercel" || preset === "vercel-static")
|
|
317
329
|
return path6.resolve(projectRoot, ".vercel/output");
|
|
@@ -429,10 +441,7 @@ async function createViteConfig(options) {
|
|
|
429
441
|
database: true
|
|
430
442
|
},
|
|
431
443
|
database: {
|
|
432
|
-
default:
|
|
433
|
-
connector: "sqlite",
|
|
434
|
-
options: { name: "chronicle-search" }
|
|
435
|
-
}
|
|
444
|
+
default: getDatabaseConnector(preset)
|
|
436
445
|
}
|
|
437
446
|
}
|
|
438
447
|
};
|
|
@@ -561,6 +570,11 @@ var RESERVED_ROUTE_SEGMENTS = [
|
|
|
561
570
|
"robots.txt",
|
|
562
571
|
"sitemap.xml"
|
|
563
572
|
];
|
|
573
|
+
var redirectSchema = z.object({
|
|
574
|
+
from: z.string(),
|
|
575
|
+
to: z.string(),
|
|
576
|
+
permanent: z.boolean().optional()
|
|
577
|
+
});
|
|
564
578
|
var chronicleConfigSchema = z.object({
|
|
565
579
|
site: siteSchema,
|
|
566
580
|
url: z.string().optional(),
|
|
@@ -573,6 +587,7 @@ var chronicleConfigSchema = z.object({
|
|
|
573
587
|
navigation: navigationSchema.optional(),
|
|
574
588
|
search: searchSchema.optional(),
|
|
575
589
|
api: z.array(apiSchema).optional(),
|
|
590
|
+
redirects: z.array(redirectSchema).optional(),
|
|
576
591
|
analytics: analyticsSchema.optional(),
|
|
577
592
|
telemetry: telemetrySchema.optional()
|
|
578
593
|
}).strict().refine((cfg) => allUnique(cfg.content, (c) => c.dir), {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raystack/chronicle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Config-driven documentation framework",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"glob": "^11.0.0",
|
|
60
60
|
"gray-matter": "^4.0.3",
|
|
61
61
|
"h3": "^2.0.1-rc.16",
|
|
62
|
+
"http-status-codes": "^2.3.0",
|
|
62
63
|
"lodash-es": "^4.17.23",
|
|
63
64
|
"mermaid": "^11.13.0",
|
|
64
65
|
"nitro": "3.0.260311-beta",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Node, Folder } from 'fumadocs-core/page-tree';
|
|
2
|
+
|
|
3
|
+
const NodeType = {
|
|
4
|
+
Page: 'page',
|
|
5
|
+
Folder: 'folder',
|
|
6
|
+
} as const;
|
|
7
|
+
|
|
8
|
+
export function parentPath(url: string): string {
|
|
9
|
+
const parts = url.split('/').filter(Boolean);
|
|
10
|
+
parts.pop();
|
|
11
|
+
return '/' + parts.join('/');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function getFolderPath(node: Folder): string | null {
|
|
15
|
+
if (node.index) return node.index.url;
|
|
16
|
+
for (const child of node.children) {
|
|
17
|
+
if (child.type === NodeType.Page) return parentPath(child.url);
|
|
18
|
+
}
|
|
19
|
+
for (const child of node.children) {
|
|
20
|
+
if (child.type === NodeType.Folder) {
|
|
21
|
+
const childPath = getFolderPath(child as Folder);
|
|
22
|
+
if (childPath) return parentPath(childPath);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
@@ -60,7 +60,7 @@ describe('resolveRoute — root', () => {
|
|
|
60
60
|
expect(resolveRoute('/', singleContent())).toEqual({
|
|
61
61
|
type: RouteType.Redirect,
|
|
62
62
|
to: '/docs',
|
|
63
|
-
status:
|
|
63
|
+
status: 307,
|
|
64
64
|
})
|
|
65
65
|
})
|
|
66
66
|
|
|
@@ -75,7 +75,7 @@ describe('resolveRoute — root', () => {
|
|
|
75
75
|
expect(resolveRoute('/', multiContentNoLanding())).toEqual({
|
|
76
76
|
type: RouteType.Redirect,
|
|
77
77
|
to: '/docs',
|
|
78
|
-
status:
|
|
78
|
+
status: 307,
|
|
79
79
|
})
|
|
80
80
|
})
|
|
81
81
|
|
|
@@ -83,7 +83,7 @@ describe('resolveRoute — root', () => {
|
|
|
83
83
|
expect(resolveRoute('/v2', versioned())).toEqual({
|
|
84
84
|
type: RouteType.Redirect,
|
|
85
85
|
to: '/v2/docs',
|
|
86
|
-
status:
|
|
86
|
+
status: 307,
|
|
87
87
|
})
|
|
88
88
|
})
|
|
89
89
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StatusCodes } from 'http-status-codes'
|
|
1
2
|
import type { ChronicleConfig } from '@/types'
|
|
2
3
|
import { getLatestContentRoots, getVersionContentRoots } from './config'
|
|
3
4
|
import { type VersionContext, resolveVersionFromUrl } from './version-source'
|
|
@@ -13,7 +14,7 @@ export const RouteType = {
|
|
|
13
14
|
export type RouteType = (typeof RouteType)[keyof typeof RouteType]
|
|
14
15
|
|
|
15
16
|
export type Route =
|
|
16
|
-
| { type: typeof RouteType.Redirect; to: string; status:
|
|
17
|
+
| { type: typeof RouteType.Redirect; to: string; status: StatusCodes.TEMPORARY_REDIRECT | StatusCodes.PERMANENT_REDIRECT }
|
|
17
18
|
| { type: typeof RouteType.DocsIndex; version: VersionContext }
|
|
18
19
|
| { type: typeof RouteType.DocsPage; version: VersionContext; slug: string[] }
|
|
19
20
|
| { type: typeof RouteType.ApiIndex; version: VersionContext }
|
|
@@ -45,6 +46,15 @@ export function resolveRoute(
|
|
|
45
46
|
pathname: string,
|
|
46
47
|
config: ChronicleConfig,
|
|
47
48
|
): Route {
|
|
49
|
+
const redirect = config.redirects?.find((r) => r.from === pathname)
|
|
50
|
+
if (redirect) {
|
|
51
|
+
return {
|
|
52
|
+
type: RouteType.Redirect,
|
|
53
|
+
to: redirect.to,
|
|
54
|
+
status: redirect.permanent ? StatusCodes.PERMANENT_REDIRECT : StatusCodes.TEMPORARY_REDIRECT,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
48
58
|
const parts = pathname.split('/').filter(Boolean)
|
|
49
59
|
const version = resolveVersionFromUrl(pathname, config)
|
|
50
60
|
const remainder =
|
|
@@ -65,7 +75,7 @@ export function resolveRoute(
|
|
|
65
75
|
return {
|
|
66
76
|
type: RouteType.Redirect,
|
|
67
77
|
to: `${version.urlPrefix}/${dirs[0]}`,
|
|
68
|
-
status:
|
|
78
|
+
status: StatusCodes.TEMPORARY_REDIRECT,
|
|
69
79
|
}
|
|
70
80
|
}
|
|
71
81
|
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import type { Node, Folder } from 'fumadocs-core/page-tree'
|
|
3
|
+
import { parentPath, getFolderPath } from './folder-utils'
|
|
4
|
+
|
|
5
|
+
function page(url: string): Node {
|
|
6
|
+
return { type: 'page', name: 'Page', url } as Node
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function folder(name: string, children: Node[], indexUrl?: string): Folder {
|
|
10
|
+
return {
|
|
11
|
+
type: 'folder',
|
|
12
|
+
name,
|
|
13
|
+
children,
|
|
14
|
+
...(indexUrl ? { index: { url: indexUrl } } : {}),
|
|
15
|
+
} as Folder
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('parentPath', () => {
|
|
19
|
+
test('returns parent of page URL', () => {
|
|
20
|
+
expect(parentPath('/docs/guides/install')).toBe('/docs/guides')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('returns root for top-level page', () => {
|
|
24
|
+
expect(parentPath('/docs')).toBe('/')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('handles trailing segments', () => {
|
|
28
|
+
expect(parentPath('/a/b/c/d')).toBe('/a/b/c')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('handles root', () => {
|
|
32
|
+
expect(parentPath('/')).toBe('/')
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
describe('getFolderPath', () => {
|
|
37
|
+
test('returns index URL when folder has index', () => {
|
|
38
|
+
const f = folder('Guides', [page('/docs/guides/install')], '/docs/guides')
|
|
39
|
+
expect(getFolderPath(f)).toBe('/docs/guides')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('derives path from direct child page', () => {
|
|
43
|
+
const f = folder('Guides', [page('/docs/guides/install')])
|
|
44
|
+
expect(getFolderPath(f)).toBe('/docs/guides')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('derives path from subfolder child (not deeply nested)', () => {
|
|
48
|
+
const f = folder('Tasking', [
|
|
49
|
+
folder('Via Order Desk', [page('/docs/tasking/via_order_desk/package')])
|
|
50
|
+
])
|
|
51
|
+
expect(getFolderPath(f)).toBe('/docs/tasking')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('handles folder with & in path', () => {
|
|
55
|
+
const f = folder('Cart & Order', [page('/docs/cart&order/working_with_cart')])
|
|
56
|
+
expect(getFolderPath(f)).toBe('/docs/cart&order')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('handles folder with space in path', () => {
|
|
60
|
+
const f = folder('My Folder', [page('/docs/my folder/intro')])
|
|
61
|
+
expect(getFolderPath(f)).toBe('/docs/my folder')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('returns null for empty folder', () => {
|
|
65
|
+
const f = folder('Empty', [])
|
|
66
|
+
expect(getFolderPath(f)).toBeNull()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('prefers direct child page over subfolder', () => {
|
|
70
|
+
const f = folder('Mixed', [
|
|
71
|
+
page('/docs/mixed/intro'),
|
|
72
|
+
folder('Sub', [page('/docs/mixed/sub/deep')])
|
|
73
|
+
])
|
|
74
|
+
expect(getFolderPath(f)).toBe('/docs/mixed')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('deeply nested only-subfolder chain', () => {
|
|
78
|
+
const f = folder('Root', [
|
|
79
|
+
folder('Mid', [
|
|
80
|
+
folder('Deep', [page('/a/b/c/d/page')])
|
|
81
|
+
])
|
|
82
|
+
])
|
|
83
|
+
expect(getFolderPath(f)).toBe('/a/b')
|
|
84
|
+
})
|
|
85
|
+
})
|
package/src/lib/source.ts
CHANGED
|
@@ -3,6 +3,13 @@ import path from 'node:path';
|
|
|
3
3
|
import { loader } from 'fumadocs-core/source';
|
|
4
4
|
import { flattenTree } from 'fumadocs-core/page-tree';
|
|
5
5
|
import type { Root, Node, Folder } from 'fumadocs-core/page-tree';
|
|
6
|
+
|
|
7
|
+
import { parentPath, getFolderPath } from './folder-utils';
|
|
8
|
+
|
|
9
|
+
const NodeType = {
|
|
10
|
+
Page: 'page',
|
|
11
|
+
Folder: 'folder',
|
|
12
|
+
} as const;
|
|
6
13
|
import type { MDXContent } from 'mdx/types';
|
|
7
14
|
import type { TableOfContents } from 'fumadocs-core/toc';
|
|
8
15
|
import {
|
|
@@ -120,28 +127,10 @@ export function invalidate() {
|
|
|
120
127
|
cachedNavMap = null;
|
|
121
128
|
}
|
|
122
129
|
|
|
123
|
-
function getFolderPath(node: Folder): string | null {
|
|
124
|
-
const firstPage = findFirstPage(node);
|
|
125
|
-
if (!firstPage) return null;
|
|
126
|
-
const parts = firstPage.url.split('/').filter(Boolean);
|
|
127
|
-
parts.pop();
|
|
128
|
-
return '/' + parts.join('/');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function findFirstPage(node: Folder): { url: string } | null {
|
|
132
|
-
for (const child of node.children) {
|
|
133
|
-
if (child.type === 'page') return child;
|
|
134
|
-
if (child.type === 'folder') {
|
|
135
|
-
const found = findFirstPage(child);
|
|
136
|
-
if (found) return found;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return node.index ?? null;
|
|
140
|
-
}
|
|
141
130
|
|
|
142
131
|
function getOrder(node: Node, pageOrderMap: Map<string, number>, folderOrderMap: Map<string, number>): number | undefined {
|
|
143
|
-
if (node.type ===
|
|
144
|
-
if (node.type ===
|
|
132
|
+
if (node.type === NodeType.Page) return pageOrderMap.get(node.url);
|
|
133
|
+
if (node.type === NodeType.Folder) {
|
|
145
134
|
const folderPath = getFolderPath(node);
|
|
146
135
|
if (folderPath) return folderOrderMap.get(folderPath);
|
|
147
136
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import type { Node } from 'fumadocs-core/page-tree'
|
|
3
|
+
import { getFirstPageUrl, findFolderFirstPage, resolveDocsRedirect } from './tree-utils'
|
|
4
|
+
|
|
5
|
+
function page(url: string, name = 'Page'): Node {
|
|
6
|
+
return { type: 'page', name, url } as Node
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function folder(name: string, children: Node[], indexUrl?: string): Node {
|
|
10
|
+
return {
|
|
11
|
+
type: 'folder',
|
|
12
|
+
name,
|
|
13
|
+
children,
|
|
14
|
+
...(indexUrl ? { index: { url: indexUrl } } : {}),
|
|
15
|
+
} as Node
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('getFirstPageUrl', () => {
|
|
19
|
+
test('returns first page url', () => {
|
|
20
|
+
expect(getFirstPageUrl([page('/docs/intro')])).toBe('/docs/intro')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('returns first page from nested folder', () => {
|
|
24
|
+
const nodes = [folder('Guides', [page('/docs/guides/install')])]
|
|
25
|
+
expect(getFirstPageUrl(nodes)).toBe('/docs/guides/install')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('skips empty folders', () => {
|
|
29
|
+
const nodes = [folder('Empty', []), page('/docs/hello')]
|
|
30
|
+
expect(getFirstPageUrl(nodes)).toBe('/docs/hello')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('returns null for empty list', () => {
|
|
34
|
+
expect(getFirstPageUrl([])).toBeNull()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('returns null for folders with no pages', () => {
|
|
38
|
+
expect(getFirstPageUrl([folder('Empty', [])])).toBeNull()
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('findFolderFirstPage', () => {
|
|
43
|
+
test('finds folder by index url', () => {
|
|
44
|
+
const nodes = [
|
|
45
|
+
folder('Guides', [page('/docs/guides/install'), page('/docs/guides/config')], '/docs/guides'),
|
|
46
|
+
]
|
|
47
|
+
expect(findFolderFirstPage(nodes, '/docs/guides')).toBe('/docs/guides/install')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('finds folder without index by child page path', () => {
|
|
51
|
+
const nodes = [
|
|
52
|
+
folder('Guides', [page('/docs/guides/install'), page('/docs/guides/config')]),
|
|
53
|
+
]
|
|
54
|
+
expect(findFolderFirstPage(nodes, '/docs/guides')).toBe('/docs/guides/install')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('finds nested folder', () => {
|
|
58
|
+
const nodes = [
|
|
59
|
+
folder('Docs', [
|
|
60
|
+
folder('Advanced', [page('/docs/advanced/perf'), page('/docs/advanced/debug')]),
|
|
61
|
+
]),
|
|
62
|
+
]
|
|
63
|
+
expect(findFolderFirstPage(nodes, '/docs/advanced')).toBe('/docs/advanced/perf')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('returns null for non-matching path', () => {
|
|
67
|
+
const nodes = [folder('Guides', [page('/docs/guides/install')])]
|
|
68
|
+
expect(findFolderFirstPage(nodes, '/docs/api')).toBeNull()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('returns null for empty folder', () => {
|
|
72
|
+
const nodes = [folder('Empty', [])]
|
|
73
|
+
expect(findFolderFirstPage(nodes, '/docs/empty')).toBeNull()
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('resolveDocsRedirect', () => {
|
|
78
|
+
const tree = {
|
|
79
|
+
children: [
|
|
80
|
+
page('/docs/intro'),
|
|
81
|
+
folder('Guides', [page('/docs/guides/install')]),
|
|
82
|
+
] as Node[],
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
test('redirects to index_page when set', () => {
|
|
86
|
+
expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs', index_page: 'getting-started' }))
|
|
87
|
+
.toBe('/docs/getting-started')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('redirects content root to first page', () => {
|
|
91
|
+
expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs' }))
|
|
92
|
+
.toBe('/docs/intro')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('redirects folder to first child', () => {
|
|
96
|
+
expect(resolveDocsRedirect(['docs', 'guides'], tree, { dir: 'docs' }))
|
|
97
|
+
.toBe('/docs/guides/install')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('returns null for non-matching path', () => {
|
|
101
|
+
expect(resolveDocsRedirect(['docs', 'nonexistent'], tree, { dir: 'docs' }))
|
|
102
|
+
.toBeNull()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('returns null without content config', () => {
|
|
106
|
+
expect(resolveDocsRedirect(['other'], tree)).toBeNull()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('index_page takes priority over first page', () => {
|
|
110
|
+
expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs', index_page: 'custom' }))
|
|
111
|
+
.toBe('/docs/custom')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Node } from 'fumadocs-core/page-tree';
|
|
2
|
+
|
|
3
|
+
export const NodeType = {
|
|
4
|
+
Page: 'page',
|
|
5
|
+
Folder: 'folder',
|
|
6
|
+
Separator: 'separator',
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export function getFirstPageUrl(nodes: Node[]): string | null {
|
|
10
|
+
for (const node of nodes) {
|
|
11
|
+
if (node.type === NodeType.Page) return node.url;
|
|
12
|
+
if (node.type === NodeType.Folder) {
|
|
13
|
+
const url = getFirstPageUrl(node.children);
|
|
14
|
+
if (url) return url;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getFolderPath(node: Node): string | null {
|
|
21
|
+
if (node.type !== NodeType.Folder) return null;
|
|
22
|
+
if (node.index) return node.index.url;
|
|
23
|
+
const firstPage = getFirstPageUrl(node.children);
|
|
24
|
+
if (!firstPage) return null;
|
|
25
|
+
const parts = firstPage.split('/').filter(Boolean);
|
|
26
|
+
parts.pop();
|
|
27
|
+
return '/' + parts.join('/');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function findFolderFirstPage(nodes: Node[], pathname: string): string | null {
|
|
31
|
+
for (const node of nodes) {
|
|
32
|
+
if (node.type === NodeType.Folder) {
|
|
33
|
+
const folderPath = getFolderPath(node);
|
|
34
|
+
if (folderPath === pathname) return getFirstPageUrl(node.children);
|
|
35
|
+
const found = findFolderFirstPage(node.children, pathname);
|
|
36
|
+
if (found) return found;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function resolveDocsRedirect(
|
|
43
|
+
slug: string[],
|
|
44
|
+
tree: { children: Node[] },
|
|
45
|
+
contentConfig?: { dir: string; index_page?: string },
|
|
46
|
+
): string | null {
|
|
47
|
+
const isContentRoot = slug.length === 1 && slug[0] === contentConfig?.dir;
|
|
48
|
+
|
|
49
|
+
if (isContentRoot) {
|
|
50
|
+
if (contentConfig?.index_page) {
|
|
51
|
+
return `/${contentConfig.dir}/${contentConfig.index_page}`;
|
|
52
|
+
}
|
|
53
|
+
return getFirstPageUrl(tree.children);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return findFolderFirstPage(tree.children, `/${slug.join('/')}`);
|
|
57
|
+
}
|
package/src/pages/DocsPage.tsx
CHANGED
|
@@ -1,32 +1,10 @@
|
|
|
1
1
|
import { Navigate } from 'react-router';
|
|
2
|
+
import { StatusCodes } from 'http-status-codes';
|
|
2
3
|
import { Head } from '@/lib/head';
|
|
3
4
|
import { usePageContext } from '@/lib/page-context';
|
|
5
|
+
import { resolveDocsRedirect } from '@/lib/tree-utils';
|
|
4
6
|
import { NotFound } from '@/pages/NotFound';
|
|
5
7
|
import { getTheme } from '@/themes/registry';
|
|
6
|
-
import type { Node } from 'fumadocs-core/page-tree';
|
|
7
|
-
|
|
8
|
-
function getFirstPageUrl(nodes: Node[]): string | null {
|
|
9
|
-
for (const node of nodes) {
|
|
10
|
-
if (node.type === 'page') return node.url;
|
|
11
|
-
if (node.type === 'folder') {
|
|
12
|
-
const url = getFirstPageUrl(node.children);
|
|
13
|
-
if (url) return url;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function findFolderFirstPage(nodes: Node[], pathname: string): string | null {
|
|
20
|
-
for (const node of nodes) {
|
|
21
|
-
if (node.type === 'folder') {
|
|
22
|
-
const folderUrl = node.index?.url;
|
|
23
|
-
if (folderUrl === pathname) return getFirstPageUrl(node.children);
|
|
24
|
-
const found = findFolderFirstPage(node.children, pathname);
|
|
25
|
-
if (found) return found;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
8
|
|
|
31
9
|
interface DocsPageProps {
|
|
32
10
|
slug: string[];
|
|
@@ -35,19 +13,10 @@ interface DocsPageProps {
|
|
|
35
13
|
export function DocsPage({ slug }: DocsPageProps) {
|
|
36
14
|
const { config, tree, page, isLoading, errorStatus } = usePageContext();
|
|
37
15
|
|
|
38
|
-
if (errorStatus ===
|
|
39
|
-
const pathname = `/${slug.join('/')}`;
|
|
16
|
+
if (errorStatus === StatusCodes.NOT_FOUND) {
|
|
40
17
|
const contentConfig = config.content?.find(c => c.dir === slug[0]);
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
return <Navigate to={`/${contentConfig.dir}/${contentConfig.index_page}`} replace />;
|
|
44
|
-
}
|
|
45
|
-
if (isContentRoot) {
|
|
46
|
-
const firstUrl = getFirstPageUrl(tree.children);
|
|
47
|
-
if (firstUrl) return <Navigate to={firstUrl} replace />;
|
|
48
|
-
}
|
|
49
|
-
const folderFirstUrl = findFolderFirstPage(tree.children, pathname);
|
|
50
|
-
if (folderFirstUrl) return <Navigate to={folderFirstUrl} replace />;
|
|
18
|
+
const redirectUrl = resolveDocsRedirect(slug, tree, contentConfig);
|
|
19
|
+
if (redirectUrl) return <Navigate to={redirectUrl} replace />;
|
|
51
20
|
return <NotFound />;
|
|
52
21
|
}
|
|
53
22
|
if (errorStatus) return <NotFound />;
|
|
@@ -10,6 +10,9 @@ import { loadApiSpecs } from '@/lib/openapi';
|
|
|
10
10
|
import { PageProvider } from '@/lib/page-context';
|
|
11
11
|
import { resolveRoute, RouteType } from '@/lib/route-resolver';
|
|
12
12
|
import { getPageTree, getPage, getPageNav, loadPageModule, extractFrontmatter, getRelativePath, getOriginalPath } from '@/lib/source';
|
|
13
|
+
import { getFirstApiUrl } from '@/lib/api-routes';
|
|
14
|
+
import { StatusCodes } from 'http-status-codes';
|
|
15
|
+
import { resolveDocsRedirect } from '@/lib/tree-utils';
|
|
13
16
|
import { useNitroApp } from 'nitro/app';
|
|
14
17
|
import { App } from './App';
|
|
15
18
|
|
|
@@ -45,6 +48,30 @@ export default {
|
|
|
45
48
|
getPageTree(),
|
|
46
49
|
route.type === RouteType.DocsPage ? getPage(route.slug) : Promise.resolve(null),
|
|
47
50
|
]);
|
|
51
|
+
// SSR redirects for index pages
|
|
52
|
+
if (route.type === RouteType.ApiIndex) {
|
|
53
|
+
const firstUrl = getFirstApiUrl(apiSpecs);
|
|
54
|
+
if (firstUrl) {
|
|
55
|
+
return new Response(null, { status: StatusCodes.TEMPORARY_REDIRECT, headers: { Location: firstUrl } });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (route.type === RouteType.DocsPage && !page) {
|
|
60
|
+
const versionPrefix = route.version.urlPrefix;
|
|
61
|
+
const slugWithoutVersion = versionPrefix && route.slug[0] === route.version.dir
|
|
62
|
+
? route.slug.slice(1)
|
|
63
|
+
: route.slug;
|
|
64
|
+
const contentEntries = route.version.dir
|
|
65
|
+
? config.versions?.find(v => v.dir === route.version.dir)?.content ?? config.content
|
|
66
|
+
: config.content;
|
|
67
|
+
const contentConfig = contentEntries?.find((c: { dir: string }) => c.dir === slugWithoutVersion[0]);
|
|
68
|
+
const redirectUrl = resolveDocsRedirect(slugWithoutVersion, tree, contentConfig);
|
|
69
|
+
if (redirectUrl) {
|
|
70
|
+
const fullUrl = versionPrefix ? `${versionPrefix}${redirectUrl}` : redirectUrl;
|
|
71
|
+
return new Response(null, { status: StatusCodes.TEMPORARY_REDIRECT, headers: { Location: fullUrl } });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
48
75
|
const nav = page ? await getPageNav(pageSlug) : { prev: null, next: null };
|
|
49
76
|
|
|
50
77
|
const relativePath = page ? getRelativePath(page) : null;
|
|
@@ -120,7 +147,7 @@ export default {
|
|
|
120
147
|
|
|
121
148
|
const renderDuration = performance.now() - renderStart;
|
|
122
149
|
|
|
123
|
-
const status = route.type === RouteType.DocsPage && !page ?
|
|
150
|
+
const status = route.type === RouteType.DocsPage && !page ? StatusCodes.NOT_FOUND : StatusCodes.OK;
|
|
124
151
|
|
|
125
152
|
// biome-ignore lint/correctness/useHookAtTopLevel: useNitroApp is a Nitro DI accessor, not a React hook
|
|
126
153
|
useNitroApp().hooks.callHook('chronicle:ssr-rendered', pathname, status, renderDuration);
|
|
@@ -12,6 +12,19 @@ import remarkResolveLinks from '../lib/remark-resolve-links';
|
|
|
12
12
|
import remarkReadingTime from 'remark-reading-time';
|
|
13
13
|
import remarkUnusedDirectives from '../lib/remark-unused-directives';
|
|
14
14
|
|
|
15
|
+
function getDatabaseConnector(preset?: string): { connector: string; options?: Record<string, unknown> } {
|
|
16
|
+
switch (preset) {
|
|
17
|
+
case 'bun':
|
|
18
|
+
return { connector: 'bun-sqlite', options: { name: 'chronicle-search' } };
|
|
19
|
+
case 'cloudflare':
|
|
20
|
+
case 'cloudflare-pages':
|
|
21
|
+
case 'cloudflare-module':
|
|
22
|
+
return { connector: 'cloudflare-d1', options: { bindingName: 'CHRONICLE_DB' } };
|
|
23
|
+
default:
|
|
24
|
+
return { connector: 'sqlite', options: { name: 'chronicle-search' } };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
function resolveOutputDir(projectRoot: string, preset?: string): string {
|
|
16
29
|
if (preset === 'vercel' || preset === 'vercel-static') return path.resolve(projectRoot, '.vercel/output');
|
|
17
30
|
return path.resolve(projectRoot, '.output');
|
|
@@ -140,10 +153,7 @@ export async function createViteConfig(
|
|
|
140
153
|
database: true,
|
|
141
154
|
},
|
|
142
155
|
database: {
|
|
143
|
-
default:
|
|
144
|
-
connector: 'sqlite',
|
|
145
|
-
options: { name: 'chronicle-search' },
|
|
146
|
-
},
|
|
156
|
+
default: getDatabaseConnector(preset),
|
|
147
157
|
},
|
|
148
158
|
},
|
|
149
159
|
};
|
package/src/types/config.ts
CHANGED
|
@@ -131,6 +131,12 @@ const RESERVED_ROUTE_SEGMENTS = [
|
|
|
131
131
|
'sitemap.xml',
|
|
132
132
|
] as const
|
|
133
133
|
|
|
134
|
+
const redirectSchema = z.object({
|
|
135
|
+
from: z.string(),
|
|
136
|
+
to: z.string(),
|
|
137
|
+
permanent: z.boolean().optional(),
|
|
138
|
+
})
|
|
139
|
+
|
|
134
140
|
export const chronicleConfigSchema = z
|
|
135
141
|
.object({
|
|
136
142
|
site: siteSchema,
|
|
@@ -144,6 +150,7 @@ export const chronicleConfigSchema = z
|
|
|
144
150
|
navigation: navigationSchema.optional(),
|
|
145
151
|
search: searchSchema.optional(),
|
|
146
152
|
api: z.array(apiSchema).optional(),
|
|
153
|
+
redirects: z.array(redirectSchema).optional(),
|
|
147
154
|
analytics: analyticsSchema.optional(),
|
|
148
155
|
telemetry: telemetrySchema.optional(),
|
|
149
156
|
})
|
|
@@ -225,6 +232,7 @@ export type SocialLink = z.infer<typeof socialLinkSchema>
|
|
|
225
232
|
export type SearchConfig = z.infer<typeof searchSchema>
|
|
226
233
|
export type ApiConfig = z.infer<typeof apiSchema>
|
|
227
234
|
export type ApiServerConfig = z.infer<typeof apiServerSchema>
|
|
235
|
+
export type RedirectConfig = z.infer<typeof redirectSchema>
|
|
228
236
|
export type ApiAuthConfig = z.infer<typeof apiAuthSchema>
|
|
229
237
|
export type AnalyticsConfig = z.infer<typeof analyticsSchema>
|
|
230
238
|
export type GoogleAnalyticsConfig = z.infer<typeof googleAnalyticsSchema>
|