Skip to content

Commit 1ed02d8

Browse files
committed
Finish serving md files for all doc pages
1 parent f1d6aa4 commit 1ed02d8

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

app/routes/$libraryId/$version.docs.$.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from '@tanstack/react-router'
1+
import { createFileRoute, redirect } from '@tanstack/react-router'
22
import { seo } from '~/utils/seo'
33
import { Doc } from '~/components/Doc'
44
import { loadDocs } from '~/utils/docs'
@@ -11,6 +11,20 @@ export const Route = createFileRoute('/$libraryId/$version/docs/$')({
1111
const { _splat: docsPath, version, libraryId } = ctx.params
1212
const library = getLibrary(libraryId)
1313

14+
const isMarkdown = !!docsPath?.endsWith('.md')
15+
16+
if (isMarkdown) {
17+
const href =
18+
'/api/md' +
19+
ctx.location.pathname.slice(
20+
0,
21+
ctx.location.pathname.length - '.md'.length
22+
)
23+
throw redirect({
24+
href,
25+
})
26+
}
27+
1428
return loadDocs({
1529
repo: library.repo,
1630
branch: getBranch(library, version),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createAPIRoute } from '@tanstack/start/api'
2+
import { getBranch, getLibrary } from '~/libraries'
3+
import { loadDocs } from '~/utils/docs'
4+
5+
export const APIRoute = createAPIRoute('/api/md/$libraryId/$version/docs/framework/$framework/$')({
6+
GET: async ({ params, request }) => {
7+
const { _splat: docsPath, version, libraryId } = params
8+
const library = getLibrary(libraryId)
9+
10+
const location = new URL(request.url)
11+
12+
const loadDocsArgs = {
13+
repo: library.repo,
14+
branch: getBranch(library, version),
15+
docsPath: `${library.docsRoot || 'docs'}/${docsPath}`,
16+
currentPath: location.pathname.slice('/api/md'.length),
17+
redirectPath: `/${library.id}/${version}/docs/overview.md`,
18+
useServerFn: false
19+
}
20+
21+
const res = await loadDocs(loadDocsArgs)
22+
23+
const { content, description, title} = res
24+
25+
// Generate or fetch the Markdown content dynamically
26+
const markdownContent = `# ${title}\n\n> ${description}\n\n${content}`
27+
28+
const filename = (docsPath || 'file').split('/').join('-')
29+
30+
// Return the Markdown content as a response
31+
return new Response(markdownContent, {
32+
headers: {
33+
'Content-Type': 'text/markdown',
34+
'Content-Disposition': `inline; filename="${filename}.md"`,
35+
},
36+
})
37+
},
38+
})

app/routes/api/md/$libraryId/$version.docs.framework.$framework.$.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const APIRoute = createAPIRoute('/api/md/$libraryId/$version/docs/framewo
1616
library.docsRoot || 'docs'
1717
}/framework/${framework}/${docsPath}`,
1818
currentPath: location.pathname.slice('/api/md'.length),
19-
redirectPath: `/${library.id}/${version}/docs/overview`,
19+
redirectPath: `/${library.id}/${version}/docs/overview.md`,
2020
useServerFn: false
2121
}
2222

0 commit comments

Comments
 (0)