Hexo安装Next主题

基于github pages完成博客搭建后,已经可以正常发文了,但是文字展示不美观,本文展示如何在Linux为Hexo博客安装Next主题,并进行详细配置

安装Next主题

下载主题

在博客的目录下,运行以下命令获得Next主题

1
git clone https://github.com/theme-next/hexo-theme-next themes/next

更新主题

运行以下命令即可更新主题,但这会覆盖原来的自定义修改

1
cd themes/next;git pull

配置Next主题

配置博客涉及两个配置文件,一个路径是[myblog]/_config.yml[myblog]/theme/next/_config.yml,前一个是站点配置,后一个是主题配置。下面就分别就这两个文件进行配置:

配置站点_config.yml

打开文件[myblog]/_config.yml,每个配置项按如下配置

配置站点基本信息

1
2
3
4
5
6
7
8
# Site
title: 年轻人起来冲
subtitle: ''
description: '害怕失败是本能,勇敢面对才是本事'
keywords:
author: 绍桂
language: zh-CN
timezone: 'Asia/Shanghai'

主题配置

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

URL配置

1
2
3
4
5
6
7
8
9
# URL
## If your site is put in a subdirectory, set url as 'http://example.com/child' and root as '/child/'
url: https://shaogui.life/
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks

翻页配置

1
2
3
4
# Pagination
## Set per_page to 0 to disable pagination
per_page: 8
pagination_dir: page

配置主题_config.yml

根据 Hexo 官方的推荐,不要直接修改主题的配置文件

The file should be placed in your site folder, both yml and json are supported. theme inside _config.yml must be configured for Hexo to read _config.[theme].yml

而是将配置文件复制到和[myblog]/_config.yml同目录下,并命名为:_config.next.yml,在该文件上填写自己需要自定义的内容

网站logo配置

1
2
3
4
5
6
7
favicon:
small: /images/favicon-16x16-next.png
medium: /images/favicon-32x32-next.png
apple_touch_icon: /images/apple-touch-icon-next.png
safari_pinned_tab: /images/logo.svg
#android_manifest: /images/manifest.json
#ms_browserconfig: /images/browserconfig.xml

主题风格配置

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

菜单栏配置

1
2
3
4
5
6
7
8
9
menu:
home: / || fa fa-home
categories: /categories/ || fa fa-th
tags: /tags/ || fa fa-tags
archives: /archives/ || fa fa-archive
about: /about/ || fa fa-user
#schedule: /schedule/ || fa fa-calendar
#sitemap: /sitemap.xml || fa fa-sitemap
#commonweal: /404/ || fa fa-heartbeat

注:||后面指的是图标,各个菜单实际显示的是中文,中英文对应配置文件位于\themes\next\languages\zh-CN.yml中,如果有其他菜单项可以在该文件配置其中文

文章分类及标签[1]

菜单栏中categoriestagsabout点击后提示找不到页面,这时需要使用以下命令新建这三个页面

1
2
3
hexo new page categories
hexo new page tags
hexo new page about

运行三个命令后,均在source目录下生成相应的文件夹,此时菜单栏中可以打开这些项,但是没有内容,需要配置各自的index.md

打开source/categories/index.md,添加type项

1
2
3
4
5
---
title: categories
date: 2021-02-18 21:44:21
type: categories
---

打开source/tags/index.md,添加type项

1
2
3
4
5
---
title: tags
date: 2021-02-18 21:44:21
type: tags
---

编辑文章时,在开头加入categoriestags项即刻对文章进行分类及打标签

1
2
3
4
title: xxx
date: 2020-06-01 23:47:44
tags: [xx,xx]
categories: xx

顶部加载条

安装插件到Next主题

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

开启进度条

1
2
3
4
5
6
7
8
9
# Progress bar in the top during page loading.
# Dependencies: https://github.com/theme-next/theme-next-pace
# For more information: https://github.com/HubSpot/pace
pace:
enable: true
# Themes list:
# big-counter | bounce | barber-shop | center-atom | center-circle | center-radar | center-simple
# corner-indicator | fill-left | flat-top | flash | loading-bar | mac-osx | material | minimal
theme: minimal

添加访问量

修改themes/next/layout/_partials/footer.swig文件

1
2
3
4
5
6
7
<div class="powered-by">
{%- set next_site = 'https://theme-next.org' %}
{%- if theme.scheme !== 'Gemini' %}
{%- set next_site = 'https://' + theme.scheme | lower + '.theme-next.org' %}
{%- endif %}
{{- __('footer.powered', next_url('https://hexo.io', 'Hexo', {class: 'theme-link'}) + ' & ' + next_url(next_site, 'NexT.' + theme.scheme, {class: 'theme-link'})) }}
</div>

