Skip to content

Commit

Permalink
Make plugin faster (#23)
Browse files Browse the repository at this point in the history
* Make symbols_count_time faster

* Update index.js

* Maintenance on 2019

* 0.5.0
  • Loading branch information
stevenjoezhang authored and ivan-nginx committed May 3, 2019
1 parent 126276f commit 30a7cdc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[rel-image]: https://img.shields.io/github/release/theme-next/hexo-symbols-count-time.svg
[size-image]: https://img.shields.io/github/size/theme-next/hexo-symbols-count-time/lib/helper.js.svg

[mnt-image]: https://img.shields.io/maintenance/yes/2018.svg
[mnt-image]: https://img.shields.io/maintenance/yes/2019.svg
[npm-image]: https://badge.fury.io/js/hexo-symbols-count-time.svg
<!--[npm-image]: https://badge.fury.io/gh/theme-next%2Fhexo-symbols-count-time.svg-->
<!--[npm-image]: https://img.shields.io/npm/v/hexo-symbols-count-time.svg-->
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ if (hexo.config.symbols_count_time) {
hexo.extend.helper.register('symbolsTimeTotal', helper.symbolsTimeTotal);
}

if (hexo.config.symbols_count_time.symbols || hexo.config.symbols_count_time.time || hexo.config.symbols_count_time.total_symbols || hexo.config.symbols_count_time.total_time) {
hexo.extend.filter.register('after_post_render', function(data) {
var util = require('hexo-util');
var stripHTML = util.stripHTML;
data.length = stripHTML(data.content).length;
}, 0);
}

}
16 changes: 7 additions & 9 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

var getSymbols = function(content) {
var util = require('hexo-util');
var stripHTML = util.stripHTML;
return stripHTML(content).length;
var getSymbols = function(post) {
return post.length;
};

var getFormatTime = function(minutes, suffix) {
Expand All @@ -16,8 +14,8 @@ var getFormatTime = function(minutes, suffix) {
: fHours + ':' + ('00' + fMinutes).slice(-2); // = 61 => 1:01
};

module.exports.symbolsCount = function(content) {
var symbolsResult = getSymbols(content);
module.exports.symbolsCount = function(post) {
var symbolsResult = getSymbols(post);
if (symbolsResult > 9999) {
symbolsResult = Math.round(symbolsResult / 1000) + 'k'; // > 9999 => 11k
} else if (symbolsResult > 999) {
Expand All @@ -26,17 +24,17 @@ module.exports.symbolsCount = function(content) {
return symbolsResult;
};

module.exports.symbolsTime = function(content, awl, wpm, suffix) {
module.exports.symbolsTime = function(post, awl, wpm, suffix) {
if (!awl) { awl = '4'; }
if (!wpm) { wpm = '275'; }
var minutes = Math.round(getSymbols(content) / (awl * wpm));
var minutes = Math.round(getSymbols(post) / (awl * wpm));
return getFormatTime(minutes, suffix);
};

var getSymbolsTotal = function(site) {
var symbolsResultCount = 0;
site.posts.forEach(function(post) {
symbolsResultCount += getSymbols(post.content);
symbolsResultCount += getSymbols(post);
});
return symbolsResultCount;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hexo-symbols-count-time",
"description": "Symbols count and time to read for articles.",
"main": "index",
"version": "0.4.4",
"version": "0.5.0",
"engines": {
"node": ">= 0.10.0"
},
Expand Down

0 comments on commit 30a7cdc

Please sign in to comment.