Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conent optimizations #84

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ search:

- **path** - file path. By default is `search.xml` . If the file extension is `.json`, the output format will be JSON. Otherwise XML format file will be exported.
- **field** - the search scope you want to search, you can chose:
* **post** (Default) - will only covers all the posts of your blog.
* **page** - will only covers all the pages of your blog.
* **all** - will covers all the posts and pages of your blog.
- **content** - whether contains the whole content of each article. If `false`, the generated results only cover title and other meta info without mainbody. By default is `true`.
* `post` (Default) - will only covers all the posts of your blog.
* `page` - will only covers all the pages of your blog.
* `all` - will covers all the posts and pages of your blog.
- **content** - whether contains the whole content of each article.
* `true` (Default) - generated results use the mainbody.
* `rendered` - generated results use the rendered mainbody if available. (json only)
* `excerpt` - generated results use the excerpts as content if available.
* `raw` - generated results use the raw mainbody if available. (json only, also contains the front-matter)
* `false` - generated results only cover title and other meta info without mainbody.
- **template** (Optional) - path to a custom XML template
- **strip_html** (Optional) - when `true` all HTML tags will be removed from the content.
- **permalinks** (Optional) - when `true` the tags and categories in json output will also contain the permalinks.

## Exclude indexing

Expand Down
125 changes: 72 additions & 53 deletions lib/json_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module.exports = function(locals){
var searchConfig = config.search;
var searchfield = searchConfig.field;
var content = searchConfig.content;
if (content == undefined) content = true;
var stripHtml = searchConfig.strip_html;
if (stripHtml == undefined) stripHtml = false;
var permalinks = searchConfig.permalinks;
if (permalinks == undefined) permalinks = false;

var posts, pages;

Expand All @@ -23,67 +28,81 @@ module.exports = function(locals){
posts = locals.posts.sort('-date');
}

var res = new Array()
var res = new Array()
var index = 0
if(posts){

if(posts){
posts.each(function(post) {
if (post.indexing != undefined && !post.indexing) return;
var temp_post = new Object()
var temp_post = new Object()
temp_post.title = post.title || 'No 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 = [];
if (post.path) {
temp_post.url = config.root + post.path
}
if (content != false) {
if (content == 'rendered' && post.content) {
temp_post.content = post.content
} else if (content == 'excerpt' && post.excerpt) {
temp_post.content = post.excerpt
} else if (content == 'raw' && post.raw) {
temp_post.content = post.raw
} else if (post._content) {
temp_post.content = post._content
}
if (stripHtml && temp_post.content) {
temp_post.content = temp_post.content.replace(/<[^>]+>/g, '')
}
}
temp_post.tags = [];
if (post.tags && post.tags.length > 0) {
post.tags.forEach(function (tag) {
tags.push(tag.name);
});
temp_post.tags = tags
}
if (post.categories && post.categories.length > 0) {
var categories = [];
if (permalinks) {
temp_post.tags.push([ tag.name, tag.permalink ]);
} else {
temp_post.tags.push(tag.name);
}
});
}
temp_post.categories = [];
if (post.categories && post.categories.length > 0) {
post.categories.forEach(function (cate) {
categories.push(cate.name);
});
temp_post.categories = categories
}
res[index] = temp_post;
index += 1;
});
}
if(pages){
if (permalinks) {
temp_post.categories.push([ cate.name, cate.permalink ]);
} else {
temp_post.categories.push(cate.name);
}
});
}
res[index] = temp_post;
index += 1;
});
}
if(pages){
pages.each(function(page){
if (page.indexing != undefined && !page.indexing) return;
var temp_page = new Object()
temp_post.title = post.title || 'No 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 temp_page = new Object()
temp_page.title = page.title || 'No Title'
if (page.path) {
temp_page.url = config.root + page.path
}
if (content != false) {
if (content == 'rendered' && page.content) {
temp_page.content = page.content
} else if (content == 'excerpt' && page.excerpt) {
temp_page.content = page.excerpt
} else if (content == 'raw' && page.raw) {
temp_page.content = page.raw
} else if (page._content) {
temp_page.content = page._content
}
if (stripHtml && temp_page.content) {
temp_page.content = temp_page.content.replace(/<[^>]+>/g, '')
}
}
res[index] = temp_page;
index += 1;
});
}


var json = JSON.stringify(res);
Expand Down
30 changes: 29 additions & 1 deletion lib/xml_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ module.exports = function(locals){
var template = searchTmpl;
var searchfield = searchConfig.field;
var content = searchConfig.content;
if (content == undefined) content=true;
if (content == undefined) content = true;
var stripHtml = searchConfig.strip_html;
if (stripHtml == undefined) stripHtml = false;

var posts, pages;

Expand All @@ -40,6 +42,32 @@ module.exports = function(locals){
posts = locals.posts.sort('-date');
}

if (content == 'excerpt') {
posts.data.forEach(function (p) {
if (p.excerpt) {
p.content = p.excerpt;
}
});
pages.data.forEach(function (p) {
if (p.excerpt) {
p.content = p.excerpt;
}
});
}

if (stripHtml) {
posts.data.forEach(function (p) {
if (p.content) {
p.content = p.content.replace(/<[^>]+>/g, '');
}
});
pages.data.forEach(function (p) {
if (p.content) {
p.content = p.content.replace(/<[^>]+>/g, '');
}
});
}

var rootURL;
if (config.root == null){
rootURL = "/";
Expand Down