-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
44 lines (34 loc) · 1.15 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
'use strict'
var toH = require('hast-to-hyperscript')
var tableCellStyle = require('@mapbox/hast-util-table-cell-style')
module.exports = rehypeReact
var has = {}.hasOwnProperty
// Add a React compiler.
function rehypeReact(options) {
var settings = options || {}
var createElement = settings.createElement
var Fragment = settings.Fragment
var components = settings.components || {}
this.Compiler = compiler
function compiler(node) {
var result = toH(h, tableCellStyle(node), settings.prefix)
if (node.type === 'root') {
// Invert <https://github.com/syntax-tree/hast-to-hyperscript/blob/d227372/index.js#L46-L56>.
if (
result.type === 'div' &&
(node.children.length !== 1 || node.children[0].type !== 'element')
) {
result = result.props.children
} else {
result = [result]
}
return createElement(Fragment || 'div', {}, result)
}
return result
}
// Wrap `createElement` to pass components in.
function h(name, props, children) {
var component = has.call(components, name) ? components[name] : name
return createElement(component, props, children)
}
}