hexo进阶之添加分类、标签、搜索菜单

hexo进阶之添加菜单

分类、标签、搜索

  • 工具:hexo + NexT
  • 环境:mac(10.13.2)、node.js(9.4.0)、npm(5.6.0)

前言

上一篇已经搭建了最简单的博客,没有常见的菜单:分类、标签、音乐、搜索、RSS订阅、个人,这一篇就是完善这个菜单功能。

添加菜单

89A00507-FB87-4FD0-8A1C-9E817208C45D

修改NexT下的主题配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ vim theme/next/_config.yml
## 添加菜单(配置菜单对应的图标,NexT使用的是Font Awesome提供的图标https://fontawesome.com/)
## || 后面为图标名称
menu:
home: / || home
categories: /categories/ || th
archives: /archives/ || archive
tags: /tags/ || tags
#search: /search/ || search ## 不需要配置,使用了local search
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
about: /about/ || user
#commonweal: /404/ || heartbeat
# Enable/Disable menu icons.
menu_icons:
enable: true

编辑页面显示的菜单中文名称

编辑Next目录下的languages/{language}.yml文件,{language}.yml为站点_config.yml中配置的languages: zh-Hans对应

1
2
3
4
5
6
7
8
9
10
11
$ vim /theme/next/languages/zh-Hans.yml
menu:
home: 首页
archives: 归档
categories: 分类
tags: 标签
about: 关于
search: 搜索
schedule: 日程表
sitemap: 站点地图
commonweal: 公益404

到这里,已经添加了归档、分类等菜单,但是点击是没有效果的。

配置菜单对应内容

分类菜单

1
2
3
4
5
6
7
## 创建categories页面
$ hexo new page "categories"
$ vim source/categories/index.md
title: All categories
date: 2018-03-26 12:39:04
type: "categories"
comments: false

标签菜单

1
2
3
4
5
6
7
## 创建tags页面
$ hexo new page "tags"
$ vim source/tags/index.md
title: All tags
date: 2014-12-22 12:39:04
type: "tags"
comments: false

注:分类和标签还没有和分类关联起来,需要在每篇文章头部添加categories和tags属性,这样就可以自动关联了,为了后续方便,修改scaffolds/post.md模版即可。

1
2
3
4
5
6
title: hexo+github快速搭建博客
date: 2018-03-25 14:12:32
categories: hexo
tags:
- hexo+github+next
- hexo

搜索菜单

采用Hexo提供的Local Search站内搜索,原理是通过hexo-generator-search插件在本地生成一个search.xml文件,搜索的时候从这个文件中根据关键字检索出相应的链接。

1
2
3
4
5
6
7
8
9
10
11
12
## 在站点根目录下安装
$ npm install hexo-generator-search --save
$ npm install hexo-generator-searchdb --save
## 站点_config.yml配置
search:
path: search.xml
field: post
format: html
limit: 10000
## 主题配置文件修改
local_search:
enable: true

注:由于使用了local search,主题配置文件中就menu属性就不需要添加search了。

参考

本文作者:ttbb
本文地址http://steven-ji.github.io/blog/2018/03/26/hexo进阶之分类、标签、搜索/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

坚持原创技术分享,您的支持将鼓励我继续创作!
Fork me on GitHub