以上修改为以下:

1
2
3
4
5
6
7
8
9
10
<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>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_pv">
本站访问量<span id="busuanzi_value_site_pv"></span>
</span>
</div>

返回顶部按钮

1
2
3
4
5
6
back2top:
enable: true
# Back to top in sidebar.
sidebar: true
# Scroll percent label in b2t button.
scrollpercent: true

修改底部标签样式

1
2
# Use icon instead of the symbol # to indicate the tag at the bottom of the post
tag_icon: true

社交链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Social Links
# Usage: `Key: permalink || icon`
# Key is the link label showing to end users.
# Value before `||` delimiter is the target permalink, value after `||` delimiter is the name of Font Awesome icon.
social:
GitHub: https://github.com/WuShaogui || fab fa-github
E-Mail: wshglearn@163.com || fa fa-envelope
#Weibo: https://weibo.com/yourname || fab fa-weibo
#Google: https://plus.google.com/yourname || fab fa-google
#Twitter: https://twitter.com/yourname || fab fa-twitter
#FB Page: https://www.facebook.com/yourname || fab fa-facebook
#StackOverflow: https://stackoverflow.com/yourname || fab fa-stack-overflow
#YouTube: https://youtube.com/yourname || fab fa-youtube
#Instagram: https://instagram.com/yourname || fab fa-instagram
#Skype: skype:yourname?call|chat || fab fa-skype

social_icons:
enable: true
icons_only: false
transition: false

打赏配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# Reward (Donate)
# Front-matter variable (unsupport animation).
reward_settings:
# If true, reward will be displayed in every article by default.
enable: true
animation: true
comment: 坚持原创技术分享,您的支持将鼓励我继续创作!.

reward:
wechatpay: /images/wechatpay.png
alipay: /images/alipay.png
#paypal: /images/paypal.png
#bitcoin: /images/bitcoin.png

Follow_me

1
2
3
4
5
follow_me:
#Twitter: https://twitter.com/username || fab fa-twitter
#Telegram: https://t.me/channel_name || fab fa-telegram
#WeChat: /images/wechat_channel.jpg || fab fa-weixin
RSS: /atom.xml || fa fa-rss

Github角标

1
2
3
4
5
# `Follow me on GitHub` banner in the top-right corner.
github_banner:
enable: true
permalink: https://github.com/WuShaogui
title: Follow me on GitHub

搜索功能

搜索功能需要安装插件实现,使用npm管理器安装以下插件

1
npm install hexo-generator-searchdb --save

在主题配置文件配置以下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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: 1
# Unescape html strings to the readable one.
unescape: false
# Preload the search data when the page loads.
preload: false

扩增Markdown的功能[2]

hexo默认的渲染插件marked功能比较少,比如不支持脚注功能,部署时,脚注会出错,可以通过安装更强的渲染插件实现功能扩展。

1
2
3
4
5
#卸载原始markdown渲染插件
npm un hexo-renderer-marked --save

#安装新的markdown渲染插件
npm i @upupming/hexo-renderer-markdown-it-plus --save

修改站点的配置文件[myblog]/_config.yml,增加以下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Markdown config
markdown_it_plus:
render:
html: true
xhtmlOut: false
breaks: true
linkify: true
typographer: true
quotes: '“”‘’'
plugins:
- markdown-it-abbr
- markdown-it-footnote
anchors:
level: 2
collisionSuffix: 'v'
permalink: true
permalinkClass: header-anchor
permalinkSide: 'left'
permalinkSymbol: ¶

