|
1
|
import { addExtraToMd, getCleanMd } from "docs-utils" |
|
2
|
import { unstable_cache } from "next/cache" |
|
3
|
import { |
|
4
|
addUrlToRelativeLink, |
|
5
|
crossProjectLinksPlugin, |
|
6
|
localLinksRehypePlugin, |
|
7
|
} from "remark-rehype-plugins" |
|
8
|
import type { Plugin } from "unified" |
|
9
|
|
|
10
|
type Options = { |
|
11
|
removeExtra?: boolean |
|
12
|
} |
|
13
|
|
|
14
|
// slug is used as the cache key; content is the MDX source fetched from /raw-mdx/ |
|
15
|
export const getCleanMdCached = unstable_cache( |
|
16
|
async (content: string, options: Options = {}) => { |
|
17
|
const { removeExtra } = options |
|
18
|
const md = await getCleanMd({ |
|
19
|
file: content, |
|
20
|
type: "content", |
|
21
|
plugins: { |
|
22
|
before: [ |
|
23
|
[ |
|
24
|
crossProjectLinksPlugin, |
|
25
|
{ |
|
26
|
baseUrl: process.env.NEXT_PUBLIC_BASE_URL, |
|
27
|
projectUrls: { |
|
28
|
resources: { |
|
29
|
url: process.env.NEXT_PUBLIC_RESOURCES_URL, |
|
30
|
}, |
|
31
|
"user-guide": { |
|
32
|
url: process.env.NEXT_PUBLIC_RESOURCES_URL, |
|
33
|
}, |
|
34
|
ui: { |
|
35
|
url: process.env.NEXT_PUBLIC_RESOURCES_URL, |
|
36
|
}, |
|
37
|
api: { |
|
38
|
url: process.env.NEXT_PUBLIC_RESOURCES_URL, |
|
39
|
}, |
|
40
|
}, |
|
41
|
useBaseUrl: |
|
42
|
process.env.NODE_ENV === "production" || |
|
43
|
process.env.VERCEL_ENV === "production" || |
|
44
|
!!process.env.CLOUDFLARE_ENV, |
|
45
|
}, |
|
46
|
], |
|
47
|
[localLinksRehypePlugin], |
|
48
|
] as unknown as Plugin[], |
|
49
|
after: [ |
|
50
|
[addUrlToRelativeLink, { url: process.env.NEXT_PUBLIC_BASE_URL }], |
|
51
|
] as unknown as Plugin[], |
|
52
|
}, |
|
53
|
}) |
|
54
|
return removeExtra |
|
55
|
? md |
|
56
|
: addExtraToMd(md, { |
|
57
|
baseUrl: process.env.NEXT_PUBLIC_BASE_URL || "", |
|
58
|
}) |
|
59
|
}, |
|
60
|
["clean-md"], |
|
61
|
{ |
|
62
|
revalidate: 3600, |
|
63
|
} |
|
64
|
) |