forked from juxt/site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd-layout-loader.js
37 lines (32 loc) · 1.14 KB
/
md-layout-loader.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
const fm = require('gray-matter');
const path = require('path');
// Makes mdx in next.js suck less by injecting necessary exports so that
// the docs are still readable on github.
//
// Layout component for a .mdx or .md page can be specfied in the frontmatter.
// This plugin assumes that the layout file and named export are the same. This
// easily changed by modifying the string below.
//
// All metadata can be written in yaml front matter. It will be passed to the
// layout component as `meta` prop.
//
// (Shamelessly stolen from Expo.io docs)
// @see https://github.com/expo/expo/blob/master/docs/common/md-loader.js
module.exports = async function (src) {
const callback = this.async();
const {content, data} = fm(src);
const pageParentDir = path
.dirname(path.relative('./src/pages', this.resourcePath))
.split(path.sep)
.shift();
const layoutMap = {
learn: 'Learn',
reference: 'API',
};
const layout = layoutMap[pageParentDir] || 'Learn';
const code =
`import withLayout from 'components/Layout/Layout${layout}';
export default withLayout(${JSON.stringify(data)})
` + content;
return callback(null, code);
};