-
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
6 changed files
with
79 additions
and
0 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,5 @@ | ||
[ | ||
"neotw-reading-time", | ||
"neotw-tiddlers-view", | ||
"markdown-importer", | ||
"neotw-tag-route", | ||
|
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,16 @@ | ||
{ | ||
"title": "$:/plugins/oeyoews/neotw-reading-time", | ||
"description": "neotw-reading-time", | ||
"author": "oeyoews", | ||
"version": "0.0.1", | ||
"core-version": ">=5.3.4", | ||
"type": "application/json", | ||
"plugin-type": "plugin", | ||
"name": "neotw-reading-time", | ||
"meat#disabled": "yes", | ||
"qrcode": "yes", | ||
"created": "20250227", | ||
"dependents": [], | ||
"stability": "STABILITY_2_STABLE", | ||
"list": "readme" | ||
} |
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,4 @@ | ||
title: $:/plugins/oeyoews/neotw-reading-time/readme | ||
description: neotw-reading-time | ||
|
||
[[在线文档|https://neotw.vercel.app/docs/plugins/neotw-reading-time]] |
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,31 @@ | ||
/*\ | ||
title: $:/plugins/oeyoews/neotw-reading-time/lib.js | ||
type: application/javascript | ||
module-type: library | ||
\*/ | ||
|
||
/** | ||
* 估算阅读时间 | ||
* @param {string} text - 需要阅读的文本 | ||
* @param {number} chineseSpeed - 每分钟中文阅读速度(默认 400 字) | ||
* @param {number} englishSpeed - 每分钟英文阅读速度(默认 200 词) | ||
* @returns {string} 预估阅读时间(例如 "3 分钟") | ||
*/ | ||
function estimateReadingTime(text, chineseSpeed = 400, englishSpeed = 200) { | ||
if (!text) return '0 分钟'; | ||
|
||
// 统计中文字符数量(汉字、标点等) | ||
const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length; | ||
|
||
// 统计英文单词数量(按空格分隔) | ||
const englishWords = (text.match(/[a-zA-Z]+/g) || []).length; | ||
|
||
// 计算阅读时间(分钟) | ||
const time = chineseChars / chineseSpeed + englishWords / englishSpeed; | ||
|
||
// 向上取整,至少 1 分钟 | ||
return `${Math.max(Math.ceil(time), 1)}分钟`; | ||
} | ||
|
||
module.exports = estimateReadingTime; |
26 changes: 26 additions & 0 deletions
26
plugins/oeyoews/neotw-reading-time/tiddlers/reading-time.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,26 @@ | ||
/*\ | ||
title: $:/plugins/oeyoews/neotw-reading-time/reading-time.js | ||
type: application/javascript | ||
module-type: macro | ||
\*/ | ||
(function () { | ||
/*jslint node: true, browser: true */ | ||
/*global $tw: false */ | ||
'use strict'; | ||
|
||
const estimateReadingTime = require('./lib'); | ||
exports.name = 'reading-time'; // book image header | ||
|
||
exports.params = [ | ||
{ | ||
name: 'tiddler', | ||
}, | ||
]; | ||
|
||
exports.run = function (_tiddler) { | ||
const tiddler = this.getVariable('currentTiddler') || _tiddler; | ||
const text = $tw.wiki.getTiddler(tiddler).fields.text; | ||
return estimateReadingTime(text); | ||
}; | ||
})(); |
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