-
Notifications
You must be signed in to change notification settings - Fork 5
/
withtoc.js
executable file
·59 lines (46 loc) · 1.36 KB
/
withtoc.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
#!/usr/bin/env node
// Reads Markdown from standard input, and renders HTML
// as normal, but also renders the Table of Contents just
// above the content itself.
var hoedown = require("..");
// Here we pick all block and span extensions, instead of specifying them one by one.
// Hint: you don't do this at production.
var exts = hoedown.EXT_BLOCK | hoedown.EXT_SPAN | hoedown.Extensions.SPACE_HEADERS;
// Maximum header level to be included in the TOC
var toc = 4;
// create renderer for the Table of Contents
var rendertoc = hoedown({
extensions: exts,
maxNesting: 10,
renderer: {
type: hoedown.HTML.TOC,
tocLevel: toc
}
});
// create regular HTML renderer
var render = hoedown({
extensions: exts,
renderer: {
type: hoedown.HTML,
flags: hoedown.HTML.Flags.HARD_WRAP,
tocLevel: toc
}
});
// create HTML SmartyPanter
var panter = hoedown.HTML.smartypants();
collectInput(process.stdin, function(input) {
// render both TOC and content
var tochtml = rendertoc.do(input);
var html = render.do(input);
// smartypant them
tochtml = panter.do(tochtml);
html = panter.do(html);
// output them
process.stdout.write(tochtml + "\n\n" + html);
});
function collectInput(stream, cb) {
stream.setEncoding("utf8");
var data = "";
stream.on("data", function(chunk) { data += chunk; });
stream.on("end", function() { cb(data); });
}