-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcontentPages.tsx
More file actions
192 lines (167 loc) · 6.48 KB
/
contentPages.tsx
File metadata and controls
192 lines (167 loc) · 6.48 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { MDXProvider } from '@mdx-js/react'
import DOMPurify from 'dompurify'
import { type ComponentType, useEffect, useState } from 'react'
import { renderToString } from 'react-dom/server'
import { render } from 'vike/abort'
import type { OnBeforePrerenderStartSync, OnBeforeRenderAsync, PageContext } from 'vike/types'
import type { PageContextForTitle } from '../../renderer/+title.ts'
import { MDX_COMPONENTS } from '../components/content/MdxComponents.tsx'
export interface ConfigForContentPages {
title: (pageContext: PageContext) => string
}
export interface ContentPageInfo {
title: string
group: string
order?: number
slug: string
}
export interface PageContextForContentPage {
contentPageInfo: ContentPageInfo
/**
* Available in {@link PageContext} on server pre-rendered pages and on non-initial client page
* loads.
*/
contentPageComponent?: ComponentType
/** Used on initial client page loads. */
contentPageHtml?: TrustedHTML
}
export interface ContentPages {
/** @example "/docs" */
routePath: string
/** @example ../../content/docs */
fsPath: string
/**
* @example () => Object.keys(import.meta.glob('../../../content/docs/*.mdx', { query: '?url' }))
*/
listContentPagePaths(): string[]
/**
* @example () => import.meta.glob('../../../content/docs/*.mdx')
*/
importContentPages(): Record<string, () => Promise<unknown>>
/**
* If the content pages live in subdirectories, this function needs to try importing the slug in
* each subdirectory (because Vite does not support import path interpolations with path
* separators).
*
* @example (slug) => import(`../../../content/docs/${slug}.mdx`)
*/
importContentPage(slug: string): Promise<unknown> | Promise<unknown>[]
}
export function createOnBeforePrerenderStart(content: ContentPages): OnBeforePrerenderStartSync {
return () =>
content
.listContentPagePaths()
.map(path => contentPagePathToSlug(path, content.fsPath))
.sort()
.map(slug => `${content.routePath}/${slug}`)
}
export function createOnBeforeRender(content: ContentPages): OnBeforeRenderAsync {
return async (pageContext: PageContext): ReturnType<OnBeforeRenderAsync> => {
const slug = slugFromPageContext(pageContext)
const { MDXContent, info } = await getContentPage(content, slug)
const infos = await getAllContentPageInfos(content)
return {
pageContext: {
contentPageInfo: { ...info, slug },
contentPageComponent: MDXContent,
contentPageHtml:
typeof window === 'undefined'
? DOMPurify.sanitize(
renderToString(
<MDXProvider components={MDX_COMPONENTS}>
<MDXContent />
</MDXProvider>,
),
{ RETURN_TRUSTED_TYPE: true },
)
: undefined,
contentPageInfos: infos,
pageTitle: info?.title,
} satisfies PageContextForContentPage & PageContextForContentPageIndex & PageContextForTitle,
}
}
}
function contentPagePathToSlug(path: string, base: string): string {
return path.slice(base.length + 1).replace(/\.mdx$/, '')
}
interface ContentPage {
MDXContent: ComponentType
info: ContentPageInfo
}
export interface PageContextForContentPageIndex {
contentPageInfos: ContentPageInfo[]
}
async function getAllContentPageInfos(content: ContentPages): Promise<ContentPageInfo[]> {
// TODO(sqs): this causes all .mdx files to be parsed, can make Vite dev slow
const mdxFiles = content.importContentPages()
return Promise.all(
Object.entries(mdxFiles).map(([path, load]) =>
load().then(mdxModule => ({
slug: contentPagePathToSlug(path, content.fsPath),
...(mdxModule as { info: Omit<ContentPageInfo, 'slug'> }).info,
})),
),
)
}
async function getContentPage(content: ContentPages, slug: string): Promise<ContentPage> {
let result = content.importContentPage(slug)
if (Array.isArray(result)) {
const allResults = await Promise.allSettled(result)
const unexpectedErrors = allResults
.filter(
(res): res is PromiseRejectedResult =>
res.status === 'rejected' &&
!String(res.reason).includes('Unknown variable dynamic import'),
)
.map(res => res.reason)
if (unexpectedErrors.length > 0) {
throw new Error(
`unexpected errors in getContentPage(${slug}): ${unexpectedErrors.join(', ')}`,
)
}
const fulfilledResult = allResults.find(
(value): value is PromiseFulfilledResult<unknown> => value.status === 'fulfilled',
)
if (!fulfilledResult) {
throw render(404)
}
result = Promise.resolve(fulfilledResult.value)
}
const mdxModule = (await result) as {
default: ContentPage['MDXContent']
info: ContentPage['info']
}
return { MDXContent: mdxModule.default, info: mdxModule.info }
}
export function slugFromPageContext(pageContext: PageContext): string {
const slug = pageContext.routeParams?.['*']
if (slug === undefined) {
throw new Error('no slug')
}
return slug === '' ? 'index' : slug
}
export function useContentPageComponent(
content: ContentPages,
pageContext: PageContext & PageContextForContentPage,
): ComponentType | undefined {
const slug = slugFromPageContext(pageContext)
const [component, setComponent] = useState<ComponentType | undefined>(() =>
// Server page render.
'contentPageComponent' in pageContext ? pageContext.contentPageComponent : undefined,
)
useEffect(() => {
if ('contentPageComponent' in pageContext) {
// Non-initial client page render.
setComponent(() => pageContext.contentPageComponent)
} else {
// Initial client page render.
setComponent(undefined)
getContentPage(content, slug)
.then(contentPage => {
setComponent(() => contentPage.MDXContent)
})
.catch(console.error)
}
}, [content, pageContext, slug])
return component
}