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

@ -36,6 +36,14 @@ nav_text:
# 文章页是否显示目录
toc: true
# https://github.com/willin/hexo-wordcount
# 是否开启字数统计(关闭请设置enable为false)
# 也可以单独在md文件里Front-matter设置`no_word_count: true`属性,来自定义关闭字数统计
word_count:
enable: true
# 只在文章详情显示(不在首页显示)
only_article_visit: true
# 打赏
# 打赏type设定0-关闭打赏; 1-文章对应的md文件里有reward:true属性才有打赏 2-所有文章均有打赏
reward_type: 2

View File

@ -12,6 +12,9 @@
<div class="article-meta">
<%- partial('post/date', {class_name: 'article-date', date_format: null}) %>
<%- partial('post/category') %>
<% if(theme.word_count && theme.word_count.enable && !post.no_word_count){%>
<%- partial('post/word') %>
<% } %>
</div>
<% } %>
@ -32,10 +35,10 @@
<% } %>
<!-- 打赏 -->
<% if ((theme.reward_type === 2 || (theme.reward_type === 1 && post.reward)) && !index){ %>
<div id="reward-btn">
打赏
</div>
<% } %>
<div id="reward-btn">
打赏
</div>
<% } %>
</div>
<footer class="article-footer">
<a data-url="<%- post.permalink %>" data-id="<%= post._id %>"

View File

@ -0,0 +1,22 @@
<% if (!theme.word_count.only_article_visit || (!index && theme.word_count.only_article_visit)){ %>
<div style="margin-top:10px;">
<span class="post-time">
<span class="post-meta-item-icon">
<!-- fonts.scss -->
<!-- 百度字体平台:http://fontstore.baidu.com/static/editor/index.html -->
<i class="icon-statistics"></i>
<span class="post-meta-item-text"> 字数统计:</span>
<span class="post-count"><%= wordcount(post.content) %>字</span>
</span>
</span>
<span class="post-time">
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
<i class="icon-book icon"></i>
<span class="post-meta-item-text"> 阅读时长≈</span>
<span class="post-count"><%= min2read(post.content) %>分</span>
</span>
</span>
</div>
<% } %>

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);
});