hexo/node_modules/hexo-theme-fluid/scripts/helpers/wordcount.js

38 lines
940 B
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
/* global hexo */
'use strict';
const { stripHTML } = require('hexo-util');
const getWordCount = (post) => {
if (!post.wordcount) {
post.wordcount = stripHTML(post.content).replace(/\r?\n|\r/g, '').replace(/\s+/g, '').length;
}
return post.wordcount;
};
const symbolsCount = (count) => {
if (count > 9999) {
count = Math.round(count / 1000) + 'k'; // > 9999 => 11k
} else if (count > 999) {
count = (Math.round(count / 100) / 10) + 'k'; // > 999 => 1.1k
} // < 999 => 111
return count;
};
hexo.extend.helper.register('min2read', (post, { awl, wpm }) => {
return Math.floor(getWordCount(post) / ((awl || 2) * (wpm || 60))) + 1;
});
hexo.extend.helper.register('wordcount', (post) => {
return symbolsCount(getWordCount(post));
});
hexo.extend.helper.register('wordtotal', (site) => {
let count = 0;
site.posts.forEach(post => {
count += getWordCount(post);
});
return symbolsCount(count);
});