/
loader.ts
255 lines (220 loc) · 7.02 KB
/
loader.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import type { LoaderOptions, MdxPath, PageOpts } from './types'
import path from 'node:path'
import grayMatter from 'gray-matter'
import slash from 'slash'
import { LoaderContext } from 'webpack'
import { findPagesDir } from 'next/dist/lib/find-pages-dir.js'
import { addPage } from './content-dump'
import { parseFileName } from './utils'
import { compileMdx } from './compile'
import { getPageMap } from './page-map'
import { collectFiles, collectMdx } from './plugin'
import {
IS_PRODUCTION,
DEFAULT_LOCALE,
OFFICIAL_THEMES,
MARKDOWN_EXTENSION_REGEX,
CWD
} from './constants'
const PAGES_DIR = findPagesDir(CWD).pages
// TODO: create this as a webpack plugin.
const indexContentEmitted = new Set<string>()
const IS_WEB_CONTAINER = !!process.versions.webcontainer
const initGitRepo = (async () => {
if (!IS_WEB_CONTAINER) {
const { Repository } = await import('@napi-rs/simple-git')
try {
const repository = Repository.discover(CWD)
if (repository.isShallow()) {
if (process.env.VERCEL) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.'
)
} else if (process.env.GITHUB_ACTION) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.'
)
} else {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented.'
)
}
}
// repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it
const gitRoot = path.join(repository.path(), '..')
return { repository, gitRoot }
} catch (e) {
console.warn('[nextra] Init git repository failed', e)
}
}
return {}
})()
async function loader(
context: LoaderContext<LoaderOptions>,
source: string
): Promise<string> {
const {
pageImport,
theme,
themeConfig,
defaultLocale,
unstable_defaultShowCopyCode,
unstable_flexsearch,
unstable_staticImage,
unstable_readingTime,
mdxOptions,
pageMapCache,
newNextLinkBehavior,
allowFutureImage
} = context.getOptions()
context.cacheable(true)
// Check if there's a theme provided
if (!theme) {
throw new Error('No Nextra theme found!')
}
const mdxPath = context.resourcePath as MdxPath
if (mdxPath.includes('/pages/api/')) {
console.warn(
`[nextra] Ignoring ${mdxPath} because it is located in the "pages/api" folder.`
)
return ''
}
const { items, fileMap } = IS_PRODUCTION
? pageMapCache.get()!
: await collectFiles(PAGES_DIR)
// mdx is imported but is outside the `pages` directory
if (!fileMap[mdxPath]) {
fileMap[mdxPath] = await collectMdx(mdxPath)
context.addMissingDependency(mdxPath)
}
const { locale } = parseFileName(mdxPath)
for (const [filePath, file] of Object.entries(fileMap)) {
if (file.kind === 'Meta' && (!locale || file.locale === locale)) {
context.addDependency(filePath)
}
}
// Add the entire directory `pages` as the dependency,
// so we can generate the correct page map.
context.addContextDependency(PAGES_DIR)
// Extract frontMatter information if it exists
const { data: frontMatter, content } = grayMatter(source)
const { result, headings, structurizedData, hasJsxInH1, readingTime } =
await compileMdx(
content,
{
mdxOptions,
unstable_readingTime,
unstable_defaultShowCopyCode,
unstable_staticImage,
unstable_flexsearch,
allowFutureImage
},
mdxPath
)
// @ts-expect-error
const cssImport = OFFICIAL_THEMES.includes(theme)
? `import '${theme}/style.css'`
: ''
// Imported as a normal component, no need to add the layout.
if (!pageImport) {
return `
${cssImport}
${result}
export default MDXContent`.trimStart()
}
const { route, title, pageMap } = getPageMap({
filePath: mdxPath,
fileMap,
defaultLocale,
pageMap: items
})
const skipFlexsearchIndexing =
IS_PRODUCTION && indexContentEmitted.has(mdxPath)
if (unstable_flexsearch && !skipFlexsearchIndexing) {
if (frontMatter.searchable !== false) {
addPage({
locale: locale || DEFAULT_LOCALE,
route,
title,
structurizedData
})
}
indexContentEmitted.add(mdxPath)
}
let timestamp: PageOpts['timestamp']
const { repository, gitRoot } = await initGitRepo
if (repository && gitRoot) {
try {
timestamp = await repository.getFileLatestModifiedDateAsync(
path.relative(gitRoot, mdxPath)
)
} catch {
// Failed to get timestamp for this file. Silently ignore it.
}
}
// Relative path instead of a package name
const layout =
theme.startsWith('.') || theme.startsWith('/') ? path.resolve(theme) : theme
const themeConfigImport = themeConfig
? `import __nextra_themeConfig__ from '${slash(path.resolve(themeConfig))}'`
: ''
const pageOpts: Omit<PageOpts, 'title'> = {
filePath: slash(path.relative(CWD, mdxPath)),
route,
frontMatter,
pageMap,
headings,
hasJsxInH1,
timestamp,
unstable_flexsearch,
newNextLinkBehavior,
readingTime
}
const pageNextRoute =
'/' +
slash(path.relative(PAGES_DIR, mdxPath))
// Remove the `mdx?` extension
.replace(MARKDOWN_EXTENSION_REGEX, '')
// Remove the `*/index` suffix
.replace(/\/index$/, '')
// Remove the only `index` route
.replace(/^index$/, '')
return `
import { SSGContext as __nextra_SSGContext__ } from 'nextra/ssg'
${themeConfigImport}
${cssImport}
const __nextra_pageOpts__ = ${JSON.stringify(pageOpts)}
globalThis.__nextra_internal__ = {
pageMap: __nextra_pageOpts__.pageMap,
route: __nextra_pageOpts__.route
}
${result}
__nextra_pageOpts__.title =
${JSON.stringify(frontMatter.title)} ||
(typeof __nextra_title__ === 'string' && __nextra_title__) ||
${JSON.stringify(title /* Fallback as sidebar link name */)}
const Content = props => (
<__nextra_SSGContext__.Provider value={props}>
<MDXContent />
</__nextra_SSGContext__.Provider>
)
globalThis.__nextra_pageContext__ ||= Object.create(null)
// Make sure the same component is always returned so Next.js will render the
// stable layout. We then put the actual content into a global store and use
// the route to identify it.
globalThis.__nextra_pageContext__[${JSON.stringify(pageNextRoute)}] = {
Content,
pageOpts: __nextra_pageOpts__,
themeConfig: ${themeConfigImport ? '__nextra_themeConfig__' : 'null'}
}
export { default } from '${layout}'`.trimStart()
}
export default function syncLoader(
this: LoaderContext<LoaderOptions>,
source: string,
callback: (err?: null | Error, content?: string | Buffer) => void
): void {
loader(this, source)
.then(result => callback(null, result))
.catch(err => callback(err))
}