-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
plugins/oeyoews/neotw-reading-time/tiddlers/reading-time-full.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
})(); |