![0d23693a8faf1e9fc80a2ddb283f1fe1.png](https://img-blog.csdnimg.cn/img_convert/0d23693a8faf1e9fc80a2ddb283f1fe1.png)
随着时间的推移,个人站点的博客文章会越来越多,那怎么样才能快速找到你印象中的文章呢?增加一个站点内的搜索功能是非常有必要和方便的。Hexo自带的搜索功能能够满足我们基础的需求,只需要安装一个提供搜索服务的插件,在配置文件中做相应配置就可以完成。
本地搜索不需要任何外部第三方服务,并且可以由搜索引擎额外索引。建议大多数用户使用此搜索方法。本文的配置是基于Hexo5.2.0的系统和NexT8.0以上的主题。版本如果不正确可能无法正常应用。
具体操作的步骤
1. 安装搜索需要的插件
在Hexo的根目录下,打开命令可执行窗口,执行如下命令:
npm install hexo-generator-search --save
npm install hexo-generator-searchdb --save
2. 在站点全局配置文件(_config.yml),新增如下内容:
search:
path: search.xml
field: post
content: true
format: html
3. hexo主题配置文件(_config_next.yml),修改local_search的enable为true:
# Local search
# Dependencies: https://github.com/next-theme/hexo-generator-searchdb
local_search:
enable: true # 这个部分原来是false
# 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
重新生成站点:
hexo clean
hexo g
hexo s
操作完成后可以在菜单中多出一个“搜索”的项目。
外部搜索服务
除了配置本地搜索服务,还可以采用其它外部提供的搜索服务。可以参考NexT配置外部搜索服务的文章