-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-to-json.js
118 lines (73 loc) · 2.57 KB
/
markdown-to-json.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
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
var fs = require('fs');
var path = require('path');
var jsdom = require('jsdom');
var marked = require('marked');
var Gatherer = require('gm-gather');
var inPath = process.argv[2];
var inNode = process.argv[3];
var output = process.argv[4];
/*
var example = fs.readFileSync('./src/pandoc/CG15/pathways/Managing Type 2 Diabetes/managing-type-2-diabetes.md', 'utf8');
var ex = marked(example);
var doc = jsdom.env(ex, ['http://code.jquery.com/jquery.js'], function (err, window){
var code = JSON.parse(window.$('code').text());
window.$('a[rel="/pathways/link-down"]');
console.log(window.$('code').text())
});
*/
function loadNode (filepath, file, node, callback){
fs.readFile(path.join(filepath, file), 'utf8', function (err, data){
var html = '<div>' + marked(data) + '</div>';
if (!err){
jsdom.env(html, ['http://code.jquery.com/jquery.js'], function (err, window){
var $ = window.$;
if (!err){
// extract data...
node.meta = JSON.parse($('code').text() || "{}") ;
// remove the pre/code block...
$('pre').remove();
if($('a[rel="/pathways/link-down"]').length){
node.childNodes = [];
var gather = new Gatherer();
$('a[rel="/pathways/link-down"]').each(function (index, link){
var childNode = {};
node.childNodes.push(childNode);
$(link).remove();
gather.task(function (done, error){
loadNode( filepath, $(link).attr('href') + '.md', childNode, function (err){
if (!err){
done();
} else {
error(err);
}
});
});
});
gather.run(function (err){
if (!err){
node.label = $('h1').text();
node.html = encodeURIComponent($('div').html());
callback( false, node);
}
});
} else {
// copy across the html...
node.label = $('h1').text();
node.html = encodeURIComponent($('div').html());
// and pass along our new node...
callback( false, node );
}
} else { callback( err ); }
});
} else { callback( err ); }
});
}
loadNode (inPath, inNode, {}, function (err, node){
fs.writeFile(output, JSON.stringify(node), 'utf8', function (err){
if (!err){
console.log('Okay');
} else {
console.error(err);
}
});
});