-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy path$version.docs.{$}[.]md.tsx
More file actions
68 lines (60 loc) · 2.22 KB
/
$version.docs.{$}[.]md.tsx
File metadata and controls
68 lines (60 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createFileRoute, notFound } from '@tanstack/react-router'
import { getBranch, getLibrary, type LibraryId } from '~/libraries'
import { isDocsNotFoundError } from '~/utils/docs-errors'
import { loadDocs } from '~/utils/docs'
import { filterFrameworkContent } from '~/utils/markdown/filterFrameworkContent'
import { getPackageManager } from '~/utils/markdown/installCommand'
export const Route = createFileRoute('/$libraryId/$version/docs/{$}.md')({
server: {
handlers: {
GET: async ({
request,
params,
}: {
request: Request
params: { libraryId: string; version: string; _splat: string }
}) => {
const url = new URL(request.url)
const framework = url.searchParams.get('framework')
const pm = getPackageManager(url.searchParams.get('pm'))
const keepMarkers = url.searchParams.get('keep_markers') === 'true'
const { libraryId, version, _splat: docsPath } = params
const library = getLibrary(libraryId as LibraryId)
const root = library.docsRoot || 'docs'
let doc
try {
doc = await loadDocs({
repo: library.repo,
branch: getBranch(library, version),
docsRoot: root,
docsPath,
})
} catch (error) {
if (isDocsNotFoundError(error)) {
throw notFound()
}
throw error
}
// Filter framework-specific content only if framework is explicitly specified
const filteredContent = framework
? filterFrameworkContent(doc.content, {
framework,
packageManager: pm,
keepMarkers,
})
: doc.content
const markdownContent = `# ${doc.title}\n${filteredContent}`
const filename = (docsPath || 'file').split('/').join('-')
return new Response(markdownContent, {
headers: {
'Content-Type': 'text/markdown',
'Content-Disposition': `inline; filename="${filename}.md"`,
'Cache-Control': 'public, max-age=0, must-revalidate',
'Cdn-Cache-Control':
'max-age=300, stale-while-revalidate=300, durable',
},
})
},
},
},
})