UNPKG

991 BJavaScriptView Raw
1const crypto = require('crypto');
2const grayMatter = require('gray-matter');
3
4module.exports = async ({ node, actions, loadNodeContent, createNodeId }) => {
5 const { createNode, createParentChildLink } = actions;
6
7 if (node.internal.type === 'File' && node.ext === '.mdx') {
8 const content = await loadNodeContent(node);
9 const { data: frontmatter, content: body } = grayMatter(content);
10
11 const mdxNode = {
12 id: createNodeId(`${node.id} -> Mdx`),
13 children: [],
14 parent: node.id,
15 internal: { type: 'Mdx' },
16 fileAbsolutePath: node.absolutePath,
17 sourceName: node.sourceInstanceName,
18 frontmatter: {
19 ...frontmatter,
20 title: frontmatter.title == null ? '' : frontmatter.title,
21 },
22 body,
23 };
24
25 mdxNode.internal.contentDigest = crypto
26 .createHash('md5')
27 .update(JSON.stringify(mdxNode))
28 .digest('hex');
29
30 createNode(mdxNode);
31 createParentChildLink({ parent: node, child: mdxNode });
32 }
33};