-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mdx-hast-to-jsx.js
263 lines (217 loc) · 7.66 KB
/
mdx-hast-to-jsx.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const {transformSync} = require('@babel/core')
const uniq = require('lodash.uniq')
const {serializeTags} = require('remark-mdx/lib/serialize/mdx-element')
const serializeMdxExpression = require('remark-mdx/lib/serialize/mdx-expression')
const toH = require('hast-to-hyperscript')
const BabelPluginApplyMdxProp = require('babel-plugin-apply-mdx-type-prop')
const BabelPluginExtractImportNames = require('babel-plugin-extract-import-names')
const BabelPluginExtractExportNames = require('babel-plugin-extract-export-names')
function toJSX(node, parentNode = {}, options = {}) {
if (node.type === 'root') {
return serializeRoot(node, options)
}
if (node.type === 'element') {
return serializeElement(node, options, parentNode)
}
// Wraps text nodes inside template string, so that we don't run into escaping issues.
if (node.type === 'text') {
return serializeText(node, options, parentNode)
}
if (node.type === 'mdxBlockExpression' || node.type === 'mdxSpanExpression') {
return serializeMdxExpression(node)
}
// To do: pass `parentName` in?
if (node.type === 'mdxBlockElement' || node.type === 'mdxSpanElement') {
return serializeComponent(node, options, parentNode)
}
if (node.type === 'import' || node.type === 'export') {
return serializeEsSyntax(node)
}
}
function serializeRoot(node, options) {
const {
// Default options
skipExport = false,
wrapExport
} = options
const groups = {import: [], export: [], rest: []}
for (const child of node.children) {
groups[
child.type === 'import' || child.type === 'export' ? child.type : 'rest'
].push(child)
}
// Find a default export, assumes there’s zero or one.
const defaultExport = groups.export.find(child => child.default)
const layout = defaultExport
? defaultExport.value
.replace(/^export\s+default\s+/, '')
.replace(/;\s*$/, '')
: null
const importStatements = groups.import
.map(childNode => toJSX(childNode, node))
.join('\n')
const exportStatements = groups.export
.filter(child => !child.default)
.map(childNode => toJSX(childNode, node))
.join('\n')
const doc = groups.rest
.map(childNode => toJSX(childNode, node, options))
.join('')
.replace(/^\n+|\n+$/, '')
const fn = `function MDXContent({ components, ...props }) {
return (
<MDXLayout {...props} components={components}>
${doc}
</MDXLayout>
)
};
MDXContent.isMDXComponent = true`
// Check JSX nodes against imports
const babelPluginExtractImportNamesInstance = new BabelPluginExtractImportNames()
const babelPluginExtractExportNamesInstance = new BabelPluginExtractExportNames()
const importsAndExports = [importStatements, exportStatements].join('\n')
transformSync(importsAndExports, {
configFile: false,
babelrc: false,
plugins: [
require('@babel/plugin-syntax-jsx'),
require('@babel/plugin-syntax-object-rest-spread'),
babelPluginExtractImportNamesInstance.plugin,
babelPluginExtractExportNamesInstance.plugin
]
})
const importNames = babelPluginExtractImportNamesInstance.state.names
const exportNames = babelPluginExtractExportNamesInstance.state.names
// Add `mdxType` props.
const babelPluginApplyMdxPropInstance = new BabelPluginApplyMdxProp()
const babelPluginApplyMdxPropToExportsInstance = new BabelPluginApplyMdxProp()
const fnPostMdxTypeProp = transformSync(fn, {
configFile: false,
babelrc: false,
plugins: [
require('@babel/plugin-syntax-jsx'),
require('@babel/plugin-syntax-object-rest-spread'),
babelPluginApplyMdxPropInstance.plugin
]
}).code
const exportStatementsPostMdxTypeProps = transformSync(exportStatements, {
configFile: false,
babelrc: false,
plugins: [
require('@babel/plugin-syntax-jsx'),
require('@babel/plugin-syntax-object-rest-spread'),
babelPluginApplyMdxPropToExportsInstance.plugin
]
}).code
const allJsxNames = [
...babelPluginApplyMdxPropInstance.state.names,
...babelPluginApplyMdxPropToExportsInstance.state.names
]
const jsxNames = allJsxNames.filter(name => name !== 'MDXLayout')
const importExportNames = importNames.concat(exportNames)
const fakedModulesForGlobalScope =
`const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
return <div {...props}/>
};
` +
uniq(jsxNames)
.filter(name => !importExportNames.includes(name))
.map(name => `const ${name} = makeShortcode("${name}");`)
.join('\n')
const moduleBase = `${importStatements}
${exportStatementsPostMdxTypeProps}
${fakedModulesForGlobalScope}
const MDXLayout = ${layout || '"wrapper"'}`
if (skipExport) {
return `${moduleBase}
${fnPostMdxTypeProp}`
}
if (wrapExport) {
return `${moduleBase}
${fnPostMdxTypeProp}
export default ${wrapExport}(MDXContent)`
}
return `${moduleBase}
export default ${fnPostMdxTypeProp}`
}
function serializeElement(node, options, parentNode) {
const parentName = parentNode.tagName
const {type, props} = toH(
fakeReactCreateElement,
Object.assign({}, node, {children: []}),
{prefix: false}
)
const content = serializeChildren(node, options)
delete props.key
const data = parentName ? {...props, parentName} : props
const spread =
Object.keys(data).length === 0 ? null : ' {...' + JSON.stringify(data) + '}'
return (
'<' +
type +
(spread ? ' ' + spread : '') +
(content ? '>' + content + '</' + type + '>' : '/>')
)
}
function serializeComponent(node, options) {
let content = serializeChildren(node, options)
const tags = serializeTags(
Object.assign({}, node, {children: content ? ['!'] : []})
)
if (node.type === 'mdxBlockElement' && content) {
content = '\n' + content + '\n'
}
return tags.open + content + (tags.close || '')
}
function serializeText(node, options, parentNode) {
// Don't wrap newlines unless specifically instructed to by the flag,
// to avoid issues like React warnings caused by text nodes in tables.
const preserveNewlines =
options.preserveNewlines || parentNode.tagName === 'p'
if (node.value === '\n' && !preserveNewlines) {
return node.value
}
return toTemplateLiteral(node.value)
}
function serializeEsSyntax(node) {
return node.value
}
function serializeChildren(node, options) {
const children = node.children || []
const childOptions = Object.assign({}, options, {
// Tell all children inside <pre> tags to preserve newlines as text nodes
preserveNewlines: options.preserveNewlines || node.tagName === 'pre'
})
return children.map(child => toJSX(child, node, childOptions)).join('\n')
}
// We only do this for the props, so we’re ignoring children.
function fakeReactCreateElement(name, props) {
return {
type: name,
props: props,
// Needed for `toH` to think this is React.
key: null,
_owner: null
}
}
function toTemplateLiteral(value) {
const escaped = value
.replace(/\\(?!\$)/g, '\\\\') // Escape all "\" to avoid unwanted escaping in text nodes
// and ignore "\$" since it's already escaped and is common
// with prettier https://github.com/mdx-js/mdx/issues/606
.replace(/`/g, '\\`') // Escape "`"" since
.replace(/(\\\$)/g, '\\$1') // Escape \$ so render it as it is
.replace(/(\\\$)(\{)/g, '\\$1\\$2') // Escape \${} so render it as it is
.replace(/\$\{/g, '\\${') // Escape ${} in text so that it doesn't eval
return '{`' + escaped + '`}'
}
function compile(options = {}) {
function compiler(tree) {
return toJSX(tree, undefined, options)
}
this.Compiler = compiler
}
module.exports = compile
compile.default = compile
compile.toJSX = toJSX