Skip to content

Commit

Permalink
feat: add neotw-reading-time plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Feb 27, 2025
1 parent 4556e5d commit c7ad4b0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/dev-plugins.json
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",
Expand Down
16 changes: 16 additions & 0 deletions plugins/oeyoews/neotw-reading-time/plugin.info
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"
}
4 changes: 4 additions & 0 deletions plugins/oeyoews/neotw-reading-time/readme.tid
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]]
31 changes: 31 additions & 0 deletions plugins/oeyoews/neotw-reading-time/tiddlers/lib.js
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 plugins/oeyoews/neotw-reading-time/tiddlers/reading-time.js
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);
};
})();
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +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>>
<<opublish>>
<!-- <<AddTags>> -->
</$list>
Expand Down

0 comments on commit c7ad4b0

Please sign in to comment.