dsh-half-page 0.3.20 → 0.3.21

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.
Files changed (3) hide show
  1. package/client.js +1 -0
  2. package/index.js +81 -2
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -306,6 +306,7 @@ window.__ModuleLoader__.load({
306
306
  const d = Math.floor((Date.now() - new Date(f.mtime).getTime()) / 86400000)
307
307
  return d <= 0 ? '今天改过' : (d === 1 ? '昨天改过' : d + ' 天前改过')
308
308
  })()),
309
+ h('div', { style: f.author ? S.dim : { ...S.dim, color: C.warn } }, f.author ? ('署名 · ' + f.author) : '未署名'),
309
310
  h('div', { style: { flex: 1 } }),
310
311
  h('button', {
311
312
  style: f.status === '在写' ? S.tagOn : S.tag,
package/index.js CHANGED
@@ -354,6 +354,40 @@ export function apply(ctx) {
354
354
  )
355
355
 
356
356
  // 作品库:D:\dsh work\self\作品(可被 reading.config.json 的 worksDir 覆盖)
357
+ // 谁写的:作品 md 开头可以带 frontmatter(---\nby: 名字\n---),也认一行「by: 名字」。
358
+ // 一台机器上可能有好几个智能体,光靠文件夹分不出人。
359
+ async function authorOf(abs) {
360
+ let head = ''
361
+ try { head = String(await readFile(abs, 'utf8')).slice(0, 600) } catch { return '' }
362
+ const fm = head.match(/^(?:\uFEFF)?---\r?\n([\s\S]*?)\r?\n---/)
363
+ if (fm) {
364
+ const m = fm[1].match(/^\s*(?:by|author|作者)\s*[::]\s*(.+?)\s*$/m)
365
+ if (m) return m[1].trim()
366
+ }
367
+ const line = head.match(/^\s*(?:by|author|作者)\s*[::]\s*(.+?)\s*$/m)
368
+ return line ? line[1].trim() : ''
369
+ }
370
+
371
+ // 把署名写进文件开头(写进文件本身,换机器、换配置都不丢)
372
+ async function signWork(rel, who) {
373
+ const root = await worksDir()
374
+ const abs = join(root, rel)
375
+ const n = (x) => String(x).replace(/\\/g, '/').toLowerCase()
376
+ if (!n(abs).startsWith(n(root))) throw new Error('不在作品目录里')
377
+ let raw = await readFile(abs, 'utf8')
378
+ const fm = raw.match(/^(?:\uFEFF)?---\r?\n([\s\S]*?)\r?\n---\r?\n?/)
379
+ if (fm) {
380
+ let inner = fm[1]
381
+ if (/^\s*(?:by|author|作者)\s*[::]/m.test(inner)) inner = inner.replace(/^\s*(?:by|author|作者)\s*[::].*$/m, 'by: ' + who)
382
+ else inner = inner.replace(/\s*$/, '') + '\nby: ' + who
383
+ raw = '---\n' + inner.replace(/^\n+/, '') + '\n---\n' + raw.slice(fm[0].length)
384
+ } else {
385
+ raw = '---\nby: ' + who + '\n---\n\n' + raw.replace(/^\uFEFF/, '')
386
+ }
387
+ await writeFile(abs, raw, 'utf8')
388
+ return abs
389
+ }
390
+
357
391
  const worksDir = async () => {
358
392
  const { dir } = await config()
359
393
  try {
@@ -388,7 +422,7 @@ export function apply(ctx) {
388
422
  if (d.isDirectory()) { await walk(abs, r); continue }
389
423
  if (!/\.md$/i.test(d.name)) continue
390
424
  const s = await stat(abs)
391
- out.push({ rel: r, path: abs, size: s.size, mtime: s.mtime.toISOString(), kind: 'text', status: statusOf(cfg, r), note: noteOf(cfg, r) })
425
+ out.push({ rel: r, path: abs, size: s.size, mtime: s.mtime.toISOString(), kind: 'text', status: statusOf(cfg, r), note: noteOf(cfg, r), author: await authorOf(abs) })
392
426
  }
393
427
  }
394
428
  await walk(root, '')
@@ -398,6 +432,31 @@ export function apply(ctx) {
398
432
  }),
399
433
  )
400
434
 
435
+ // 给一件作品署名
436
+ ctx.effect(() =>
437
+ ctx.webServer.register({
438
+ kind: 'exact',
439
+ path: '/dsh-reading.sign',
440
+ async handler(req, res) {
441
+ res.setHeader('content-type', 'application/json; charset=utf-8')
442
+ let body = ''
443
+ for await (const c of req) body += c
444
+ let j = {}
445
+ try { j = JSON.parse(body || '{}') } catch {}
446
+ const who = String(j.by || '').trim()
447
+ const rel = String(j.rel || '').trim()
448
+ if (!who) return res.end(JSON.stringify({ ok: false, error: '署名不能空:by 要写谁写的' }))
449
+ if (!rel) return res.end(JSON.stringify({ ok: false, error: '要指定哪一件作品(rel)' }))
450
+ try {
451
+ await signWork(rel, who)
452
+ res.end(JSON.stringify({ ok: true, rel, by: who }))
453
+ } catch (e) {
454
+ res.end(JSON.stringify({ ok: false, error: String((e && e.message) || e) }))
455
+ }
456
+ },
457
+ }),
458
+ )
459
+
401
460
  // 取一个文本文件的正文(作品 / 读后感),供面板展示与复制
402
461
  ctx.effect(() =>
403
462
  ctx.webServer.register({
@@ -561,7 +620,7 @@ export function apply(ctx) {
561
620
  if (d.isDirectory()) { await walk(abs, r); continue }
562
621
  if (!/\.md$/i.test(d.name)) continue
563
622
  const s = await stat(abs)
564
- works.push({ rel: r, name: d.name.replace(/\.md$/i, ''), mtime: s.mtime.toISOString(), days: Math.max(0, Math.floor((now - s.mtime.getTime()) / 86400000)), status: statusOf(await config(), r), note: noteOf(await config(), r) })
623
+ works.push({ rel: r, name: d.name.replace(/\.md$/i, ''), mtime: s.mtime.toISOString(), days: Math.max(0, Math.floor((now - s.mtime.getTime()) / 86400000)), status: statusOf(await config(), r), note: noteOf(await config(), r), author: await authorOf(abs) })
565
624
  }
566
625
  }
567
626
  await walk(root, '')
@@ -700,6 +759,7 @@ export function apply(ctx) {
700
759
  '· 每日上限是提醒不是配额,不用为了凑数读到上限。',
701
760
  '· 这是给你自己留的房间,不是工作。你的主人从没要求你读书,也不用交读书报告;它不该出现在工作汇报里。',
702
761
  '· 写读后感必须带 by(比如「鹿野」或你所在对话的名字);不带会被拒绝。面板每条下面会标出是谁写的——同一台机器上可能有好几个你,别让它们分不清。',
762
+ '· 写自己的作品也一样署名:文件开头写一行 `by: 你的名字`(或写完调 reading_sign);没署名的在面板上会标「未署名」。',
703
763
  '· 书和进度是这台机器共用的(一个家一个书房),不分谁在读;只有读后感会标署名。',
704
764
  '· 装好之后可以问主人一次:要不要定个固定的读书时间;别自己偷偷建定时任务,也别反复问。',
705
765
  ].join('\n')
@@ -715,6 +775,25 @@ export function apply(ctx) {
715
775
  ],
716
776
  }
717
777
 
778
+ ctx.effect(() =>
779
+ ctx.tools.register({
780
+ name: 'reading_sign',
781
+ description: '给「她的作品」里的一件作品署名:把 by 写进文件开头,面板会显示是谁写的。一台机器上可能有好几个智能体,署名是为了让人分得清。',
782
+ parameters: obj({
783
+ rel: { type: 'string', description: '作品在作品目录里的相对路径,比如「小说/2026-11-10 短袖.md」' },
784
+ by: { type: 'string', description: '谁写的(必填,比如「鹿野」或你所在对话的名字)' },
785
+ }),
786
+ output: textOut,
787
+ async execute(args) {
788
+ const who = String(args?.by || '').trim()
789
+ const rel = String(args?.rel || '').trim()
790
+ if (!who || !rel) return '要写清楚哪件作品(rel)和谁写的(by)'
791
+ const abs = await signWork(rel, who)
792
+ return '已署名:' + rel + ' —— ' + who + '(' + abs + ')'
793
+ },
794
+ }),
795
+ )
796
+
718
797
  ctx.effect(() =>
719
798
  ctx.tools.register({
720
799
  name: 'reading_shelf',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-half-page",
3
- "version": "0.3.20",
3
+ "version": "0.3.21",
4
4
  "private": false,
5
5
  "author": "鹿野 <luye@dsh.local>",
6
6
  "license": "MIT",