2015-05-11 18:09:13 +08:00
|
|
|
var _ = require('lodash');
|
|
|
|
|
2015-05-09 17:36:15 +08:00
|
|
|
module.exports = function(nico) {
|
|
|
|
var exports = {};
|
2015-07-05 15:43:07 +08:00
|
|
|
var Categories = {};
|
2015-07-21 14:04:01 +08:00
|
|
|
var Posts = [];
|
|
|
|
|
|
|
|
function getAllPosts(pages) {
|
|
|
|
if (Posts && Posts.length > 0) {
|
|
|
|
return Posts;
|
|
|
|
}
|
|
|
|
Object.keys(pages).map(function(key) {
|
|
|
|
Posts.push(pages[key]);
|
|
|
|
});
|
|
|
|
return Posts;
|
|
|
|
}
|
2015-05-09 17:36:15 +08:00
|
|
|
|
|
|
|
exports.reader = function(post) {
|
2015-05-12 17:50:03 +08:00
|
|
|
var filepath = post.meta.filepath.toLowerCase();
|
|
|
|
if (filepath.indexOf('components') === 0) {
|
2015-05-09 17:36:15 +08:00
|
|
|
post.template = post.meta.template = 'component';
|
|
|
|
} else {
|
|
|
|
post.template = post.meta.template = (post.meta.template || 'page');
|
|
|
|
}
|
2015-05-12 17:50:03 +08:00
|
|
|
if (filepath === 'readme.md') {
|
2015-05-09 17:36:15 +08:00
|
|
|
post.filename = post.meta.filename = 'index';
|
2015-06-04 17:05:28 +08:00
|
|
|
post.template = post.meta.template = 'home';
|
2015-05-09 17:36:15 +08:00
|
|
|
}
|
2015-05-15 16:00:08 +08:00
|
|
|
if (filepath.indexOf('/demo/') > 0) {
|
|
|
|
post.template = post.meta.template = 'code';
|
|
|
|
}
|
2015-05-09 17:36:15 +08:00
|
|
|
return post;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.filters = {
|
2015-07-21 13:50:37 +08:00
|
|
|
find_category: function(posts, cats) {
|
|
|
|
if (typeof cats === 'string') {
|
|
|
|
cats = [cats];
|
|
|
|
}
|
2015-05-09 17:36:15 +08:00
|
|
|
var ret = [];
|
2015-07-21 14:04:01 +08:00
|
|
|
getAllPosts(posts).forEach(function(post) {
|
|
|
|
if (cats.indexOf(post.meta.category) >= 0) {
|
|
|
|
ret.push(post);
|
2015-05-09 17:36:15 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
ret = ret.sort(function(a, b) {
|
|
|
|
a = a.meta.order || 10;
|
|
|
|
b = b.meta.order || 10;
|
|
|
|
return parseInt(a, 10) - parseInt(b, 10);
|
|
|
|
});
|
|
|
|
return ret;
|
2015-05-11 18:09:13 +08:00
|
|
|
},
|
2015-07-05 15:43:07 +08:00
|
|
|
get_categories: function(posts, post) {
|
|
|
|
var rootDirectory = post.directory.split('/')[0];
|
2015-08-22 17:21:47 +08:00
|
|
|
if (!rootDirectory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var directories = [rootDirectory];
|
|
|
|
// docs 和 components 放在同一页
|
|
|
|
if (rootDirectory === 'docs' || rootDirectory === 'components') {
|
|
|
|
directories = ['docs', 'components'];
|
|
|
|
}
|
|
|
|
var cacheKey = directories.join('-');
|
|
|
|
var categories = Categories[cacheKey] || _.uniq(getAllPosts(posts).map(function(item) {
|
|
|
|
var itemDirectory = item.directory.split('/')[0];
|
|
|
|
if (directories.indexOf(itemDirectory) >= 0) {
|
2015-07-05 15:43:07 +08:00
|
|
|
return item.meta.category;
|
2015-06-05 10:28:11 +08:00
|
|
|
}
|
2015-08-22 17:21:47 +08:00
|
|
|
})).filter(function(n) {
|
|
|
|
return n != undefined;
|
|
|
|
});
|
|
|
|
// React 的分类排序
|
|
|
|
categories = categories.sort(function(a, b) {
|
|
|
|
var cats = ['React', 'Components'];
|
|
|
|
a = cats.indexOf(a);
|
|
|
|
b = cats.indexOf(b);
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
// 设计的分类排序
|
2015-07-02 17:31:06 +08:00
|
|
|
categories = categories.sort(function(a, b) {
|
2015-07-29 17:23:11 +08:00
|
|
|
var cats = ['文字', '色彩', '动画'];
|
|
|
|
a = cats.indexOf(a);
|
|
|
|
b = cats.indexOf(b);
|
|
|
|
return a - b;
|
2015-07-05 15:43:07 +08:00
|
|
|
})
|
2015-08-22 17:21:47 +08:00
|
|
|
Categories[cacheKey] = categories;
|
2015-06-25 15:33:08 +08:00
|
|
|
return categories;
|
2015-05-16 15:03:33 +08:00
|
|
|
},
|
2015-05-16 15:04:50 +08:00
|
|
|
find_demo_in_component: function(pages, directory) {
|
2015-05-16 15:03:33 +08:00
|
|
|
var ret = [];
|
2015-07-21 14:04:01 +08:00
|
|
|
getAllPosts(pages).forEach(function(post) {
|
|
|
|
if (post.filepath.indexOf(directory + '/demo/') === 0) {
|
|
|
|
ret.push(post);
|
2015-05-16 15:03:33 +08:00
|
|
|
}
|
|
|
|
});
|
2015-05-27 20:41:16 +08:00
|
|
|
ret = ret.sort(function(a, b) {
|
|
|
|
if (/index$/i.test(a.filename)) {
|
|
|
|
a.meta.order = 1;
|
|
|
|
}
|
|
|
|
if (/index$/i.test(b.filename)) {
|
|
|
|
b.meta.order = 1;
|
|
|
|
}
|
|
|
|
a = a.meta.order || 100;
|
|
|
|
b = b.meta.order || 100;
|
|
|
|
return parseInt(a, 10) - parseInt(b, 10);
|
|
|
|
});
|
2015-05-16 15:03:33 +08:00
|
|
|
return ret;
|
2015-05-18 17:33:42 +08:00
|
|
|
},
|
|
|
|
// For Debug
|
|
|
|
console: function(target) {
|
|
|
|
console.log(target);
|
|
|
|
},
|
|
|
|
parsePost: function(filepath) {
|
|
|
|
return nico.sdk.post.read(filepath);
|
2015-05-27 11:35:01 +08:00
|
|
|
},
|
|
|
|
odd: function(items) {
|
|
|
|
return items.filter(function(item, i) {
|
|
|
|
return (i+1)%2 === 1;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
even: function(items) {
|
|
|
|
return items.filter(function(item, i) {
|
|
|
|
return (i+1)%2 === 0;
|
|
|
|
});
|
2015-07-07 11:52:10 +08:00
|
|
|
},
|
2015-08-22 17:21:47 +08:00
|
|
|
rootDirectoryIn: function(directory, rootDirectories) {
|
|
|
|
return rootDirectories.indexOf(directory.split('/')[0]) >= 0;
|
2015-08-04 11:36:04 +08:00
|
|
|
},
|
|
|
|
removeCodeBoxIdPrefix: function(id) {
|
|
|
|
return id.split('-').slice(2).join('-');
|
2015-05-09 17:36:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
2015-05-07 18:50:36 +08:00
|
|
|
};
|