Hexo博客优化之Next主题功能强化


前言

这里是Hexo博客优化的最后一篇了,主要讲的是讲的是功能层面的优化,如:网站加速、评论功能、在线聊天功能、一键分享功能等等。

1. 添加网站评论功能

1.1来必力

来必力是一款韩国的评论软件,先进入注册登录,然后安装免费版本:

1548349243475

安装之后获取安装代码里面的uid,放入主题配置文件:

1
livere_uid: 你的uid

效果如下:

1548349386333

进入管理后台可以管理评论:

1548349437963

1.2Valine

Valine是国内的一款极简风格的评论软件,首先进入LeanCloud注册,然后在控制台随便创建一个项目后,获取密钥:

1548349609105

然后修改主题配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
valine:
enable: true # When enable is set to be true, leancloud_visitors is recommended to be closed for the re-initialization problem within different leancloud adk version.
appid: 你的appid
appkey: 你的appkey
notify: false # 邮件提醒
verify: false # 验证码
placeholder: ヾノ≧∀≦)o 来呀!吐槽一番吧! # 默认输入信息
avatar: mm # gravatar style
guest_info: nick # 用户可输入的信息,支持:昵称nick,邮箱mail和链接link
pageSize: 10 # pagination size,每页评论数
visitor: false # leancloud-counter-security is not supported for now. When visitor is set to be true, appid and appkey are recommended to be the same as leancloud_visitors' for counter compatibility. Article reading statistic https://valine.js.org/visitor.html
comment_count: true # 评论计数

其实在Web设置中可以添加我们的域名,差不多,效果如下:

1548349895518

其后台在我们的项目-存储-Comments中:

1548349972701

鉴于Valine比较适合我博客主题风格,我还是选择它。

2.添加网站分享功能

这里我采用的是needmoreshare2,首先在themes/next/下执行:

1
git clone https://github.com/theme-next/theme-next-needmoreshare2 source/lib/needsharebutton

然后配置主题文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
needmoreshare2:
enable: true
postbottom:
enable: true #是否开启博客分享按钮
options:
iconStyle: box
boxForm: horizontal
position: bottomCenter
networks: Weibo,Wechat,Douban,QQZone,Twitter,Facebook
float:
enable: true #网站分享按钮
options:
iconStyle: box
boxForm: horizontal
position: middleRight
networks: Weibo,Wechat,Douban,QQZone,Twitter,Facebook

效果如下:

1548348093270

3.博文压缩

我们利用Hexo生成的博客文件中存在大量的空格和空行,从而使得博客资源中有很多不必要的内存消耗,使得网站加载变慢,所以可以利用neat进行博文压缩,首先安装:

1
npm install hexo-neat --save

为了在开启hexo-neat的同时,不要将我们的动态配置压缩了,可在站点配置文件中加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# hexo-neat
# 博文压缩
neat_enable: true
# 压缩html
neat_html:
enable: true
exclude:
# 压缩css
neat_css:
enable: true
exclude:
- '**/*.min.css'
# 压缩js
neat_js:
enable: true
mangle: true
output:
compress:
exclude:
- '**/*.min.js'
- '**/jquery.fancybox.pack.js'
- '**/index.js'
- '**/clicklove.js'
- '**/fireworks.js'

当然,除此之外还可以用gulp插件进行压缩,先安装:

1
2
npm install gulp -g
npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save

然后在blog主目录下添加gulpfile.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
33
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
// 压缩 public 目录 css
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
// 压缩 public 目录 html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩 public/js 目录 js
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 执行 gulp 命令时执行的任务
gulp.task('default', [
'minify-html','minify-css','minify-js'
]);

4.DaoVoice在线联系

首先我们在DaoVoice上注册一个账号,然后进入应用设置-安装到网站-仅匿名用户:

1548344816726

聊天设置中可以设置聊天窗口样式:

1548344816726

相应地,我们还能设置接受消息方式,既能控制台访问,也能微信或者邮件接受消息:

1548345514881

5.加速鼠标响应

在themes/next/下执行:

1
git clone https://github.com/theme-next/theme-next-fastclick source/lib/fastclick

然后设置主题配置文件:

1
fastclick = true

6.添加cdn加速

Next主题官方提供了一些软件的CDN加速,我们可以在主题配置文件中设置:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
vendors:
# Internal path prefix. Please do not edit it.
_internal: lib

