-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvertindex.js
More file actions
43 lines (39 loc) · 1.27 KB
/
Copy pathconvertindex.js
File metadata and controls
43 lines (39 loc) · 1.27 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
var React = require('react')
var ReactDOMServer = require('react-dom')
var HtmlToReactParser = require('html-to-react')
var fs = require('fs')
// var htmlToReactParser = new HtmlToReactParser();
var htmlInput = fs.createReadStream('index.html').read()
fs.close
var isValidNode = function () {
return true
}
// Order matters. Instructions are processed in the order they're defined
var processNodeDefinitions = HtmlToReactParser.ProcessNodeDefinitions(React)
var processingInstructions = [
{
// This is REQUIRED, it tells the parser
// that we want to insert our React
// component as a child
replaceChildren: true,
shouldProcessNode: function (node) {
return node.attribs && node.attribs['data-test'] === 'foo'
},
processNode: function (node, children, index) {
return React.createElement('h1', {key: index }, 'Heading')
}
},
{
// Anything else
shouldProcessNode: function (node) {
return true
},
processNode: processNodeDefinitions.processDefaultNode
}
]
var reactComponent = processNodeDefinitions.parseWithInstructions(
htmlInput, isValidNode, processingInstructions)
var reactHtml = ReactDOMServer.renderToStaticMarkup(
reactComponent)
fs.createWriteStream('react.html').write(reactHtml)
fs.close