plugins指定需要扩展的功能,支持添加以下功能

  • markdown-it-emoji支持emoji,:cat:→`🐱
  • markdown-it-sub 支持H~2~O→H2O
  • markdown-it-sup 支持X^2^→X2
  • markdown-it-deflist 支持自定义列表
  • markdown-it-abbr支持<abbr>标签
  • markdown-it-footnote支持引入参考文献。emmm就是上标数字,最后附上文献那种
  • markdown-it-ins支持++Inserted++ →Inserted, ~~Del~~ →Del
  • markdown-it-mark支持==marked==→inserted
  • markdown-it-katex支持katex公式
  • markdown-it-toc-and-anchor支持@[toc]生成目录

使用以上扩展功能前,需使用以下命令安装该功能的插件

1
npm install markdown-it-... --save

添加MathJax 数学公式支持

在以上配置基础上,修改两个文件

访客统计

1
2
3
4
5
6
7
8
9
10
# Show Views / Visitors of the website / page with busuanzi.
# Get more information on http://ibruce.info/2015/04/04/busuanzi
busuanzi_count:
enable: true
total_visitors: true
total_visitors_icon: fa fa-user
total_views: true
total_views_icon: fa fa-eye
post_views: true
post_views_icon: fa fa-eye

阅读全文[3]

在文章中,使用标记<!-- more -->实现文章摘要功能,该标记前的内容为文章摘要,显示在首页上

文章置顶[4]

两个步骤完成该操作

更换插件

通过以下命令更换支持置顶的插件

1
2
npm uninstall hexo-generator-index --save
npm install hexo-generator-index-pin-top --save

设置置顶标志

打开文件:/blog/themes/next/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=7D26CD>置顶</font>
<span class="post-meta-divider">|</span>
{% endif %}

往后写文章时,准备置顶的文章需要在Front-matter中添加top: true

文章字数统计

安装字数统计插件

1
sudo npm install hexo-symbols-count-time

配置开启数字统计

1
2
3
4
5
6
7
8
# Post wordcount display settings
# Dependencies: https://github.com/theme-next/hexo-symbols-count-time
symbols_count_time:
separated_meta: true
item_text_post: true
item_text_total: true
awl: 4
wpm: 275

文章内链接样式

原始样式只是加一条下划线,为了更加明显显示链接,通过在文件Blog/themes/next/source/css/_common/components/post/post.styl添加以下样式实现

1
2
3
4
5
6
7
8
9
10
11
// 文章内链接文本样式
.post-body p a{
color: #0593d3; //原始链接颜色
border-bottom: none;
border-bottom: 1px solid #0593d3; //底部分割线颜色
&:hover {
color: #fc6423; //鼠标经过颜色
border-bottom: none;
border-bottom: 1px solid #fc6423; //底部分割线颜色
}
}

文章结束标志

通过3个步骤完成该优化

新建结束文件

在目录Blog\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:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
{% endif %}
</div>

配置结束文件至文章末尾

打开文件Blog\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>

配置主题配置文件

1
2
3
# 文章末尾添加“本文结束”标记
passage_end_tag:
enabled: true

文章添加阴影

打开\themes\next\source\css\_custom\custom.styl,向里面加入以下代码

1
2
3
4
5
6
7
8
// 主页文章添加阴影效果
.post {
margin-top: 60px;
margin-bottom: 60px;
padding: 25px;
-webkit-box-shadow: 0 0 5px rgba(202, 203, 203, .5);
-moz-box-shadow: 0 0 5px rgba(202, 203, 204, .5);
}

文字底部评论

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Valine
# For more information: https://valine.js.org, https://github.com/xCss/Valine
valine:
enable: true
appid: xxxxxxxxxxxxxxxxx # Your leancloud application appid
appkey: xxxxxxxxxxxxxxxxx # Your leancloud application appkey
notify: false # Mail notifier
verify: false # Verification code
placeholder: 留下你的评论吧 # Comment box placeholder
avatar: mm # Gravatar style
guest_info: nick,mail,link # Custom comment header
pageSize: 10 # Pagination size
language: # Language, available values: en, zh-cn
visitor: true # Article reading statistic
comment_count: true # If false, comment count will only be displayed in post page, not in home page
recordIP: true # Whether to record the commenter IP
serverURLs: # When the custom domain name is enabled, fill it in here (it will be detected automatically by default, no need to fill in)
#post_meta_order: 0

版权声明

1
2
3
4
5
6
7
8
9
10
# Creative Commons 4.0 International License.
# See: https://creativecommons.org/share-your-work/licensing-types-examples
# Available values of license: by | by-nc | by-nc-nd | by-nc-sa | by-nd | by-sa | zero
# You can set a language value if you prefer a translated version of CC license, e.g. deed.zh
# CC licenses are available in 39 languages, you can find the specific and correct abbreviation you need on https://creativecommons.org
creative_commons:
license: by-nc-sa
sidebar: true
post: true
language:

侧边栏配置

将菜单栏配置在左边还是右边,以及大小

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
# ---------------------------------------------------------------
# Sidebar Settings
# See: https://theme-next.org/docs/theme-settings/sidebar
# ---------------------------------------------------------------

sidebar:
# Sidebar Position.
position: left
# position: right

# Manual define the sidebar width. If commented, will be default for:
# Muse | Mist: 320
# Pisces | Gemini: 240
width: 240

# Sidebar Display (only for Muse | Mist), available values:
# - post expand on posts automatically. Default.
# - always expand for all pages automatically.
# - hide expand only when click on the sidebar toggle icon.
# - remove totally remove sidebar including sidebar toggle.
display: post

# Sidebar padding in pixels.
padding: 18
# Sidebar offset from top menubar in pixels (only for Pisces | Gemini).
offset: 12
# Enable sidebar on narrow view (only for Muse | Mist).
onmobile: false

# Sidebar Avatar
avatar:
# Replace the default image and set the url here.
url: /images/avatar.gif
# If true, the avatar will be dispalyed in circle.
rounded: true
# If true, the avatar will be rotated with the cursor.
rotated: true

RSS及RSS侧边栏

安装rss插件

运行下面命令安装rss插件

1
npm install --save hexo-generator-feed

配置rss功能

_config.next.yml中找到rss配置项,修改为以下形式

1
rss: /atom.xml

设置头像

网站背景

动态背景

略,暂不设置,需要消耗电脑资源,并且干扰阅读

网站运行时间

打开文件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
<div>
<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date();
function createtime() {
var grt= new Date("03/04/2020 00:00:00");
now.setTime(now.getTime()+250);
days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 ";
document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()",250);
</script>
</div>

添加网易云音乐

获得单曲或者歌单的外链播放器,将代码添加到themes/next/layout/_macro/sidebar.swig下,以下是一个外链播放器

1
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=481853876&auto=1&height=66"></iframe>

添加彩色标签

将标签页的标签打上颜色[5]

新建文件:themes\next\layout\tag-color.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
71
72
73
74
75
76
77
78
79
80
81
<script type="text/javascript">
var alltags = document.getElementsByClassName('tag-cloud-tags');
var tags = alltags[0].getElementsByTagName('a');
for (var i = tags.length - 1; i >= 0; i--) {
var golden_ratio = 0.618033988749895;
var s = 0.5;
var v = 0.999;
var h = golden_ratio + Math.random()*0.8 - 0.5;
var h_i = parseInt(h * 6);
var f = h * 6 - h_i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
var r, g, b;
switch (h_i) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3 :
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
case 5:
r = v;
g = p;
b = q;
break;
default:
r = 1;
g = 1;
b = 1;
}
tags[i].style.background = "rgba("+parseInt(r*255)+","+parseInt(g*255)+","+parseInt(b*255)+","+0.5+")";
}
</script>

<style>
.tag-cloud-tags{
text-align: center;
counter-reset: tags;
}
.tag-cloud-tags a{
display: inline-block;
border: 0px;
border-radius: 10px;
padding: 0px 10px;
margin: 8px;
color: rgba(34, 34, 34, 0.8);

}
/* 文字前添加相应的符号,content后的Unicode可以自定义*/
.tag-cloud-tags a:before{
font-family: 'Font Awesome 5 Free';
content: "\f02b";
font-weight: 900;
}

.tag-cloud-tags a:hover{
box-shadow: 0px 5px 15px 0px rgba(0,0,0,.4);
transform: scale(1.1);
transition-duration: 0.15s;
}
</style>

打开文件:theme/next/layout/page.swig,修改节点<div class="tag-cloud-title">的信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="post-body{%- if page.direction and page.direction.toLowerCase() === 'rtl' %} rtl{%- endif %}">
{%- if page.type === 'tags' %}
<div class="tag-cloud">
<div class="tag-cloud-title">
{{ _p('counter.tag_cloud', site.tags.length) }}
</div>
<div class="tag-cloud-tags">
{{ tagcloud({
min_font : theme.tagcloud.min,
max_font : theme.tagcloud.max,
amount : theme.tagcloud.amount,
color : false,
start_color: theme.tagcloud.start,
end_color : theme.tagcloud.end})
}}
</div>
</div>
{% include 'tag-color.swig'%}

参考资料[6][7][8]


  1. Hexo 添加分类及标签 ↩︎

  2. hexo markdown渲染器 @upupming/hexo-renderer-markdown-it-plus ↩︎

  3. 设置hexo首页只显示部分摘要(不显示全文) ↩︎

  4. hexo博客优化之文章置顶+置顶标签 ↩︎

  5. 给next添加彩色标签 ↩︎

  6. Hexo博客设置以及Next主题美化 ↩︎

  7. hexo+next个性化配置 ↩︎

  8. Hexo-NexT配置超炫网页效果 ↩︎