Skip to content

Commit 285e63c

Browse files
feat: directly serve library document markdown
Co-authored-by: SeanCassiere <[email protected]>
1 parent c9a51c8 commit 285e63c

File tree

3 files changed

+156
-9
lines changed

3 files changed

+156
-9
lines changed

.tanstack-start/server-routes/routeTree.gen.ts

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,114 @@ import {
1919
createServerFileRoute,
2020
} from '@tanstack/react-start/server'
2121

22+
import { ServerRoute as LibraryIdVersionDocsChar123Char125DotmdRouteImport } from './../../src/routes/$libraryId/$version.docs.{$}[.]md'
23+
import { ServerRoute as LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRouteImport } from './../../src/routes/$libraryId/$version.docs.framework.$framework.{$}[.]md'
24+
2225
// Create/Update Routes
2326

2427
const rootRoute = createServerRoute()
2528

29+
const LibraryIdVersionDocsChar123Char125DotmdRoute =
30+
LibraryIdVersionDocsChar123Char125DotmdRouteImport.update({
31+
id: '/$libraryId/$version/docs/{$}.md',
32+
path: '/$libraryId/$version/docs/{$}.md',
33+
getParentRoute: () => rootRoute,
34+
} as any)
35+
36+
const LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute =
37+
LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRouteImport.update({
38+
id: '/$libraryId/$version/docs/framework/$framework/{$}.md',
39+
path: '/$libraryId/$version/docs/framework/$framework/{$}.md',
40+
getParentRoute: () => rootRoute,
41+
} as any)
42+
2643
// Populate the FileRoutesByPath interface
2744

2845
declare module '@tanstack/react-start/server' {
29-
interface FileRoutesByPath {}
46+
interface FileRoutesByPath {
47+
'/$libraryId/$version/docs/{$}.md': {
48+
id: '/$libraryId/$version/docs/{$}.md'
49+
path: '/$libraryId/$version/docs/{$}.md'
50+
fullPath: '/$libraryId/$version/docs/{$}.md'
51+
preLoaderRoute: typeof LibraryIdVersionDocsChar123Char125DotmdRouteImport
52+
parentRoute: typeof rootRoute
53+
}
54+
'/$libraryId/$version/docs/framework/$framework/{$}.md': {
55+
id: '/$libraryId/$version/docs/framework/$framework/{$}.md'
56+
path: '/$libraryId/$version/docs/framework/$framework/{$}.md'
57+
fullPath: '/$libraryId/$version/docs/framework/$framework/{$}.md'
58+
preLoaderRoute: typeof LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRouteImport
59+
parentRoute: typeof rootRoute
60+
}
61+
}
3062
}
3163

3264
// Add type-safety to the createFileRoute function across the route tree
3365

66+
declare module './../../src/routes/$libraryId/$version.docs.{$}[.]md' {
67+
const createServerFileRoute: CreateServerFileRoute<
68+
FileRoutesByPath['/$libraryId/$version/docs/{$}.md']['parentRoute'],
69+
FileRoutesByPath['/$libraryId/$version/docs/{$}.md']['id'],
70+
FileRoutesByPath['/$libraryId/$version/docs/{$}.md']['path'],
71+
FileRoutesByPath['/$libraryId/$version/docs/{$}.md']['fullPath'],
72+
unknown
73+
>
74+
}
75+
declare module './../../src/routes/$libraryId/$version.docs.framework.$framework.{$}[.]md' {
76+
const createServerFileRoute: CreateServerFileRoute<
77+
FileRoutesByPath['/$libraryId/$version/docs/framework/$framework/{$}.md']['parentRoute'],
78+
FileRoutesByPath['/$libraryId/$version/docs/framework/$framework/{$}.md']['id'],
79+
FileRoutesByPath['/$libraryId/$version/docs/framework/$framework/{$}.md']['path'],
80+
FileRoutesByPath['/$libraryId/$version/docs/framework/$framework/{$}.md']['fullPath'],
81+
unknown
82+
>
83+
}
84+
3485
// Create and export the route tree
3586

36-
export interface FileRoutesByFullPath {}
87+
export interface FileRoutesByFullPath {
88+
'/$libraryId/$version/docs/{$}.md': typeof LibraryIdVersionDocsChar123Char125DotmdRoute
89+
'/$libraryId/$version/docs/framework/$framework/{$}.md': typeof LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute
90+
}
3791

38-
export interface FileRoutesByTo {}
92+
export interface FileRoutesByTo {
93+
'/$libraryId/$version/docs/{$}.md': typeof LibraryIdVersionDocsChar123Char125DotmdRoute
94+
'/$libraryId/$version/docs/framework/$framework/{$}.md': typeof LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute
95+
}
3996

4097
export interface FileRoutesById {
4198
__root__: typeof rootRoute
99+
'/$libraryId/$version/docs/{$}.md': typeof LibraryIdVersionDocsChar123Char125DotmdRoute
100+
'/$libraryId/$version/docs/framework/$framework/{$}.md': typeof LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute
42101
}
43102

