-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepareData.js
51 lines (42 loc) · 982 Bytes
/
prepareData.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
"use strict";
var fs = require('fs');
var files = fs.readdirSync("_posts/");
var posts = [];
for (var i = 0; i < files.length; i++) {
// Exclude git files
if (files[i].indexOf(".") === 0) {
continue;
}
var file = parseFilename(files[i]);
posts.push({
path: file.path,
title: file.title,
date: file.date
});
}
posts.sort(comparePaths);
function comparePaths(a, b) {
if (a.path < b.path)
return 1;
if (a.path > b.path)
return -1;
return 0;
}
function parseFilename(file) {
var path = file.slice(0, -3);
var arr = path.split('-');
var date = arr[0] + '-' + arr[1] + '-' + arr[2];
arr.splice(0, 3);
var title = arr.join(' ').substr(0, 1).toUpperCase() + arr.join(' ').substr(1);
return {
path: path,
title: title,
date: date
};
}
fs.writeFile("posts.json", JSON.stringify(posts, null, 2), function(err) {
if(err) {
return console.log(err);
}
console.log("The data file was created!");
});