@raystack/chronicle 0.1.0-canary.111b55a → 0.1.0-canary.1e5fdae
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 +212 -833
- package/package.json +13 -9
- package/src/cli/commands/build.ts +30 -70
- package/src/cli/commands/dev.ts +24 -13
- package/src/cli/commands/init.ts +38 -123
- package/src/cli/commands/serve.ts +35 -50
- package/src/cli/commands/start.ts +20 -16
- package/src/cli/index.ts +14 -14
- package/src/cli/utils/config.ts +25 -26
- package/src/cli/utils/index.ts +3 -2
- package/src/cli/utils/resolve.ts +7 -3
- package/src/cli/utils/scaffold.ts +14 -16
- package/src/components/mdx/details.module.css +0 -2
- package/src/components/mdx/image.tsx +5 -20
- package/src/components/mdx/index.tsx +18 -4
- package/src/components/mdx/link.tsx +24 -20
- package/src/components/ui/breadcrumbs.tsx +8 -42
- package/src/components/ui/footer.tsx +2 -3
- package/src/components/ui/search.tsx +116 -71
- package/src/lib/api-routes.ts +6 -8
- package/src/lib/config.ts +31 -29
- package/src/lib/get-llm-text.ts +10 -0
- package/src/lib/head.tsx +26 -22
- package/src/lib/openapi.ts +8 -8
- package/src/lib/page-context.tsx +74 -58
- package/src/lib/source.ts +136 -114
- package/src/pages/ApiLayout.tsx +22 -18
- package/src/pages/ApiPage.tsx +32 -27
- package/src/pages/DocsLayout.tsx +7 -7
- package/src/pages/DocsPage.tsx +11 -11
- package/src/pages/NotFound.tsx +11 -4
- package/src/server/App.tsx +35 -27
- package/src/server/api/apis-proxy.ts +69 -0
- package/src/server/api/health.ts +5 -0
- package/src/server/api/page/[...slug].ts +17 -0
- package/src/server/api/search.ts +170 -0
- package/src/server/api/specs.ts +9 -0
- package/src/server/build-search-index.ts +78 -68
- package/src/server/entry-client.tsx +67 -55
- package/src/server/entry-server.tsx +100 -35
- package/src/server/routes/llms.txt.ts +61 -0
- package/src/server/routes/og.tsx +75 -0
- package/src/server/routes/robots.txt.ts +11 -0
- package/src/server/routes/sitemap.xml.ts +40 -0
- package/src/server/utils/safe-path.ts +17 -0
- package/src/server/vite-config.ts +87 -47
- package/src/themes/default/Layout.tsx +78 -47
- package/src/themes/default/Page.module.css +0 -16
- package/src/themes/default/Page.tsx +9 -11
- package/src/themes/default/Toc.tsx +25 -39
- package/src/themes/default/index.ts +7 -9
- package/src/themes/paper/ChapterNav.tsx +63 -43
- package/src/themes/paper/Layout.module.css +1 -1
- package/src/themes/paper/Layout.tsx +24 -12
- package/src/themes/paper/Page.module.css +16 -4
- package/src/themes/paper/Page.tsx +56 -62
- package/src/themes/paper/ReadingProgress.tsx +160 -139
- package/src/themes/paper/index.ts +5 -5
- package/src/themes/registry.ts +7 -7
- package/src/types/content.ts +5 -21
- package/src/types/globals.d.ts +3 -0
- package/src/types/theme.ts +4 -3
- package/src/cli/__tests__/config.test.ts +0 -25
- package/src/cli/__tests__/scaffold.test.ts +0 -10
- package/src/pages/__tests__/head.test.tsx +0 -57
- package/src/server/__tests__/entry-server.test.tsx +0 -35
- package/src/server/__tests__/handlers.test.ts +0 -77
- package/src/server/__tests__/og.test.ts +0 -23
- package/src/server/__tests__/router.test.ts +0 -72
- package/src/server/__tests__/vite-config.test.ts +0 -25
- package/src/server/adapters/vercel.ts +0 -133
- package/src/server/dev.ts +0 -156
- package/src/server/entry-prod.ts +0 -97
- package/src/server/entry-vercel.ts +0 -28
- package/src/server/handlers/apis-proxy.ts +0 -52
- package/src/server/handlers/health.ts +0 -3
- package/src/server/handlers/llms.ts +0 -58
- package/src/server/handlers/og.ts +0 -87
- package/src/server/handlers/robots.ts +0 -11
- package/src/server/handlers/search.ts +0 -172
- package/src/server/handlers/sitemap.ts +0 -39
- package/src/server/handlers/specs.ts +0 -9
- package/src/server/index.html +0 -12
- package/src/server/prod.ts +0 -18
- package/src/server/request-handler.ts +0 -63
- package/src/server/router.ts +0 -42
- package/src/themes/default/font.ts +0 -4
package/src/lib/config.ts
CHANGED
|
@@ -1,56 +1,58 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { parse } from 'yaml'
|
|
4
|
-
import type { ChronicleConfig } from '@/types'
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse } from 'yaml';
|
|
4
|
+
import type { ChronicleConfig } from '@/types';
|
|
5
5
|
|
|
6
|
-
const CONFIG_FILE = 'chronicle.yaml'
|
|
6
|
+
const CONFIG_FILE = 'chronicle.yaml';
|
|
7
7
|
|
|
8
8
|
const defaultConfig: ChronicleConfig = {
|
|
9
9
|
title: 'Documentation',
|
|
10
10
|
theme: { name: 'default' },
|
|
11
|
-
search: { enabled: true, placeholder: 'Search...' }
|
|
12
|
-
}
|
|
11
|
+
search: { enabled: true, placeholder: 'Search...' }
|
|
12
|
+
};
|
|
13
13
|
|
|
14
14
|
function resolveConfigPath(): string | null {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
const projectRoot =
|
|
16
|
+
typeof __CHRONICLE_PROJECT_ROOT__ !== 'undefined'
|
|
17
|
+
? __CHRONICLE_PROJECT_ROOT__
|
|
18
|
+
: process.cwd();
|
|
19
|
+
|
|
20
|
+
const rootPath = path.join(projectRoot, CONFIG_FILE);
|
|
21
|
+
if (fs.existsSync(rootPath)) return rootPath;
|
|
22
|
+
|
|
23
|
+
const contentDir =
|
|
24
|
+
typeof __CHRONICLE_CONTENT_DIR__ !== 'undefined'
|
|
25
|
+
? __CHRONICLE_CONTENT_DIR__
|
|
26
|
+
: undefined;
|
|
26
27
|
if (contentDir) {
|
|
27
|
-
const contentPath = path.join(contentDir, CONFIG_FILE)
|
|
28
|
-
if (fs.existsSync(contentPath)) return contentPath
|
|
28
|
+
const contentPath = path.join(contentDir, CONFIG_FILE);
|
|
29
|
+
if (fs.existsSync(contentPath)) return contentPath;
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
+
|
|
32
|
+
return null;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
export function loadConfig(): ChronicleConfig {
|
|
34
|
-
const configPath = resolveConfigPath()
|
|
36
|
+
const configPath = resolveConfigPath();
|
|
35
37
|
|
|
36
38
|
if (!configPath) {
|
|
37
|
-
return defaultConfig
|
|
39
|
+
return defaultConfig;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
const raw = fs.readFileSync(configPath, 'utf-8')
|
|
41
|
-
const userConfig = parse(raw) as Partial<ChronicleConfig
|
|
42
|
+
const raw = fs.readFileSync(configPath, 'utf-8');
|
|
43
|
+
const userConfig = parse(raw) as Partial<ChronicleConfig>;
|
|
42
44
|
|
|
43
45
|
return {
|
|
44
46
|
...defaultConfig,
|
|
45
47
|
...userConfig,
|
|
46
48
|
theme: {
|
|
47
49
|
name: userConfig.theme?.name ?? defaultConfig.theme!.name,
|
|
48
|
-
colors: { ...defaultConfig.theme?.colors, ...userConfig.theme?.colors }
|
|
50
|
+
colors: { ...defaultConfig.theme?.colors, ...userConfig.theme?.colors }
|
|
49
51
|
},
|
|
50
52
|
search: { ...defaultConfig.search, ...userConfig.search },
|
|
51
53
|
footer: userConfig.footer,
|
|
52
54
|
api: userConfig.api,
|
|
53
55
|
llms: { enabled: false, ...userConfig.llms },
|
|
54
|
-
analytics: { enabled: false, ...userConfig.analytics }
|
|
55
|
-
}
|
|
56
|
-
}
|
|
56
|
+
analytics: { enabled: false, ...userConfig.analytics }
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { source } from '@/lib/source'
|
|
2
|
+
import type { InferPageType } from 'fumadocs-core/source'
|
|
3
|
+
|
|
4
|
+
export async function getLLMText(page: InferPageType<typeof source>) {
|
|
5
|
+
const processed = await page.data.getText('processed')
|
|
6
|
+
|
|
7
|
+
return `# ${page.data.title} (${page.url})
|
|
8
|
+
|
|
9
|
+
${processed}`
|
|
10
|
+
}
|
package/src/lib/head.tsx
CHANGED
|
@@ -1,45 +1,49 @@
|
|
|
1
|
-
import type { ChronicleConfig } from '@/types'
|
|
1
|
+
import type { ChronicleConfig } from '@/types';
|
|
2
2
|
|
|
3
3
|
export interface HeadProps {
|
|
4
|
-
title: string
|
|
5
|
-
description?: string
|
|
6
|
-
config: ChronicleConfig
|
|
7
|
-
jsonLd?: Record<string, unknown
|
|
4
|
+
title: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
config: ChronicleConfig;
|
|
7
|
+
jsonLd?: Record<string, unknown>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export function Head({ title, description, config, jsonLd }: HeadProps) {
|
|
11
|
-
const fullTitle = `${title} | ${config.title}
|
|
12
|
-
const ogParams = new URLSearchParams({ title })
|
|
13
|
-
if (description) ogParams.set('description', description)
|
|
11
|
+
const fullTitle = `${title} | ${config.title}`;
|
|
12
|
+
const ogParams = new URLSearchParams({ title });
|
|
13
|
+
if (description) ogParams.set('description', description);
|
|
14
14
|
|
|
15
15
|
return (
|
|
16
16
|
<>
|
|
17
17
|
<title>{fullTitle}</title>
|
|
18
|
-
{description && <meta name=
|
|
18
|
+
{description && <meta name='description' content={description} />}
|
|
19
19
|
|
|
20
20
|
{config.url && (
|
|
21
21
|
<>
|
|
22
|
-
<meta property=
|
|
23
|
-
{description &&
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
<meta property=
|
|
27
|
-
<meta property=
|
|
28
|
-
<meta property=
|
|
22
|
+
<meta property='og:title' content={title} />
|
|
23
|
+
{description && (
|
|
24
|
+
<meta property='og:description' content={description} />
|
|
25
|
+
)}
|
|
26
|
+
<meta property='og:site_name' content={config.title} />
|
|
27
|
+
<meta property='og:type' content='website' />
|
|
28
|
+
<meta property='og:image' content={`/og?${ogParams.toString()}`} />
|
|
29
|
+
<meta property='og:image:width' content='1200' />
|
|
30
|
+
<meta property='og:image:height' content='630' />
|
|
29
31
|
|
|
30
|
-
<meta name=
|
|
31
|
-
<meta name=
|
|
32
|
-
{description &&
|
|
33
|
-
|
|
32
|
+
<meta name='twitter:card' content='summary_large_image' />
|
|
33
|
+
<meta name='twitter:title' content={title} />
|
|
34
|
+
{description && (
|
|
35
|
+
<meta name='twitter:description' content={description} />
|
|
36
|
+
)}
|
|
37
|
+
<meta name='twitter:image' content={`/og?${ogParams.toString()}`} />
|
|
34
38
|
</>
|
|
35
39
|
)}
|
|
36
40
|
|
|
37
41
|
{jsonLd && (
|
|
38
42
|
<script
|
|
39
|
-
type=
|
|
43
|
+
type='application/ld+json'
|
|
40
44
|
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd, null, 2) }}
|
|
41
45
|
/>
|
|
42
46
|
)}
|
|
43
47
|
</>
|
|
44
|
-
)
|
|
48
|
+
);
|
|
45
49
|
}
|
package/src/lib/openapi.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
3
|
import { parse as parseYaml } from 'yaml'
|
|
4
4
|
import type { OpenAPIV2, OpenAPIV3 } from 'openapi-types'
|
|
5
5
|
import type { ApiConfig, ApiServerConfig, ApiAuthConfig } from '@/types/config'
|
|
@@ -17,14 +17,14 @@ export interface ApiSpec {
|
|
|
17
17
|
export type { SchemaField } from './schema'
|
|
18
18
|
export { flattenSchema } from './schema'
|
|
19
19
|
|
|
20
|
-
export function loadApiSpecs(apiConfigs: ApiConfig[]): ApiSpec[] {
|
|
21
|
-
const
|
|
22
|
-
return apiConfigs.map((config) => loadApiSpec(config,
|
|
20
|
+
export async function loadApiSpecs(apiConfigs: ApiConfig[]): Promise<ApiSpec[]> {
|
|
21
|
+
const projectRoot = typeof __CHRONICLE_PROJECT_ROOT__ !== 'undefined' ? __CHRONICLE_PROJECT_ROOT__ : process.cwd()
|
|
22
|
+
return Promise.all(apiConfigs.map((config) => loadApiSpec(config, projectRoot)))
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export function loadApiSpec(config: ApiConfig,
|
|
26
|
-
const specPath = path.resolve(
|
|
27
|
-
const raw = fs.
|
|
25
|
+
export async function loadApiSpec(config: ApiConfig, projectRoot: string): Promise<ApiSpec> {
|
|
26
|
+
const specPath = path.resolve(projectRoot, config.spec)
|
|
27
|
+
const raw = await fs.readFile(specPath, 'utf-8')
|
|
28
28
|
const isYaml = specPath.endsWith('.yaml') || specPath.endsWith('.yml')
|
|
29
29
|
const doc = (isYaml ? parseYaml(raw) : JSON.parse(raw)) as OpenAPIV2.Document | OpenAPIV3.Document
|
|
30
30
|
|
package/src/lib/page-context.tsx
CHANGED
|
@@ -1,95 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { useLocation } from 'react-router';
|
|
9
|
+
import type { ApiSpec } from '@/lib/openapi';
|
|
10
|
+
import type { ChronicleConfig, Frontmatter, Root, TableOfContents } from '@/types';
|
|
11
|
+
|
|
12
|
+
export type MdxLoader = (relativePath: string) => Promise<{ content: ReactNode; toc: TableOfContents }>;
|
|
8
13
|
|
|
9
14
|
interface PageData {
|
|
10
|
-
slug: string[]
|
|
11
|
-
frontmatter: Frontmatter
|
|
12
|
-
content: ReactNode
|
|
15
|
+
slug: string[];
|
|
16
|
+
frontmatter: Frontmatter;
|
|
17
|
+
content: ReactNode;
|
|
18
|
+
toc: TableOfContents;
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
interface PageContextValue {
|
|
16
|
-
config: ChronicleConfig
|
|
17
|
-
tree:
|
|
18
|
-
page: PageData | null
|
|
19
|
-
apiSpecs: ApiSpec[]
|
|
22
|
+
config: ChronicleConfig;
|
|
23
|
+
tree: Root;
|
|
24
|
+
page: PageData | null;
|
|
25
|
+
apiSpecs: ApiSpec[];
|
|
20
26
|
}
|
|
21
27
|
|
|
22
|
-
const PageContext = createContext<PageContextValue | null>(null)
|
|
28
|
+
const PageContext = createContext<PageContextValue | null>(null);
|
|
23
29
|
|
|
24
30
|
export function usePageContext(): PageContextValue {
|
|
25
|
-
const ctx = useContext(PageContext)
|
|
31
|
+
const ctx = useContext(PageContext);
|
|
26
32
|
if (!ctx) {
|
|
27
|
-
console.error('usePageContext: no context found!')
|
|
33
|
+
console.error('usePageContext: no context found!');
|
|
28
34
|
return {
|
|
29
35
|
config: { title: 'Documentation' },
|
|
30
|
-
tree: { name: 'root', children: [] },
|
|
36
|
+
tree: { name: 'root', children: [] } as Root,
|
|
31
37
|
page: null,
|
|
32
|
-
apiSpecs: []
|
|
33
|
-
}
|
|
38
|
+
apiSpecs: []
|
|
39
|
+
};
|
|
34
40
|
}
|
|
35
|
-
return ctx
|
|
41
|
+
return ctx;
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
interface PageProviderProps {
|
|
39
|
-
initialConfig: ChronicleConfig
|
|
40
|
-
initialTree:
|
|
41
|
-
initialPage: PageData | null
|
|
42
|
-
initialApiSpecs: ApiSpec[]
|
|
43
|
-
|
|
45
|
+
initialConfig: ChronicleConfig;
|
|
46
|
+
initialTree: Root;
|
|
47
|
+
initialPage: PageData | null;
|
|
48
|
+
initialApiSpecs: ApiSpec[];
|
|
49
|
+
loadMdx: MdxLoader;
|
|
50
|
+
children: ReactNode;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
export function PageProvider({
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
export function PageProvider({
|
|
54
|
+
initialConfig,
|
|
55
|
+
initialTree,
|
|
56
|
+
initialPage,
|
|
57
|
+
initialApiSpecs,
|
|
58
|
+
loadMdx,
|
|
59
|
+
children
|
|
60
|
+
}: PageProviderProps) {
|
|
61
|
+
const { pathname } = useLocation();
|
|
62
|
+
const [tree] = useState<Root>(initialTree);
|
|
63
|
+
const [page, setPage] = useState<PageData | null>(initialPage);
|
|
64
|
+
const [apiSpecs, setApiSpecs] = useState<ApiSpec[]>(initialApiSpecs);
|
|
65
|
+
const [currentPath, setCurrentPath] = useState(pathname);
|
|
52
66
|
|
|
53
67
|
useEffect(() => {
|
|
54
|
-
if (pathname === currentPath) return
|
|
55
|
-
setCurrentPath(pathname)
|
|
68
|
+
if (pathname === currentPath) return;
|
|
69
|
+
setCurrentPath(pathname);
|
|
56
70
|
|
|
57
|
-
|
|
71
|
+
const cancelled = { current: false };
|
|
58
72
|
|
|
59
73
|
if (pathname.startsWith('/apis')) {
|
|
60
|
-
// Fetch API specs if not already loaded
|
|
61
74
|
if (apiSpecs.length === 0) {
|
|
62
75
|
fetch('/api/specs')
|
|
63
|
-
.then(
|
|
64
|
-
.then(
|
|
65
|
-
|
|
76
|
+
.then(res => res.json())
|
|
77
|
+
.then(specs => {
|
|
78
|
+
if (!cancelled.current) setApiSpecs(specs);
|
|
79
|
+
})
|
|
80
|
+
.catch(() => {});
|
|
66
81
|
}
|
|
67
|
-
return () => { cancelled = true }
|
|
82
|
+
return () => { cancelled.current = true; };
|
|
68
83
|
}
|
|
69
84
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const [sourcePage, newTree] = await Promise.all([getPage(slug), buildPageTree()])
|
|
74
|
-
if (cancelled || !sourcePage) return
|
|
85
|
+
const slug = pathname === '/'
|
|
86
|
+
? []
|
|
87
|
+
: pathname.slice(1).split('/').filter(Boolean);
|
|
75
88
|
|
|
76
|
-
|
|
77
|
-
if (cancelled) return
|
|
89
|
+
const apiPath = slug.length === 0 ? '/api/page/' : `/api/page/${slug.join('/')}`;
|
|
78
90
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
fetch(apiPath)
|
|
92
|
+
.then(res => res.json())
|
|
93
|
+
.then(async (data: { frontmatter: Frontmatter; relativePath: string }) => {
|
|
94
|
+
if (cancelled.current) return;
|
|
95
|
+
const { content, toc } = await loadMdx(data.relativePath);
|
|
96
|
+
if (cancelled.current) return;
|
|
97
|
+
setPage({ slug, frontmatter: data.frontmatter, content, toc });
|
|
84
98
|
})
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return () => { cancelled = true }
|
|
88
|
-
}, [pathname])
|
|
99
|
+
.catch(() => {});
|
|
100
|
+
|
|
101
|
+
return () => { cancelled.current = true; };
|
|
102
|
+
}, [pathname]);
|
|
89
103
|
|
|
90
104
|
return (
|
|
91
|
-
<PageContext.Provider
|
|
105
|
+
<PageContext.Provider
|
|
106
|
+
value={{ config: initialConfig, tree, page, apiSpecs }}
|
|
107
|
+
>
|
|
92
108
|
{children}
|
|
93
109
|
</PageContext.Provider>
|
|
94
|
-
)
|
|
110
|
+
);
|
|
95
111
|
}
|
package/src/lib/source.ts
CHANGED
|
@@ -1,138 +1,160 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
export interface SourcePage {
|
|
14
|
-
url: string
|
|
15
|
-
slugs: string[]
|
|
16
|
-
filePath: string
|
|
17
|
-
frontmatter: Frontmatter
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { loader } from 'fumadocs-core/source';
|
|
4
|
+
import type { Root, Node, Folder } from 'fumadocs-core/page-tree';
|
|
5
|
+
import matter from 'gray-matter';
|
|
6
|
+
import type { MDXContent } from 'mdx/types';
|
|
7
|
+
import type { TableOfContents } from 'fumadocs-core/toc';
|
|
8
|
+
import type { Frontmatter } from '@/types';
|
|
9
|
+
|
|
10
|
+
function getContentDir(): string {
|
|
11
|
+
return __CHRONICLE_CONTENT_DIR__ || path.join(process.cwd(), 'content');
|
|
18
12
|
}
|
|
19
13
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
async function scanFiles(contentDir: string) {
|
|
15
|
+
const files: {
|
|
16
|
+
type: 'page' | 'meta';
|
|
17
|
+
path: string;
|
|
18
|
+
data: Record<string, unknown>;
|
|
19
|
+
}[] = [];
|
|
20
|
+
|
|
21
|
+
async function scan(dir: string, prefix: string[] = []) {
|
|
22
|
+
try {
|
|
23
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
24
|
+
for (const entry of entries) {
|
|
25
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules')
|
|
26
|
+
continue;
|
|
27
|
+
const fullPath = path.join(dir, entry.name);
|
|
28
|
+
const relativePath = [...prefix, entry.name].join('/');
|
|
29
|
+
|
|
30
|
+
if (entry.isDirectory()) {
|
|
31
|
+
await scan(fullPath, [...prefix, entry.name]);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {
|
|
36
|
+
const raw = await fs.readFile(fullPath, 'utf-8');
|
|
37
|
+
const { data } = matter(raw);
|
|
38
|
+
files.push({
|
|
39
|
+
type: 'page',
|
|
40
|
+
path: relativePath,
|
|
41
|
+
data: { ...data, _relativePath: relativePath }
|
|
42
|
+
});
|
|
43
|
+
} else if (entry.name === 'meta.json' || entry.name === 'meta.yaml') {
|
|
44
|
+
try {
|
|
45
|
+
const raw = await fs.readFile(fullPath, 'utf-8');
|
|
46
|
+
const data = entry.name.endsWith('.json')
|
|
47
|
+
? JSON.parse(raw)
|
|
48
|
+
: matter(raw).data;
|
|
49
|
+
files.push({ type: 'meta', path: relativePath, data });
|
|
50
|
+
} catch {
|
|
51
|
+
/* malformed meta file */
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
/* directory not readable */
|
|
31
57
|
}
|
|
32
58
|
}
|
|
33
|
-
return first.slice(0, depth).join('/') + '/'
|
|
34
|
-
}
|
|
35
59
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
function filePathToSlugs(filePath: string): string[] {
|
|
39
|
-
const relative = filePath.slice(prefix.length)
|
|
40
|
-
const withoutExt = relative.replace(/\.(mdx|md)$/, '')
|
|
41
|
-
const parts = withoutExt.split('/').filter(Boolean)
|
|
42
|
-
if (parts[parts.length - 1] === 'index') parts.pop()
|
|
43
|
-
return parts
|
|
60
|
+
await scan(contentDir);
|
|
61
|
+
return files;
|
|
44
62
|
}
|
|
45
63
|
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
let cachedSource: ReturnType<typeof loader> | null = null;
|
|
65
|
+
|
|
66
|
+
async function getSource() {
|
|
67
|
+
if (cachedSource) return cachedSource;
|
|
68
|
+
const contentDir = getContentDir();
|
|
69
|
+
const files = await scanFiles(contentDir);
|
|
70
|
+
cachedSource = loader({
|
|
71
|
+
source: { files },
|
|
72
|
+
baseUrl: '/'
|
|
73
|
+
});
|
|
74
|
+
return cachedSource;
|
|
48
75
|
}
|
|
49
76
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
export async function getPages(): Promise<SourcePage[]> {
|
|
53
|
-
if (cachedPages) return cachedPages
|
|
54
|
-
|
|
55
|
-
cachedPages = Object.entries(meta).map(([filePath, fm]) => {
|
|
56
|
-
const slugs = filePathToSlugs(filePath)
|
|
57
|
-
const baseName = slugs[slugs.length - 1] ?? 'index'
|
|
58
|
-
return {
|
|
59
|
-
url: slugsToUrl(slugs),
|
|
60
|
-
slugs,
|
|
61
|
-
filePath,
|
|
62
|
-
frontmatter: {
|
|
63
|
-
title: fm?.title ?? baseName,
|
|
64
|
-
description: fm?.description,
|
|
65
|
-
order: fm?.order,
|
|
66
|
-
icon: fm?.icon,
|
|
67
|
-
lastModified: fm?.lastModified,
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
})
|
|
77
|
+
export { getSource as source };
|
|
71
78
|
|
|
72
|
-
|
|
79
|
+
export function invalidate() {
|
|
80
|
+
cachedSource = null;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return
|
|
83
|
+
function getOrder(node: Node, orderMap: Map<string, number>): number | undefined {
|
|
84
|
+
if (node.type === 'page') return orderMap.get(node.url);
|
|
85
|
+
if (node.type === 'folder' && node.index) return orderMap.get(node.index.url);
|
|
86
|
+
return undefined;
|
|
79
87
|
}
|
|
80
88
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
89
|
+
function sortNodes(nodes: Node[], orderMap: Map<string, number>): Node[] {
|
|
90
|
+
return [...nodes]
|
|
91
|
+
.map(n =>
|
|
92
|
+
n.type === 'folder'
|
|
93
|
+
? ({ ...n, children: sortNodes(n.children, orderMap) } as Folder)
|
|
94
|
+
: n
|
|
95
|
+
)
|
|
96
|
+
.sort(
|
|
97
|
+
(a, b) =>
|
|
98
|
+
(getOrder(a, orderMap) ?? Number.MAX_SAFE_INTEGER) -
|
|
99
|
+
(getOrder(b, orderMap) ?? Number.MAX_SAFE_INTEGER)
|
|
100
|
+
);
|
|
86
101
|
}
|
|
87
102
|
|
|
88
|
-
|
|
89
|
-
|
|
103
|
+
function sortTreeByOrder(tree: Root, pages: { url: string; data: unknown }[]): Root {
|
|
104
|
+
const orderMap = new Map<string, number>();
|
|
105
|
+
for (const page of pages) {
|
|
106
|
+
const d = page.data as Record<string, unknown>;
|
|
107
|
+
const order = d.order as number | undefined;
|
|
108
|
+
if (order !== undefined) orderMap.set(page.url, order);
|
|
109
|
+
if (page.url === '/') orderMap.set('/', order ?? 0);
|
|
110
|
+
}
|
|
111
|
+
return { ...tree, children: sortNodes(tree.children, orderMap) };
|
|
90
112
|
}
|
|
91
113
|
|
|
92
|
-
export async function
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
pages.forEach((page) => {
|
|
98
|
-
const isIndex = page.url === '/'
|
|
99
|
-
const item: PageTreeItem = {
|
|
100
|
-
type: 'page',
|
|
101
|
-
name: page.frontmatter.title,
|
|
102
|
-
url: page.url,
|
|
103
|
-
order: page.frontmatter.order ?? (isIndex ? 0 : undefined),
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (page.slugs.length > 1) {
|
|
107
|
-
const folder = page.slugs[0]
|
|
108
|
-
if (!folders.has(folder)) {
|
|
109
|
-
folders.set(folder, [])
|
|
110
|
-
}
|
|
111
|
-
folders.get(folder)?.push(item)
|
|
112
|
-
} else {
|
|
113
|
-
rootPages.push(item)
|
|
114
|
-
}
|
|
115
|
-
})
|
|
114
|
+
export async function getPageTree(): Promise<Root> {
|
|
115
|
+
const s = await getSource();
|
|
116
|
+
return sortTreeByOrder(s.pageTree as Root, s.getPages());
|
|
117
|
+
}
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
export async function getPages() {
|
|
120
|
+
const s = await getSource();
|
|
121
|
+
return s.getPages();
|
|
122
|
+
}
|
|
119
123
|
|
|
120
|
-
|
|
124
|
+
export async function getPage(slugs?: string[]) {
|
|
125
|
+
const s = await getSource();
|
|
126
|
+
return s.getPage(slugs);
|
|
127
|
+
}
|
|
121
128
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
})
|
|
133
|
-
})
|
|
129
|
+
export function extractFrontmatter(page: { data: unknown }, fallbackTitle?: string): Frontmatter {
|
|
130
|
+
const d = page.data as Record<string, unknown>;
|
|
131
|
+
return {
|
|
132
|
+
title: (d.title as string) ?? fallbackTitle ?? 'Untitled',
|
|
133
|
+
description: d.description as string | undefined,
|
|
134
|
+
order: d.order as number | undefined,
|
|
135
|
+
icon: d.icon as string | undefined,
|
|
136
|
+
lastModified: d.lastModified as string | undefined,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
134
139
|
|
|
135
|
-
|
|
140
|
+
export function getRelativePath(page: { data: unknown }): string {
|
|
141
|
+
return ((page.data as Record<string, unknown>)._relativePath as string) ?? '';
|
|
142
|
+
}
|
|
136
143
|
|
|
137
|
-
|
|
144
|
+
const ssrModules = import.meta.glob<{ default?: MDXContent; toc?: TableOfContents }>(
|
|
145
|
+
'../../.content/**/*.{mdx,md}'
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
export async function loadPageModule(
|
|
149
|
+
relativePath: string
|
|
150
|
+
): Promise<{ default: MDXContent | null; toc: TableOfContents }> {
|
|
151
|
+
if (!relativePath || relativePath.includes('..')) return { default: null, toc: [] };
|
|
152
|
+
const withoutExt = relativePath.replace(/\.(mdx|md)$/, '');
|
|
153
|
+
const key = relativePath.endsWith('.md')
|
|
154
|
+
? `../../.content/${withoutExt}.md`
|
|
155
|
+
: `../../.content/${withoutExt}.mdx`;
|
|
156
|
+
const loader = ssrModules[key];
|
|
157
|
+
if (!loader) return { default: null, toc: [] };
|
|
158
|
+
const mod = await loader();
|
|
159
|
+
return { default: mod.default ?? null, toc: mod.toc ?? [] };
|
|
138
160
|
}
|