-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.js
49 lines (44 loc) · 1.54 KB
/
markdown.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
var glob = require('glob'),
fs = require('fs');
var sections = glob.sync('sections/*.json').map(function(f) {
return JSON.parse(fs.readFileSync(f));
}).filter(function(section) {
return section.heading && section.heading.identifier;
});
var title = null, chapter = null;
// To properly order identifiers, we need to sort them by leading integers
// rather than lexicographically or with the +'10a' hack which will break here
function pi(x) { return parseInt(x, 10); }
sections.sort(function(a, b) {
// sort by title, then chapter, then identifier
return (pi(a.title.identifier) - pi(b.title.identifier)) ||
(pi(a.chapter.identifier) - pi(b.chapter.identifier)) ||
(pi(a.heading.identifier) - pi(b.heading.identifier));
});
// # Title
// ## Chapter
// ### Section
// #### Subsection
sections.forEach(function(s) {
if (title !== s.title.identifier) {
console.log('# ' + s.title.identifier + ' ' + s.title.text + '\n');
title = s.title.identifier;
}
if (chapter !== s.chapter.identifier) {
console.log('## ' + s.chapter.identifier + ' ' + s.chapter.text + '\n');
chapter = s.chapter.identifier;
}
console.log('### ' + s.heading.identifier + ' ' + s.heading.catch_text);
if (s.text) {
console.log('\n');
console.log(s.text);
console.log('\n');
}
if (s.sections) {
s.sections.forEach(function(a) {
console.log('#### ' + a.prefix + '\n');
console.log(a.text.trim());
console.log('\n');
});
}
});