@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.
Files changed (87) hide show
  1. package/dist/cli/index.js +212 -833
  2. package/package.json +13 -9
  3. package/src/cli/commands/build.ts +30 -70
  4. package/src/cli/commands/dev.ts +24 -13
  5. package/src/cli/commands/init.ts +38 -123
  6. package/src/cli/commands/serve.ts +35 -50
  7. package/src/cli/commands/start.ts +20 -16
  8. package/src/cli/index.ts +14 -14
  9. package/src/cli/utils/config.ts +25 -26
  10. package/src/cli/utils/index.ts +3 -2
  11. package/src/cli/utils/resolve.ts +7 -3
  12. package/src/cli/utils/scaffold.ts +14 -16
  13. package/src/components/mdx/details.module.css +0 -2
  14. package/src/components/mdx/image.tsx +5 -20
  15. package/src/components/mdx/index.tsx +18 -4
  16. package/src/components/mdx/link.tsx +24 -20
  17. package/src/components/ui/breadcrumbs.tsx +8 -42
  18. package/src/components/ui/footer.tsx +2 -3
  19. package/src/components/ui/search.tsx +116 -71
  20. package/src/lib/api-routes.ts +6 -8
  21. package/src/lib/config.ts +31 -29
  22. package/src/lib/get-llm-text.ts +10 -0
  23. package/src/lib/head.tsx +26 -22
  24. package/src/lib/openapi.ts +8 -8
  25. package/src/lib/page-context.tsx +74 -58
  26. package/src/lib/source.ts +136 -114
  27. package/src/pages/ApiLayout.tsx +22 -18
  28. package/src/pages/ApiPage.tsx +32 -27
  29. package/src/pages/DocsLayout.tsx +7 -7
  30. package/src/pages/DocsPage.tsx +11 -11
  31. package/src/pages/NotFound.tsx +11 -4
  32. package/src/server/App.tsx +35 -27
  33. package/src/server/api/apis-proxy.ts +69 -0
  34. package/src/server/api/health.ts +5 -0
  35. package/src/server/api/page/[...slug].ts +17 -0
  36. package/src/server/api/search.ts +170 -0
  37. package/src/server/api/specs.ts +9 -0
  38. package/src/server/build-search-index.ts +78 -68
  39. package/src/server/entry-client.tsx +67 -55
  40. package/src/server/entry-server.tsx +100 -35
  41. package/src/server/routes/llms.txt.ts +61 -0
  42. package/src/server/routes/og.tsx +75 -0
  43. package/src/server/routes/robots.txt.ts +11 -0
  44. package/src/server/routes/sitemap.xml.ts +40 -0
  45. package/src/server/utils/safe-path.ts +17 -0
  46. package/src/server/vite-config.ts +87 -47
  47. package/src/themes/default/Layout.tsx +78 -47
  48. package/src/themes/default/Page.module.css +0 -16
  49. package/src/themes/default/Page.tsx +9 -11
  50. package/src/themes/default/Toc.tsx +25 -39
  51. package/src/themes/default/index.ts +7 -9
  52. package/src/themes/paper/ChapterNav.tsx +63 -43
  53. package/src/themes/paper/Layout.module.css +1 -1
  54. package/src/themes/paper/Layout.tsx +24 -12
  55. package/src/themes/paper/Page.module.css +16 -4
  56. package/src/themes/paper/Page.tsx +56 -62
  57. package/src/themes/paper/ReadingProgress.tsx +160 -139
  58. package/src/themes/paper/index.ts +5 -5
  59. package/src/themes/registry.ts +7 -7
  60. package/src/types/content.ts +5 -21
  61. package/src/types/globals.d.ts +3 -0
  62. package/src/types/theme.ts +4 -3
  63. package/src/cli/__tests__/config.test.ts +0 -25
  64. package/src/cli/__tests__/scaffold.test.ts +0 -10
  65. package/src/pages/__tests__/head.test.tsx +0 -57
  66. package/src/server/__tests__/entry-server.test.tsx +0 -35
  67. package/src/server/__tests__/handlers.test.ts +0 -77
  68. package/src/server/__tests__/og.test.ts +0 -23
  69. package/src/server/__tests__/router.test.ts +0 -72
  70. package/src/server/__tests__/vite-config.test.ts +0 -25
  71. package/src/server/adapters/vercel.ts +0 -133
  72. package/src/server/dev.ts +0 -156
  73. package/src/server/entry-prod.ts +0 -97
  74. package/src/server/entry-vercel.ts +0 -28
  75. package/src/server/handlers/apis-proxy.ts +0 -52
  76. package/src/server/handlers/health.ts +0 -3
  77. package/src/server/handlers/llms.ts +0 -58
  78. package/src/server/handlers/og.ts +0 -87
  79. package/src/server/handlers/robots.ts +0 -11
  80. package/src/server/handlers/search.ts +0 -172
  81. package/src/server/handlers/sitemap.ts +0 -39
  82. package/src/server/handlers/specs.ts +0 -9
  83. package/src/server/index.html +0 -12
  84. package/src/server/prod.ts +0 -18
  85. package/src/server/request-handler.ts +0 -63
  86. package/src/server/router.ts +0 -42
  87. 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
