You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB

var _ = require('lodash');
var Posts;
10 years ago
module.exports = function(nico) {
var exports = {};
var Categories = {};
10 years ago
exports.reader = function(post) {
10 years ago
var filepath = post.meta.filepath.toLowerCase();
if (filepath.indexOf('components') === 0) {
10 years ago
post.template = post.meta.template = 'component';
} else {
post.template = post.meta.template = (post.meta.template || 'page');
}
10 years ago
if (filepath === 'readme.md') {
10 years ago
post.filename = post.meta.filename = 'index';
10 years ago
post.template = post.meta.template = 'home';
10 years ago
}
10 years ago
if (filepath.indexOf('/demo/') > 0) {
post.template = post.meta.template = 'code';
}
10 years ago
return post;
};
exports.filters = {
find_category: function(posts, cats) {
if (typeof cats === 'string') {
cats = [cats];
}
10 years ago
var ret = [];
Object.keys(posts).forEach(function(key) {
var item = posts[key];
if (cats.indexOf(item.meta.category) >= 0) {
10 years ago
ret.push(item);
}
});
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;
},
get_categories: function(posts, post) {
var rootDirectory = post.directory.split('/')[0];
var categories = Categories[rootDirectory] || _.uniq(Object.keys(posts).map(function(key) {
var item = posts[key];
if (item.directory.split('/')[0] === post.directory.split('/')[0]) {
return item.meta.category;
10 years ago
}
})).filter(function(n){ return n != undefined });
10 years ago
categories = categories.sort(function(a, b) {
return a.length - b.length;
})
Categories[rootDirectory] = categories;
10 years ago
return categories;
},
10 years ago
find_demo_in_component: function(pages, directory) {
var ret = [];
Object.keys(pages).forEach(function(key) {
var page = pages[key];
if (key.indexOf(directory + '/demo/') === 0) {
ret.push(page);
}
});
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);
});
return ret;
},
// For Debug
console: function(target) {
console.log(target);
},
parsePost: function(filepath) {
return nico.sdk.post.read(filepath);
},
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;
});
},
rootDirectoryIs: function(directory, rootDirectory) {
return directory.split('/')[0] === rootDirectory;
10 years ago
}
};
return exports;
10 years ago
};