forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdx-loader.js
52 lines (39 loc) · 1.3 KB
/
mdx-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const _ = require("lodash");
const { getOptions } = require("loader-utils");
const mdx = require("./utils/mdx");
const debug = require("debug")("gatsby-mdx:mdx-loader");
const hasDefaultExport = str => /\nexport default/.test(str);
module.exports = async function(content) {
const callback = this.async();
const { getNodes, pluginOptions } = getOptions(this);
const fileNode = _.first(
getNodes().filter(
node =>
node.internal.type === `File` && node.absolutePath === this.resourcePath
)
);
const source = fileNode && fileNode.sourceInstanceName;
// get the default layout for the file source group, or if it doesn't
// exist, the overall default layout
const defaultLayout = _.get(
pluginOptions.defaultLayouts, source,
_.get(pluginOptions.defaultLayouts, "default")
);
let code = content;
// after running mdx, the code *always* has a default export, so this
// check needs to happen first.
if (!hasDefaultExport(content) && !!defaultLayout) {
debug("inserting default layout", defaultLayout);
code = `import DefaultLayout from "${defaultLayout}"
export default DefaultLayout
${content}`;
}
code = await mdx(code, pluginOptions);
return callback(
null,
`import React from 'react'
import { MDXTag } from '@mdx-js/tag'
${code}
`
);
};