mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-17 07:26:46 +00:00
feat(原生支持字数统计和阅读时长): 统一使用WordCount组件
(cherry picked from commit d8180e1a783ad50c501b741adc72f2747896bdc1)
This commit is contained in:
27
lib/plugins/wordCount.js
Normal file
27
lib/plugins/wordCount.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 更新字数统计和阅读时间
|
||||
*/
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user