44103
export interface FileRouteTypes {
45104
fileRoutesByFullPath: FileRoutesByFullPath
46-
fullPaths: never
105+
fullPaths:
106+
| '/$libraryId/$version/docs/{$}.md'
107+
| '/$libraryId/$version/docs/framework/$framework/{$}.md'
47108
fileRoutesByTo: FileRoutesByTo
48-
to: never
49-
id: '__root__'
109+
to:
110+
| '/$libraryId/$version/docs/{$}.md'
111+
| '/$libraryId/$version/docs/framework/$framework/{$}.md'
112+
id:
113+
| '__root__'
114+
| '/$libraryId/$version/docs/{$}.md'
115+
| '/$libraryId/$version/docs/framework/$framework/{$}.md'
50116
fileRoutesById: FileRoutesById
51117
}
52118

53-
export interface RootRouteChildren {}
119+
export interface RootRouteChildren {
120+
LibraryIdVersionDocsChar123Char125DotmdRoute: typeof LibraryIdVersionDocsChar123Char125DotmdRoute
121+
LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute: typeof LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute
122+
}
54123

55-
const rootRouteChildren: RootRouteChildren = {}
124+
const rootRouteChildren: RootRouteChildren = {
125+
LibraryIdVersionDocsChar123Char125DotmdRoute:
126+
LibraryIdVersionDocsChar123Char125DotmdRoute,
127+
LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute:
128+
LibraryIdVersionDocsFrameworkFrameworkChar123Char125DotmdRoute,
129+
}
56130

57131
export const routeTree = rootRoute
58132
._addFileChildren(rootRouteChildren)
@@ -63,7 +137,16 @@ export const routeTree = rootRoute
63137
"routes": {
64138
"__root__": {
65139
"filePath": "__root.tsx",
66-
"children": []
140+
"children": [
141+
"/$libraryId/$version/docs/{$}.md",
142+
"/$libraryId/$version/docs/framework/$framework/{$}.md"
143+
]
144+
},
145+
"/$libraryId/$version/docs/{$}.md": {
146+
"filePath": "$libraryId/$version.docs.{$}[.]md.tsx"
147+
},
148+
"/$libraryId/$version/docs/framework/$framework/{$}.md": {
149+
"filePath": "$libraryId/$version.docs.framework.$framework.{$}[.]md.tsx"
67150
}
68151
}
69152
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getBranch, getLibrary } from '~/libraries'
2+
import { loadDocs } from '~/utils/docs'
3+
4+
export const ServerRoute = createServerFileRoute().methods({
5+
GET: async ({ request, params }) => {
6+
const url = new URL(request.url)
7+
8+
const { libraryId, version, framework, _splat: docsPath } = params
9+
const library = getLibrary(libraryId)
10+
const root = library.docsRoot || 'docs'
11+
12+
const doc = await loadDocs({
13+
repo: library.repo,
14+
branch: getBranch(library, version),
15+
docsPath: `${root}/framework/${framework}/${docsPath}`,
16+
currentPath: url.pathname,
17+
redirectPath: `/${library.id}/${version}/docs/overview`,
18+
})
19+
20+
const markdownContent = `# ${doc.title}\n${doc.content}`
21+
const filename = (docsPath || 'file').split('/').join('-')
22+
23+
return new Response(markdownContent, {
24+
headers: {
25+
'Content-Type': 'text/markdown',
26+
'Content-Disposition': `inline; filename="${filename}.md"`,
27+
'Cache-Control': 'public, max-age=0, must-revalidate',
28+
'Cdn-Cache-Control': 'max-age=300, stale-while-revalidate=300, durable',
29+
},
30+
})
31+
},
32+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getBranch, getLibrary } from '~/libraries'
2+
import { loadDocs } from '~/utils/docs'
3+
4+
export const ServerRoute = createServerFileRoute().methods({
5+
GET: async ({ request, params }) => {
6+
const url = new URL(request.url)
7+
8+
const { libraryId, version, _splat: docsPath } = params
9+
const library = getLibrary(libraryId)
10+
const root = library.docsRoot || 'docs'
11+
12+
const doc = await loadDocs({
13+
repo: library.repo,
14+
branch: getBranch(library, version),
15+
docsPath: `${root}/${docsPath}`,
16+
currentPath: url.pathname,
17+
redirectPath: `/${library.id}/${version}/docs/overview`,
18+
})
19+
20+
const markdownContent = `# ${doc.title}\n${doc.content}`
21+
const filename = (docsPath || 'file').split('/').join('-')
22+
23+
return new Response(markdownContent, {
24+
headers: {
25+
'Content-Type': 'text/markdown',
26+
'Content-Disposition': `inline; filename="${filename}.md"`,
27+
'Cache-Control': 'public, max-age=0, must-revalidate',
28+
'Cdn-Cache-Control': 'max-age=300, stale-while-revalidate=300, durable',
29+
},
30+
})
31+
},
32+
})

0 commit comments

Comments
 (0)