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

Conversation

Shengw3n
Copy link

@Shengw3n Shengw3n commented Sep 2, 2024

问题描述:在最新版本(1.9.8)中,英文文章的字数统计仍然不准确。例如,我的一篇全英文博客的字数应该是大约 2.8k 字,但实际显示只有 760 字。

示例

修复前
修复前

代码

const getWordCount = (post) => {
  const content = stripHTML(post.origin || post.content).replace(/\r?\n|\r/g, ' ').trim();
  
  if (!post.wordcount) {
    // 更准确地匹配单词和字符
    const zhCount = (content.match(/[\u4E00-\u9FA5]/g) || []).length;
    const enCount = (content.match(/[a-zA-Z0-9]+/g) || []).length;
    post.wordcount = zhCount + enCount;
  }
  return post.wordcount;
};

修复后
修复后

描述

更新后的 getWordCount 函数提高了对中文和英文文本字数统计的准确性。此更改解决了 1.9.8 版本中存在的统计不足的问题。

@zkqiang
Copy link
Member

zkqiang commented Sep 24, 2024

这样改是不对的,Fluid 并不只支持中文和英文,代码中正则是匹配除中文外的字符(如拉丁 日韩),英文统计不准应该是正则有问题,我有空再看看。

或者你可以尝试改为:

const getWordCount = (post) => {
  if (!post.wordcount) {
    // 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, '');
    post.wordcount = content.length;
  }
  return post.wordcount;
};

看是否符合预期

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants