-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblog.js
96 lines (78 loc) · 2.5 KB
/
blog.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
var async = require('async');
var fs = require('fs');
var path = require('path');
var marked = require('marked');
var RSS = require('rss');
function render(blogdir, contrib, callback) {
var posts = [];
return fs.readdir(blogdir, function(err, files) {
return async.forEach(files, function(file, cb) {
file = path.join(blogdir, file);
return fs.stat(file, function(err, stat) {
if (!stat.isDirectory()) return cb(); // nothing to do
return renderPost(file, contrib, function(err, post) {
posts.push(post);
return cb();
});
});
}, function(err) {
if (err) return callback(err);
posts.sort(function(a, b) {
var da = Date.parse(a.date).valueOf();
var db = Date.parse(b.date).valueOf();
return db - da;
});
return renderRss(blogdir, posts, function(err, rss) {
if (err) return callback(err);
return callback(null, {
posts: posts,
rss: rss.xml(),
});
});
});
});
}
function renderPost(postdir, contrib, callback) {
var index = path.join(postdir, 'index.md');
var metafile = path.join(postdir, 'blog.json');
var post = null;
return fs.readFile(metafile, function(err, data) {
if (err) return callback(err);
post = JSON.parse(data);
post.name = path.basename(postdir);
return fs.readFile(index, function(err, data) {
if (err) return callback(err);
post.html = marked(data.toString());
var header = '';
header += "<h1>" + post.title + "</h1>";
header += "<p>";
post.author = contrib[post.author];
var author = '<a href="' + post.author.home + '">' + post.author.name + '</a>';
var date = new Date(post.date).toLocaleDateString();
header += "<small>Posted on " + date + " by " + author + "</small>";
header += "<br/>";
header += "</p>";
post.html = header + post.html;
return callback(null, post);
});
});
}
function renderRss(blogdir, posts, callback) {
fs.readFile(path.join(blogdir, 'rss.json'), function(err, data) {
if (err) return callback(err);
var options = JSON.parse(data);
var feed = new RSS(options);
posts.forEach(function(post) {
var item = {
title: post.title,
description: post.html,
url: 'http://anodejs.org/#' + post.name,
author: post.author.name,
date: post.date,
};
feed.item(item);
});
return callback(null, feed);
});
}
module.exports = render;