Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复英文文章字数统计问题 #1128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions scripts/helpers/wordcount.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
const { stripHTML } = require('hexo-util');

const getWordCount = (post) => {
// post.origin is the original post content of hexo-blog-encrypt
const content = stripHTML(post.origin || post.content).replace(/\r?\n|\r/g, '').replace(/\s+/g, '');

const content = stripHTML(post.origin || post.content).replace(/\r?\n|\r/g, ' ').trim();

if (!post.wordcount) {
// Match words and characters more accurately
const zhCount = (content.match(/[\u4E00-\u9FA5]/g) || []).length;
const enCount = (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;
post.wordcount = zhCount + enCount
const enCount = (content.match(/[a-zA-Z0-9]+/g) || []).length;
post.wordcount = zhCount + enCount;
}
return post.wordcount;
};


const symbolsCount = (count) => {
if (count > 9999) {
count = Math.round(count / 1000) + 'k'; // > 9999 => 11k
Expand Down
Loading