@raystack/chronicle 0.1.0-canary.0efaef0
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/bin/chronicle.js +2 -0
- package/dist/cli/index.js +276 -0
- package/package.json +71 -0
- package/src/cli/commands/build.ts +34 -0
- package/src/cli/commands/dev.ts +32 -0
- package/src/cli/commands/init.ts +69 -0
- package/src/cli/commands/serve.ts +40 -0
- package/src/cli/commands/start.ts +28 -0
- package/src/cli/index.ts +21 -0
- package/src/cli/utils/config.ts +42 -0
- package/src/cli/utils/index.ts +3 -0
- package/src/cli/utils/resolve.ts +10 -0
- package/src/cli/utils/scaffold.ts +18 -0
- package/src/components/api/code-snippets.module.css +7 -0
- package/src/components/api/code-snippets.tsx +76 -0
- package/src/components/api/endpoint-page.module.css +58 -0
- package/src/components/api/endpoint-page.tsx +283 -0
- package/src/components/api/field-row.module.css +126 -0
- package/src/components/api/field-row.tsx +204 -0
- package/src/components/api/field-section.module.css +24 -0
- package/src/components/api/field-section.tsx +100 -0
- package/src/components/api/index.ts +8 -0
- package/src/components/api/json-editor.module.css +9 -0
- package/src/components/api/json-editor.tsx +61 -0
- package/src/components/api/key-value-editor.module.css +13 -0
- package/src/components/api/key-value-editor.tsx +62 -0
- package/src/components/api/method-badge.module.css +4 -0
- package/src/components/api/method-badge.tsx +29 -0
- package/src/components/api/response-panel.module.css +8 -0
- package/src/components/api/response-panel.tsx +44 -0
- package/src/components/common/breadcrumb.tsx +3 -0
- package/src/components/common/button.tsx +3 -0
- package/src/components/common/callout.module.css +7 -0
- package/src/components/common/callout.tsx +27 -0
- package/src/components/common/code-block.tsx +3 -0
- package/src/components/common/dialog.tsx +3 -0
- package/src/components/common/index.ts +10 -0
- package/src/components/common/input-field.tsx +3 -0
- package/src/components/common/sidebar.tsx +3 -0
- package/src/components/common/switch.tsx +3 -0
- package/src/components/common/table.tsx +3 -0
- package/src/components/common/tabs.tsx +3 -0
- package/src/components/mdx/code.module.css +42 -0
- package/src/components/mdx/code.tsx +27 -0
- package/src/components/mdx/details.module.css +37 -0
- package/src/components/mdx/details.tsx +18 -0
- package/src/components/mdx/image.tsx +9 -0
- package/src/components/mdx/index.tsx +35 -0
- package/src/components/mdx/link.tsx +41 -0
- package/src/components/mdx/mermaid.module.css +9 -0
- package/src/components/mdx/mermaid.tsx +37 -0
- package/src/components/mdx/paragraph.module.css +8 -0
- package/src/components/mdx/paragraph.tsx +19 -0
- package/src/components/mdx/table.tsx +40 -0
- package/src/components/ui/breadcrumbs.tsx +72 -0
- package/src/components/ui/client-theme-switcher.tsx +18 -0
- package/src/components/ui/footer.module.css +27 -0
- package/src/components/ui/footer.tsx +30 -0
- package/src/components/ui/search.module.css +111 -0
- package/src/components/ui/search.tsx +218 -0
- package/src/lib/api-routes.ts +120 -0
- package/src/lib/config.ts +58 -0
- package/src/lib/get-llm-text.ts +10 -0
- package/src/lib/head.tsx +49 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/openapi.ts +188 -0
- package/src/lib/page-context.tsx +117 -0
- package/src/lib/remark-unused-directives.ts +30 -0
- package/src/lib/schema.ts +99 -0
- package/src/lib/snippet-generators.ts +87 -0
- package/src/lib/source.ts +186 -0
- package/src/pages/ApiLayout.module.css +22 -0
- package/src/pages/ApiLayout.tsx +33 -0
- package/src/pages/ApiPage.tsx +73 -0
- package/src/pages/DocsLayout.tsx +18 -0
- package/src/pages/DocsPage.tsx +43 -0
- package/src/pages/NotFound.tsx +17 -0
- package/src/server/App.tsx +67 -0
- package/src/server/api/apis-proxy.ts +69 -0
- package/src/server/api/health.ts +5 -0
- package/src/server/api/page/[...slug].ts +18 -0
- package/src/server/api/search.ts +170 -0
- package/src/server/api/specs.ts +9 -0
- package/src/server/build-search-index.ts +117 -0
- package/src/server/entry-client.tsx +73 -0
- package/src/server/entry-server.tsx +95 -0
- 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 +39 -0
- package/src/server/utils/safe-path.ts +17 -0
- package/src/server/vite-config.ts +71 -0
- package/src/themes/default/Layout.module.css +81 -0
- package/src/themes/default/Layout.tsx +160 -0
- package/src/themes/default/Page.module.css +46 -0
- package/src/themes/default/Page.tsx +19 -0
- package/src/themes/default/Toc.module.css +48 -0
- package/src/themes/default/Toc.tsx +68 -0
- package/src/themes/default/index.ts +11 -0
- package/src/themes/paper/ChapterNav.module.css +71 -0
- package/src/themes/paper/ChapterNav.tsx +115 -0
- package/src/themes/paper/Layout.module.css +33 -0
- package/src/themes/paper/Layout.tsx +37 -0
- package/src/themes/paper/Page.module.css +181 -0
- package/src/themes/paper/Page.tsx +126 -0
- package/src/themes/paper/ReadingProgress.module.css +132 -0
- package/src/themes/paper/ReadingProgress.tsx +315 -0
- package/src/themes/paper/index.ts +8 -0
- package/src/themes/registry.ts +14 -0
- package/src/types/config.ts +80 -0
- package/src/types/content.ts +36 -0
- package/src/types/globals.d.ts +4 -0
- package/src/types/index.ts +3 -0
- package/src/types/theme.ts +22 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Head } from '@/lib/head';
|
|
2
|
+
import { usePageContext } from '@/lib/page-context';
|
|
3
|
+
import { getTheme } from '@/themes/registry';
|
|
4
|
+
|
|
5
|
+
interface DocsPageProps {
|
|
6
|
+
slug: string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function DocsPage({ slug }: DocsPageProps) {
|
|
10
|
+
const { config, tree, page } = usePageContext();
|
|
11
|
+
|
|
12
|
+
if (!page) return null;
|
|
13
|
+
|
|
14
|
+
const { Page } = getTheme(config.theme?.name);
|
|
15
|
+
const pageUrl = config.url ? `${config.url}/${slug.join('/')}` : undefined;
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<>
|
|
19
|
+
<Head
|
|
20
|
+
title={page.frontmatter.title}
|
|
21
|
+
description={page.frontmatter.description}
|
|
22
|
+
config={config}
|
|
23
|
+
jsonLd={{
|
|
24
|
+
'@context': 'https://schema.org',
|
|
25
|
+
'@type': 'Article',
|
|
26
|
+
headline: page.frontmatter.title,
|
|
27
|
+
description: page.frontmatter.description,
|
|
28
|
+
...(pageUrl && { url: pageUrl })
|
|
29
|
+
}}
|
|
30
|
+
/>
|
|
31
|
+
<Page
|
|
32
|
+
page={{
|
|
33
|
+
slug,
|
|
34
|
+
frontmatter: page.frontmatter,
|
|
35
|
+
content: page.content,
|
|
36
|
+
toc: []
|
|
37
|
+
}}
|
|
38
|
+
config={config}
|
|
39
|
+
tree={tree}
|
|
40
|
+
/>
|
|
41
|
+
</>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Flex, Headline, Text } from '@raystack/apsara';
|
|
2
|
+
|
|
3
|
+
export function NotFound() {
|
|
4
|
+
return (
|
|
5
|
+
<Flex
|
|
6
|
+
direction='column'
|
|
7
|
+
align='center'
|
|
8
|
+
justify='center'
|
|
9
|
+
style={{ minHeight: '60vh' }}
|
|
10
|
+
>
|
|
11
|
+
<Headline size='large' as='h1'>
|
|
12
|
+
404
|
|
13
|
+
</Headline>
|
|
14
|
+
<Text size={3}>Page not found</Text>
|
|
15
|
+
</Flex>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import '@raystack/apsara/normalize.css';
|
|
2
|
+
import '@raystack/apsara/style.css';
|
|
3
|
+
import { ThemeProvider } from '@raystack/apsara';
|
|
4
|
+
import { useLocation } from 'react-router';
|
|
5
|
+
import { Head } from '@/lib/head';
|
|
6
|
+
import { usePageContext } from '@/lib/page-context';
|
|
7
|
+
import { ApiLayout } from '@/pages/ApiLayout';
|
|
8
|
+
import { ApiPage } from '@/pages/ApiPage';
|
|
9
|
+
import { DocsLayout } from '@/pages/DocsLayout';
|
|
10
|
+
import { DocsPage } from '@/pages/DocsPage';
|
|
11
|
+
import type { ChronicleConfig } from '@/types';
|
|
12
|
+
|
|
13
|
+
function resolveRoute(pathname: string) {
|
|
14
|
+
if (pathname.startsWith('/apis')) {
|
|
15
|
+
const slug = pathname
|
|
16
|
+
.replace(/^\/apis\/?/, '')
|
|
17
|
+
.split('/')
|
|
18
|
+
.filter(Boolean);
|
|
19
|
+
return { type: 'api' as const, slug };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const slug =
|
|
23
|
+
pathname === '/' ? [] : pathname.slice(1).split('/').filter(Boolean);
|
|
24
|
+
return { type: 'docs' as const, slug };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function App() {
|
|
28
|
+
const { pathname } = useLocation();
|
|
29
|
+
const { config } = usePageContext();
|
|
30
|
+
const route = resolveRoute(pathname);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<ThemeProvider enableSystem>
|
|
34
|
+
<RootHead config={config} />
|
|
35
|
+
{route.type === 'api' ? (
|
|
36
|
+
<ApiLayout>
|
|
37
|
+
<ApiPage slug={route.slug} />
|
|
38
|
+
</ApiLayout>
|
|
39
|
+
) : (
|
|
40
|
+
<DocsLayout>
|
|
41
|
+
<DocsPage slug={route.slug} />
|
|
42
|
+
</DocsLayout>
|
|
43
|
+
)}
|
|
44
|
+
</ThemeProvider>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function RootHead({ config }: { config: ChronicleConfig }) {
|
|
49
|
+
return (
|
|
50
|
+
<Head
|
|
51
|
+
title={config.title}
|
|
52
|
+
description={config.description}
|
|
53
|
+
config={config}
|
|
54
|
+
jsonLd={
|
|
55
|
+
config.url
|
|
56
|
+
? {
|
|
57
|
+
'@context': 'https://schema.org',
|
|
58
|
+
'@type': 'WebSite',
|
|
59
|
+
name: config.title,
|
|
60
|
+
description: config.description,
|
|
61
|
+
url: config.url
|
|
62
|
+
}
|
|
63
|
+
: undefined
|
|
64
|
+
}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { defineHandler, HTTPError } from 'nitro';
|
|
2
|
+
import { loadConfig } from '@/lib/config';
|
|
3
|
+
import { loadApiSpecs } from '@/lib/openapi';
|
|
4
|
+
|
|
5
|
+
interface ProxyRequest {
|
|
6
|
+
specName: string;
|
|
7
|
+
method: string;
|
|
8
|
+
path: string;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
body?: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default defineHandler(async event => {
|
|
14
|
+
if (event.req.method !== 'POST') {
|
|
15
|
+
throw new HTTPError({ status: 405, message: 'Method not allowed' });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { specName, method, path, headers, body } =
|
|
19
|
+
(await event.req.json()) as ProxyRequest;
|
|
20
|
+
|
|
21
|
+
if (!specName || !method || !path) {
|
|
22
|
+
throw new HTTPError({
|
|
23
|
+
status: 400,
|
|
24
|
+
message: 'Missing specName, method, or path'
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const config = loadConfig();
|
|
29
|
+
const specs = await loadApiSpecs(config.api ?? []);
|
|
30
|
+
const spec = specs.find(s => s.name === specName);
|
|
31
|
+
|
|
32
|
+
if (!spec) {
|
|
33
|
+
throw new HTTPError({ status: 404, message: `Unknown spec: ${specName}` });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (/^[a-z]+:\/\//i.test(path) || path.includes('..')) {
|
|
37
|
+
throw new HTTPError({ status: 400, message: 'Invalid path' });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const url = spec.server.url + path;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(url, {
|
|
44
|
+
method,
|
|
45
|
+
headers,
|
|
46
|
+
body: body ? JSON.stringify(body) : undefined
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
50
|
+
const responseBody = contentType.includes('application/json')
|
|
51
|
+
? await response.json()
|
|
52
|
+
: await response.text();
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
status: response.status,
|
|
56
|
+
statusText: response.statusText,
|
|
57
|
+
body: responseBody
|
|
58
|
+
};
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const message =
|
|
61
|
+
error instanceof Error
|
|
62
|
+
? `${error.message}${error.cause ? `: ${(error.cause as Error).message}` : ''}`
|
|
63
|
+
: 'Request failed';
|
|
64
|
+
throw new HTTPError({
|
|
65
|
+
status: 502,
|
|
66
|
+
message: `Could not reach ${url}\n${message}`
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { defineHandler, HTTPError } from 'nitro';
|
|
3
|
+
import { getPage } from '@/lib/source';
|
|
4
|
+
|
|
5
|
+
export default defineHandler(async event => {
|
|
6
|
+
const slugParam = event.context.params?.slug ?? '';
|
|
7
|
+
const slug = slugParam ? slugParam.split('/') : [];
|
|
8
|
+
const page = await getPage(slug);
|
|
9
|
+
|
|
10
|
+
if (!page) {
|
|
11
|
+
throw new HTTPError({ status: 404, message: 'Page not found' });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const contentDir = __CHRONICLE_CONTENT_DIR__;
|
|
15
|
+
const relativePath = path.relative(contentDir, page.filePath);
|
|
16
|
+
|
|
17
|
+
return { frontmatter: page.frontmatter, relativePath };
|
|
18
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import matter from 'gray-matter';
|
|
3
|
+
import MiniSearch from 'minisearch';
|
|
4
|
+
import { defineHandler } from 'nitro';
|
|
5
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { getSpecSlug } from '@/lib/api-routes';
|
|
8
|
+
import { loadConfig } from '@/lib/config';
|
|
9
|
+
import { loadApiSpecs } from '@/lib/openapi';
|
|
10
|
+
|
|
11
|
+
interface SearchDocument {
|
|
12
|
+
id: string;
|
|
13
|
+
url: string;
|
|
14
|
+
title: string;
|
|
15
|
+
content: string;
|
|
16
|
+
type: 'page' | 'api';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let searchIndex: MiniSearch<SearchDocument> | null = null;
|
|
20
|
+
let cachedDocs: SearchDocument[] | null = null;
|
|
21
|
+
|
|
22
|
+
function createIndex(docs: SearchDocument[]): MiniSearch<SearchDocument> {
|
|
23
|
+
const index = new MiniSearch<SearchDocument>({
|
|
24
|
+
fields: ['title', 'content'],
|
|
25
|
+
storeFields: ['url', 'title', 'type'],
|
|
26
|
+
searchOptions: {
|
|
27
|
+
boost: { title: 2 },
|
|
28
|
+
fuzzy: 0.2,
|
|
29
|
+
prefix: true
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
index.addAll(docs);
|
|
33
|
+
return index;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function loadPrebuiltIndex(): Promise<SearchDocument[] | null> {
|
|
37
|
+
try {
|
|
38
|
+
const indexPath = path.resolve(__dirname, 'search-index.json');
|
|
39
|
+
const raw = await fs.readFile(indexPath, 'utf-8');
|
|
40
|
+
return JSON.parse(raw);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getContentDir(): string {
|
|
47
|
+
return __CHRONICLE_CONTENT_DIR__ || path.join(process.cwd(), 'content');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function scanContent(): Promise<SearchDocument[]> {
|
|
51
|
+
const contentDir = getContentDir();
|
|
52
|
+
const docs: SearchDocument[] = [];
|
|
53
|
+
|
|
54
|
+
async function scan(dir: string, prefix: string[] = []) {
|
|
55
|
+
try {
|
|
56
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
57
|
+
for (const entry of entries) {
|
|
58
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules')
|
|
59
|
+
continue;
|
|
60
|
+
const fullPath = path.join(dir, entry.name);
|
|
61
|
+
|
|
62
|
+
if (entry.isDirectory()) {
|
|
63
|
+
await scan(fullPath, [...prefix, entry.name]);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!entry.name.endsWith('.mdx') && !entry.name.endsWith('.md'))
|
|
68
|
+
continue;
|
|
69
|
+
|
|
70
|
+
const raw = await fs.readFile(fullPath, 'utf-8');
|
|
71
|
+
const { data: fm, content } = matter(raw);
|
|
72
|
+
const baseName = entry.name.replace(/\.(mdx|md)$/, '');
|
|
73
|
+
const slugs = baseName === 'index' ? prefix : [...prefix, baseName];
|
|
74
|
+
const url = slugs.length === 0 ? '/' : `/${slugs.join('/')}`;
|
|
75
|
+
|
|
76
|
+
docs.push({
|
|
77
|
+
id: url,
|
|
78
|
+
url,
|
|
79
|
+
title: fm.title ?? baseName,
|
|
80
|
+
content: content.slice(0, 5000),
|
|
81
|
+
type: 'page'
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
/* directory not readable */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await scan(contentDir);
|
|
90
|
+
return docs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function buildApiDocs(): Promise<SearchDocument[]> {
|
|
94
|
+
const config = loadConfig();
|
|
95
|
+
if (!config.api?.length) return [];
|
|
96
|
+
|
|
97
|
+
const docs: SearchDocument[] = [];
|
|
98
|
+
const specs = await loadApiSpecs(config.api);
|
|
99
|
+
|
|
100
|
+
for (const spec of specs) {
|
|
101
|
+
const specSlug = getSpecSlug(spec);
|
|
102
|
+
const paths = spec.document.paths ?? {};
|
|
103
|
+
for (const [, pathItem] of Object.entries(paths)) {
|
|
104
|
+
if (!pathItem) continue;
|
|
105
|
+
for (const method of ['get', 'post', 'put', 'delete', 'patch'] as const) {
|
|
106
|
+
const op = pathItem[method] as OpenAPIV3.OperationObject | undefined;
|
|
107
|
+
if (!op?.operationId) continue;
|
|
108
|
+
const url = `/apis/${specSlug}/${encodeURIComponent(op.operationId)}`;
|
|
109
|
+
docs.push({
|
|
110
|
+
id: url,
|
|
111
|
+
url,
|
|
112
|
+
title: `${method.toUpperCase()} ${op.summary ?? op.operationId}`,
|
|
113
|
+
content: op.description ?? '',
|
|
114
|
+
type: 'api'
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return docs;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function loadDocuments(): Promise<SearchDocument[]> {
|
|
124
|
+
const prebuilt = await loadPrebuiltIndex();
|
|
125
|
+
if (prebuilt) return prebuilt;
|
|
126
|
+
|
|
127
|
+
const [contentDocs, apiDocs] = await Promise.all([
|
|
128
|
+
scanContent(),
|
|
129
|
+
buildApiDocs()
|
|
130
|
+
]);
|
|
131
|
+
return [...contentDocs, ...apiDocs];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function getDocs(): Promise<SearchDocument[]> {
|
|
135
|
+
if (cachedDocs) return cachedDocs;
|
|
136
|
+
cachedDocs = await loadDocuments();
|
|
137
|
+
return cachedDocs;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function getIndex(): Promise<MiniSearch<SearchDocument>> {
|
|
141
|
+
if (searchIndex) return searchIndex;
|
|
142
|
+
const docs = await getDocs();
|
|
143
|
+
searchIndex = createIndex(docs);
|
|
144
|
+
return searchIndex;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export default defineHandler(async event => {
|
|
148
|
+
const query = event.url.searchParams.get('query') ?? '';
|
|
149
|
+
const index = await getIndex();
|
|
150
|
+
|
|
151
|
+
if (!query) {
|
|
152
|
+
const docs = await getDocs();
|
|
153
|
+
return docs
|
|
154
|
+
.filter(d => d.type === 'page')
|
|
155
|
+
.slice(0, 8)
|
|
156
|
+
.map(d => ({
|
|
157
|
+
id: d.id,
|
|
158
|
+
url: d.url,
|
|
159
|
+
type: d.type,
|
|
160
|
+
content: d.title
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return index.search(query).map(r => ({
|
|
165
|
+
id: r.id,
|
|
166
|
+
url: r.url,
|
|
167
|
+
type: r.type,
|
|
168
|
+
content: r.title
|
|
169
|
+
}));
|
|
170
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineHandler } from 'nitro';
|
|
2
|
+
import { loadConfig } from '@/lib/config';
|
|
3
|
+
import { loadApiSpecs } from '@/lib/openapi';
|
|
4
|
+
|
|
5
|
+
export default defineHandler(async () => {
|
|
6
|
+
const config = loadConfig();
|
|
7
|
+
const specs = config.api?.length ? await loadApiSpecs(config.api) : [];
|
|
8
|
+
return specs;
|
|
9
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import matter from 'gray-matter';
|
|
4
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
5
|
+
import remarkParse from 'remark-parse';
|
|
6
|
+
import { unified } from 'unified';
|
|
7
|
+
import { visit } from 'unist-util-visit';
|
|
8
|
+
import type { Heading, Text } from 'mdast';
|
|
9
|
+
import { getSpecSlug } from '@/lib/api-routes';
|
|
10
|
+
import { loadConfig } from '@/lib/config';
|
|
11
|
+
import { loadApiSpecs } from '@/lib/openapi';
|
|
12
|
+
|
|
13
|
+
interface SearchDocument {
|
|
14
|
+
id: string;
|
|
15
|
+
url: string;
|
|
16
|
+
title: string;
|
|
17
|
+
content: string;
|
|
18
|
+
type: 'page' | 'api';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function extractHeadings(markdown: string): string {
|
|
22
|
+
const tree = unified().use(remarkParse).parse(markdown);
|
|
23
|
+
const headings: string[] = [];
|
|
24
|
+
visit(tree, 'heading', (node: Heading) => {
|
|
25
|
+
const text = node.children
|
|
26
|
+
.filter((child): child is Text => child.type === 'text')
|
|
27
|
+
.map(child => child.value)
|
|
28
|
+
.join('');
|
|
29
|
+
if (text) headings.push(text);
|
|
30
|
+
});
|
|
31
|
+
return headings.join(' ');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function scanContent(contentDir: string): Promise<SearchDocument[]> {
|
|
35
|
+
const docs: SearchDocument[] = [];
|
|
36
|
+
|
|
37
|
+
async function scan(dir: string, prefix: string[] = []) {
|
|
38
|
+
try {
|
|
39
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
40
|
+
for (const entry of entries) {
|
|
41
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules')
|
|
42
|
+
continue;
|
|
43
|
+
const fullPath = path.join(dir, entry.name);
|
|
44
|
+
|
|
45
|
+
if (entry.isDirectory()) {
|
|
46
|
+
await scan(fullPath, [...prefix, entry.name]);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!entry.name.endsWith('.mdx') && !entry.name.endsWith('.md'))
|
|
51
|
+
continue;
|
|
52
|
+
|
|
53
|
+
const raw = await fs.readFile(fullPath, 'utf-8');
|
|
54
|
+
const { data: fm, content } = matter(raw);
|
|
55
|
+
const baseName = entry.name.replace(/\.(mdx|md)$/, '');
|
|
56
|
+
const slugs = baseName === 'index' ? prefix : [...prefix, baseName];
|
|
57
|
+
const url = slugs.length === 0 ? '/' : `/${slugs.join('/')}`;
|
|
58
|
+
|
|
59
|
+
docs.push({
|
|
60
|
+
id: url,
|
|
61
|
+
url,
|
|
62
|
+
title: fm.title ?? baseName,
|
|
63
|
+
content: extractHeadings(content),
|
|
64
|
+
type: 'page'
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
} catch {
|
|
68
|
+
/* directory not readable */
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await scan(contentDir);
|
|
73
|
+
return docs;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function buildApiDocs(): Promise<SearchDocument[]> {
|
|
77
|
+
const config = loadConfig();
|
|
78
|
+
if (!config.api?.length) return [];
|
|
79
|
+
|
|
80
|
+
const docs: SearchDocument[] = [];
|
|
81
|
+
const specs = await loadApiSpecs(config.api);
|
|
82
|
+
|
|
83
|
+
for (const spec of specs) {
|
|
84
|
+
const specSlug = getSpecSlug(spec);
|
|
85
|
+
const paths = spec.document.paths ?? {};
|
|
86
|
+
for (const [, pathItem] of Object.entries(paths)) {
|
|
87
|
+
if (!pathItem) continue;
|
|
88
|
+
for (const method of ['get', 'post', 'put', 'delete', 'patch'] as const) {
|
|
89
|
+
const op = pathItem[method] as OpenAPIV3.OperationObject | undefined;
|
|
90
|
+
if (!op?.operationId) continue;
|
|
91
|
+
const url = `/apis/${specSlug}/${encodeURIComponent(op.operationId)}`;
|
|
92
|
+
docs.push({
|
|
93
|
+
id: url,
|
|
94
|
+
url,
|
|
95
|
+
title: `${method.toUpperCase()} ${op.summary ?? op.operationId}`,
|
|
96
|
+
content: op.description ?? '',
|
|
97
|
+
type: 'api'
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return docs;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function generateSearchIndex(contentDir: string, outDir: string) {
|
|
107
|
+
const [contentDocs, apiDocs] = await Promise.all([
|
|
108
|
+
scanContent(contentDir),
|
|
109
|
+
buildApiDocs()
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
const documents = [...contentDocs, ...apiDocs];
|
|
113
|
+
const outPath = path.join(outDir, 'search-index.json');
|
|
114
|
+
await fs.writeFile(outPath, JSON.stringify(documents));
|
|
115
|
+
|
|
116
|
+
return documents.length;
|
|
117
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import '@vitejs/plugin-react/preamble';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
4
|
+
import { BrowserRouter } from 'react-router';
|
|
5
|
+
import { mdxComponents } from '@/components/mdx';
|
|
6
|
+
import { PageProvider } from '@/lib/page-context';
|
|
7
|
+
import type { ChronicleConfig, Frontmatter, PageTree } from '@/types';
|
|
8
|
+
import type { ApiSpec } from '@/lib/openapi';
|
|
9
|
+
import type { ReactNode } from 'react';
|
|
10
|
+
import { App } from './App';
|
|
11
|
+
|
|
12
|
+
interface EmbeddedData {
|
|
13
|
+
config: ChronicleConfig;
|
|
14
|
+
tree: PageTree;
|
|
15
|
+
slug: string[];
|
|
16
|
+
frontmatter: Frontmatter;
|
|
17
|
+
relativePath: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function hydrate() {
|
|
21
|
+
try {
|
|
22
|
+
const embedded = (
|
|
23
|
+
window as unknown as { __PAGE_DATA__?: EmbeddedData }
|
|
24
|
+
).__PAGE_DATA__;
|
|
25
|
+
|
|
26
|
+
const config: ChronicleConfig = embedded?.config ?? {
|
|
27
|
+
title: 'Documentation'
|
|
28
|
+
};
|
|
29
|
+
const tree: PageTree = embedded?.tree ?? { name: 'root', children: [] };
|
|
30
|
+
const isApiPage =
|
|
31
|
+
window.location.pathname.startsWith('/apis') && !!config.api?.length;
|
|
32
|
+
const apiSpecs: ApiSpec[] = isApiPage
|
|
33
|
+
? await fetch('/api/specs')
|
|
34
|
+
.then(r => r.json())
|
|
35
|
+
.catch(() => [])
|
|
36
|
+
: [];
|
|
37
|
+
|
|
38
|
+
const page = embedded?.relativePath
|
|
39
|
+
? await loadPage(embedded)
|
|
40
|
+
: null;
|
|
41
|
+
|
|
42
|
+
hydrateRoot(
|
|
43
|
+
document.getElementById('root') as HTMLElement,
|
|
44
|
+
<BrowserRouter>
|
|
45
|
+
<PageProvider
|
|
46
|
+
initialConfig={config}
|
|
47
|
+
initialTree={tree}
|
|
48
|
+
initialPage={page}
|
|
49
|
+
initialApiSpecs={apiSpecs}
|
|
50
|
+
>
|
|
51
|
+
<App />
|
|
52
|
+
</PageProvider>
|
|
53
|
+
</BrowserRouter>
|
|
54
|
+
);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
console.error('Hydration failed:', err);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function loadPage(
|
|
61
|
+
embedded: EmbeddedData
|
|
62
|
+
): Promise<{ slug: string[]; frontmatter: Frontmatter; content: ReactNode }> {
|
|
63
|
+
const withoutExt = embedded.relativePath.replace(/\.(mdx|md)$/, '');
|
|
64
|
+
const mod = embedded.relativePath.endsWith('.md')
|
|
65
|
+
? await import(`../../.content/${withoutExt}.md`)
|
|
66
|
+
: await import(`../../.content/${withoutExt}.mdx`);
|
|
67
|
+
const content = mod.default
|
|
68
|
+
? React.createElement(mod.default, { components: mdxComponents })
|
|
69
|
+
: null;
|
|
70
|
+
return { slug: embedded.slug, frontmatter: embedded.frontmatter, content };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
hydrate();
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import '@raystack/apsara/normalize.css';
|
|
2
|
+
import '@raystack/apsara/style.css';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { renderToReadableStream } from 'react-dom/server.edge';
|
|
6
|
+
import { StaticRouter } from 'react-router';
|
|
7
|
+
import { mdxComponents } from '@/components/mdx';
|
|
8
|
+
import { loadConfig } from '@/lib/config';
|
|
9
|
+
import { loadApiSpecs } from '@/lib/openapi';
|
|
10
|
+
import { PageProvider } from '@/lib/page-context';
|
|
11
|
+
import { buildPageTree, getPage, loadPageComponent } from '@/lib/source';
|
|
12
|
+
import { App } from './App';
|
|
13
|
+
|
|
14
|
+
// @ts-expect-error virtual import from Nitro
|
|
15
|
+
import clientAssets from './entry-client?assets=client';
|
|
16
|
+
// @ts-expect-error virtual import from Nitro
|
|
17
|
+
import serverAssets from './entry-server?assets=ssr';
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
async fetch(req: Request) {
|
|
21
|
+
const url = new URL(req.url);
|
|
22
|
+
const pathname = url.pathname;
|
|
23
|
+
const slug = pathname === '/' ? [] : pathname.slice(1).split('/').filter(Boolean);
|
|
24
|
+
|
|
25
|
+
const config = loadConfig();
|
|
26
|
+
const apiSpecs = config.api?.length
|
|
27
|
+
? await loadApiSpecs(config.api).catch(() => [])
|
|
28
|
+
: [];
|
|
29
|
+
|
|
30
|
+
const [tree, sourcePage] = await Promise.all([
|
|
31
|
+
buildPageTree(),
|
|
32
|
+
getPage(slug),
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const pageData = sourcePage
|
|
36
|
+
? {
|
|
37
|
+
slug,
|
|
38
|
+
frontmatter: sourcePage.frontmatter,
|
|
39
|
+
content: await loadPageComponent(sourcePage).then(component =>
|
|
40
|
+
component ? React.createElement(component, { components: mdxComponents }) : null
|
|
41
|
+
),
|
|
42
|
+
}
|
|
43
|
+
: null;
|
|
44
|
+
|
|
45
|
+
const relativePath = sourcePage
|
|
46
|
+
? path.relative(__CHRONICLE_CONTENT_DIR__, sourcePage.filePath)
|
|
47
|
+
: null;
|
|
48
|
+
|
|
49
|
+
const embeddedData = {
|
|
50
|
+
config,
|
|
51
|
+
tree,
|
|
52
|
+
slug,
|
|
53
|
+
frontmatter: pageData?.frontmatter ?? null,
|
|
54
|
+
relativePath,
|
|
55
|
+
};
|
|
56
|
+
const safeJson = JSON.stringify(embeddedData).replace(/</g, '\\u003c');
|
|
57
|
+
|
|
58
|
+
const assets = clientAssets.merge(serverAssets);
|
|
59
|
+
|
|
60
|
+
const stream = await renderToReadableStream(
|
|
61
|
+
<html lang="en">
|
|
62
|
+
<head>
|
|
63
|
+
<meta charSet="UTF-8" />
|
|
64
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
65
|
+
{assets.css.map((attr: { href: string }) => (
|
|
66
|
+
<link key={attr.href} rel="stylesheet" {...attr} />
|
|
67
|
+
))}
|
|
68
|
+
{assets.js.map((attr: { href: string }) => (
|
|
69
|
+
<link key={attr.href} rel="modulepreload" {...attr} />
|
|
70
|
+
))}
|
|
71
|
+
<script type="module" src={assets.entry} />
|
|
72
|
+
<script dangerouslySetInnerHTML={{ __html: `window.__PAGE_DATA__ = ${safeJson}` }} />
|
|
73
|
+
</head>
|
|
74
|
+
<body>
|
|
75
|
+
<div id="root">
|
|
76
|
+
<StaticRouter location={pathname}>
|
|
77
|
+
<PageProvider
|
|
78
|
+
initialConfig={config}
|
|
79
|
+
initialTree={tree}
|
|
80
|
+
initialPage={pageData}
|
|
81
|
+
initialApiSpecs={apiSpecs}
|
|
82
|
+
>
|
|
83
|
+
<App />
|
|
84
|
+
</PageProvider>
|
|
85
|
+
</StaticRouter>
|
|
86
|
+
</div>
|
|
87
|
+
</body>
|
|
88
|
+
</html>,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return new Response(stream, {
|
|
92
|
+
headers: { 'Content-Type': 'text/html;charset=utf-8' },
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
};
|