-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
68 lines (58 loc) · 2.04 KB
/
index.js
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 { NextraPlugin, pageMapCache } from './plugin'
import { DEFAULT_LOCALE, MARKDOWN_EXTENSION_REGEX } from './constants'
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']
const MARKDOWN_EXTENSIONS = ['md', 'mdx']
const nextra = (...config) =>
function withNextra(nextConfig = {}) {
const nextraConfig =
typeof config[0] === "string"
? {
theme: config[0],
themeConfig: config[1]
}
: config[0]
const nextraPlugin = new NextraPlugin(nextraConfig)
const { pageExtensions = DEFAULT_EXTENSIONS } = nextConfig
if (nextConfig.i18n?.locales) {
console.log(
'[nextra] You have Next.js i18n enabled, read here https://nextjs.org/docs/advanced-features/i18n-routing for the docs.'
)
}
// If `i18n.locales` and `i18n.defaultLocale` were not specified,
// client will receive error - Text content does not match server-rendered HTML.
// Due to `const { locale } = useRouter()` where `locale` will be `undefined`
// To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale`
const i18n = {
locales: nextConfig.i18n?.locales || [DEFAULT_LOCALE],
defaultLocale: nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE
}
return {
...nextConfig,
i18n,
pageExtensions: [...pageExtensions, ...MARKDOWN_EXTENSIONS],
webpack(config, options) {
config.plugins ||= []
config.plugins.push(nextraPlugin)
config.module.rules.push({
test: MARKDOWN_EXTENSION_REGEX,
use: [
options.defaultLoaders.babel,
{
loader: 'nextra/loader',
options: {
...nextraConfig,
locales: i18n.locales,
defaultLocale: i18n.defaultLocale,
pageMapCache
}
}
]
})
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options)
}
return config
}
}
}
module.exports = nextra