[Hexo] Use hexo-generator-searchdb plugin and hexo-theme-next (NexT) theme to enable local search in Hexo

hexo-generator-searchdb

Hexo is a fast, simple & powerful blog framework. It use plugins to provide features powerful APIs for limitless extensibility. Various plugins are available to support most template engines (EJS, Pug, Nunjucks, and many others). Easily integrate with existing NPM packages (Babel, PostCSS, Less/Sass, etc).

hexo-generator-searchdb is used for generating a search index file, which contains all the necessary data of your articles that you can use to write a local search engine for your blog. Supports both XML and JSON format output.

Installation

Use npm to install hexo-generator-searchdb package.

1
2
3
$ npm install hexo-generator-searchdb

$ hexo clean

Configuration

First, configure this plugin in your Hexo root _config.yml. All the arguments are optional.

1
2
3
4
5
6
7
# _config.yml

search:
path: search.xml
field: post
content: true
format: html

The follow is the options:

  • 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 cover all the posts of your blog.

    • page - will only cover all the pages of your blog.

    • all - will cover 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.

  • format - the form of the page contents, options are:

    • html (Default) - original html string being minified.

    • striptags - original html string being minified, and remove all the tags.

    • raw - markdown text of each posts or pages.

Then, configure this plugin in your hexo-theme-next plugin _config_next.yml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# _config_next.yml

# Local Search
# Dependencies: https://github.com/next-theme/hexo-generator-searchdb
local_search:
# DoItYourself
# enable: false
enable: true
# If auto, trigger search by changing input.
# If manual, trigger search by pressing enter key or search button.
trigger: auto
# Show top n results per article, show all results by setting to -1
top_n_per_article: 1
# Unescape html strings to the readable one.
unescape: false
# Preload the search data when the page loads.
preload: false

Finally, you can visit your site in the browser, and check whether the site works correctly after Hexo restarted or regenerated.

References

[1] hexojs/hexo-generator-searchdb: Add nofollow attribute to all external links automatically. - https://github.com/hexojs/hexo-generator-searchdb/

[2] Local Search - 第三方服务集成 - NexT 使用文档 - https://theme-next.iissnan.com/third-party-services.html#local-search

[3] Hexo - https://hexo.io/

[4] next-theme/awesome-next: 😎 Theme NexT, AWESOME NexT! - https://github.com/next-theme/awesome-next