-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md_to_json.js
113 lines (85 loc) · 2.26 KB
/
md_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
let marked = require('marked');
function Renderer(options){
this.options = options || {};
this.firstHeader = true;
}
Renderer.prototype.code = function(code,lang,escaped){
return JSON.stringify(code);
},
Renderer.prototype.blockquote = function(quote){
return quote;
},
Renderer.prototype.heading = function(text,level,raw){
if (this.firstHeader){
this.firstHeader = false;
return text.replace(/\s/g, '_') + ':';
} else {
return ',' + text.replace(/\s/g, '_') + ':';
}
},
Renderer.prototype.hr = function(){
return "";
},
Renderer.prototype.list = function(body, ordered){
return "[" + body.replace(/,\s*$/, "").replace(/\n/g, "") + "]";
},
Renderer.prototype.listitem = function(text){
return text+",";
},
Renderer.prototype.paragraph = function(text){
return text;
},
Renderer.prototype.table = function(header, body){
return;
},
Renderer.prototype.tablerow = function(content){
return text;
},
Renderer.prototype.tablecell = function(content, flags){
return text;
},
Renderer.prototype.strong = function(text){
return text;
},
Renderer.prototype.em = function(text){
return text;
},
Renderer.prototype.codespan = function(text){
return text;
},
Renderer.prototype.br = function(text){
return text;
},
Renderer.prototype.del = function(text){
return text;
},
Renderer.prototype.link = function(href, title, text){
return '"' + text + '"';
},
Renderer.prototype.image = function(href, title, text){
return text;
},
Renderer.prototype.text = function(text){
return '"' + text + '"';
};
let renderer = new Renderer();
marked.setOptions({
renderer: renderer
});
function parse( text ){
renderer.firstHeader = true;
var json = '{' + marked( text ) + '}';
json = json.replace(/""/g, '').replace(/:,/g, ':"",');
json = JSON.parse(json);
json = JSON.stringify(json, null, 2);
return json;
}
this.parse = parse;
module.exports = function(file, enc, callback){
file.path = file.relative.split('.').shift(/\//g, '.');
var inputParsed = parse(file.contents.toString(enc));
//file.path = gutil.replaceExtension(file.path, '.json');
file.contents = new Buffer( inputParsed );
callback(file);
return file;
};