forked from remarkjs/react-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast-to-react.js
More file actions
246 lines (219 loc) Β· 6.89 KB
/
ast-to-react.js
File metadata and controls
246 lines (219 loc) Β· 6.89 KB
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
'use strict'
const React = require('react')
const xtend = require('xtend')
const ReactIs = require('react-is');
const defaultNodePosition = {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 1, offset: 0}
}
function astToReact(node, options, parent = {}, index = 0) {
const renderer = options.renderers[node.type]
// nodes generated by plugins may not have position data
// much of the code after this point will attempt to access properties of the node.position
// this will set the node position to the parent node's position to prevent errors
if (node.position === undefined) {
node.position = (parent.node && parent.node.position) || defaultNodePosition
}
const pos = node.position.start
const key = [node.type, pos.line, pos.column, index].join('-')
if (!ReactIs.isValidElementType(renderer)) {
throw new Error(`Renderer for type \`${node.type}\` not defined or is not renderable`)
}
const nodeProps = getNodeProps(node, key, options, renderer, parent, index)
return React.createElement(
renderer,
nodeProps,
nodeProps.children || resolveChildren() || undefined
)
function resolveChildren() {
return (
node.children &&
node.children.map((childNode, i) =>
astToReact(childNode, options, {node, props: nodeProps}, i)
)
)
}
}
// eslint-disable-next-line max-params, complexity
function getNodeProps(node, key, opts, renderer, parent, index) {
const props = {key}
const isTagRenderer = typeof renderer === 'string'
// `sourcePos` is true if the user wants source information (line/column info from markdown source)
if (opts.sourcePos && node.position) {
props['data-sourcepos'] = flattenPosition(node.position)
}
if (opts.rawSourcePos && !isTagRenderer) {
props.sourcePosition = node.position
}
// If `includeNodeIndex` is true, pass node index info to all non-tag renderers
if (opts.includeNodeIndex && parent.node && parent.node.children && !isTagRenderer) {
props.index = parent.node.children.indexOf(node)
props.parentChildCount = parent.node.children.length
}
const ref =
node.identifier !== null && node.identifier !== undefined
? opts.definitions[node.identifier] || {}
: null
switch (node.type) {
case 'root':
assignDefined(props, {className: opts.className})
break
case 'text':
props.nodeKey = key
props.children = node.value
break
case 'heading':
props.level = node.depth
break
case 'list':
props.start = node.start
props.ordered = node.ordered
props.tight = !node.loose
props.depth = node.depth
break
case 'listItem':
props.checked = node.checked
props.tight = !node.loose
props.ordered = node.ordered
props.index = node.index
props.children = getListItemChildren(node, parent).map((childNode, i) => {
return astToReact(childNode, opts, {node: node, props: props}, i)
})
break
case 'definition':
assignDefined(props, {identifier: node.identifier, title: node.title, url: node.url})
break
case 'code':
assignDefined(props, {language: node.lang && node.lang.split(/\s/, 1)[0]})
break
case 'inlineCode':
props.children = node.value
props.inline = true
break
case 'link':
assignDefined(props, {
title: node.title || undefined,
target:
typeof opts.linkTarget === 'function'
? opts.linkTarget(node.url, node.children, node.title)
: opts.linkTarget,
href: opts.transformLinkUri
? opts.transformLinkUri(node.url, node.children, node.title)
: node.url
})
break
case 'image':
assignDefined(props, {
alt: node.alt || undefined,
title: node.title || undefined,
src: opts.transformImageUri
? opts.transformImageUri(node.url, node.children, node.title, node.alt)
: node.url
})
break
case 'linkReference':
assignDefined(
props,
xtend(ref, {
href: opts.transformLinkUri ? opts.transformLinkUri(ref.href) : ref.href
})
)
break
case 'imageReference':
assignDefined(props, {
src:
opts.transformImageUri && ref.href
? opts.transformImageUri(ref.href, node.children, ref.title, node.alt)
: ref.href,
title: ref.title || undefined,
alt: node.alt || undefined
})
break
case 'table':
case 'tableHead':
case 'tableBody':
props.columnAlignment = node.align
break
case 'tableRow':
props.isHeader = parent.node.type === 'tableHead'
props.columnAlignment = parent.props.columnAlignment
break
case 'tableCell':
assignDefined(props, {
isHeader: parent.props.isHeader,
align: parent.props.columnAlignment[index]
})
break
case 'virtualHtml':
props.tag = node.tag
break
case 'html':
// @todo find a better way than this
props.isBlock = node.position.start.line !== node.position.end.line
props.escapeHtml = opts.escapeHtml
props.skipHtml = opts.skipHtml
break
case 'parsedHtml': {
let parsedChildren
if (node.children) {
parsedChildren = node.children.map((child, i) => astToReact(child, opts, {node, props}, i))
}
props.escapeHtml = opts.escapeHtml
props.skipHtml = opts.skipHtml
props.element = mergeNodeChildren(node, parsedChildren)
break
}
default:
assignDefined(
props,
xtend(node, {
type: undefined,
position: undefined,
children: undefined
})
)
}
if (!isTagRenderer && node.value) {
props.value = node.value
}
return props
}
function assignDefined(target, attrs) {
for (const key in attrs) {
if (typeof attrs[key] !== 'undefined') {
target[key] = attrs[key]
}
}
}
function mergeNodeChildren(node, parsedChildren) {
const el = node.element
if (Array.isArray(el)) {
const Fragment = React.Fragment || 'div'
return React.createElement(Fragment, null, el)
}
if (el.props.children || parsedChildren) {
const children = React.Children.toArray(el.props.children).concat(parsedChildren)
return React.cloneElement(el, null, children)
}
return React.cloneElement(el, null)
}
function flattenPosition(pos) {
return [pos.start.line, ':', pos.start.column, '-', pos.end.line, ':', pos.end.column]
.map(String)
.join('')
}
function getListItemChildren(node, parent) {
if (node.loose) {
return node.children
}
if (parent.node && node.index > 0 && parent.node.children[node.index - 1].loose) {
return node.children
}
return unwrapParagraphs(node)
}
function unwrapParagraphs(node) {
return node.children.reduce((array, child) => {
return array.concat(child.type === 'paragraph' ? child.children || [] : [child])
}, [])
}
module.exports = astToReact