hexo+next博客添加搜索

一、为何添加algolia搜索javascript

第一固然是能够方便的查找所需文章,第二点就是以前经常使用的swiftype插件再也不免费。个人我的博客是这个月初搭建完成的,这时候swiftype已经再也不免费,并且只开放企业邮箱注册。java

二、添加效果git

三、开始添加github

下载最新的next主题(5.1.0),由于最新版的已经集成了algolia搜索,能够省去不少配置和修改。swift

个人版本:api

(themes\next\_config.yml文件最下方查看)bash

 去algolia官网注册帐号(我直接使用的github的帐号)hexo

参照下图新建indexapp

打开API Keys页面,里面的信息等会须要写到hexo的配置文件中url

 

在根目录的站点配置文件_config.yml中加入以下配置,参照上图的各类key值

#添加搜索
algolia:
appId: 'appid'
apiKey: 'apikey'
adminApiKey: 'adminApiKey'
indexName: '本身新建的index名称'
chunkSize: 5000
fields:
- title
- slug
- path
- content:strip

 在git bash中执行hexo algolia。以下图

说明已经提交到了algolia。以下图

若是没法提交成功,先执行hexo clean便可。

在\themes\next下找到_config.yml,找到以下内容,将enable修改成true,labels修改成本身须要的

在themes\next\layout\_partials中找到header.swig,找到如下代码并修改

<nav class="site-nav">
<!-- 添加 theme.algolia_search.enable --> {% set hasSearch = theme.swiftype_key || theme.algolia_search.enable || theme.tinysou_Key || config.search %} {% if theme.menu %} <ul id="menu" class="menu"> {% for name, path in theme.menu %} {% set itemName = name.toLowerCase() %} <li class="menu-item menu-item-{{ itemName }}"> <a href="{{ url_for(path) }}" rel="section"> {% if theme.menu_icons.enable %} <i class="menu-item-icon fa fa-fw fa-{{theme.menu_icons[itemName] | default('question-circle') | lower }}"></i> <br /> {% endif %} {{ __('menu.' + itemName) }} </a> </li> {% endfor %} {% if hasSearch %} <li class="menu-item menu-item-search"> {% if theme.swiftype_key %} <a href="javascript:;" class="st-search-show-outputs"> {% elseif config.search %} <a href="javascript:;" class="popup-trigger">
      <!--增长的代码 start-->
{% elseif theme.algolia %} <a href="javascript:;" class="popup-trigger">       <!-- 增长的代码 end-->

{% endif %} {% if theme.menu_icons.enable %}
<i class="menu-item-icon fa fa-search fa-fw"></i> <br /> {% endif %} {{ __('menu.search') }} </a> </li> {% endif %} </ul> {% endif %} {% if hasSearch %} <div class="site-search"> {% include 'search.swig' %} </div> {% endif %} </nav>

这样就完成了添加。

 

有问题能够在下面留言,欢迎你们讨论。