- // Check project root via env var
16
- const projectRoot = process.env.CHRONICLE_PROJECT_ROOT
17
- if (projectRoot) {
18
- const rootPath = path.join(projectRoot, CONFIG_FILE)
19
- if (fs.existsSync(rootPath)) return rootPath
20
- }
21
- // Check cwd
22
- const cwdPath = path.join(process.cwd(), CONFIG_FILE)
23
- if (fs.existsSync(cwdPath)) return cwdPath
24
- // Check content dir
25
- const contentDir = process.env.CHRONICLE_CONTENT_DIR
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
- return null
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="description" content={description} />}
18
+ {description && <meta name='description' content={description} />}
19
19
 
20
20
  {config.url && (
21
21
  <>
22
- <meta property="og:title" content={title} />
23
- {description && <meta property="og:description" content={description} />}
24
- <meta property="og:site_name" content={config.title} />
25
- <meta property="og:type" content="website" />
26
- <meta property="og:image" content={`/og?${ogParams.toString()}`} />
27
- <meta property="og:image:width" content="1200" />
28
- <meta property="og:image:height" content="630" />
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="twitter:card" content="summary_large_image" />
31
- <meta name="twitter:title" content={title} />
32
- {description && <meta name="twitter:description" content={description} />}
33
- <meta name="twitter:image" content={`/og?${ogParams.toString()}`} />
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="application/ld+json"
43
+ type='application/ld+json'
40
44
  dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd, null, 2) }}
41
45
  />
42
46
  )}
43
47
  </>
44
- )
48
+ );
45
49
  }
