|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -var fs = require('fs'); |
4 |
| -var path = require('path'); |
5 |
| -var pkg = require('../package.json'); |
| 3 | +var data = require('./data'); |
6 | 4 |
|
7 |
| -var dir = path.join(__dirname, '../', pkg._buildOutput, 'blog/posts'); |
8 |
| - |
9 |
| -var posts; |
10 |
| -var JSONPath = dir + '/posts.json'; |
11 |
| - |
12 |
| -try { |
13 |
| - posts = require(JSONPath); |
14 |
| -} catch (e){ |
15 |
| - console.error(JSONPath + " doesn not exist. \n" + |
16 |
| - "Did you build the blog with 'node build/blog'?"); |
17 |
| - throw e; |
18 |
| -} |
19 |
| - |
20 |
| -var content = posts.map(function(post){ |
21 |
| - return fs.readFileSync(dir + '/' + post.htmlFile); |
22 |
| -}); |
23 |
| - |
24 |
| -var postsByURL = {}; |
25 |
| -var tags = {}; |
| 5 | +var perPage = 2; |
26 | 6 |
|
27 |
| -posts.forEach(function(post, i){ |
28 |
| - post.date = new Date(post.date); |
29 |
| - postsByURL[post.permalink] = i; |
30 |
| - if (post.tags && Array.isArray(post.tags)) post.tags.forEach(function(tag){ |
31 |
| - tag = tag.toLowerCase(); |
32 |
| - (tags[tag] || (tags[tag] = [])).push(i); |
| 7 | +function getData(req, res, next){ |
| 8 | + data.get(function(err, posts){ |
| 9 | + if (err) return next(err); |
| 10 | + res.locals._posts = posts; |
| 11 | + next(); |
33 | 12 | });
|
34 |
| -}); |
35 |
| - |
36 |
| -var perPage = 2; |
| 13 | +} |
37 | 14 |
|
38 | 15 | module.exports = function(app){
|
39 | 16 |
|
40 | 17 | var index = function(req, res, next){
|
41 | 18 | var page = req.params.page ? parseInt(req.params.page, 10) : 1;
|
42 | 19 | var tag = req.params.tag;
|
| 20 | + var posts = res.locals._posts.posts; |
| 21 | + var tags = res.locals._posts.tags; |
43 | 22 |
|
44 | 23 | // posts with this tag do not exist
|
45 | 24 | if (tag && !tags[tag]) return next();
|
@@ -68,22 +47,24 @@ module.exports = function(app){
|
68 | 47 |
|
69 | 48 | };
|
70 | 49 |
|
71 |
| - app.get('/blog', index); |
72 |
| - app.get('/blog/page/:page', index); |
73 |
| - app.get('/blog/category/:tag', index); |
74 |
| - app.get('/blog/category/:tag/page/:page', index); |
| 50 | + app.get('/blog', getData, index); |
| 51 | + app.get('/blog/page/:page', getData, index); |
| 52 | + app.get('/blog/category/:tag', getData, index); |
| 53 | + app.get('/blog/category/:tag/page/:page', getData, index); |
75 | 54 |
|
76 |
| - app.get(/\/blog\/(.+)/, function(req, res, next){ |
| 55 | + app.get(/\/blog\/(.+)/, getData, function(req, res, next){ |
77 | 56 | var url = req.params[0];
|
| 57 | + var posts = res.locals._posts.posts; |
| 58 | + var urls = res.locals._posts.urls; |
78 | 59 |
|
79 |
| - var postIndex = postsByURL[url]; |
| 60 | + var postIndex = urls[url]; |
80 | 61 | var post = postIndex != null && posts[postIndex];
|
81 | 62 | if (!post) return next();
|
82 | 63 |
|
83 | 64 | res.render('blog/post', {
|
84 | 65 | title: "MooTools Blog: " + post.title,
|
85 | 66 | post: post,
|
86 |
| - content: content[postIndex], |
| 67 | + content: post.content, |
87 | 68 | page: 'blog'
|
88 | 69 | });
|
89 | 70 |
|
|
0 commit comments