Skip to content

Commit

Permalink
feat: add reading-time-full macro
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Feb 27, 2025
1 parent 9c4c017 commit 7d8e511
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugins/oeyoews/neotw-reading-time/readme.tid
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
title: $:/plugins/oeyoews/neotw-reading-time/readme
description: neotw-reading-time

[[在线文档|https://neotw.vercel.app/docs/plugins/neotw-reading-time]]
[[在线文档|https://neotw.vercel.app/docs/plugins/neotw-reading-time]]

当前wiki总字数(英文按照单词统计): <<reading-time-full>>
2 changes: 1 addition & 1 deletion plugins/oeyoews/neotw-reading-time/tiddlers/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module-type: library
* @param {string} text - 需要阅读的文本
* @param {number} chineseSpeed - 每分钟中文阅读速度(默认 400 字)
* @param {number} englishSpeed - 每分钟英文阅读速度(默认 200 词)
* @returns {string} 预估阅读时间和字数统计(格式:"阅读时间: 3 分钟,字数: 1200"
* @returns {string} 预估阅读时间和字数统计(格式:阅读时间: 3 分钟,字数: 1200
*/
function estimateReadingTime(text, chineseSpeed = 400, englishSpeed = 200) {
if (!text) return '阅读时间: 0 分钟,字数: 0';
Expand Down
48 changes: 48 additions & 0 deletions plugins/oeyoews/neotw-reading-time/tiddlers/reading-time-full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*\
title: $:/plugins/oeyoews/neotw-reading-time/reading-time-full.js
type: application/javascript
module-type: macro
\*/
(function () {
/*jslint node: true, browser: true */
/*global $tw: false */
'use strict';

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

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

// 计算总字数
const totalWords = chineseChars + englishWords;
return totalWords || 0;
}

exports.name = 'reading-time-full'; // book image header

exports.params = [
{
name: 'filter',
},
];

exports.run = function (_filter) {
const filter = '[!is[system]!is[binary]!type[application/json]]';
const tiddlers = $tw.wiki.filterTiddlers(_filter || filter);
if (!tiddlers) return 0;
let wordsTotal = 0;
console.log(tiddlers);

tiddlers.forEach((tiddler) => {
if (!$tw.wiki.tiddlerExists(tiddler)) return;
const text = $tw.wiki.getTiddler(tiddler).fields?.text;
if (text) {
wordsTotal += getTextTotal(text);
}
});
return wordsTotal.toLocaleString();
};
})();

0 comments on commit 7d8e511

Please sign in to comment.