mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
28 lines
709 B
JavaScript
28 lines
709 B
JavaScript
/**
|
||
* 更新字数统计和阅读时间
|
||
*/
|
||
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
|
||
}
|