使用Hexo+Github搭建自己的博客(进阶)

主题的配置:这里以NexT主题作为题材

安装NexT

在其文件夹中鼠标右键,点击Git Base Here

输入命令:git clone https://github.com/iissnan/hexo-theme-next themes/next

1
2
3
4
5
6
7
Administrator@yd MINGW64 /d/GitHub/hexo
$ git clone https://github.com/iissnan/hexo-theme-next themes/next
Cloning into 'themes/next'...
remote: Enumerating objects: 12033, done.
remote: Total 12033 (delta 0), reused 0 (delta 0), pack-reused 12033
Receiving objects: 100% (12033/12033), 12.95 MiB | 824.00 KiB/s, done.
Resolving deltas: 100% (6960/6960), done.

启用主题

在站点目录中(hexo),打开配置文件_config.yml,修改theme:next

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next

验证主题

端口号被占用还是得先修改端口号,然后启动服务

1
2
3
4
5
6
7
8
9
10
11
Administrator@yd MINGW64 /d/GitHub/hexo
$ hexo s
INFO Start processing
WARN ===============================================================
WARN ========================= ATTENTION! ==========================
WARN ===============================================================
WARN NexT repository is moving here: https://github.com/theme-next
WARN ===============================================================
WARN It's rebase to v6.0.0 and future maintenance will resume there
WARN ===============================================================
INFO Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

主题设定

在next主题目录下的_config.yml文件中将scheme设定为Pisces

1
2
3
4
5
# Schemes
#scheme: Muse
#scheme: Mist
scheme: Pisces
#scheme: Gemini

语言设定

在站点根目录下修改配置文件_cofig.yml中的languagezh-Hans(简体中文)

1
2
3
4
5
6
7
8
# Site
title: Hexo
subtitle:
description:
keywords:
author: John Doe
language: zh-Hans
timezone:

修改菜单项

在主题目录下修改配置文件_cofig.yml中的menu,增添一个something(注:千万不要在这设置中文,后面的值那是查找文件的地方!若你的站点运行在子目录中,请将链接前缀的 / 去掉)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#在子目录(例如domain.tld/blog)中运行站点时,从链接值(/archives -> archives)中删除前导斜杠。
#用法:`Key:/ link / ||icon`
#Key是菜单项的名称。 如果此菜单的翻译将在语言中找到 - 此翻译将被加载; 如果不是 - 将使用密钥名称。
#关键是案例敏感。
#` ||'delimeter之前的值是目标链接。
#在|||`delimeter之后的值是FontAwesome图标的名称。
#如果未指定图标(带或不带分隔符),则将加载问题图标。
menu:
home: / || home
#about: /about/ || user
#tags: /tags/ || tags
#categories: /categories/ || th
archives: /archives/ || archive
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
#commonweal: /404/ || heartbeat
something: /something

这些配置都要与你主题目录下的languages文件中对应的yml文档里配置相关联。比如你在站点根目录中的配置文件设置languagezh-Hans,那么就要进入到主题目录下的languages文件中修改zh-Hans.yml,这样才能显示出菜单项新增的中文内容(以something为例子)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
title:
archive: 归档
category: 分类
tag: 标签
schedule: 日程表

author: 博主

menu:
home: 首页
archives: 归档
categories: 分类
tags: 标签
about: 关于
search: 搜索
schedule: 日程表
sitemap: 站点地图
commonweal: 公益404
something: 什么事

设置侧栏位置

修改主题目录下sidebarposition

1
2
3
4
sidebar:
# Sidebar Position, available value: left | right (only for Pisces | Gemini).
#position: left
position: right

设置头像

在站点根目录下载配置文件中新增avatar,值设置为头像的链接地址。地址可以是网络地址,也可以是本地地址(放置在source/images/目录下)

1
2
3
4
# Sidebar Avatar
# in theme directory(source/images): /images/avatar.gif
# in site directory(source/uploads): /uploads/avatar.gif
avatar: /images/user.jpg

设置文章代码主题

在主题目录下修改配置文件highlight_theme,默认值为nomal。可以设置为night

1
2
3
4
5
# Code Highlight theme
# Available value:
# normal | night | night eighties | night blue | night bright
# https://github.com/chriskempson/tomorrow-theme
highlight_theme: normal

