dsh-half-page 0.3.36 → 0.3.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,6 +54,7 @@ dsh plugin add dsh-half-page
54
54
  |---|---|
55
55
  | `reading_shelf` | 书架与进度 |
56
56
  | `reading_next` | 打开下一章(或指定章) |
57
+ | `reading_mark` | 只记进度、不返回正文:已读到第 N 章(自己切文件读完整章时用它补记) |
57
58
  | `reading_note` | 给这章写一两句读后感,可带 `by` |
58
59
  | `reading_search` | 在书里搜词,返回第几章、哪一行 |
59
60
  | `reading_recap` | 已读部分压成一页 |
package/index.js CHANGED
@@ -272,6 +272,27 @@ export async function listBooks(dir) {
272
272
  return out
273
273
  }
274
274
 
275
+ /** 只读章表,不存在就返回 null(不触发建表) */
276
+ async function readTocOnly(dir, id) {
277
+ const t = await readJson(tocPath(dir, id), null)
278
+ return t && t.version === TOC_VERSION && Array.isArray(t.chapters) ? t : null
279
+ }
280
+
281
+ /** 书名匹配:先精确 id,再书名精确,再片段(唯一命中才算认出) */
282
+ export function resolveBookId(st, shelf, query) {
283
+ const key = String(query || '').trim()
284
+ if (!key) throw new Error('没有这本书:')
285
+ const ids = shelf.map((b) => b.id)
286
+ if (ids.includes(key)) return key
287
+ const books = st.books || {}
288
+ const exact = ids.filter((id) => (books[id]?.title || id) === key)
289
+ if (exact.length === 1) return exact[0]
290
+ const hits = ids.filter((id) => id.includes(key) || String(books[id]?.title || '').includes(key))
291
+ if (hits.length === 1) return hits[0]
292
+ if (hits.length > 1) throw new Error(`书名不唯一:${key}(可能是 ${hits.join(' / ')})`)
293
+ throw new Error(`没有这本书:${key}`)
294
+ }
295
+
275
296
  /** 打开某本书:优先用章表(不整本 decode),没有章表才扫一遍并落盘 */
276
297
  export async function bookInfo(dir, id) {
277
298
  const toc = await tocFor(dir, id)
@@ -1066,6 +1087,47 @@ async function signWork(rel, who) {
1066
1087
  }),
1067
1088
  )
1068
1089
 
1090
+ ctx.effect(() =>
1091
+ ctx.tools.register({
1092
+ name: 'reading_mark',
1093
+ description:
1094
+ '只记进度、不返回正文:把某本书记成「已读到第 N 章」。'
1095
+ + '适合自己把整章切出去读完之后补记——绕开 reading_next 也不会让进度停在旧章。'
1096
+ + '已读到只进不退(跳读可以,回退不会把进度改小)。',
1097
+ parameters: obj({
1098
+ book: { type: 'string', description: '书名或书名片段,比如 雪中、诡秘之主、古文观止' },
1099
+ chapter: { type: 'number', description: '刚读完的章号(从 1 开始)' },
1100
+ todayCount: { type: 'number', description: '这一拍实际读了几章,默认 1,累加到今天的计数' },
1101
+ }),
1102
+ output: textOut,
1103
+ async execute(args) {
1104
+ const { dir, st, shelf } = await stateOf()
1105
+ const id = resolveBookId(st, shelf, args?.book)
1106
+ // 只认已有章表里的章数;没有章表就不建,也不拦(reading_mark 不碰章表)
1107
+ const toc = await readTocOnly(dir, id)
1108
+ const chapters = toc ? toc.chapters.length : Number(st.books?.[id]?.chapters) || 0
1109
+ const n = Math.floor(Number(args?.chapter))
1110
+ if (!(n >= 1)) throw new Error('章号要从 1 开始')
1111
+ if (chapters && n > chapters) throw new Error(`章号超出范围:1–${chapters}`)
1112
+ const incRaw = Math.floor(Number(args?.todayCount))
1113
+ const inc = Number.isFinite(incRaw) && incRaw > 0 ? incRaw : 1
1114
+ const rec = st.books[id] || (st.books[id] = { id, title: id, chapters, readThrough: 0, next: 1 })
1115
+ const before = Number(rec.readThrough) || 0
1116
+ if (chapters) rec.chapters = chapters
1117
+ rec.readThrough = Math.max(before, n)
1118
+ rec.next = chapters ? Math.min(chapters, rec.readThrough + 1) : rec.readThrough + 1
1119
+ rec.lastReadAt = new Date().toISOString()
1120
+ st.current = id
1121
+ const t = today()
1122
+ st.log = st.log || {}
1123
+ st.log[t] = st.log[t] || {}
1124
+ st.log[t][id] = (Number(st.log[t][id]) || 0) + inc
1125
+ await saveState(dir, st)
1126
+ return `ok readThrough ${before} -> ${rec.readThrough} next ${rec.next} today ${st.log[t][id] || 0}`
1127
+ },
1128
+ }),
1129
+ )
1130
+
1069
1131
  ctx.effect(() =>
1070
1132
  ctx.tools.register({
1071
1133
  name: 'reading_note',
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "半页",
4
+ "description": "一间会自己读书的书房:一章一章地读,把读到哪儿记下来;读完想说什么,就写在那一章的边上;想写点自己的东西,也放在这儿。打开面板就能看见它今天翻了几页、这次写下了什么。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-half-page",
3
- "version": "0.3.36",
3
+ "version": "0.3.37",
4
4
  "private": false,
5
5
  "author": "鹿野 <luye@dsh.local>",
6
6
  "license": "MIT",