Zexin Li

Please keep honest, open, patient, happy and visionary.

0%

hexo+next主题配置之怎么配成我这样

总述

这篇文用来介绍本站点使用的Hexo+Next主题配置及各种相关的依赖配置。

引子

什么是hexo:一个基于Node.js的博客框架
什么是next:hexo的一个主题

步骤

第一步:Node.js,npm环境配置

Windows:移步https://juejin.im/post/5a92b735f265da4e761fea22
MacOS:

1
2
brew install node
brew install npm

第二步:hexo的安装

Windows:这有个坑“所有hexo命令前需要加npx,不然就会:’hexo’ 不是内部或外部命令,也不是可运行的程序或批处理文件。”

1
2
3
4
5
$ npm install hexo-cli -g
$ npx hexo init your-hexo-site
$ cd your-hexo-site
$ npm install
$ npx hexo server

MacOS:

1
2
3
4
5
$ npm install hexo-cli -g
$ hexo init your-hexo-site
$ cd your-hexo-site
$ npm install
$ hexo server

第三步:Next的安装

这里只针对v7.8及以上版本,老的版本教程很多过时了

从源安装Next主题

1
2
3
$ cd your-hexo-site
$ git clone https://github.com/theme-next/hexo-theme-next.git themes/next
$ # next的git repo迁移过了,用官方的

改动站点根目录下的_config.yml,指定使用Next主题

1
theme: next

改动theme/next目录下的_config.yml,更改Next样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#scheme: Muse
#scheme: Mist
#scheme: Pisces
scheme: Gemini

# 设置站点配置语言
language: zh-CN #简体中文,英文是en

# 改作者名
copyright: your-name

# 自定义配置文件路径,注意!此处路径是在Hexo目录下而非theme/next目录下
custom_file_path:
#head: source/_data/head.swig
#header: source/_data/header.swig
#sidebar: source/_data/sidebar.swig
#postMeta: source/_data/post-meta.swig
#postBodyEnd: source/_data/post-body-end.swig
#footer: source/_data/footer.swig
#bodyEnd: source/_data/body-end.swig
variable: source/_data/variables.styl
#mixin: source/_data/mixins.styl
style: source/_data/styles.styl

自定义样式文件

variables.styl内容是(值得一提的是,原作者的设置也在这里做出了改动)-> 这个蓝色不能覆盖左上角Blog名称,非常难看

也可以不改variables.styl 采用默认的next配色

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Custom styles.

/* 背景设置 */
/* 给页面内的所有元素设置一个带透明度的白色背景色 */
// 页面的几大部分
[class="container sidebar-position-left "], .main, [class$="page-home"] #footer,
[class$="page-post-detail"] #footer, .page-archive {
background: rgba(255, 255, 255, 0.83);
}
[class="container sidebar-position-left "] .main,
[class$="sidebar-position-left"] #footer, .page-archive .main {
background: transparent;
}
.pagination, .post-block, .comments,
.container .header-inner, .sidebar-inner {
background: rgba(255, 255, 255, 0.45);
}

// 评论区
.gt-comment-content {
background-color: rgba(255, 255, 255, 0.42) ;
box-shadow: 0 2px 2px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.06), 0 1px 5px 0 rgba(0,0,0,0.12);

&:hover {
box-shadow: 0 2px 2px 0 #6190e8b8, 0 3px 1px -2px #6190e8b8, 0 1px 5px 0 #6190e8b8 !important;
}
}

// 显示目录的侧边栏
#sidebar {
background: rgba(255, 255, 255, 0);
}

// 菜单
.menu .menu-item a:hover {
background: #2222223b;
border-bottom-color: transparent;
}
.menu-item-active a {
background: #fbcb3fbd;
border-bottom-color: transparent;
}

// NexT主题特有的选项卡元素
.tabs ul.nav-tabs li.tab.active,
.tabs .tab-content {
background-color: rgba(255, 255, 255, 0.62) !important;
}



