Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
forever201406 committed Aug 24, 2015
0 parents commit 0ced673
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions .npmignore
@@ -0,0 +1,10 @@
test/
tmp/
coverage/
*.log
.jshintrc
.travis.yml
gulpfile.js
.idea/
appveyor.yml
node_modules
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (c) 2015 Mao shuhao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
@@ -0,0 +1,25 @@
# hexo-generator-search

Generate search data.

## Install

``` bash
$ npm install hexo-generator-search --save
```

- Hexo 3: 1.x
- Hexo 2: 0.x

## Options

You can configure this plugin in `_config.yml`.

``` yaml
search:
path: search.xml
field: post
```

- **path** - file path. (Default: search.xml)
- **search_field** - the field you want to search, you can chose post 、page or post,page(Default:post)
15 changes: 15 additions & 0 deletions index.js
@@ -0,0 +1,15 @@
var pathFn = require('path');

var config = hexo.config.search;

// Set default search path
if (!config.path){
config.path = 'search.xml';
}

// Add extension name if don't have
if (!pathFn.extname(config.path)){
config.path += '.xml';
}

hexo.extend.generator.register('search', require('./lib/generator'));
43 changes: 43 additions & 0 deletions lib/generator.js
@@ -0,0 +1,43 @@
var ejs = require('ejs');
var pathFn = require('path');
var fs = require('fs');

ejs.filters.cdata = function(str){
return str ? '<![CDATA[' + str + ']]>' : '';
};

var searchTmplSrc = pathFn.join(__dirname, '../search.ejs');
var searchTmpl = ejs.compile(fs.readFileSync(searchTmplSrc, 'utf8'));

module.exports = function(locals){
var config = this.config;
var searchConfig = config.search;
var template = searchTmpl;
var searchfields = searchConfig.search_field;

var posts, pages;

if(searchfields.trim() != '' && (searchfields.indexOf('post') >= 0 || searchfields.indexOf('page') >= 0)){
if(searchfields.indexOf('post') >= 0 ){
posts = locals.posts.sort('-date');
}

if(searchfields.indexOf('page') >= 0 ){
pages = locals.pages;
}
}else{
posts = locals.posts.sort('-date');
}

var xml = template({
config: config,
posts: posts,
pages: pages,
feed_url: config.root + searchConfig.path
});

return {
path: searchConfig.path,
data: xml
};
};
43 changes: 43 additions & 0 deletions package.json
@@ -0,0 +1,43 @@
{
"name": "hexo-generator-search",
"version": "1.0.0",
"description": "Seach generator plugin for Hexo",
"main": "index",
"directories": {
"lib": "./lib"
},
"engines": {
"node": ">= 0.10.0"
},
"keywords": [
"hexo",
"search",
"generator"
],
"author": {
"name": "PaicHyperion",
"email": "PaicHyperion@qq.com",
"url": "https://github.com/PaicHyperionDev"
},
"license": "MIT",
"dependencies": {
"ejs": "^1.0.0"
},
"bugs": {
"url": "https://github.com/PaicHyperionDev/hexo-generator-search/issues"
},
"homepage": "https://github.com/hexojs/hexo-generator-search#readme",
"_id": "hexo-generator-search@1.0.0",
"_npmVersion": "2.10.1",
"_nodeVersion": "0.12.4",
"_npmUser": {
"name": "PaicHyperion",
"email": "PaicHyperion@qq.com"
},
"maintainers": [
{
"name": "maoshuhao",
"email": "326810048@qq.com"
}
]
}
24 changes: 24 additions & 0 deletions search.ejs
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<search>
<% var url = config.url + config.root %>
<% if(posts){ %>
<% posts.each(function(post){ %>
<entry>
<title><%-: post.title | cdata %></title>
<link href="<%- encodeURI(url + post.path) %>"/>
<url><%- encodeURI(url + post.path) %></url>
<content type="html"><%-: post.content | cdata %></content>
</entry>
<% }) %>
<% } %>
<% if(pages){ %>
<% pages.each(function(page){ %>
<entry>
<title><%-: page.title | cdata %></title>
<link href="<%- encodeURI(url + page.path) %>"/>
<url><%- encodeURI(url + page.path) %></url>
<content type="html"><%-: page.content | cdata %></content>
</entry>
<% }) %>
<% } %>
</search>

0 comments on commit 0ced673

Please sign in to comment.