-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathsource.ts
More file actions
105 lines (100 loc) · 3.15 KB
/
Copy pathsource.ts
File metadata and controls
105 lines (100 loc) · 3.15 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { createElement, Fragment } from 'react'
import { loader, multiple } from 'fumadocs-core/source'
import type { DocData, DocMethods } from 'fumadocs-mdx/runtime/types'
import { openapiSource } from 'fumadocs-openapi/server'
import { docs } from '@/.source/server'
import { i18n } from './i18n'
import { openapi } from './openapi'
const METHOD_COLORS: Record<string, string> = {
GET: 'text-green-600 dark:text-green-400',
HEAD: 'text-green-600 dark:text-green-400',
OPTIONS: 'text-green-600 dark:text-green-400',
POST: 'text-blue-600 dark:text-blue-400',
PUT: 'text-yellow-600 dark:text-yellow-400',
PATCH: 'text-orange-600 dark:text-orange-400',
DELETE: 'text-red-600 dark:text-red-400',
}
/**
* Custom openapi plugin that places method badges BEFORE the page name
* in the sidebar (like Mintlify/Gumloop) instead of after.
*/
function openapiPluginBadgeLeft() {
return {
name: 'fumadocs:openapi-badge-left',
enforce: 'pre' as const,
transformPageTree: {
file(
this: {
storage: {
read: (path: string) => { format: string; data: Record<string, unknown> } | undefined
}
},
node: { name: React.ReactNode },
filePath: string | undefined
) {
if (!filePath) return node
const file = this.storage.read(filePath)
if (!file || file.format !== 'page') return node
const openApiData = file.data._openapi as { method?: string; webhook?: boolean } | undefined
if (!openApiData || typeof openApiData !== 'object') return node
if (openApiData.webhook) {
node.name = createElement(
Fragment,
null,
node.name,
' ',
createElement(
'span',
{
className:
'ms-auto border border-current px-1 rounded-lg text-xs text-nowrap font-mono',
},
'Webhook'
)
)
} else if (openApiData.method) {
const method = openApiData.method.toUpperCase()
const colorClass = METHOD_COLORS[method] ?? METHOD_COLORS.GET
node.name = createElement(
Fragment,
null,
createElement(
'span',
{
className: `font-mono font-medium me-1.5 text-[10px] text-nowrap ${colorClass}`,
'data-method': method.toLowerCase(),
},
method
),
node.name
)
}
return node
},
},
}
}
export const source = loader(
multiple({
docs: docs.toFumadocsSource(),
openapi: await openapiSource(openapi, {
baseDir: 'en/api-reference/(generated)',
groupBy: 'tag',
}),
}),
{
baseUrl: '/',
i18n,
plugins: [openapiPluginBadgeLeft() as never],
}
)
/** Diátaxis page type surfaced as a badge near the page title. */
export type DocsPageType = 'tutorial' | 'guide' | 'reference' | 'concept'
/** Full page data type including MDX content and metadata */
export type PageData = DocData &
DocMethods & {
title: string
description?: string
full?: boolean
pageType?: DocsPageType
}