-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gen-mdx.js
186 lines (168 loc) · 4.68 KB
/
gen-mdx.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const babel = require(`@babel/core`)
const grayMatter = require(`gray-matter`)
const mdx = require(`@mdx-js/mdx`)
const objRestSpread = require(`@babel/plugin-proposal-object-rest-spread`)
const debug = require(`debug`)(`gatsby-plugin-mdx:gen-mdx`)
const getSourcePluginsAsRemarkPlugins = require(`./get-source-plugins-as-remark-plugins`)
const htmlAttrToJSXAttr = require(`./babel-plugin-html-attr-to-jsx-attr`)
const removeExportKeywords = require(`./babel-plugin-remove-export-keywords`)
const BabelPluginPluckImports = require(`./babel-plugin-pluck-imports`)
/*
* function mutateNode({
* pluginOptions,
* mdxNode,
* getNode,
* files,
* reporter,
* cache
* }) {
* return Promise.each(pluginOptions.gatsbyRemarkPlugins, plugin => {
* const requiredPlugin = require(plugin.resolve);
* if (_.isFunction(requiredPlugin.mutateSource)) {
* return requiredPlugin.mutateSource(
* {
* mdxNode,
* files: fileNodes,
* getNode,
* reporter,
* cache
* },
* plugin.pluginOptions
* );
* } else {
* return Promise.resolve();
* }
* });
* }
* */
module.exports = async function genMDX(
{
isLoader,
node,
options,
getNode,
getNodes,
reporter,
cache,
pathPrefix,
...helpers
},
{ forceDisableCache = false } = {}
) {
const pathPrefixCacheStr = pathPrefix || ``
const payloadCacheKey = node =>
`gatsby-plugin-mdx-entire-payload-${node.internal.contentDigest}-${pathPrefixCacheStr}`
if (!forceDisableCache) {
const cachedPayload = await cache.get(payloadCacheKey(node))
if (cachedPayload) {
return cachedPayload
}
}
let results = {
mdast: undefined,
hast: undefined,
html: undefined,
scopeImports: [],
scopeIdentifiers: [],
body: undefined,
}
// TODO: a remark and a hast plugin that pull out the ast and store it in results
/* const cacheMdast = () => ast => {
* results.mdast = ast;
* return ast;
* };
* const cacheHast = () => ast => {
* results.hast = ast;
* return ast;
* }; */
// pull classic style frontmatter off the raw MDX body
debug(`processing classic frontmatter`)
const { data, content: frontMatterCodeResult } = grayMatter(node.rawBody)
const content = `${frontMatterCodeResult}
export const _frontmatter = ${JSON.stringify(data)}`
// get mdast by itself
// in the future it'd be nice to not do this twice
debug(`generating AST`)
const compiler = mdx.createMdxAstCompiler(options)
results.mdast = compiler.parse(content)
/* await mutateNode({
* pluginOptions,
* mdxNode,
* files: getNodes().filter(n => n.internal.type === `File`),
* getNode,
* reporter,
* cache
* }); */
const gatsbyRemarkPluginsAsremarkPlugins = await getSourcePluginsAsRemarkPlugins(
{
gatsbyRemarkPlugins: options.gatsbyRemarkPlugins,
mdxNode: node,
// files,
getNode,
getNodes,
reporter,
cache,
pathPrefix,
...helpers,
}
)
debug(`running mdx`)
let code = await mdx(content, {
...options,
remarkPlugins: options.remarkPlugins.concat(
gatsbyRemarkPluginsAsremarkPlugins
),
})
results.rawMDXOutput = `/* @jsx mdx */
import { mdx } from '@mdx-js/react';
${code}`
if (!isLoader) {
debug(`compiling scope`)
const instance = new BabelPluginPluckImports()
const result = babel.transform(code, {
configFile: false,
plugins: [
instance.plugin,
objRestSpread,
htmlAttrToJSXAttr,
removeExportKeywords,
],
presets: [
require(`@babel/preset-react`),
[
require(`@babel/preset-env`),
{
useBuiltIns: `entry`,
corejs: 2,
modules: false,
},
],
],
})
const identifiers = Array.from(instance.state.identifiers)
const imports = Array.from(instance.state.imports)
if (!identifiers.includes(`React`)) {
identifiers.push(`React`)
imports.push(`import * as React from 'react'`)
}
results.scopeImports = imports
results.scopeIdentifiers = identifiers
// TODO: be more sophisticated about these replacements
results.body = result.code
.replace(
/export\s*default\s*function\s*MDXContent\s*/,
`return function MDXContent`
)
.replace(
/export\s*{\s*MDXContent\s+as\s+default\s*};?/,
`return MDXContent;`
)
}
/* results.html = renderToStaticMarkup(
* React.createElement(MDXRenderer, null, results.body)
* ); */
if (!forceDisableCache) {
await cache.set(payloadCacheKey(node), results)
}
return results
}