-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathlist2tables.js
57 lines (51 loc) · 1.53 KB
/
list2tables.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
var parser = require('mdast');
var fs = require('fs');
var _ = require( 'lodash' )
var raw = fs.readFileSync(__dirname + '/README.md', 'utf8');
// var raw = fs.readFileSync(__dirname + '/demo.md', 'utf8');
var resultsInString = "";
function listToTable(list){
var results = [];
var listItems = list.children;
_.each(listItems, item => {
var nodes = item.children[1];
var nodeName = item.children[0].children[0].value;
console.log(nodeName);
if(nodes && nodes.children) {
var result = {
name: nodeName,
children: []
}
_.each(nodes.children, function(node){
name = node.children[0].children[0].value;
result.children.push({
name: name
})
})
results.push(result);
}
});
return results;
}
function getData(string) {
var tokens = parser.parse(string)
var results = [];
var heading = '';
_.forEach(tokens.children, function(token){
if(token.type === 'heading') {
heading = token.children[0].value;
}
if(token.type === 'list') {
var result = listToTable(token, heading);
if(!_.isEmpty(result)){
results.push({
name: heading,
children: result
});
}
}
})
return results;
}
var result = getData(raw);
fs.writeFileSync('api/flare.json', JSON.stringify(result, null, 4));