-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.mjs
83 lines (76 loc) · 2.26 KB
/
next.config.mjs
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
import rehypeShiki from '@leafac/rehype-shiki'
import nextMDX from '@next/mdx'
import { Parser } from 'acorn'
import jsx from 'acorn-jsx'
import escapeStringRegexp from 'escape-string-regexp'
import * as path from 'path'
import { recmaImportImages } from 'recma-import-images'
import remarkGfm from 'remark-gfm'
import { remarkRehypeWrap } from 'remark-rehype-wrap'
import remarkUnwrapImages from 'remark-unwrap-images'
import shiki from 'shiki'
import { unifiedConditional } from 'unified-conditional'
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
}
function remarkMDXLayout(source, metaName) {
let parser = Parser.extend(jsx())
let parseOptions = { ecmaVersion: 'latest', sourceType: 'module' }
return (tree) => {
let imp = `import _Layout from '${source}'`
let exp = `export default function Layout(props) {
return <_Layout {...props} ${metaName}={${metaName}} />
}`
tree.children.push(
{
type: 'mdxjsEsm',
value: imp,
data: { estree: parser.parse(imp, parseOptions) },
},
{
type: 'mdxjsEsm',
value: exp,
data: { estree: parser.parse(exp, parseOptions) },
},
)
}
}
export default async function config() {
let highlighter = await shiki.getHighlighter({
theme: 'css-variables',
})
let withMDX = nextMDX({
extension: /\.mdx$/,
options: {
recmaPlugins: [recmaImportImages],
rehypePlugins: [
[rehypeShiki, { highlighter }],
[
remarkRehypeWrap,
{
node: { type: 'mdxJsxFlowElement', name: 'Typography' },
start: ':root > :not(mdxJsxFlowElement)',
end: ':root > mdxJsxFlowElement',
},
],
],
remarkPlugins: [
remarkGfm,
remarkUnwrapImages,
[
unifiedConditional,
[
new RegExp(`^${escapeStringRegexp(path.resolve('src/app/blog'))}`),
[[remarkMDXLayout, '@/app/blog/wrapper', 'article']],
],
[
new RegExp(`^${escapeStringRegexp(path.resolve('src/app/work'))}`),
[[remarkMDXLayout, '@/app/work/wrapper', 'caseStudy']],
],
],
],
},
})
return withMDX(nextConfig)
}