@@ -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 contentDir = process.env.CHRONICLE_CONTENT_DIR ?? process.cwd()
22
- return apiConfigs.map((config) => loadApiSpec(config, contentDir))
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, contentDir: string): ApiSpec {
26
- const specPath = path.resolve(contentDir, config.spec)
27
- const raw = fs.readFileSync(specPath, 'utf-8')
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
 
@@ -1,95 +1,111 @@
1
- import { createContext, useContext, useState, useEffect, type ReactNode } from 'react'
2
- import { useLocation } from 'react-router-dom'
3
- import type { ChronicleConfig, Frontmatter, PageTree } from '@/types'
4
- import type { ApiSpec } from '@/lib/openapi'
5
- import { getPage, loadPageComponent, buildPageTree } from '@/lib/source'
6
- import { mdxComponents } from '@/components/mdx'
7
- import React from 'react'
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: PageTree
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: PageTree
41
- initialPage: PageData | null
42
- initialApiSpecs: ApiSpec[]
43
- children: ReactNode
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({ initialConfig, initialTree, initialPage, initialApiSpecs, children }: PageProviderProps) {
47
- const { pathname } = useLocation()
48
- const [tree, setTree] = useState<PageTree>(initialTree)
49
- const [page, setPage] = useState<PageData | null>(initialPage)
50
- const [apiSpecs, setApiSpecs] = useState<ApiSpec[]>(initialApiSpecs)
51
- const [currentPath, setCurrentPath] = useState(pathname)
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
- let cancelled = false
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((res) => res.json())
64
- .then((specs) => { if (!cancelled) setApiSpecs(specs) })
65
- .catch(() => {})
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
- async function load() {
71
- const slug = pathname === '/' ? [] : pathname.slice(1).split('/').filter(Boolean)
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
- const component = await loadPageComponent(sourcePage)
77
- if (cancelled) return
89
+ const apiPath = slug.length === 0 ? '/api/page/' : `/api/page/${slug.join('/')}`;
78
90
 
79
- setTree(newTree)
80
- setPage({
81
- slug,
82
- frontmatter: sourcePage.frontmatter,
83
- content: component ? React.createElement(component, { components: mdxComponents }) : null,
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
- load()
87
- return () => { cancelled = true }
88
- }, [pathname])
99
+ .catch(() => {});
100
+
101
+ return () => { cancelled.current = true; };
102
+ }, [pathname]);
89
103
 
90
104
  return (
91
- <PageContext.Provider value={{ config: initialConfig, tree, page, apiSpecs }}>
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 type { MDXContent } from 'mdx/types'
2
- import type { Frontmatter, PageTree, PageTreeItem } from '@/types'
3
-
4
- const meta: Record<string, Frontmatter> = import.meta.glob(
5
- '@content/**/*.{mdx,md}',
6
- { eager: true, import: 'frontmatter' }
7
- )
8
-
9
- const loaders: Record<string, () => Promise<{ default: MDXContent }>> = import.meta.glob(
10
- '@content/**/*.{mdx,md}'
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
- // Compute common directory prefix of all glob keys once
21
- function computePrefix(keys: string[]): string {
22
- if (keys.length === 0) return ''
23
- const dirs = keys.map((k) => k.split('/').slice(0, -1)) // drop filename
24
- const first = dirs[0]
25
- let depth = 0
26
- for (let i = 0; i < first.length; i++) {
27
- if (dirs.every((d) => d[i] === first[i])) {
28
- depth = i + 1
29
- } else {
30
- break
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
- const prefix = computePrefix(Object.keys(meta))
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
- function slugsToUrl(slugs: string[]): string {
47
- return slugs.length === 0 ? '/' : '/' + slugs.join('/')
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
- let cachedPages: SourcePage[] | null = null
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
- return cachedPages
79
+ export function invalidate() {
80
+ cachedSource = null;
73
81
  }
74
82
 
75
- export async function getPage(slug?: string[]): Promise<SourcePage | null> {
76
- const pages = await getPages()
77
- const targetUrl = !slug || slug.length === 0 ? '/' : '/' + slug.join('/')
78
- return pages.find((p) => p.url === targetUrl) ?? null
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
- export async function loadPageComponent(page: SourcePage): Promise<MDXContent | null> {
82
- const loader = loaders[page.filePath]
83
- if (!loader) return null
84
- const mod = await loader()
85
- return mod.default
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
- export function invalidate() {
89
- cachedPages = null
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 buildPageTree(): Promise<PageTree> {
93
- const pages = await getPages()
94
- const folders = new Map<string, PageTreeItem[]>()
95
- const rootPages: PageTreeItem[] = []
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
- const sortByOrder = (items: PageTreeItem[]) =>
118
- items.sort((a, b) => (a.order ?? Number.MAX_SAFE_INTEGER) - (b.order ?? Number.MAX_SAFE_INTEGER))
119
+ export async function getPages() {
120
+ const s = await getSource();
121
+ return s.getPages();
122
+ }
119
123
 
120
- const children: PageTreeItem[] = sortByOrder(rootPages)
124
+ export async function getPage(slugs?: string[]) {
125
+ const s = await getSource();
126
+ return s.getPage(slugs);
127
+ }
121
128
 
122
- const folderItems: PageTreeItem[] = []
123
- folders.forEach((items, folder) => {
124
- const sorted = sortByOrder(items)
125
- const indexPage = items.find(item => item.url === `/${folder}`)
126
- const folderOrder = indexPage?.order ?? sorted[0]?.order
127
- folderItems.push({
128
- type: 'folder',
129
- name: folder.charAt(0).toUpperCase() + folder.slice(1),
130
- order: folderOrder,
131
- children: sorted,
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
- children.push(...sortByOrder(folderItems))
140
+ export function getRelativePath(page: { data: unknown }): string {
141
+ return ((page.data as Record<string, unknown>)._relativePath as string) ?? '';
142
+ }
136
143
 
137
- return { name: 'root', children }
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
  }