NexT 主题已经实现这个功能,它用了 Hexo 的拓展包 hexo-generator-searchdb,预先生成了一个文本库 search.xml,然后传到了网站里面。在本地搜索的时候,NexT 直接用 javascript 调用了这个文件,从而实现了静态网站的本地搜索。
插件地址
hexo-generator-searchdb
安装配置
安装插件:
1
npm install hexo-generator-searchdb --save
然后我们修改站点配置_config.yml 文件,添加如下内容:
1 2 3 4 5 6
# 本地搜索 search: path: search.xml field: post format: html limit: 100
path:索引文件的路径,相对于站点根目录
field:搜索范围,默认是 post,还可以选择 page、all,设置成 all 表示搜索所有页面
limit:限制搜索的条目数
然后修改主题配置文件_config.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13
# Local Search # Dependencies: https://github.com/theme-next/hexo-generator-searchdb 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 # Unescape html strings to the readable one. unescape: false # Preload the search data when the page loads. preload: false
ERROR Plugin load failed: hexo-leancloud-counter-security Error: Cannot find module 'babel-runtime/regenerator' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Module.require (internal/modules/cjs/loader.js:690:17) at require (/Users/anran/Desktop/MyProject/blog/node_modules/hexo/lib/hexo/index.js:219:21) at /Users/anran/Desktop/MyProject/blog/node_modules/hexo-leancloud-counter-security/index.js:3:20 at fs.readFile.then.script (/Users/anran/Desktop/MyProject/blog/node_modules/hexo/lib/hexo/index.js:232:12) at tryCatcher (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:517:31) at Promise._settlePromise (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:574:18) at Promise._settlePromise0 (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:619:10) at Promise._settlePromises (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:699:18) at Promise._fulfill (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:643:18) at Promise._resolveCallback (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:437:57) at Promise._settlePromiseFromHandler (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:529:17) at Promise._settlePromise (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:574:18) at Promise._settlePromise0 (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:619:10) at Promise._settlePromises (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:699:18) at Promise._fulfill (/Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/promise.js:643:18) at /Users/anran/Desktop/MyProject/blog/node_modules/bluebird/js/release/nodeback.js:42:21 at /Users/anran/Desktop/MyProject/blog/node_modules/graceful-fs/graceful-fs.js:115:16 at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3) Usage: hexo <command>