添加标签页面

前面通过修改next主题下的_config.yml文件中的menu选项,可以在主页面的菜单栏添加标签选项,但是此时点击标签,跳转的页面会显示page not found。此时我们要新建一个页面

1
2
3
Administrator@yd MINGW64 /d/GitHub/hexo
$ hexo new page tags
INFO Created: D:\GitHub\hexo\source\tags\index.md

在新建的index.md文件中添加type: "tags"

1
2
3
title: tags
date: 2019-03-19 07:31:27
type: tags

当要为某一篇文章添加标签,只需在blog/source/_post目录下的具体文章的tags中添加标签即可,如:

1
2
3
4
title: '你好,Hexo'
date: 2019-03-19 07:31:27
type: [hexo,github,npm,基础]
categories: 搭建博客

添加关于我页面,步骤和以上差不多

1
2
3
Administrator@yd MINGW64 /d/GitHub/hexo
$ hexo new page about
INFO Created: D:\GitHub\hexo\source\about\index.md

在新建的index.md文件中添加如下内容

1
2
3
4
5
6
7
8
9
---
title: about
date: 2019-03-19 07:27:47
---
## 关于我

一个爱学习的好宝宝~~~

email:994300880@qq.com

在首页添加github导航条

点击这里选择需要的样式,然后将代码复制到themes/next/layout/_layout.swig

1
2
3
4
5
6
7
8
9
10
11
<div class="{{ container_class }} {% block page_class %}{% endblock %}">
<div class="headband"></div>
<!-- 添加到这里 -->
<a href="https://github.com/you">
<img width="149"
height="149"
src="https://github.blog/wp-content/uploads/2008/12/forkme_right_gray_6d6d6d.png?resize=149%2C149"
class="attachment-full size-full"
alt="Fork me on GitHub"
data-recalc-dims="1"></a>
<!-- 结束 -->

并将href改为你的github地址

实现点击出现桃心效果

创建js文件:在/themes/next/source/js/src下新建文件clicklove.js,接着把该链接下的代码拷贝粘贴到clicklove.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);

修改_layout.swig:在\themes\next\layout\_layout.swig文件末尾添加:

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

修改作者头像并旋转

打开\themes\next\source\css\_common\components\sidebar\sidebar-author.styl,在里面添加如下代码:

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
.site-author-image {
display: block;
margin: 0 auto;
padding: $site-author-image-padding;
max-width: $site-author-image-width;
height: $site-author-image-height;
border: $site-author-image-border-width solid $site-author-image-border-color;

/* 头像圆形 */
border-radius: 80px;
-webkit-border-radius: 80px;
-moz-border-radius: 80px;
box-shadow: inset 0 -1px 0 #333sf;

/* 设置循环动画 [animation: (play)动画名称 (2s)动画播放时长单位秒或微秒
(ase-out)动画播放的速度曲线为以低速结束
(1s)等待1秒然后开始动画 (1)动画播放次数(infinite为循环播放) ]*/


/* 鼠标经过头像旋转360度 */
-webkit-transition: -webkit-transform 1.0s ease-out;
-moz-transition: -moz-transform 1.0s ease-out;
transition: transform 1.0s ease-out;
}

img:hover {
/* 鼠标经过停止头像旋转
-webkit-animation-play-state:paused;
animation-play-state:paused;*/

/* 鼠标经过头像旋转360度 */
-webkit-transform: rotateZ(360deg);
-moz-transform: rotateZ(360deg);
transform: rotateZ(360deg);
}

/* Z 轴旋转动画 */
@-webkit-keyframes play {
0% {
-webkit-transform: rotateZ(0deg);
}
100% {
-webkit-transform: rotateZ(-360deg);
}
}
@-moz-keyframes play {
0% {
-moz-transform: rotateZ(0deg);
}
100% {
-moz-transform: rotateZ(-360deg);
}
}
@keyframes play {
0% {
transform: rotateZ(0deg);
}
100% {
transform: rotateZ(-360deg);
}
}

在网站底部加上访问量

打开\themes\next\layout\_partials\footer.swig文件,在类copyright前(在第一行)加上:

1
2
3
4
5
6
<!-- 统计JS -->
<script async
src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<!-- 结束 -->
<div class="copyright">
......

