-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (35 loc) · 1.04 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
var React = require("react");
var server = require("react-dom/server");
var matchHelper = require("posthtml-match-helper");
var attrToProp = {
"class": "className",
"for": "htmlFor"
};
function toReact (node, components) {
var tag = node.tag;
var attrs = node.attrs;
var element = (tag in components) ? components[tag] : (tag in React.DOM) ? tag : "div";
var props = {
key: Math.random().toString(32).slice(2)
};
if (attrs) {
Object.keys(attrs).map(function (attr) {
var prop = attr in attrToProp ? attrToProp[attr] : attr;
props[prop] = attrs[attr];
});
}
var children = null;
if (Array.isArray(node.content)) {
children = node.content.map(function (_node) {
return typeof _node === "string" ? _node : toReact(_node, components);
});
}
return React.createElement(element, props, children);
}
module.exports = function (matcher, components) {
return function posthtmlStaticReact (tree) {
tree.match(matchHelper(matcher), function (node) {
return server.renderToStaticMarkup(toReact(node, components));
});
};
};