0%

Hexo Notes

记录我自己的 Hexo 配置变动
非常有用的 Hexo 功能扩展

在主题中开启mathjax开关

如何使用了主题了,别忘了在主题(Theme)中开启mathjax开关,下面以next主题为例,介绍下如何打开mathjax开关。

进入到主题目录,找到_config.yml配置问题,把mathjax默认的false修改为true,具体如下:

1
2
3
4
# MathJax Support
mathjax:
enable: true
per_page: true

别着急,这样还不够,还需要在文章的Front-matter里打开mathjax开关,如下:

1
2
3
4
5
6
---
title: index.html
date: 2016-12-28 21:01:30
tags:
mathjax: true
--

Update Hexo

1
npm update

gulp 压缩站点

gulpfile.js

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
82
83
84
/* Refrences:
1. http://notes.iissnan.com/2016/publishing-github-pages-with-travis-ci
2. https://github.com/chrisjlee/hexo-theme-zurb-foundation/blob/e82f45a82bbaaee063bcb1298cd9793575afb142/gulpfile.js
3. https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
4. https://hexo.io/api/
5. https://github.com/iissnan/theme-next-docs/blob/master/.travis.yml
*/

var gulp = require('gulp');
var minifycss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
var imagemin = require('gulp-imagemin');
var del = require('del');
var runSequence = require('run-sequence');
var Hexo = require('hexo');


gulp.task('clean', function() {
return del(['public/**/*']);
});

// generate html with 'hexo generate'
var hexo = new Hexo(process.cwd(), {});
gulp.task('generate', function(cb) {
hexo.init().then(function() {
return hexo.call('generate', {
watch: false
});
}).then(function() {
return hexo.exit();
}).then(function() {
return cb()
}).catch(function(err) {
console.log(err);
hexo.exit(err);
return cb(err);
})
})

gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss({
compatibility: 'ie8'
}))
.pipe(gulp.dest('./public'));
});

gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});

gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});

gulp.task('minify-img', function() {
return gulp.src('./public/images/*')
.pipe(imagemin())
.pipe(gulp.dest('./public/images'))
})

gulp.task('compress', function(cb) {
runSequence(['minify-html', 'minify-css', 'minify-js', 'minify-img'], cb);
});


//gulp.task('build', ['clean', 'generate', 'compress']);
gulp.task('build', function(cb) {
runSequence('clean', 'generate', 'compress', cb)
});

gulp.task('default', [])

文章置顶

  • Usage:
    sticky: true # 添加图钉
    top: 1 # 置顶排位

  • 设置:
    修改 hero-generator-index 插件,把文件:node_modules/hexo-generator-index/lib/generator.js 内的代码替换为:

    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
    'use strict';
    var pagination = require('hexo-pagination');
    module.exports = function(locals){
    var config = this.config;
    var posts = locals.posts;
    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; // 都没定义按照文章日期降序排
    });
    var paginationDir = config.pagination_dir || 'page';
    return pagination('', posts, {
    perPage: config.index_generator.per_page,
    layout: ['index', 'archive'],
    format: paginationDir + '/%d/',
    data: {
    __index: true
    }
    });
    };

Front-matter 举例

1
2
3
4
5
6
7
8
9
10
11
12
13
---
title: Python - Regular Expression
date: 2018-02-05 18:10:34
categories:
- Languages
- Python
tags:
- Python
- RE
- Regular Expression
sticky: true
top: 1
---

添加加载进度条

修改主题配置文件(_config.yml)将pace: false改为pace: true就行了,你还可以换不同样式的加载条,如下图:
参考更多形状

修改文章底部的那个带#号的标签

具体实现方法

修改模板/themes/next/layout/_macro/post.swig,搜索 rel=”tag”>#,将 # 换成

实现摘要

在文章开头写好摘要后,另起一行键入<!−− more −−>即可,就像这样

1
2
3
4
---
这是摘要
<!-- more -->
这是正文

实现文章统计功能

具体实现方法

在根目录下安装 hexo-wordcount,运行:

1
$ npm install hexo-wordcount --save

然后在主题的配置文件中,配置如下:

1
2
3
4
5
6
# Post wordcount display settings
# Dependencies: https://github.com/willin/hexo-wordcount
post_wordcount:
item_text: true
wordcount: true
min2read: true

实现网站统计功能

具体方法实现
切换到根目录下,然后运行如下代码

1
$ npm install hexo-wordcount --save

然后在/themes/next/layout/_partials/footer.swig文件尾部加上:

1
2
3
4
<div class="theme-info">
<div class="powered-by"></div>
<span class="post-count">Words count: {{ totalcount(site) }} </span>
</div>