Hexo中添加本地搜索

今天增加了一个新功能,博客文章检索。记得之前尝试过,觉得很麻烦,并且文章也少。但现在文章多起来。下面简单记录下过程

安装搜索插件

这个需要在hexo根目录执行,目前这个插件相比几年前的search插件更稳定。

1
npm install hexo-generator-searchdb --save

配置文件

打开Hexo根目录下的”_config.yml”文件,在文件末尾添加以下配置:

1
2
3
4
5
6
# 本地搜索数据生成配置
search:
path: search.xml # 生成的搜索数据文件名,推荐 .xml 或 .json
field: post # 搜索范围:post(仅文章), page(仅页面), all(全部)
content: true # 是否在搜索数据中包含文章全文内容 (true/false)
template:

主题里的config.yml

主题开启本地搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
search:
# Choose: algolia_search / local_search / docsearch
# leave it empty if you don't need search
use: local_search
placeholder: 搜索中...

# Algolia Search
algolia_search:
# Number of search results per page
hitsPerPage: 6

# Local Search
local_search:
# Preload the search data when the page loads.
preload: false
# 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
# Enable pagination for search results
pagination:
enable: false
# Number of search results per page
hitsPerPage: 8
CDN:

测试调试

配置完成后,需要清理缓存并重新生成静态文件,

1
2
3
hexo clean
hexo g
hexo s

/public目录下也能找到该文件

同时,浏览器访问localhost:4000/search.xml会看到检索表。

若提示Cannot GET /search.xml,代表文件没有生成,需要重新校对以上步骤。

效果如下:
感觉还行哦,还算流畅