记录一些博客搭建的小知识。
本文持续更新
5.14更新:增加文章永久链接
5.22更新:增加文章置顶

1 加入Valine评论系统

教程:官网就有,不过官网是针对一般的博客而言的。如果自己的主题自带对valine的支持的话是可以直接在配置中填写好的。
我用的主题正好支持,所以只要填写相关的配置就好了。如果主题不支持,那就得自己弄了,要修改layout,虽然也不是很难,但是主题支持最好。

主题配置范例:

1
2
3
4
5
6
7
8
9
10
11
# Valine Comment system. https://valine.js.org
valine:
enable: true # 如果你想使用valine,请将值设置为 true
appId: # your leancloud appId
appKey: # your leancloud appKey
notify: false # Mail notify
verify: false # Verify code
avatar: mm # Gravatar style : mm/identicon/monsterid/wavatar/retro/hide
placeholder: 欢迎评论! # Comment Box placeholder
guest_info: nick,mail,link # Comment header info
pageSize: 10 # comment list page size

2 添加emoji支持

要做两个事:

使用方法官网都可以看到。

  • 安装
1
$ npm install hexo-filter-github-emojis --save
  • 配置文件修改:在根目录config文件中增加:
1
2
3
4
5
6
githubEmojis:
enable: true
className: github-emoji
inject: true
styles:
customEmojis:

3 关于插入图片

我觉得好用的是两种。一种来自于hexo官方的采用资源文件夹的方法;一种是利用Markdown语法实现的外链图片插入方法。

3.1 先说内部文件法

在配置文件中加入:

1
post_asset_folder: true

每新建一个post都会在post文件里建一个和文章标题一样的子文件夹,图片就放在那里,需要引用的时候添加如下代码:

1
{% asset_img example.jpg title %}

3.2 外链法

对于一些不太重要而又很大的图片,用这个比较好。因为加载速度快。

插入外链图片的代码:

1
![title](URL)

两个比较好用的图床:

小贱贱图床速度比较快;路过图床注册账号后可以添加文件夹。并且可以直接生成Markdown代码,如下图:

路过图床
路过图床

此外路过图床可以设置图片是否对外可见。感觉不是很重要的,又比较大的图片放在小贱贱图床比较好。然后稍微重要的放路过不可丢失的图片放在资源文件夹。而其实最安全的是云存储+图床。

暂时这么多吧。。。
以后再更新。
(2019.3.28)

4 live2D(3.30更新)

加一个看板娘,好萌啊。还有公主殿下,啊,我等骑士团。。。

hexo-helper-live2d

这里面使用方法将的很清楚了,为了以后不再翻网页,直接把东西copy在这里。

首先需要安装这个插件:

1
npm install --save hexo-helper-live2d

然后需要选择需要的模型。(模型预览)

1
npm install --save live2d-widget-model-xxx

在配置中添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 看板娘
live2d:
# 固定写法,具体用途参阅下方文档
enable: true
scriptFrom: local
pluginRootPath: live2dw/
pluginJsPath: lib/
pluginModelPath: assets/
tagMode: false
debug: false
model:
# 这里根据已安装的素材包填写,如:live2d-widget-model-z16
use: live2d-widget-model-miku
# 摆放位置
display:
position: right
width: 150
height: 300
mobile:
show: true

OK,完美!
哦对了,据测试要放在根目录下的config文件才有效。为了这个弄了半天才弄明白。。。
其实放在主题目录和根目录下的config应该都是可以的,我第一次用是在根目录下,然后改到主题目录下的config,结果改了相关的参数但是没有用。后来在根目录下的config改了才生效,值得注意。

5 文章的永久链接

参考教程


作者:雨临Lewis
来源:CSDN
原文:https://blog.csdn.net/lewky_liu/article/details/80517635


需要安装插件:

1
npm install hexo-abbrlink --save

根目录下的配置:

1
2
3
4
5
6
# permalink: :year/:month/:day/:title/
# permalink_defaults:
permalink: posts/:abbrlink.html
abbrlink:
alg: crc32 # 算法:crc16(default) and crc32
rep: hex # 进制:dec(default) and hex

6 增加文章置顶

参考TIMD的教程,在此感谢他。


作者:TIMD
来源:CSDN
原文:https://blog.csdn.net/weixin_42556146/article/details/80836875


修改Hexo文件夹下的node_modules/hexo-generator-index/lib/generator.js,在生成文章之前进行文章top值排序。即在var posts = locals.posts.sort(config.index_generator.order_by)之后插入下面的代码。

需添加的代码:

1
2
3
4
5
6
7
8
9
10
posts.data = posts.data.sort(function(a, b) {
if (a.top && b.top) { // 两篇文章top都有定义
if (a.top == b.top) return b.date - a.date; // 若top值一样则按照文章日期降序排
else return b.top - a.top; // 否则按照top值降序排
} else if (a.top && !b.top) { // 以下是只有一篇文章top有定义,那么将有top的排在前面(这里用异或操作居然不行233)
return -1;
} else if (!a.top && b.top) {
return 1;
} else return b.date - a.date; // 都没定义按照文章日期降序排
});

最终的代码:

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
'use strict';

var pagination = require('hexo-pagination');

module.exports = function(locals) {
var config = this.config;
var posts = locals.posts.sort(config.index_generator.order_by);

posts.data = posts.data.sort(function(a, b) {
if (a.top && b.top) {
if (a.top == b.top) return b.date - a.date;
else return b.top - a.top;
} else if (a.top && !b.top) {
return -1;
} else if (!a.top && b.top) {
return 1;
} else return b.date - a.date;
});

var paginationDir = config.pagination_dir || 'page';
var path = config.index_generator.path || '';

return pagination(path, posts, {
perPage: config.index_generator.per_page,
layout: ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true
}
});
};

之后,只需要在文章加入top属性就行了,排序从小到大。

7 当文章中含有空格怎么办(2019.8.1更新)

在执hexo new post tittle的时候。如果tittle里面包含空格,比如:

1
Start a new day!

此时需要用引号把tittle包围起来:

1
hexo new post "Start a new day!"