博客寫多了(汗!我寫的并不多),查找是個(gè)問(wèn)題,所以我想添加個(gè)站內(nèi)搜索功能。NexT主題支持集成 Swiftype、 微搜索、Local Search 和 Algolia,但Swiftype和Algolia都只有一段時(shí)間的試用期。所以我采用了Hexo提供的Local Search,原理是通過(guò)hexo-generator-search插件在本地生成一個(gè)search.xml/json文件,通過(guò)這個(gè)文件實(shí)現(xiàn)搜索功能。
為hexo和next增加站內(nèi)搜索功能
安裝插件
npm install hexo-generator-search
npm install hexo-generator-searchdb
修改hexo配置
在你的hexo目錄下的_config.yml中增加如下配置:
search:
path: search.xml
field: post
format: html
limit: 10000
配置上之后,其實(shí)搜索已經(jīng)配置完成了,但現(xiàn)在我們還看不到搜索的入口,接下來(lái)我們需要在next的主體上進(jìn)行配置
配置next中的搜索入口
打開(kāi)themes\next_config.yml,打開(kāi)local search:
# Local search
local_search:
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
接下來(lái)就可以運(yùn)行:
$ hexo s
在本地打開(kāi)http://localhost:4000/進(jìn)行查看了。
travis-ci構(gòu)建搜索模塊
如果你的博客是使用travis-ci自動(dòng)進(jìn)行構(gòu)建的話,需要將上面提到的兩個(gè)插件在.travis.yml中進(jìn)行配置:
# S: Build Lifecycle
install:
- npm install
- npm install hexo-generator-search
- npm install hexo-generator-searchdb
效果圖如下:
20170913111047.png
歡迎訪問(wèn)我的博客:http://atecher.com/,有更多精彩文章!