This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
next.config.js
84 lines (80 loc) · 2.36 KB
/
next.config.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { parsed: localEnv } = require('dotenv').config();
const withPlugins = require('next-compose-plugins');
const withImages = require('next-images');
const withMDX = require('@next/mdx')({
extension: /\.mdx?$/,
});
const fs = require('fs');
const mdx = require('@mdx-js/mdx');
const { parse } = require('@babel/parser');
const generate = require('@babel/generator').default;
const traverse = require('@babel/traverse').default;
const visit = require('unist-util-visit');
const POSTS_DIRECTORY = './pages/blog/';
const extractMdxMeta = (filename) => {
const content = fs.readFileSync(POSTS_DIRECTORY + filename, 'utf8');
let meta = {};
mdx.sync(content, {
remarkPlugins: [
() => (tree) => {
visit(tree, 'export', (node) => {
const ast = parse(node.value, {
plugins: ['jsx'],
sourceType: 'module',
});
traverse(ast, {
VariableDeclarator: (path) => {
if (path.node.id.name === 'meta') {
// eslint-disable-next-line no-eval
meta = eval(`module.exports = ${generate(path.node.init).code}`);
}
},
});
});
},
],
});
if (meta.title === undefined) {
throw new Error("property 'title' not found in meta for post " + filename);
}
if (meta.author === undefined) {
throw new Error("property 'author' not found in meta for post " + filename);
}
if (meta.date === undefined) {
throw new Error("property 'date' not found in meta for post " + filename);
}
return meta;
};
const getBlogPostPages = () =>
fs
.readdirSync(POSTS_DIRECTORY)
.filter((v) => v.endsWith('.mdx'))
.map((filename) => ({
filename,
slug: filename.substring(0, filename.indexOf('.mdx')),
...extractMdxMeta(filename),
}))
.map((post) => ({ ...post, date: new Date(post.date) }))
.sort((a, b) => b.date - a.date);
module.exports = withPlugins([[withMDX], [withImages]], {
env: {
GA_ID: 'UA-78828365-7',
BLOG_POST_LIST: getBlogPostPages(),
...localEnv,
},
pageExtensions: ['js', 'jsx', 'mdx'],
async redirects() {
return [
{
source: '/:match*',
destination: 'https://wiki.openmultiplayer.now.sh/:match*',
permanent: true,
},
{
source: '/wiki',
destination: '/docs',
permanent: true,
},
];
},
});