Skip to content

Commit

Permalink
refactor: show word count
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Feb 27, 2025
1 parent c7ad4b0 commit 9c4c017
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions plugins/oeyoews/neotw-reading-time/tiddlers/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ module-type: library
* @param {string} text - 需要阅读的文本
* @param {number} chineseSpeed - 每分钟中文阅读速度(默认 400 字)
* @param {number} englishSpeed - 每分钟英文阅读速度(默认 200 词)
* @returns {string} 预估阅读时间(例如 "3 分钟")
* @returns {string} 预估阅读时间和字数统计(格式:"阅读时间: 3 分钟,字数: 1200")
*/
function estimateReadingTime(text, chineseSpeed = 400, englishSpeed = 200) {
if (!text) return '0 分钟';
if (!text) return '阅读时间: 0 分钟,字数: 0';

// 统计中文字符数量(汉字、标点等)
const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;

// 统计英文单词数量(按空格分隔)
const englishWords = (text.match(/[a-zA-Z]+/g) || []).length;

// 计算总字数
const totalWords = chineseChars + englishWords;

// 计算阅读时间(分钟)
const time = chineseChars / chineseSpeed + englishWords / englishSpeed;

// 向上取整,至少 1 分钟
return `${Math.max(Math.ceil(time), 1)}分钟`;
return `阅读时间: ${Math.max(Math.ceil(time), 1)} 分钟,字数: ${totalWords}`;
}

module.exports = estimateReadingTime;
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ modified <$neotw-dayjs time={{!!modified}} />
<!-- https have bug, will creat http: tid -->
<!-- GettingStarted not work filter -->
<$list filter="[all[current]!is[system]!is[binary]]">
<svg xmlns="http://www.w3.org/2000/svg" class="om-subtitle" width="1em" height="1em" viewBox="0 0 512 512"><path fill="currentColor" fill-rule="evenodd" d="M448 192v213.334q-106.667 0-192 64q-85.334-64-192-64V192q106.667 0 192 64q85.333-64 192-64m-42.667 45.739l-6.458 1.03c-42.354 7.299-81.272 24.363-117.275 51.365l-25.6 19.2l-25.6-19.2c-36.003-27.002-74.921-44.066-117.275-51.366l-6.458-1.029v127.317l4.645.527c49.5 6.216 95.876 22.36 138.773 48.314l5.894 3.66l5.936-3.66c42.897-25.954 89.273-42.098 138.773-48.314l4.645-.527zM256 42.667c47.128 0 85.333 38.205 85.333 85.333S303.128 213.334 256 213.334S170.667 175.129 170.667 128c0-47.128 38.205-85.333 85.333-85.333m0 42.667c-23.564 0-42.667 19.102-42.667 42.666s19.103 42.667 42.667 42.667s42.667-19.103 42.667-42.667S279.564 85.334 256 85.334"/></svg> 阅读大概需要<<reading-time>>
<svg xmlns="http://www.w3.org/2000/svg" class="om-subtitle" width="1em" height="1em" viewBox="0 0 512 512"><path fill="currentColor" fill-rule="evenodd" d="M448 192v213.334q-106.667 0-192 64q-85.334-64-192-64V192q106.667 0 192 64q85.333-64 192-64m-42.667 45.739l-6.458 1.03c-42.354 7.299-81.272 24.363-117.275 51.365l-25.6 19.2l-25.6-19.2c-36.003-27.002-74.921-44.066-117.275-51.366l-6.458-1.029v127.317l4.645.527c49.5 6.216 95.876 22.36 138.773 48.314l5.894 3.66l5.936-3.66c42.897-25.954 89.273-42.098 138.773-48.314l4.645-.527zM256 42.667c47.128 0 85.333 38.205 85.333 85.333S303.128 213.334 256 213.334S170.667 175.129 170.667 128c0-47.128 38.205-85.333 85.333-85.333m0 42.667c-23.564 0-42.667 19.102-42.667 42.666s19.103 42.667 42.667 42.667s42.667-19.103 42.667-42.667S279.564 85.334 256 85.334"/></svg> <<reading-time>>
<<opublish>>
<!-- <<AddTags>> -->
</$list>
Expand Down

0 comments on commit 9c4c017

Please sign in to comment.