/* 页面中某些部分的大小 */
// 归档页面的页脚
.page-archive #footer {
position: inherit;
height: auto;
padding-top: 2.7%;
}

// 详细分类页面和总分类页面的页脚
[class$="sidebar-position-left "] #footer,
[class$="page-post-detail"] #footer {
padding-top: 3.98%
}


// 详细分类页面的主要部分
[class$="sidebar-position-left "] {
padding-bottom: calc(100vh - 89.5vh);
}


/* 侧边栏目录 */
// 普通状态下的超链接状态
.post-toc ol a {
border-bottom-color: transparent;
&:hover {
border-bottom-color: #666;
}
}

// active状态下的超链接样式
.post-toc .nav li[class$="active"]>.nav-link {
border-bottom-color: transparent;
&:hover {
border-bottom-color: #fc6423;
}
}

// 当前正在访问的超链接样式
.post-toc .nav li[class$="active-current"]>.nav-link {
color: white;
background: #337ab7;
border-radius: 3.6px;
display: block;
padding: 5px 7px;
border-bottom-color: transparent;
&:hover {
background: orange;
color: black;
}
}

//
.site-state-item {
border-left: 1px solid #b1b1b1;
}


// 各级目录的显示宽度 - 主要是为了不让边框超出侧边栏
.post-toc .nav .active-current.nav-level-2 a {
width: 303px;
}

.post-toc .nav .active-current.nav-level-3 a {
width: 296px;
}

.post-toc .nav .active-current.nav-level-4 a {
width: 286px;
}

.post-toc .nav .active-current.nav-level-5 a {
width: 276px;
}

.post-toc .nav .active-current.nav-level-6 a {
width: 266px;
}

.post-toc {
width: calc(100% + 17px);
}



/* 标题 */
// 点进文章,文章的主标题
.posts-expand article:only-of-type h1 {
font-size: 37px;
}

// 二级标题(正常显示器分辨率)
// 其实要在左下角和右下角加上一个折角、翻折的效果(一个三角形)
.post-body h2 {
border: 1px solid #ddd;
border-radius: 2px;
font-weight: bold;
height: 48px;
line-height: 40px;
margin: 20px -42.3px 15px -48px;
padding-left: 1.7%;
position: relative;
background-color: #ecf6f7;
box-shadow: 0 2px 2px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.06), 0 1px 5px 0 rgba(0,0,0,0.12);
}

// 二级标题(移动终端分辨率)
@media (min-width: 768px) and (max-width: 991px) {
.main .post-body h2 {
margin: 20px -23px 15px -28px;
}
}
@media (max-width: 767px) {
.main .post-body h2 {
margin: 20px -15px 15px -18px;
}
}

// 三级标题
.post-body h3 {
border-bottom: 1px solid #cfd8dc!important;
padding-bottom: 0.3em;
}



/* 文章页面内的各种元素 */
// note块元素
.post-body .note {
border-color: #ddd;
background-color: #f9f9f980;
}

// 文章内的普通超链接
.post-body a {
color: #0593d3;
// border-bottom: none;
// 这行会把阅读全文图标干掉,慎用
&:hover {
color: #FF4500;
}
}

//代码块内的超链接
.post-body a code {
color: #0593d3;
border-bottom: none;
&:hover {
color: #FF4500;
}
}

// 分页导航(上/下一篇文章的超链接)
.post-nav-item a {
color: #0593d3;
border-bottom: none;
&:hover {
color: #FF4500;
}
}

// 阅读全文的按钮
.post-button a {
&:hover {
font-weight: bold;
font-size: larger;
background-color: cornsilk;
}
}

// 阅读全文按钮的超链接
.post-button .btn {
border-bottom: 2px solid #555;
}

// 无序列表
.posts-expand .post-body ul li {
list-style: disc;
}

