最近发现了Butterfly主题,相比fluid,它更更改的符合我的使用习惯,以下是我的butterfly使用及美化记录

安装Butterfly

git clone 主题目录

1
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

应用butterfly主题

修改全局配置文件

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

安装插件

butterfly主题渲染器不同hexo的默认渲染器,需要另外安装

1
npm install hexo-renderer-pug hexo-renderer-stylus --save

一图流背景和顶部图修改

设置图片

修改主题配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Image (圖片設置)
# --------------------------------------

# The banner image of home page
index_img: transparent

# Beautify/Effect (美化/效果)
# --------------------------------------

# Website Background (設置網站背景)
# can set it to color or image (可設置圖片 或者 顔色)
# The formal of image: url(http://xxxxxx.com/xxx.jpg)
background: url(https://example.com/img/background.jpg)
# Footer Background
footer_bg: transparent

使用本地文件时,请使用/img/background.jpg

引入相关样式

主题文件夹新建source/css/modify.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
@import 'nib'

// 顶部图
#page-header
&, &:before
background: transparent !important
&.post-bg, &.not-home-page
height: 280px !important
#post-info
bottom: 40px !important
#page-site-info
top: 140px !important

@media screen and (max-width: 768px)
&.not-home-page
height: 200px !important
#post-info
bottom: 10px !important
#page-site-info
top: 100px !important

.top-img
height: 250px
margin: -50px -40px 50px
border-top-left-radius: inherit
border-top-right-radius: inherit
background-position: center center
background-size: cover
transition: all 0.3s

@media screen and (max-width: 768px)
height: 230px
margin: -36px -14px 36px

[data-theme='dark'] &
filter: brightness(0.8)

// 页脚
#footer:before
background-color: alpha(#FFF, .5)

[data-theme='dark'] &
background-color: alpha(#000, .5)

#footer-wrap, #footer-wrap a
color: #111
transition: unset

[data-theme='dark'] &
color: var(--light-grey)

主题配置文件修改

1
2
3
4
5
6
7
8
9
# other (其他)
# --------------------------------------

# Inject
# Insert the code to head (before '</head>' tag) and the bottom (before '</body>' tag)
# 插入代码到头部 </head> 之前 和 底部 </body> 之前
inject:
head:
- <link rel="stylesheet" href="/css/modify.css">

增加插件脚本

因为使用了cheerio解析HTML,需要安装依赖

1
npm install cheerio

主题文件夹新建scripts/modify.js,添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
const cheerio = require('cheerio');

/**
* 在页面插入新顶部图
* @param {cheerio.Root} $ Root
*/
function insertTopImg($) {
let header = $('#page-header');
if (header.length === 0) return;
let background = header.css('background-image');
if (!background) return;
$('#post, #page, #archive, #tag, #category').prepend(`<div class="top-img" style="background-image: ${background};"></div>`);
}

hexo.extend.filter.register('after_render:html', function(str, data) {
let $ = cheerio.load(str, {
decodeEntities: false
});
insertTopImg($);
return $.html();
});