# Internal version: 2.1.3
# Example:
# jquery: //cdn.jsdelivr.net/npm/jquery@2/dist/jquery.min.js
# jquery: //cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js
jquery: //cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js

# Internal version: 2.1.5
# See: https://fancyapps.com/fancybox
# Example:
# fancybox: //cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js
# fancybox: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.js
# fancybox_css: //cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css
# fancybox_css: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.css
fancybox: //cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js
fancybox_css: //cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css

# Internal version: 1.0.6
# See: https://github.com/ftlabs/fastclick
# Example:
# fastclick: //cdn.jsdelivr.net/npm/fastclick@1/lib/fastclick.min.js
# fastclick: //cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js
fastclick: //cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js

# Internal version: 1.9.7
# See: https://github.com/tuupola/jquery_lazyload
# Example:
# lazyload: //cdn.jsdelivr.net/npm/jquery-lazyload@1/jquery.lazyload.min.js
# lazyload: //cdnjs.cloudflare.com/ajax/libs/jquery_lazyload/1.9.7/jquery.lazyload.min.js
lazyload: //cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js

# Internal version: 1.2.1
# See: http://velocityjs.org
# Example:
# velocity: //cdn.jsdelivr.net/npm/velocity-animate@1/velocity.min.js
# velocity: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js
# velocity_ui: //cdn.jsdelivr.net/npm/velocity-animate@1/velocity.ui.min.js
# velocity_ui: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js
velocity: //cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js
velocity_ui: //cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js

# Internal version: 0.7.9
# See: https://faisalman.github.io/ua-parser-js
# Example:
# ua_parser: //cdn.jsdelivr.net/npm/ua-parser-js@0/src/ua-parser.min.js
# ua_parser: //cdnjs.cloudflare.com/ajax/libs/UAParser.js/0.7.9/ua-parser.min.js
ua_parser: //cdn.jsdelivr.net/ua-parser.js/0.7.10/ua-parser.min.js

# Internal version: 4.6.2
# See: https://fontawesome.com
# Example:
# fontawesome: //cdn.jsdelivr.net/npm/font-awesome@4/css/font-awesome.min.css
# fontawesome: //cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.2/css/font-awesome.min.css
fontawesome: //maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css

# Internal version: 2.4.1
# See: https://www.algolia.com
# Example:
# algolia_instant_js: //cdn.jsdelivr.net/npm/instantsearch.js@2/dist/instantsearch.js
# algolia_instant_css: //cdn.jsdelivr.net/npm/instantsearch.js@2/dist/instantsearch.min.css
algolia_instant_js:
algolia_instant_css:

# Internal version: 1.0.2
# See: https://github.com/HubSpot/pace
# Example:
# pace: //cdn.jsdelivr.net/npm/pace-js@1/pace.min.js
# pace: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js
# pace_css: //cdn.jsdelivr.net/npm/pace-js@1/themes/blue/pace-theme-minimal.css
# pace_css: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css
pace: //cdn.bootcss.com/pace/1.0.2/pace.min.js
pace_css: //cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css

# Internal version: 1.0.0
# See: https://github.com/theme-next/theme-next-canvas-nest
# Example:
# canvas_nest: //cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1/canvas-nest.min.js
# canvas_nest_nomobile: //cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1/canvas-nest-nomobile.min.js
canvas_nest: //cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js
canvas_nest_nomobile:

# Internal version: 1.0.0
# See: https://github.com/theme-next/theme-next-three
# Example:
# three: //cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three.min.js
# three_waves: //cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three-waves.min.js
# canvas_lines: //cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_lines.min.js
# canvas_sphere: //cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_sphere.min.js
three:
three_waves:
canvas_lines:
canvas_sphere:

# Internal version: 1.0.0
# See: https://github.com/zproo/canvas-ribbon
# Example:
# canvas_ribbon: //cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-ribbon@1/canvas-ribbon.js
canvas_ribbon:

# Internal version: 3.3.0
# See: https://github.com/ethantw/Han
# Example:
# han: //cdn.jsdelivr.net/npm/han-css@3/dist/han.min.css
# han: //cdnjs.cloudflare.com/ajax/libs/Han/3.3.0/han.min.css
han:

# Internal version: 3.3.0
# See: https://github.com/vinta/pangu.js
# Example:
# pangu: //cdn.jsdelivr.net/npm/pangu@3/dist/browser/pangu.min.js
# pangu: //cdnjs.cloudflare.com/ajax/libs/pangu/3.3.0/pangu.min.js
pangu:

# Internal version: 1.0.0
# See: https://github.com/revir/need-more-share2
# Example:
# needmoreshare2_js: //cdn.jsdelivr.net/gh/theme-next/theme-next-needmoreshare2@1/needsharebutton.min.js
# needmoreshare2_css: //cdn.jsdelivr.net/gh/theme-next/theme-next-needmoreshare2@1/needsharebutton.min.css
needmoreshare2_js:
needmoreshare2_css:

# Internal version: 1.0.0
# See: https://github.com/theme-next/theme-next-bookmark
# Example:
# bookmark: //cdn.jsdelivr.net/gh/theme-next/theme-next-bookmark@1/bookmark.min.js
bookmark:

# Internal version: 1.1
# See: https://github.com/theme-next/theme-next-reading-progress
# Example:
# reading_progress: //cdn.jsdelivr.net/gh/theme-next/theme-next-reading-progress@1/reading_progress.min.js
reading_progress:

# leancloud-storage
# See: https://www.npmjs.com/package/leancloud-storage
# Example:
# leancloud: //cdn.jsdelivr.net/npm/leancloud-storage@3/dist/av-min.js
leancloud:

# valine
# See: https://github.com/xCss/Valine
# Example:
# valine: //cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js
# valine: //cdnjs.cloudflare.com/ajax/libs/valine/1.3.4/Valine.min.js
valine:

# gitalk
# See: https://github.com/gitalk/gitalk
# Example:
# gitalk_js: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js
# gitalk_css: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css
gitalk_js:
gitalk_css:

# js-md5
# See: https://github.com/emn178/js-md5
# Example:
# md5: //cdn.jsdelivr.net/npm/js-md5@0/src/md5.min.js
md5:

7. 添加lazyload

lazylod可以在用户不查看的时候,不加载相关部分,从而提升网站加载速度,设置方法同上:

1
git clone https://github.com/theme-next/theme-next-jquery-lazyload source/lib/jquery_lazyload

然后配置主题文件:

1
lazyload: true

8.网站动态元素延时加载

我们的网站添加了许多动态元素之后,加载速度会变慢,所以可以先不加载动态元素,等静态元素加载完之后再加载动态元素,这样就加速了网站的登入。可设置主题文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Use velocity to animate everything.
motion:
enable: false
async: false
transition:
# Transition variants:
# fadeIn | fadeOut | flipXIn | flipXOut | flipYIn | flipYOut | flipBounceXIn | flipBounceXOut | flipBounceYIn | flipBounceYOut
# swoopIn | swoopOut | whirlIn | whirlOut | shrinkIn | shrinkOut | expandIn | expandOut
# bounceIn | bounceOut | bounceUpIn | bounceUpOut | bounceDownIn | bounceDownOut | bounceLeftIn | bounceLeftOut | bounceRightIn | bounceRightOut
# slideUpIn | slideUpOut | slideDownIn | slideDownOut | slideLeftIn | slideLeftOut | slideRightIn | slideRightOut
# slideUpBigIn | slideUpBigOut | slideDownBigIn | slideDownBigOut | slideLeftBigIn | slideLeftBigOut | slideRightBigIn | slideRightBigOut
# perspectiveUpIn | perspectiveUpOut | perspectiveDownIn | perspectiveDownOut | perspectiveLeftIn | perspectiveLeftOut | perspectiveRightIn | perspectiveRightOut
post_block: fadeIn
post_header: slideDownIn
post_body: slideDownIn
coll_header: slideLeftIn
# Only for Pisces | Gemini.
sidebar: slideUpIn

9.添加站内搜索

Next集成了站内搜索功能,可先安装依赖:

1
npm install hexo-generator-searchdb --save

然后设置主题配置文件:

1
2
3
4
5
6
7
8
9
10
11
# 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: 3
# unescape html strings to the readable one
unescape: false

效果如下:

1548347791673

10.添加百度谷歌收录