// 有序列表
.posts-expand .post-body ol {
counter-reset: counter;
}
.posts-expand .post-body ol>li {
list-style-type: none;
position: relative;
}
.posts-expand .post-body ol>li:before {
counter-increment: counter;
content: counter(counter);
background: #49b1f5;
color: #fff;
font-size: 12px;
font-weight: 500;
line-height: 1;
text-align: center;
padding: 3px;
height: 13px;
width: 13px;
border-radius: 50%;
position: absolute;
left: -27px;
top: 5px;
}

// fancybox图片
.posts-expand .post-body .fancybox img {
display: block !important;
margin-left: 0px;
cursor: pointer;
cursor: zoom-in;
cursor: -webkit-zoom-in;
}

// 图片底下的描述性文字
.post-body .image-caption, .post-body .figure .caption {
text-align: left;
}

// label块
.post-body .label {
font-weight: bold;
border-radius: .25em;
margin: 0px 3px 0px 3px;
padding: 3px 4px;
}
.post-body .label.primary {
background-color: #ecd6ff;
}
.post-body .label.info {
background-color: #c3e8f9;
}
.post-body .label.success {
background-color: #b2eabb;
}
.post-body .label.default {
background-color: #dcdcdc;
}
.post-body .label.warning {
background-color: #ffe89a;
}
.post-body .label.danger {
background-color: #f9c1ca;
}


// 代码区
.highlight .code pre {
background-color: transparent;
}
.highlight {
background-color: #f9f9f980;
border: 1px solid #ddd;
border-radius: 2px;
}

// 原样输出的代码块
pre {
background: #f9f9f980;
border: 1px solid #ddd;
border-radius: 2px;
}
pre code {
border: transparent;
}

// 块引用
blockquote {
background-color: #f9f9f980;
border-left-color: #6b8afb;
}

// 表格奇数行
.highlight table>tbody>tr {
&:nth-of-type(odd) {
background-color: transparent;
}
}

// 表格每一行鼠标经过时的颜色
table>tbody>tr {
&:hover {
background-color: #FFFFF0;
}
}

// 表格的单元格
table td, table th {
border-right: 1px solid #dcdcdc;
}

// 侧边栏头部
.site-meta {
background: #0072E3;
}

// 文章底部的标签
.posts-expand .post-tags a {
background: #e6e6e6;
padding: 3px 4px;
border-radius: 3px;
color: #2ca6cb;
border-bottom: none;
&:hover {
color: white;
background: #2ca6cb;
}
}

// 分割线
hr {
margin: 0px 0px 0px 0px;
height: 1px;
border: none;
background-image: none;
}

// code代码块
code {
background: #dcdcdc;
margin: 0 2px 0 2px;
}

// 选项卡被选中的那一项
.tabs ul.nav-tabs li.tab.active a {
font-size: 19px;
}

// 浏览器滚动条
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-thumb {
background: #49b1f5;
}
::-webkit-scrollbar-track {
background-color: transparent;
}

styles.styl的内容是(用于调整主题显示宽度)

1
2
3
4
$content-desktop         = 97.7%
$content-desktop-large = 97.7%
$content-desktop-largest = 97.7%

自定义背景图

根据自定义样式原作者ExtremeGTR的博客描述:
“自定义背景图的配置还是没法解耦出来,或许只是我想不到办法而已,我的自定义配置仍然要写到NexT的js文件里,但还是有一点点变化的。
自定义[]的配置要写到\themes\next\source\js\next-boot.js里。”
刷新界面随机加载背景图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$(document).ready(function () {
// 在最开头或者任意一处加入我们的功能:随机选取1个背景图并加载的代码
var backgroundImgUrls = [
"图片地址1",
"图片地址2",
"图片地址3",
"图片地址4",
"图片地址5",
"图片地址6",
// ...
"图片地址n"
];
var randomIndex = Math.floor(Math.random() * (backgroundImgUrls.length + 1));
if (randomIndex == backgroundImgUrls.length) {
$("body").css({"background-color":"white"});
} else {
var background_image = "url(" + backgroundImgUrls[randomIndex] + ")";
$("body").css({"background":background_image, "background-attachment":"fixed", "background-size":"cover"});
}
//...NexT原有的代码
}

点击出现小红心

\theme\*\source\js下新建click.js复制以下代码

1
!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);

