-
Notifications
You must be signed in to change notification settings - Fork 603
/
index.js
146 lines (127 loc) · 3.52 KB
/
index.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
const visit = require('unist-util-visit')
const is = require('unist-util-is')
// do i need this?
const mdxAstToMdxHast = require('@mdx-js/mdx/mdx-ast-to-mdx-hast')
// const mdx = require('@mdx-js/mdx')
// removes html comments
// const toMDXAST = require('@mdx-js/mdx/md-ast-to-mdx-ast')
// const mdxHastToJsx = require('@mdx-js/mdx/mdx-hast-to-jsx')
// const { toJSX } = require('@mdx-js/mdx/mdx-hast-to-jsx')
// custom implementation
const toTemplateLiteral = text =>
'{`' + text.replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`}'
const toJSX = (node, parent, opts = {}) => {
const { preserveNewlines = false } = opts
let children = ''
if (node.type === 'root') {
const jsxNodes = []
let layout = ''
for (const child of node.children) {
// imports/exports should already be handled for the root mdx component
if (child.type === 'import') continue
if (child.type === 'export') {
if (!child.default) continue
layout = child.value
.replace(/^export\s+default\s+/, '')
.replace(/;\s*$/, '')
continue
}
jsxNodes.push(child)
}
return [
'(props => (',
' <MDXTag',
' name="wrapper"',
layout && ` Layout={${layout}}`,
' components={props.components}>',
' ' + jsxNodes.map(child => toJSX(child, node)).join('\n '),
' </MDXTag>',
'))',
]
.filter(Boolean)
.join('\n')
}
if (node.children) {
children = node.children
.map(child => {
return toJSX(child, node, {
preserveNewlines: preserveNewlines || node.tagName === 'pre',
})
})
.join('')
}
if (node.type === 'element') {
let props = ''
if (Object.keys(node.properties).length > 0) {
props = JSON.stringify(node.properties)
}
return [
'<MDXTag',
` name="${node.tagName}"`,
' components={props.components}',
parent.tagName && ` parentName="${parent.tagName}"`,
props && ` props={${props}}`,
'>',
children,
'</MDXTag>',
]
.filter(Boolean)
.join('')
}
if (node.type === 'text') {
const shouldPreserveNewlines = preserveNewlines || parent.tagName === 'p'
if (node.value === '\n' && !shouldPreserveNewlines) {
return node.value
}
return toTemplateLiteral(node.value)
}
if (node.type === 'comment') {
return `{/*${node.value}*/}`
}
return node.value
}
const delimiter = 'thematicBreak'
module.exports = (opts = {}) => {
return (tree, file) => {
const { children } = tree
const splits = []
const slides = []
visit(tree, node => {
if (is(delimiter, node)) {
const i = children.indexOf(node)
splits.push(i)
}
})
let previousSplit = 0
for (let i = 0; i < splits.length; i++) {
const split = splits[i]
slides.push(children.slice(previousSplit, split))
previousSplit = split + 1
}
slides.push(children.slice(previousSplit))
const jsx = slides.map(slide => {
// no idea what I'm doing
// not sure how to convert ast to jsx here...
const hast = mdxAstToMdxHast()({
type: 'root',
children: slide,
})
const code = toJSX(
hast,
{},
{
skipExport: true,
}
)
return code
// const wrapped = `(() => ${code})()`
// return wrapped
})
tree.children.push({
type: 'export',
default: false,
value: `export const slides = [${jsx.join(',\n')}]`,
})
// console.log(tree)
}
}