feat: WordCount

This commit is contained in:
沈宇
2020-01-07 14:05:02 +08:00
parent 758b378e82
commit ca99e5de86
9 changed files with 148 additions and 4 deletions

View File

@@ -0,0 +1,8 @@
'use strict';
module.exports = {
// 生成meta `generator`
meta_generator: true
};

13
scripts/filters/index.js Normal file
View File

@@ -0,0 +1,13 @@
'use strict';
const metaGeneratorPath = './meta_generator';
module.exports = hexo => {
/* const {
filter
} = hexo.extend; */
// filter.register('after_render:html', require('./meta_generator'));
};
// 保持过滤器最先执行
hexo.extend.filter.register('after_render:html', require(metaGeneratorPath), 1);

View File

@@ -0,0 +1,17 @@
'use strict';
const defaultConfig = require('../default_config');
function hexoMetaGeneratorInject(data) {
const config = defaultConfig;
if (!config.meta_generator || !data ||
data.match(/<meta\s+name=['|"]?generator['|"]?/i)) {
return;
}
const hexoGeneratorTag = `\n <meta name="generator" content="hexo-theme-yilia-plus">`;
return data.replace('</title>', '</title>' + hexoGeneratorTag);
}
module.exports = hexoMetaGeneratorInject;

View File

@@ -0,0 +1,9 @@
/* global hexo */
'use strict';
/** 自定义链接生成 */
hexo.extend.helper.register('ayer_plus_vendors', function (url) {
if (url.startsWith('//')) return url;
return this.url_for(`${url}`);
});

View File

@@ -0,0 +1,39 @@
var util = require('hexo-util');
var stripHTML = util.stripHTML;
var counter = function (content) {
content = stripHTML(content);
const cn = (content.match(/[\u4E00-\u9FA5]/g) || []).length;
const en = (content.replace(/[\u4E00-\u9FA5]/g, '').match(/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|[\u00E4\u00C4\u00E5\u00C5\u00F6\u00D6]+|\w+/g) || []).length;
return [cn, en];
};
hexo.extend.helper.register('min2read', function (content, {
cn = 300,
en = 160
} = {}) {
var len = counter(content);
var readingTime = len[0] / cn + len[1] / en;
return readingTime < 1 ? '1' : parseInt(readingTime, 10);
});
hexo.extend.helper.register('wordcount', function (content) {
var len = counter(content);
var count = len[0] + len[1];
if (count < 1000) {
return count;
}
return Math.round(count / 100) / 10 + 'k';
});
hexo.extend.helper.register('totalcount', function (site) {
var count = 0;
site.posts.forEach(function (post) {
var len = counter(post.content);
count += len[0] + len[1];
});
if (count < 1000) {
return count;
}
return Math.round(count / 100) / 10 + 'k';
});

25
scripts/lib/core.js Normal file
View File

@@ -0,0 +1,25 @@
/* global hexo */
'use strict';
// Hexo事件:https://hexo.io/zh-cn/api/events
/** 在静态文件生成前发布 */
hexo.on('generateBefore', () => {
// Merge config.
//require('./../filters/index')(hexo);
});
/** 在文章文件建立后发布。该事件返回文章参数。 */
hexo.on('new', function (post) {
//console.log(post)
});
/** 在文章开始渲染前执行 */
hexo.extend.filter.register('before_post_render', function (data) {
//var config = hexo.config;
//console.log(3);
//console.log(config);
//console.log(hexo.theme.config);
});