\themes\*\layout\_layout.swig文件末尾添加

1
2
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/click.js"></script>

改动主题配置文件_config.yml

激活gitalk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Multiple Comment System Support
comments:
# Available values: tabs | buttons
style: tabs
# Choose a comment system to be displayed by default.
# Available values: changyan | disqus | disqusjs | gitalk | livere | valine
active: gitalk
# Setting `true` means remembering the comment system selected by the visitor.
storage: true
# Lazyload all comment systems.
lazyload: false
# Modify texts or order for any navs, here are some examples.
nav:
#disqus:
# text: Load Disqus
# order: -1
#gitalk:
# order: -2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Gitalk
# For more information: https://gitalk.github.io, https://github.com/gitalk/gitalk
gitalk:
enable: true
github_id: # GitHub repo owner
repo: # Repository name to store issues
client_id: # GitHub Application Client ID
client_secret: # GitHub Application Client Secret
admin_user: # GitHub repo owner and collaborators, only these guys can initialize gitHub issues
distraction_free_mode: true # Facebook-like distraction free mode
# Gitalk's display language depends on user's browser or system environment
# If you want everyone visiting your site to see a uniform language, you can set a force language value
# Available values: en | es-ES | fr | ru | zh-CN | zh-TW
language: en

第四步:插件配置

这里是hexo插件全家桶的页面,以下推荐:

hexo-deploy-git插件配置

用途:hexo deploy一键部署

1
$ npm install hexo-deployer-git --save

在Hexo站点下的_config.yml文件添加

1
2
3
4
5
6
7
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: https://github.com/your-id/your-id.github.io
branch: master
message:

symbols_count_time插件配置

用途:计算字数和阅读时间

1
$ npm install hexo-symbols-count-time

在Hexo站点下的_config.yml文件添加

1
2
3
4
5
6
7
8
9
symbols_count_time:
symbols: true
time: true
total_symbols: true
total_time: true
exclude_codeblock: false
awl: 4
wpm: 275
suffix: "mins."

hexo-generator-searchdb插件配置

用途:站点内搜索

1
$ npm install hexo-generator-searchdb --save

!注意此处不要-g全局安装

在Hexo站点下的_config.yml文件添加

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

在Next主题的_config.yml文件修改local search
把enable: false修改为true

hexo-helper-live2d插件配置

用途:萌萌哒

1
$ npm install hexo-helper-live2d

可以上github找找喜欢的模型,以下使用指代模型名称:
在Hexo站点下的_config.yml文件添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
live2d:
enable: true
scriptFrom: local
pluginRootPath: live2dw/
pluginJsPath: lib/
pluginModelPath: assets/
tagMode: false
log: false
model:
use: live2d-widget-model-<ModelName>
display:
position: right
width: 150
height: 300
mobile:
show: true

在Hexo站点根目录下新建文件夹live2d_models
然后在node_modules文件夹下找到下载好的模型,拷贝到live2d_models

1
2
3
4
$ cd <hexo-root-path>
$ mkdir live2d_models
$ npm install live2d-widget-model-<ModelName>
$ cp -r node_modules/live2d-widget-model-<ModelName> live2d_models

travis-CI配置

详见https://hexo.io/docs/github-pages.html,作者没配直接本地hexo generate && hexo deploy

参考

[1] https://juejin.im/post/5a92b735f265da4e761fea22
[2] https://theme-next.iissnan.com/getting-started.html
[3] https://hexo.io/zh-cn/index.html
[4] https://extremegtr.github.io/2017/09/27/Customize-NexT-Gemini-theme/
[5] https://hexo.io/zh-cn/docs/one-command-deployment.html
[6] https://hexo.io/docs/github-pages.html
[7] https://hexo.io/plugins/

Welcome to my other publishing channels