| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- var pathFn = require('path');
- var fs = require('fs');
- module.exports = function(locals){
- var config = this.config;
- var searchConfig = config.search;
- var searchfield = searchConfig.field;
- var content = searchConfig.content;
- var posts, pages;
- if(searchfield.trim() != ''){
- searchfield = searchfield.trim();
- if(searchfield == 'post'){
- posts = locals.posts.sort('-date');
- }else if(searchfield == 'page'){
- pages = locals.pages;
- }else{
- posts = locals.posts.sort('-date');
- pages = locals.pages;
- }
- }else{
- posts = locals.posts.sort('-date');
- }
- var res = new Array()
- var index = 0
-
- if(posts){
- posts.each(function(post) {
- if (post.indexing != undefined && !post.indexing) return;
- var temp_post = new Object()
- if (post.title) {
- temp_post.title = post.title
- }
- if (post.path) {
- temp_post.url = config.root + post.path
- }
- if (content != false && post._content) {
- temp_post.content = post._content
- }
- if (post.tags && post.tags.length > 0) {
- var tags = [];
- post.tags.forEach(function (tag) {
- tags.push(tag.name);
- });
- temp_post.tags = tags
- }
- if (post.categories && post.categories.length > 0) {
- var categories = [];
- post.categories.forEach(function (cate) {
- categories.push(cate.name);
- });
- temp_post.categories = categories
- }
- res[index] = temp_post;
- index += 1;
- });
- }
- if(pages){
- pages.each(function(page){
- if (page.indexing != undefined && !page.indexing) return;
- var temp_page = new Object()
- if (page.title) {
- temp_page.title = page.title
- }
- if (page.path) {
- temp_page.url = config.root + page.path
- }
- if (content != false && page._content) {
- temp_page.content = page._content
- }
- if (page.tags && page.tags.length > 0) {
- var tags = new Array()
- var tag_index = 0
- page.tags.each(function (tag) {
- tags[tag_index] = tag.name;
- });
- temp_page.tags = tags
- }
- if (page.categories && page.categories.length > 0) {
- temp_page.categories = []
- (page.categories.each || page.categories.forEach)(function (item) {
- temp_page.categories.push(item);
- });
- }
- res[index] = temp_page;
- index += 1;
- });
- }
- var json = JSON.stringify(res);
- return {
- path: searchConfig.path,
- data: json
- };
- };
|