前言
最近博客更新了一下主题,由之前的yilia主题更新成集成功能更齐全的next。借此也把搭建博客和优化主题的过程记录一下。
搭建博客
搭建环境
Node.js
下载安装: https://nodejs.org/en/download/git
下载安装:https://git-scm.com/downloadshexo
下载安装,搭建博客:在桌面新建
test
目录,进入test
目录右键选择git Bash Here
在命令行界面初始化hexo
(这里以test
文件夹为例,文中后续都是以test
文件夹作为根目录)1
$ hexo init
更新下载依赖包
1
$ hexo install
启动本地服务
1
$ hexo s
浏览器访问:http://localhost:4000
可以看到博客已经搭建在本地。
部署到github
上
- 创建
github
账号:https://github.com。(注册的邮箱一定要验证) - 新建一个名为
<github用户名>.github.io
的仓库,比如说,如果你的github
用户名是mygithub
,那么你就新建mygithub.github.io
的仓库。 安装
hexo-deployer-git
插件。在命令行(即Git Bash)运行以下命令即可:1
$ npm install hexo-deployer-git --save
配置
ssh
检查是否有
ssh
1
$ cd ~/. ssh #检查本机已存在的ssh密钥(No such file or directory 说明你是第一次使用git。)
生成
ssh
1
$ ssh-keygen -t rsa -C "邮件地址"(注册github时的邮箱)
然后连续3次回车,复制密钥文件内容(路径形如
C:\Users\Administrator\.ssh\id_rsa.pub
),粘贴到New SSH Key
即可。打开你的github
主页,进入个人设置 ->SSH and GPG keys
->New SSH key
:
测试是否成功配置
ssh
1
$ ssh -T git@github.com
提示 Are you sure you want to continue connecting (yes/no)?,输入
yes
返回 “You’ve successfully authenticated” 即成功:修改
_config.yml
(在test
目录下)。文件末尾添加下面代码:1
2
3
4
5
6# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com:<Github账号名称>/<Github账号名称>.github.io.git
branch: master推送到
GithubPages
。在命令行(即Git Bash)依次输入以下命令, 返回 INFO Deploy done: git 即成功推送:1
$ hexo d -g
至此,
Hexo
博客已经搭建在GithubPages
, 域名为https://<Github账号名称>.github.io
主题选择和优化
主题选择
这里我选择的是
next
主题,在/test
目录下载主题文件1
$ git clone https://github.com/iissnan/hexo-theme-next themes/next
修改
/test
目录下的_config.yml
文件,更改theme
字段,为主题文件夹的名称1
$ theme: next
主题基本配置
设置 语言
- 修改
/test
目录下的_config.yml
文件,选择语言1
language: zh-Hans(默认为英文,zh-Hans为中文简体)
设置 作者昵称
/test
目录下的_config.yml
文件,设置author
为你的昵称。1
author : <你的昵称>
设置 站点描述
- 修改
/test
目录下的_config.yml
文件,设置description
字段为你的站点描述。1
description : 站点描述可以是你喜欢的一句签名:)
设置 侧边栏社交链接
修改
/test
目录下的_config.yml
文件,设置social
字段。1
2
3
4social:
GitHub: https://github.com/your-user-name || github
Twitter: https://twitter.com/your-user-name || twitter
Weibo: http://weibo.com/your-user-name || weibo如果只想显示图标可以设置
social_icons
字段1
2
3
4social_icons:
enable: true
icons_only: true
transition: true
设置「背景动画」
- 修改
/test
目录下的_config.yml
文件,有4款背景动画,选择你喜欢的设置为true
即可1
2
3
4
5
6
7
8
9
10
11# Canvas-nest
canvas_nest: true
# three_waves
three_waves: false
# canvas_lines
canvas_lines: false
# canvas_sphere
canvas_sphere: false
设置「动画效果」
- 修改
/test
目录下的_config.yml
文件,设置motion
字段。1
2motion
enable: true
选择 Scheme
next
提供了几种主题模式可供切换,选择一款你喜欢的
/test/themes/next
目录下的_config.yml
文件,全局查找scheme
,选择一款打开注释1
2
3
4#scheme: Muse
#scheme: Mist
#scheme: Pisces
scheme: Gemini
配置 菜单栏
修改
/test/themes/next
目录下的_config.yml
文件,menu
关键字下面的配置就是菜单栏,next
的默认菜单配置
一般来说我们都会有自己的定制。给出我的配置以供参考1
2
3
4
5
6
7menu:
home: / || home
about: /about/ || user
tags: /tags/ || tags
categories: /categories/ || th
archives: /archives/ || archive
music: /music/ || music|| 后面的内容为菜单栏的图标icon,就是Font Awesome 图标名字
配置优化 用户图像
修改
/test/themes/next
目录下的_config.yml
文件,设置avatar
为一张图片,把你喜欢的图片下载并放到/test/themes/next/source/images
文件夹中1
avatar: /images/<图片名字>.jpg
找到主题配置文件:
/test/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.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);
}
}
添加「标签」页面
- 在
/test/source
目录下新建tags
文件夹,在/test/source/tags
下新建index.md
,内容为1
2
3
4---
title: 标签
type: "tags"
---
添加「分类」页面
- 在
/test/source
目录下新建categories
文件夹,在/test/source/categories
下新建index.md
,内容为1
2
3
4---
title: 标签
type: "categories"
---
设置 字体
- 由于引用国外字体镜像较慢,所以nexT 开放了 5 个特定范围的字体设定来解决问题。修改
/test/themes/next
目录下的_config.yml
文件。找到font
关键字,替换它下面的内容为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
31font:
enable: true
# 外链字体库地址,例如 //fonts.googleapis.com (默认值)
host:
# 全局字体,应用在 body 元素上
global:
external: true
family: Monda
# 标题字体 (h1, h2, h3, h4, h5, h6)
headings:
external: true
family: Roboto Slab
# 文章字体
posts:
external: true
family:
# Logo 字体
logo:
external: true
family: Lobster Two
size: 24
# 代码字体,应用于 code 以及代码块
codes:
external: true
family: PT Mono
设置代码高亮
- 修改
/test/themes/next
目录下的_config.yml
文件。修改highlight_theme
关键字,共有5款主题供你选择1
highlight_theme: normal/night/night blue/night bright/night eighties
设置友情链接
- 修改
/test/themes/next
目录下的_config.yml
文件。找到Blog rolls
进行设置1
2
3
4
5
6
7# Blog rolls
links_icon: link (icon图标)
links_title: 友情链接 (文字描述)
#links_layout: block (设置链接是一行展示一条)
links_layout: inline (设置链接是一行展示多条)
links:
test: http://macshuo.com/ (链接的名字和地址)
设置博客置顶
安装插件
1
2$ npm uninstall hexo-generator-index --save
$ npm install hexo-generator-index-pin-top --save设置置顶标志:打开
/test/themes/*/layout/_macro/post.swig
在<div class="post-meta">
下方插入代码:1
2
3
4
5{% if post.top %}
<i class="fa fa-thumb-tack"></i>
<font color=808080>置顶</font>
<span class="post-meta-divider">|</span>
{% endif %}然后在需要置顶的文章的Front-matter中加上top即可:
1
2
3
4
5---
title: 2018
date: 2018-10-25 16:10:03
top: 100
---
设置和优化打赏
- 点击进入在线生成二维码生成自己的微信和支付宝二维码。下载下来保存到
/test/themes/next/source/images
中 修改
/test/themes/next
目录下的_config.yml
文件。找到Reward
关键字进行配置1
2
3
4-Reward
reward_comment: 谢谢打赏,好人一生平安 (打赏的描述)
wechatpay: /images/weixin.png (微信二维码)
alipay: /images/zhifubao.png (支付宝二维码)移除打赏抖动,修改
/test/themes/next/source/css/_common/components/post/post-reward.styl
,注释以下代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/* 注释文字闪动函数
#wechat:hover p{
animation: roll 0.1s infinite linear;
-webkit-animation: roll 0.1s infinite linear;
-moz-animation: roll 0.1s infinite linear;
}
#alipay:hover p{
animation: roll 0.1s infinite linear;
-webkit-animation: roll 0.1s infinite linear;
-moz-animation: roll 0.1s infinite linear;
}
#bitcoin:hover p {
animation: roll 0.1s infinite linear;
-webkit-animation: roll 0.1s infinite linear;
-moz-animation: roll 0.1s infinite linear;
}
*/
每篇文章后添加结束标语
新建文件,在
/test/themes/next/layout/_macro
中新建passage-end-tag.swig
文件,添加代码至该文件中:1
2
3
4
5<div>
{% if not is_index %}
<div style="text-align:center;color: #ccc;font-size: 15px;letter-spacing: 5px;margin-top: 35px;">----------本文结束<i class="fa fa-paw"></i>感谢您的阅读-----------</div>
{% endif %}
</div>修改post.swig,打开
/test/themes/next/layout/_macro/post.swig
文件,在post-body
后,post-footer
前,添加下面内容:1
2
3
4
5<div>
{% if not is_index %}
{% include 'passage-end-tag.swig' %}
{% endif %}
</div>修改_config,打开
/test/themes/next/
下的_config.yml
,在末尾添加:1
2
3# 文章末尾添加“本文结束”标记
passage_end_tag:
enabled: true
添加转载协议声明
打开
/test/themes/next/
下的_config.yml
,找到Declare license on posts
,配置1
2
3
4
5# Declare license on posts
post_copyright:
enable: true
license: CC BY-NC-SA 3.0
license_url: https://creativecommons.org/licenses/by-nc-sa/3.0/打开
/test
下的_config.yml
,找到URL
,配置1
2# URL
url: <你的github博客地址>
主题优化
实现fork me on github
- 选择样式
GitHub Ribbons
:https://github.blog/2008-12-19-github-ribbons/ - 修改图片跳转链接,将
<a href="https://github.com/you">
中的链接换为自己Github链接 - 打开
/test/themes/next/layout/_layout.swig
文件,把代码复制到<div class="headband"></div>
下面。1
2
3<a href="https://github.com/tmgycsz/tmgycsz.github.io" class="forkme" target="_blank">
<img style="position: absolute; right: 0px;" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_white_ffffff.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1">
</a>
添加点击爱心效果
在
/test/themes/next/source/js/src
文件夹下创建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);
在
/test/themes/next/layout/_layout.swig
文件末尾添加:1
2
3<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
`
添加网页崩溃欺骗特效
在
/test/themes/next/source/js/src
文件夹下创建crash_cheat.js
,添加代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<!--崩溃欺骗-->
var OriginTitle = document.title;
var titleTime;
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
document.title = '╭(°A°`)╮ 页面崩溃啦 ~';
clearTimeout(titleTime);
}
else {
document.title = '(ฅ>ω<*ฅ) 噫又好了~' + OriginTitle;
titleTime = setTimeout(function () {
document.title = OriginTitle;
}, 2000);
}
});在
/test/themes/next/layout/_layout.swig
文件末尾添加:1
2
3<!--崩溃欺骗-->
<script type="text/javascript" src="/js/src/crash_cheat.js"></script>
`
添加访问不蒜子统计
- 打开
/test/themes/next/
下的_config.yml
,找到busuanzi_count
,配置1
2
3
4
5
6
7
8busuanzi_count:
enable: true
total_visitors: true (总的访客人数)
total_visitors_icon: user (访客人数图标)
total_views: true (总的访问量)
total_views_icon: eye (访问量图标)
post_views: true (文章总阅读次数)
post_views_icon: eye(文章总阅读图标)
添加评论系统Valine
- 请先注册LeanCloud, 注册成功进入应用列表页面,创建应用。进入刚刚创建的应用,选择左下角的
设置
>应用Key
,然后就能看到你的APP ID
和APP Key
了。
- 打开
/test/themes/next/
下的_config.yml
,找到valine
,配置1
2
3
4
5
6
7
8
9
10valine:
enable: true
appid: dHAGLgRYVcYtd02p3WpBLKiQ-gzGzoHsz
appkey: DtL0hVwsWFyrDdw4QPWhg9zR
notify: false (邮件提醒,回复是否发送到邮箱)
verify: false (回复是否需要验证)
placeholder: Just go go (回复框占位符)
avatar: mm # gravatar style
guest_info: nick,mail,link (评论可以填写的信息)
pageSize: 10 # (每页显示多少条评论)
添加文章字数统计和预计阅读时间
- 打开
/test/themes/next/
下的_config.yml
,找到post_wordcount
,配置1
2
3
4
5
6post_wordcount
item_text: true
wordcount: true
min2read: true
totalcount: true
separated_meta: true`
实现本地搜索
/test
目录下下载插件1
$ npm install hexo-generator-searchdb --save
修改
/test
下的_config.yml
,新增以下内容到任意位置:1
2
3
4
5search:
path: search.xml
field: post
format: html
limit: 10000打开
/test/themes/next/
下的_config.yml
,找到post_wordcount
,配置1
2
3
4
5local_search:
enable: true
trigger: auto (搜索方式:是输入的时候搜索还是回车或搜索键搜索)
# show top n results per article, show all results by setting to -1
top_n_per_article: 1
参考文档
next文档: http://theme-next.iissnan.com/
有什么问题欢迎在下方指出
还有其它优化,持续更新中。。。