Fork me on GitHub

【持续更新】关于Hexo优化

基本命令

  • hexo n “我的博客” == hexo new “我的博客” #新建文章
  • hexo p == hexo publish
  • hexo g == hexo generate#生成
  • hexo s == hexo server #启动服务预览
  • hexo d == hexo deploy#部署

增加站内搜索功能

安装插件

在自己博客根目录下(我的目录:D:\workspace\hexo),执行如下命令

1
cnpm install hexo-generator-searchdb --save

修改站点配置文件

修改根目录下的_config.yml(我的目录:D:\workspace\hexo_config.yml),在最底部添加如下配置

1
2
3
4
5
search:
path: search.xml
field: post
format: html
limit: 10000

修改主题配置文件

修改主体下的themes\next_config.yml配置文件(我的目录:D:\workspace\hexo\themes\next_config.yml),搜索local_search,修改enable为true

1
2
3
4
5
6
7
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

预览效果

开启本地server

1
2
3
hexo clean
hexo g
hexo s

增加博客置顶功能(自定义排序)

修改node_modules下代码

找到node_modules/hexo-generator-index/lib/generator.js文件

将内部代码替换成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';

var pagination = require('hexo-pagination');

module.exports = function(locals) {
var config = this.config;
var posts = locals.posts.sort(config.index_generator.order_by);
posts.data = posts.data.sort(function(a, b) {
if(a.top && b.top) { // 两篇文章top都有定义
if(a.top == b.top) return b.date - a.date; // 若top值一样则按照文章日期降序排
else return b.top - a.top; // 否则按照top值降序排
}
else if(a.top && !b.top) { // 以下是只有一篇文章top有定义,那么将有top的排在前面(这里用异或操作居然不行233)
return -1;
}
else if(!a.top && b.top) {
return 1;
}
else return b.date - a.date; // 都没定义按照文章日期降序排
});
var paginationDir = config.pagination_dir || 'page';
var path = config.index_generator.path || '';

return pagination(path, posts, {
perPage: config.index_generator.per_page,
layout: ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true
}
});
};

使用

在文章添加 top 标签可以设置置顶顺序顺序根据top的值来决定

1
2
3
4
5
6
---
title: 文章名
date: 文章发布时间
tags: 文章标签
top: 100(文章置顶)
---

Next主题设定代码高亮格式

需要改动的有hexo的配置文件_config.yml,以及next主题的配置文件themes/next/_config.yml文件

更改hexo的配置文件

查找highlight关键字,更改一下属性为true

1
2
3
4
5
highlight:
enable: true
line_number: true
auto_detect: true
tab_replace:

更改next主题配置文件

搜索highlight_theme关键字,这个属性有四个值:

  • normal :默认值
  • night :黑色
  • night eighties :个人感觉跟第二个差不太多
  • night blue : 底部是蓝色,跟sublime text3默认配色差不多
  • night bright : 有点闪瞎眼的感觉

具体使用哪个看个人喜好吧,本人night

增加显示文章更新时间

在next主题的配置文件themes/next/_config.yml文件中搜索post_meta关键字,将属性updated_at该为true

1
2
3
4
5
post_meta:
item_text: true
created_at: true
updated_at: true
categories: true