然后在合适的位置添加显示统计的代码(位置还是上述这个文件)

1
2
3
4
5
6
7
<!-- 合适的位置添加显示统计 -->
<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>
<!-- 统计结束 -->

找到 \themes\next\layout\_partials\下面的footer.swig文件,打开会发现,如下图的语句:

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
<!-- 统计JS -->
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
<!-- 结束 -->

<!-- 合适的位置添加显示统计 -->
<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>
<!-- 统计结束 -->

<div class="copyright">{#
#}{% set current = date(Date.now(), "YYYY") %}{#
#}&copy; {% if theme.footer.since and theme.footer.since != current %}
{{ theme.footer.since }} &mdash; {% endif %}{#
#}<span itemprop="copyrightYear">{{ current }}</span>
<span class="with-love">
<i class="fa fa-{{ theme.footer.icon }}"></i>
</span>
<!-- 第一个框 -->
<span class="author" itemprop="copyrightHolder">
{{ theme.footer.copyright || config.author }}
</span>
<!-- 结束 -->

{% if theme.post_wordcount.totalcount %}
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-area-chart"></i>
</span>
{% if theme.post_wordcount.item_text %}
<span class="post-meta-item-text">{{ __('post.totalcount') }}&#58;</span>
{% endif %}
<span title="{{ __('post.totalcount') }}">{#
#}{{ totalcount(site, '0,0.0a') }}{#
#}</span>
{% endif %}
</div>
<!-- 第二个框 -->
{% if theme.footer.powered %}
<div class="powered-by">{#
#}{{ __('footer.powered', '<a class="theme-link" target="_blank"
href="https://hexo.io">Hexo</a>') }}{#
#}</div>
{% endif %}
<!-- 结束 -->

{% if theme.footer.powered and theme.footer.theme.enable %}
<span class="post-meta-divider">|</span>
{% endif %}

<!-- 第三个框 -->
{% if theme.footer.theme.enable %}
<div class="theme-info">{#
#}{{ __('footer.theme') }} &mdash; {#
#}<a class="theme-link" target="_blank"
href="https://github.com/iissnan/hexo-theme-next">{#
#}NexT.{{ theme.scheme }}{#
#}</a>{% if theme.footer.theme.version %} v{{ theme.version }}{% endif %}{#
#}</div>
{% endif %}
<!-- 结束 -->

{% if theme.footer.custom_text %}
<div class="footer-custom">{#
#}{{ theme.footer.custom_text }}{#
#}</div>
{% endif %}

第一个框 是下面侧栏的“日期❤ XXX”

如果想像我一样加东西,一定要在双大括号外面写。如:xxx,当然你要是想改彻底可以变量都删掉,看个人意愿。

第二个,是图一当中 “由Hexo驱动” 的Hexo链接,先给删掉防止跳转,如果想跳转当然也可以自己写地址,至于中文一会处理。注意删除的时候格式不能错,只把标签这部分删除即可,留着两个单引号’’,否则会出错哦。

第三个框也是最后一个了,这个就是更改图一后半部分“主题-Next.XX”,这个比较爽直接将..都删掉,同样中文“主题”一会处理,删掉之后在上一行 ‘-’后面可以随意加上你想显示的东西,不要显示敏感信息哟,请自重。

接下来,处理剩余的中文信息。找到这个地方\themes\next\languages\ 下面的语言文件zh-Hans.yml(这里以中文为例,有的习惯用英文的配置文件,道理一样,找对应位置即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
counter:
tag_cloud:
zero: 暂无标签
one: 目前共计 1 个标签
other: "目前共计 %d 个标签"

categories:
zero: 暂无分类
one: 目前共计 1 个分类
other: "目前共计 %d 个分类"

archive_posts:
zero: 暂无日志。
one: 目前共计 1 篇日志。
other: "目前共计 %d 篇日志。"

看到了吧,这个就是传值传过去的,你想显示什么就在这里面大肆的去改动吧。其实在第二个框中,就可以把值都改掉,不用接受传值的方式,完全自己可以重写。不过我不建议那样做,因为传值这样只要是后续页面需要这几个值那么就都会通过取值去传过去,要是在刚才footer文件中直接写死,后续不一定哪个页面需要传值,但是值为空了或者还是原来的,可就尴尬了。所以还是这样改动吧。