要想让我们的博客被百度、谷歌等搜索引擎索引到,需要提交我们的域名,谷歌很快就能收录,但是百度要一两个月,具体步骤如下:

  • 在百度搜搜引擎中查看自己域名是否被收录:site:huangpiao.tech

  • 然后点击提交网址,并在百度站长中提交申请,并验证网站:

    1548352288610

    我选择将验证文件放入blog/source中,然后进行部署,为了防止渲染造成的文件失效,需要在这个验证文件上面加入:

    1
    2
    3
    ---
    layout: false
    ---
  • 验证痛过之后,在我们的博客主目录下载安装:

    1
    2
    npm install hexo-generator-sitemap --save
    npm install hexo-generator-baidu-sitemap --save

    当我们在此加载博客会在public目录生成sitemap.xmlbaidusitemap.xml

  • 修改博客站点配置文件:

    1
    2
    3
    4
    5
    # 自动生成sitemap
    sitemap:
    path: sitemap.xml
    baidusitemap:
    path: baidusitemap.xml
  • 修改博客主题配置文件:

    1
    2
    # Enable baidu push so that the blog will push the url to baidu automatically which is very helpful for SEO
    baidu_push: true

11.添加公式编辑功能

Next6主题集成了mathjax和katex两种公式编辑功能,其中后者渲染速度比前者快很多,只不过支持的功能少一点。

  • mathjax:

    在博客主目录执行:

    1
    2
    npm un hexo-renderer-marked --save
    npm i hexo-renderer-kramed --save # 或者 hexo-renderer-pandoc

    修改node_modules\kramed\lib\rules\inline.js中对应地方:

    1
    2
    escape: /^\\([`*\[\]()#$+\-.!_>])/,
    em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

    然后修改主题配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    math:
    enable: true
    ...
    engine: mathjax
    #engine: katex
    mathjax:
    cdn: //cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML
    mhchem: //cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.3.0

    在博客内容中,添加mathjax = true

  • katex:

    在博客主目录执行:

    1
    2
    npm un hexo-renderer-marked --save
    npm i hexo-renderer-markdown-it-plus --save

    然后修改主题配置文件:

    1
    2
    3
    4
    5
    math:
    enable: true
    ...
    #engine: mathjax
    engine: katex

具体问题可以查看官方文档,至少我这里没有生效:sob:

12.添加流程图支持

对于流程图flowchart或者更好的mermaid可以先下载:

1
2
3
npm install --save hexo-filter-flowchart
npm install hexo-filter-mermaid-diagrams
npm install --save hexo-filter-sequence

然后在站点配置文件中加入:

1
2
3
4
5
6
7
8
9
10
flowchart:
# raphael: # optional, the source url of raphael.js
# flowchart: # optional, the source url of flowchart.js
options: # options used for `drawSVG`
# mermaid chart
mermaid: ## mermaid url https://github.com/knsv/mermaid
enable: true # default true
version: "7.1.2" # default v7.1.2
options:
external_link: false #这个已经有了,修改即可

对于mermaid,则需要在themes/next/layout/_partial/footer.swig中加入:

1
2
3
4
5
6
7
8
{% if theme.mermaid.enable %}
<script src='https://unpkg.com/mermaid@{{ theme.mermaid.version }}/dist/mermaid.min.js'></script>
<script>
if (window.mermaid) {
mermaid.initialize({theme: 'forest'});
}
</script>
{% endif %}

13.增加文章置顶功能

修改 hero-generator-index 插件,把文件: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
'use strict';
var pagination = require('hexo-pagination');
module.exports = function(locals){
var config = this.config;
var posts = locals.posts;
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';
return pagination('', posts, {
perPage: config.index_generator.per_page,
layout: ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true
}
});
};

在文章中添加 top 值,数值越大文章越靠前,如

1
2
3
4
5
6
7
8
---
title: 解决Charles乱码问题
date: 2017-05-22 22:45:48
tags: 技巧
categories: 技巧
copyright: true
top: 100
---

14.参考链接

  1. https://hoxis.github.io/hexo-next-daovoice.html
  2. https://blog.csdn.net/blue_zy/article/details/79071414
  3. https://www.jianshu.com/p/61abc6c43220
  4. https://blog.csdn.net/blue_zy/article/details/79071414
  5. https://blog.csdn.net/lvonve/article/details/80200348
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!