-
Notifications
You must be signed in to change notification settings - Fork 8
/
next.config.mjs
144 lines (138 loc) · 4 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
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
import remarkFrontmatter from "remark-frontmatter";
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import { createLoader } from "simple-functional-loader";
import path from "path";
import fs from "fs";
import matter from "gray-matter";
import { load } from "./markdoc/loader.mjs"
const buildTree = (dir, parentName = "pages") => {
const result = {};
const list = fs.readdirSync(dir);
result.name = parentName;
for (const item of list) {
if (item.startsWith("_app.")) {
continue;
}
const itemPath = path.join(dir, item);
const stats = fs.statSync(itemPath);
if (stats.isDirectory()) {
result.folders = [...(result.folders || []), buildTree(itemPath, item)];
} else {
const frontmatter = matter(
fs.readFileSync(itemPath, { encoding: "utf-8" })
);
const p = path
.join(
path.dirname(itemPath),
path.basename(itemPath, path.extname(itemPath))
)
.replace(/^pages/, "")
.replace(/\/index$/, "");
result.files = [
...(result.files || []),
{
name: item,
path: p,
meta: frontmatter.data,
},
];
}
}
return result;
};
const nextConfig = {
basePath: '/docs',
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx", "mdoc"],
resolve: {
extensions: [".mdx", ".md"],
},
webpack(config, options) {
const files = buildTree("./pages");
config.module.rules.push({
test: { and: [/\.mdx$/] },
use: [
options.defaultLoaders.babel,
createLoader(function (source) {
// If source already had an explicitly defined `meta`
// (caught and transformed into `__motif_meta__` in the
// previous loader), this will take precedence, and
// the one extracted from the frontmatter will be ignored.
if (/export\s+(const|let|var)\s+__motif_meta__\s*=/.test(source)) {
source = source
.replace(
/export\s+(const|let|var)\s+meta\s*=/,
"export $1 __motif_frontmatter__ ="
)
.replace(
/export\s+(const|let|var)\s+__motif_meta__\s*=\s*{/,
"export $1 meta = {\n ...__motif_frontmatter__,"
);
}
const pathSegments = this.resourcePath.split(path.sep);
const filename = pathSegments.slice(-1)[0];
return `${source}
MDXContent.meta=meta
MDXContent.filename="${filename}"
MDXContent.files=${JSON.stringify(files)}`;
}),
{
loader: "@mdx-js/loader",
options: {
providerImportSource: "@mdx-js/react",
remarkPlugins: [
remarkMath,
remarkGfm,
remarkFrontmatter,
[remarkMdxFrontmatter, { name: "meta" }],
],
rehypePlugins: [],
},
},
createLoader(function (source) {
return source.replace(
/export\s+(const|let|var)\s+meta\s*=/,
"export $1 __motif_meta__ ="
);
}),
],
}
, {
test: { and: [/\.mdoc$/] },
use: [
options.defaultLoaders.babel,
createLoader(function (source) {
const callback = this.async();
try {
load(source, this.getResolve, this.getOptions, this.resourcePath, this.addContextDependency, files).then(res => {
callback(null, res);
})
} catch (error) {
console.error(error);
callback(error);
}
}),
],
}
);
return config;
},
async redirects() {
return [
{
source: '/docs/api',
destination: '/docs/enterprise-features/api',
basePath: false,
permanent: true,
},
{
source: '/',
destination: '/docs',
permanent: true,
basePath: false,
},
]
},
};
export default nextConfig;