Files
NotionNext/lib/plugins/wordCount.js
anime ff89e16ee2 feat(原生支持字数统计和阅读时长): 统一使用WordCount组件
(cherry picked from commit d8180e1a783ad50c501b741adc72f2747896bdc1)
2024-12-29 00:23:09 +08:00

28 lines
709 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 更新字数统计和阅读时间
*/
export function countWords(pageContentText) {
const wordCount = fnGetCpmisWords(pageContentText)
// 阅读速度 300-500每分钟
const readTime = Math.floor(wordCount / 400) + 1
return { wordCount, readTime }
}
// 用word方式计算正文字数
function fnGetCpmisWords(str) {
if (!str) {
return 0
}
let sLen = 0
try {
// eslint-disable-next-line no-irregular-whitespace
str = str.replace(/(\r\n+|\s+| +)/g, '龘')
// eslint-disable-next-line no-control-regex
str = str.replace(/[\x00-\xff]/g, 'm')
str = str.replace(/m+/g, '*')
str = str.replace(/龘+/g, '')
sLen = str.length
} catch (e) {}
return sLen
}