improve site perfermance

This commit is contained in:
afc163 2015-07-21 14:04:01 +08:00
parent 7a3d776a41
commit 7d962d3f80

View File

@ -1,9 +1,19 @@
var _ = require('lodash'); var _ = require('lodash');
var Posts;
module.exports = function(nico) { module.exports = function(nico) {
var exports = {}; var exports = {};
var Categories = {}; var Categories = {};
var Posts = [];
function getAllPosts(pages) {
if (Posts && Posts.length > 0) {
return Posts;
}
Object.keys(pages).map(function(key) {
Posts.push(pages[key]);
});
return Posts;
}
exports.reader = function(post) { exports.reader = function(post) {
var filepath = post.meta.filepath.toLowerCase(); var filepath = post.meta.filepath.toLowerCase();
@ -28,10 +38,9 @@ module.exports = function(nico) {
cats = [cats]; cats = [cats];
} }
var ret = []; var ret = [];
Object.keys(posts).forEach(function(key) { getAllPosts(posts).forEach(function(post) {
var item = posts[key]; if (cats.indexOf(post.meta.category) >= 0) {
if (cats.indexOf(item.meta.category) >= 0) { ret.push(post);
ret.push(item);
} }
}); });
ret = ret.sort(function(a, b) { ret = ret.sort(function(a, b) {
@ -43,8 +52,7 @@ module.exports = function(nico) {
}, },
get_categories: function(posts, post) { get_categories: function(posts, post) {
var rootDirectory = post.directory.split('/')[0]; var rootDirectory = post.directory.split('/')[0];
var categories = Categories[rootDirectory] || _.uniq(Object.keys(posts).map(function(key) { var categories = Categories[rootDirectory] || _.uniq(getAllPosts(posts).map(function(item) {
var item = posts[key];
if (item.directory.split('/')[0] === post.directory.split('/')[0]) { if (item.directory.split('/')[0] === post.directory.split('/')[0]) {
return item.meta.category; return item.meta.category;
} }
@ -57,10 +65,9 @@ module.exports = function(nico) {
}, },
find_demo_in_component: function(pages, directory) { find_demo_in_component: function(pages, directory) {
var ret = []; var ret = [];
Object.keys(pages).forEach(function(key) { getAllPosts(pages).forEach(function(post) {
var page = pages[key]; if (post.filepath.indexOf(directory + '/demo/') === 0) {
if (key.indexOf(directory + '/demo/') === 0) { ret.push(post);
ret.push(page);
} }
}); });
ret = ret.sort(function(a, b) { ret = ret.sort(function(a, b) {