-
Notifications
You must be signed in to change notification settings - Fork 24
/
read-pages-list.js
52 lines (36 loc) · 1.26 KB
/
read-pages-list.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
var fs = require('fs'),
lineReader = require('line-reader'),
CONTENT_DIR = 'content',
MARKDOWN_FILENAME_PATTERN = /(.*)\.md/,
barrier = require('./barrier');
function readFirstLine(file, callback){
lineReader.eachLine(CONTENT_DIR + '/' + file, function(line, last) {
callback(line);
return false; // stop reading the file
});
}
function markdownTitleContent(markdownLine) {
return markdownLine.replace(/#+ +(.*)/, '$1');;
}
module.exports = function readPagesList(callback) {
fs.readFile(CONTENT_DIR + '/listing', 'utf8', function(err, content){
var files = content.split('\n');
readFileList(files);
});
function readFileList(markdownFiles){
var result = [];
var bar = barrier(function(){
callback(result);
});
markdownFiles.forEach(function(file, i){
if( !file )
return // skip blank lines
var obj = {path:file.match(MARKDOWN_FILENAME_PATTERN)[1]};
readFirstLine(file, bar.add(function(firstLine){
// de-markdown the title from the file:
obj.title = markdownTitleContent(firstLine);
result[i] = obj;
}));
});
}
}