dsh-reading-inbox 0.2.0 → 0.3.0
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.en.md +211 -206
- package/README.md +211 -206
- package/VERIFY.md +133 -126
- package/index.js +80 -80
- package/package.json +83 -83
- package/src/annotations.js +45 -7
- package/src/config.js +44 -44
- package/src/fetch.js +173 -173
- package/src/markdown.js +309 -309
- package/src/routes.js +276 -254
- package/src/search.js +109 -0
- package/src/store.js +741 -647
- package/src/tools.js +441 -437
- package/web/app.js +912 -785
- package/web/index.html +83 -74
- package/web/style.css +814 -739
package/src/store.js
CHANGED
|
@@ -1,647 +1,741 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto'
|
|
2
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
3
|
-
import { join } from 'node:path'
|
|
4
|
-
import MiniSearch from 'minisearch'
|
|
5
|
-
import {
|
|
6
|
-
buildAnnotationAnchor,
|
|
7
|
-
normalizeColor,
|
|
8
|
-
normalizeNote,
|
|
9
|
-
resolveAnnotations,
|
|
10
|
-
} from './annotations.js'
|
|
11
|
-
import { fetchHtml } from './fetch.js'
|
|
12
|
-
import { exportMarkdown, parseArticle, renderMarkdown, renderedText } from './markdown.js'
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
.
|
|
32
|
-
.
|
|
33
|
-
.
|
|
34
|
-
.replace(
|
|
35
|
-
.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.replace(
|
|
46
|
-
.replace(
|
|
47
|
-
.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
seen.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
await
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
//
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
this.
|
|
153
|
-
this.
|
|
154
|
-
this.
|
|
155
|
-
this.
|
|
156
|
-
this.
|
|
157
|
-
this.
|
|
158
|
-
this.
|
|
159
|
-
this.
|
|
160
|
-
this.
|
|
161
|
-
this.
|
|
162
|
-
this.
|
|
163
|
-
this.
|
|
164
|
-
this.
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
this.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
const
|
|
228
|
-
if (
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
await this.
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
await this.
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
await this.
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
await
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
id
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
if (
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
.
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
const
|
|
634
|
-
if (
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
.
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
}
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import MiniSearch from 'minisearch'
|
|
5
|
+
import {
|
|
6
|
+
buildAnnotationAnchor,
|
|
7
|
+
normalizeColor,
|
|
8
|
+
normalizeNote,
|
|
9
|
+
resolveAnnotations,
|
|
10
|
+
} from './annotations.js'
|
|
11
|
+
import { fetchHtml } from './fetch.js'
|
|
12
|
+
import { exportMarkdown, parseArticle, renderMarkdown, renderedText } from './markdown.js'
|
|
13
|
+
import { makeSnippet, parseQuery, queryTerms } from './search.js'
|
|
14
|
+
|
|
15
|
+
const INDEX_VERSION = 1
|
|
16
|
+
const STATUSES = new Set(['unread', 'reading', 'read', 'archived'])
|
|
17
|
+
const MAX_TAGS = 20
|
|
18
|
+
const MAX_TAG_LENGTH = 40
|
|
19
|
+
const MAX_ANNOTATIONS = 500
|
|
20
|
+
|
|
21
|
+
function nowIso() {
|
|
22
|
+
return new Date().toISOString()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function digest(value, length = 12) {
|
|
26
|
+
return createHash('sha256').update(String(value)).digest('hex').slice(0, length)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function slugify(value) {
|
|
30
|
+
const ascii = String(value ?? '')
|
|
31
|
+
.normalize('NFKD')
|
|
32
|
+
.replace(/[̀-ͯ]/g, '')
|
|
33
|
+
.toLowerCase()
|
|
34
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
35
|
+
.replace(/^-+|-+$/g, '')
|
|
36
|
+
.slice(0, 60)
|
|
37
|
+
return ascii === '' ? 'clip' : ascii
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeTags(tags = []) {
|
|
41
|
+
const seen = new Set()
|
|
42
|
+
const result = []
|
|
43
|
+
for (const raw of Array.isArray(tags) ? tags : [tags]) {
|
|
44
|
+
const value = String(raw ?? '')
|
|
45
|
+
.replace(/^#/, '')
|
|
46
|
+
.replace(/[\r\n,,]/g, ' ')
|
|
47
|
+
.replace(/\s+/g, ' ')
|
|
48
|
+
.trim()
|
|
49
|
+
if (value === '' || value.length > MAX_TAG_LENGTH) continue
|
|
50
|
+
const key = value.toLowerCase()
|
|
51
|
+
if (seen.has(key)) continue
|
|
52
|
+
seen.add(key)
|
|
53
|
+
result.push(value)
|
|
54
|
+
if (result.length >= MAX_TAGS) break
|
|
55
|
+
}
|
|
56
|
+
return result
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function normalizeUrl(rawUrl) {
|
|
60
|
+
let url
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(String(rawUrl ?? '').trim())
|
|
63
|
+
} catch {
|
|
64
|
+
throw new Error('URL 格式不正确,需要以 http:// 或 https:// 开头')
|
|
65
|
+
}
|
|
66
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
67
|
+
throw new Error(`不支持的协议:${url.protocol};只允许 http/https`)
|
|
68
|
+
}
|
|
69
|
+
url.hash = ''
|
|
70
|
+
for (const key of [...url.searchParams.keys()]) {
|
|
71
|
+
if (/^(utm_|fbclid$|gclid$|spm$)/i.test(key)) url.searchParams.delete(key)
|
|
72
|
+
}
|
|
73
|
+
return url.toString()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function tokenize(text) {
|
|
77
|
+
const normalized = String(text ?? '').toLowerCase()
|
|
78
|
+
const chunks = normalized.match(/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]+|[a-z0-9_]+/gu) || []
|
|
79
|
+
const tokens = []
|
|
80
|
+
for (const chunk of chunks) {
|
|
81
|
+
if (/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u.test(chunk)) {
|
|
82
|
+
for (let index = 0; index < chunk.length; index += 1) {
|
|
83
|
+
tokens.push(chunk[index])
|
|
84
|
+
if (index + 1 < chunk.length) tokens.push(chunk.slice(index, index + 2))
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
tokens.push(chunk)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return tokens
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function createSearchIndex() {
|
|
94
|
+
return new MiniSearch({
|
|
95
|
+
fields: ['title', 'excerpt', 'body', 'tags', 'site'],
|
|
96
|
+
storeFields: ['id'],
|
|
97
|
+
idField: 'id',
|
|
98
|
+
tokenize,
|
|
99
|
+
searchOptions: {
|
|
100
|
+
boost: { title: 3, tags: 2, site: 1.5 },
|
|
101
|
+
prefix: true,
|
|
102
|
+
fuzzy: 0.2,
|
|
103
|
+
combineWith: 'AND',
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function toSearchDoc(meta, body) {
|
|
109
|
+
return {
|
|
110
|
+
id: meta.id,
|
|
111
|
+
title: meta.title,
|
|
112
|
+
excerpt: meta.excerpt,
|
|
113
|
+
body: String(body ?? '').slice(0, 40_000),
|
|
114
|
+
tags: (meta.tags ?? []).join(' '),
|
|
115
|
+
site: meta.site,
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function publicMeta(meta) {
|
|
120
|
+
return {
|
|
121
|
+
id: meta.id,
|
|
122
|
+
title: meta.title,
|
|
123
|
+
url: meta.url,
|
|
124
|
+
site: meta.site,
|
|
125
|
+
author: meta.author,
|
|
126
|
+
published: meta.published,
|
|
127
|
+
clippedAt: meta.clippedAt,
|
|
128
|
+
tags: meta.tags,
|
|
129
|
+
status: meta.status,
|
|
130
|
+
excerpt: meta.excerpt,
|
|
131
|
+
wordCount: meta.wordCount,
|
|
132
|
+
charCount: meta.charCount,
|
|
133
|
+
lang: meta.lang,
|
|
134
|
+
images: meta.images,
|
|
135
|
+
truncated: meta.truncated === true,
|
|
136
|
+
annotationCount: Number.isInteger(meta.annotationCount) ? meta.annotationCount : 0,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function writeJsonAtomic(path, value) {
|
|
141
|
+
const temporary = `${path}.tmp`
|
|
142
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
|
|
143
|
+
await rename(temporary, path)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export class ReadingInbox {
|
|
147
|
+
constructor(config, options = {}) {
|
|
148
|
+
this.config = config
|
|
149
|
+
// Leave this undefined unless a caller explicitly injects a fetch. fetch.js
|
|
150
|
+
// uses its own validating dispatcher (or ProxyAgent) when no override is
|
|
151
|
+
// present; defaulting to globalThis.fetch here used to bypass both.
|
|
152
|
+
this.fetchImpl = options.fetchImpl
|
|
153
|
+
this.dir = config.dir
|
|
154
|
+
this.clipsDir = join(this.dir, 'clips')
|
|
155
|
+
this.indexPath = join(this.dir, 'index.json')
|
|
156
|
+
this.viewsPath = join(this.dir, 'views.json')
|
|
157
|
+
this.warnings = []
|
|
158
|
+
this._ready = null
|
|
159
|
+
this._clips = []
|
|
160
|
+
this._byId = new Map()
|
|
161
|
+
this._byUrl = new Map()
|
|
162
|
+
this._search = null
|
|
163
|
+
this._writeChain = Promise.resolve()
|
|
164
|
+
this._mutationChain = Promise.resolve()
|
|
165
|
+
this._inflight = new Map()
|
|
166
|
+
this._textCache = new Map()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
ready() {
|
|
170
|
+
this._ready ??= this.init()
|
|
171
|
+
return this._ready
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async init() {
|
|
175
|
+
await mkdir(this.clipsDir, { recursive: true })
|
|
176
|
+
try {
|
|
177
|
+
const raw = await readFile(this.indexPath, 'utf8')
|
|
178
|
+
const parsed = JSON.parse(raw)
|
|
179
|
+
const clips = Array.isArray(parsed?.clips) ? parsed.clips : []
|
|
180
|
+
const byId = new Map()
|
|
181
|
+
for (const item of clips) {
|
|
182
|
+
if (item !== null && typeof item === 'object' && typeof item.id === 'string' && !byId.has(item.id)) {
|
|
183
|
+
byId.set(item.id, item)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
this._clips = [...byId.values()]
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (error?.code !== 'ENOENT') {
|
|
189
|
+
const backup = `${this.indexPath}.corrupt-${Date.now()}`
|
|
190
|
+
await rename(this.indexPath, backup).catch(() => {})
|
|
191
|
+
this.warnings.push(`索引读取失败,已备份到 ${backup}:${error instanceof Error ? error.message : String(error)}`)
|
|
192
|
+
}
|
|
193
|
+
this._clips = []
|
|
194
|
+
}
|
|
195
|
+
this._byId = new Map(this._clips.map(item => [item.id, item]))
|
|
196
|
+
this._byUrl = new Map()
|
|
197
|
+
for (const item of this._clips) {
|
|
198
|
+
if (typeof item.url === 'string') this._byUrl.set(item.url, item)
|
|
199
|
+
if (typeof item.canonicalUrl === 'string') this._byUrl.set(item.canonicalUrl, item)
|
|
200
|
+
}
|
|
201
|
+
this._search = createSearchIndex()
|
|
202
|
+
for (const item of this._clips) {
|
|
203
|
+
const body = await this.readBody(item.id).catch(() => '')
|
|
204
|
+
try {
|
|
205
|
+
this._search.add(toSearchDoc(item, body))
|
|
206
|
+
} catch (error) {
|
|
207
|
+
this.warnings.push(
|
|
208
|
+
`索引条目 ${item.id} 重复或损坏,已跳过:${error instanceof Error ? error.message : String(error)}`,
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return this
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async readBody(id) {
|
|
216
|
+
const meta = this._byId.get(id)
|
|
217
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
218
|
+
return readFile(join(this.dir, meta.articlePath), 'utf8')
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
annotationPath(meta) {
|
|
222
|
+
return join(this.clipsDir, meta.id, 'annotations.json')
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
textForClip(meta, markdown) {
|
|
226
|
+
const key = `${meta.id}:${meta.contentHash ?? ''}`
|
|
227
|
+
const cached = this._textCache.get(key)
|
|
228
|
+
if (cached !== undefined) return cached
|
|
229
|
+
const text = renderedText(markdown)
|
|
230
|
+
if (this._textCache.size > 30) this._textCache.clear()
|
|
231
|
+
this._textCache.set(key, text)
|
|
232
|
+
return text
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async readAnnotations(meta) {
|
|
236
|
+
try {
|
|
237
|
+
const raw = await readFile(this.annotationPath(meta), 'utf8')
|
|
238
|
+
const parsed = JSON.parse(raw)
|
|
239
|
+
if (Array.isArray(parsed)) return parsed
|
|
240
|
+
return Array.isArray(parsed?.annotations) ? parsed.annotations : []
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error?.code !== 'ENOENT') {
|
|
243
|
+
this.warnings.push(
|
|
244
|
+
`批注读取失败(${meta.id}):${error instanceof Error ? error.message : String(error)}`,
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
return []
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async writeAnnotations(meta, annotations) {
|
|
252
|
+
await writeJsonAtomic(this.annotationPath(meta), {
|
|
253
|
+
version: 1,
|
|
254
|
+
contentVersion: meta.contentHash ?? '',
|
|
255
|
+
annotations,
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async listAnnotations(id, body = null) {
|
|
260
|
+
await this.ready()
|
|
261
|
+
const meta = this._byId.get(String(id))
|
|
262
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
263
|
+
const markdown = body ?? await this.readBody(meta.id)
|
|
264
|
+
const text = this.textForClip(meta, markdown)
|
|
265
|
+
const annotations = await this.readAnnotations(meta)
|
|
266
|
+
return resolveAnnotations(text, annotations)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async addAnnotation(id, input = {}) {
|
|
270
|
+
await this.ready()
|
|
271
|
+
return this._withMutation(async () => {
|
|
272
|
+
const meta = this._byId.get(String(id))
|
|
273
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
274
|
+
const body = await this.readBody(meta.id)
|
|
275
|
+
const text = this.textForClip(meta, body)
|
|
276
|
+
const anchor = buildAnnotationAnchor(text, {
|
|
277
|
+
quote: input.quote,
|
|
278
|
+
prefix: input.prefix,
|
|
279
|
+
suffix: input.suffix,
|
|
280
|
+
startOffset: Number.isInteger(input.startOffset) ? input.startOffset : undefined,
|
|
281
|
+
endOffset: Number.isInteger(input.endOffset) ? input.endOffset : undefined,
|
|
282
|
+
})
|
|
283
|
+
const existing = await this.readAnnotations(meta)
|
|
284
|
+
if (existing.length >= MAX_ANNOTATIONS) {
|
|
285
|
+
throw new Error(`单篇批注数量已达上限(${MAX_ANNOTATIONS})`)
|
|
286
|
+
}
|
|
287
|
+
const now = nowIso()
|
|
288
|
+
const used = new Set(existing.map(annotation => annotation.id))
|
|
289
|
+
let annotationId = `hl-${digest(`${anchor.quote}\n${anchor.startOffset}\n${now}`, 12)}`
|
|
290
|
+
let counter = 2
|
|
291
|
+
while (used.has(annotationId)) {
|
|
292
|
+
annotationId = `hl-${digest(`${anchor.quote}\n${anchor.startOffset}\n${now}\n${counter}`, 12)}`
|
|
293
|
+
counter += 1
|
|
294
|
+
}
|
|
295
|
+
const annotation = {
|
|
296
|
+
id: annotationId,
|
|
297
|
+
...anchor,
|
|
298
|
+
color: normalizeColor(input.color),
|
|
299
|
+
note: normalizeNote(input.note),
|
|
300
|
+
contentVersion: meta.contentHash ?? '',
|
|
301
|
+
createdAt: now,
|
|
302
|
+
updatedAt: now,
|
|
303
|
+
}
|
|
304
|
+
const next = [...existing, annotation]
|
|
305
|
+
await this.writeAnnotations(meta, next)
|
|
306
|
+
meta.annotationCount = next.length
|
|
307
|
+
await this.persist()
|
|
308
|
+
return resolveAnnotations(text, [annotation])[0]
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async updateAnnotation(id, annotationId, patch = {}) {
|
|
313
|
+
await this.ready()
|
|
314
|
+
return this._withMutation(async () => {
|
|
315
|
+
const meta = this._byId.get(String(id))
|
|
316
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
317
|
+
const body = await this.readBody(meta.id)
|
|
318
|
+
const text = this.textForClip(meta, body)
|
|
319
|
+
const existing = await this.readAnnotations(meta)
|
|
320
|
+
const index = existing.findIndex(annotation => annotation.id === String(annotationId))
|
|
321
|
+
if (index === -1) throw new Error(`找不到批注:${annotationId}`)
|
|
322
|
+
const updated = {
|
|
323
|
+
...existing[index],
|
|
324
|
+
note: patch.note === undefined ? existing[index].note : normalizeNote(patch.note),
|
|
325
|
+
color: patch.color === undefined ? existing[index].color : normalizeColor(patch.color),
|
|
326
|
+
updatedAt: nowIso(),
|
|
327
|
+
}
|
|
328
|
+
const next = [...existing]
|
|
329
|
+
next[index] = updated
|
|
330
|
+
await this.writeAnnotations(meta, next)
|
|
331
|
+
meta.annotationCount = next.length
|
|
332
|
+
await this.persist()
|
|
333
|
+
return resolveAnnotations(text, [updated])[0]
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async deleteAnnotation(id, annotationId) {
|
|
338
|
+
await this.ready()
|
|
339
|
+
return this._withMutation(async () => {
|
|
340
|
+
const meta = this._byId.get(String(id))
|
|
341
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
342
|
+
const existing = await this.readAnnotations(meta)
|
|
343
|
+
const next = existing.filter(annotation => annotation.id !== String(annotationId))
|
|
344
|
+
if (next.length === existing.length) throw new Error(`找不到批注:${annotationId}`)
|
|
345
|
+
await this.writeAnnotations(meta, next)
|
|
346
|
+
meta.annotationCount = next.length
|
|
347
|
+
await this.persist()
|
|
348
|
+
return { count: next.length }
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async readViews() {
|
|
353
|
+
try {
|
|
354
|
+
const raw = await readFile(this.viewsPath, 'utf8')
|
|
355
|
+
const parsed = JSON.parse(raw)
|
|
356
|
+
return Array.isArray(parsed?.views) ? parsed.views : []
|
|
357
|
+
} catch (error) {
|
|
358
|
+
if (error?.code !== 'ENOENT') {
|
|
359
|
+
this.warnings.push(`视图读取失败:${error instanceof Error ? error.message : String(error)}`)
|
|
360
|
+
}
|
|
361
|
+
return []
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async listViews() {
|
|
366
|
+
await this.ready()
|
|
367
|
+
return this.readViews()
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async saveView(name, query) {
|
|
371
|
+
await this.ready()
|
|
372
|
+
return this._withMutation(async () => {
|
|
373
|
+
const label = String(name ?? '').trim().slice(0, 60)
|
|
374
|
+
const q = String(query ?? '').trim().slice(0, 500)
|
|
375
|
+
if (label === '') throw new Error('视图名称不能为空')
|
|
376
|
+
if (q === '') throw new Error('视图查询不能为空')
|
|
377
|
+
const views = await this.readViews()
|
|
378
|
+
const now = nowIso()
|
|
379
|
+
const existing = views.find(view => view.query === q)
|
|
380
|
+
if (existing !== undefined) {
|
|
381
|
+
existing.name = label
|
|
382
|
+
existing.updatedAt = now
|
|
383
|
+
await writeJsonAtomic(this.viewsPath, { version: 1, views })
|
|
384
|
+
return existing
|
|
385
|
+
}
|
|
386
|
+
const used = new Set(views.map(view => view.id))
|
|
387
|
+
let viewId = `view-${digest(`${q}\n${now}`, 10)}`
|
|
388
|
+
let counter = 2
|
|
389
|
+
while (used.has(viewId)) {
|
|
390
|
+
viewId = `view-${digest(`${q}\n${now}\n${counter}`, 10)}`
|
|
391
|
+
counter += 1
|
|
392
|
+
}
|
|
393
|
+
const view = { id: viewId, name: label, query: q, createdAt: now, updatedAt: now }
|
|
394
|
+
views.push(view)
|
|
395
|
+
await writeJsonAtomic(this.viewsPath, { version: 1, views })
|
|
396
|
+
return view
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async deleteView(id) {
|
|
401
|
+
await this.ready()
|
|
402
|
+
return this._withMutation(async () => {
|
|
403
|
+
const views = await this.readViews()
|
|
404
|
+
const next = views.filter(view => view.id !== String(id))
|
|
405
|
+
if (next.length === views.length) throw new Error(`找不到视图:${id}`)
|
|
406
|
+
await writeJsonAtomic(this.viewsPath, { version: 1, views: next })
|
|
407
|
+
return { count: next.length }
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async clip(rawUrl, options = {}) {
|
|
412
|
+
await this.ready()
|
|
413
|
+
const url = normalizeUrl(rawUrl)
|
|
414
|
+
const pending = this._inflight.get(url)
|
|
415
|
+
if (pending !== undefined) return pending
|
|
416
|
+
let task
|
|
417
|
+
task = this._clipOnce(url, options).finally(() => {
|
|
418
|
+
if (this._inflight.get(url) === task) this._inflight.delete(url)
|
|
419
|
+
})
|
|
420
|
+
this._inflight.set(url, task)
|
|
421
|
+
return task
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
_withMutation(fn) {
|
|
425
|
+
const run = this._mutationChain.then(() => fn(), () => fn())
|
|
426
|
+
this._mutationChain = run.then(() => undefined, () => undefined)
|
|
427
|
+
return run
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async _clipOnce(url, options = {}) {
|
|
431
|
+
const existing = this._byUrl.get(url)
|
|
432
|
+
if (existing !== undefined && options.force !== true) {
|
|
433
|
+
const body = await this.readBody(existing.id).catch(() => '')
|
|
434
|
+
return { ...publicMeta(existing), duplicate: true, preview: body.slice(0, 500) }
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const fetched = await fetchHtml(url, this.config, {
|
|
438
|
+
fetchImpl: this.fetchImpl,
|
|
439
|
+
signal: options.signal,
|
|
440
|
+
})
|
|
441
|
+
const parsed = parseArticle(fetched.html, {
|
|
442
|
+
url: fetched.finalUrl,
|
|
443
|
+
maxMarkdownChars: this.config.maxMarkdownChars,
|
|
444
|
+
})
|
|
445
|
+
if (parsed.ok !== true) throw new Error(parsed.reason)
|
|
446
|
+
|
|
447
|
+
const canonicalUrl = parsed.canonicalUrl || fetched.finalUrl
|
|
448
|
+
|
|
449
|
+
return this._withMutation(async () => {
|
|
450
|
+
// Re-check after the network await: another request may have committed
|
|
451
|
+
// the same URL, or a different URL resolving to the same canonical URL,
|
|
452
|
+
// while this one was fetching. This is the serialization point that
|
|
453
|
+
// prevents duplicate ids and a corrupted index.
|
|
454
|
+
const current =
|
|
455
|
+
this._byUrl.get(url) ??
|
|
456
|
+
this._byUrl.get(fetched.finalUrl) ??
|
|
457
|
+
this._byUrl.get(canonicalUrl)
|
|
458
|
+
if (current !== undefined && options.force !== true) {
|
|
459
|
+
const body = await this.readBody(current.id).catch(() => '')
|
|
460
|
+
return { ...publicMeta(current), duplicate: true, preview: body.slice(0, 500) }
|
|
461
|
+
}
|
|
462
|
+
if (current !== undefined && options.force === true) {
|
|
463
|
+
return this.refreshClip(current, parsed, fetched, options)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const id = this.uniqueId(parsed.title, canonicalUrl)
|
|
467
|
+
const explicitTags = normalizeTags([
|
|
468
|
+
...(Array.isArray(options.tags) ? options.tags : []),
|
|
469
|
+
...(this.config.defaultTags ?? []),
|
|
470
|
+
])
|
|
471
|
+
// Page keywords are often SEO noise. Use them only when the user gave no
|
|
472
|
+
// tags at all, so a careful reader's own taxonomy always wins.
|
|
473
|
+
const tags = explicitTags.length > 0
|
|
474
|
+
? explicitTags
|
|
475
|
+
: normalizeTags((parsed.keywords ?? []).slice(0, 3))
|
|
476
|
+
const meta = {
|
|
477
|
+
id,
|
|
478
|
+
url: fetched.finalUrl,
|
|
479
|
+
canonicalUrl,
|
|
480
|
+
title: parsed.title,
|
|
481
|
+
site: parsed.siteName,
|
|
482
|
+
author: parsed.byline,
|
|
483
|
+
published: parsed.published,
|
|
484
|
+
clippedAt: nowIso(),
|
|
485
|
+
tags,
|
|
486
|
+
status: 'unread',
|
|
487
|
+
excerpt: parsed.excerpt,
|
|
488
|
+
wordCount: parsed.wordCount,
|
|
489
|
+
charCount: parsed.charCount,
|
|
490
|
+
lang: parsed.lang,
|
|
491
|
+
images: parsed.images,
|
|
492
|
+
truncated: parsed.truncated === true,
|
|
493
|
+
annotationCount: 0,
|
|
494
|
+
contentHash: digest(parsed.markdown, 16),
|
|
495
|
+
articlePath: `clips/${id}/article.md`,
|
|
496
|
+
htmlPath: `clips/${id}/original.html`,
|
|
497
|
+
metaPath: `clips/${id}/meta.json`,
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const clipDir = join(this.clipsDir, id)
|
|
501
|
+
await mkdir(clipDir, { recursive: true })
|
|
502
|
+
await writeFile(join(this.dir, meta.articlePath), `${parsed.markdown.trim()}\n`, 'utf8')
|
|
503
|
+
await writeFile(join(this.dir, meta.htmlPath), fetched.html, 'utf8')
|
|
504
|
+
await writeFile(join(this.dir, meta.metaPath), `${JSON.stringify(meta, null, 2)}\n`, 'utf8')
|
|
505
|
+
|
|
506
|
+
this._clips.push(meta)
|
|
507
|
+
this._byId.set(meta.id, meta)
|
|
508
|
+
this._byUrl.set(meta.url, meta)
|
|
509
|
+
this._byUrl.set(meta.canonicalUrl, meta)
|
|
510
|
+
this._search.add(toSearchDoc(meta, parsed.markdown))
|
|
511
|
+
await this.persist()
|
|
512
|
+
|
|
513
|
+
return {
|
|
514
|
+
...publicMeta(meta),
|
|
515
|
+
duplicate: false,
|
|
516
|
+
preview: parsed.markdown.slice(0, 1_500),
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async refreshClip(meta, parsed, fetched, options = {}) {
|
|
522
|
+
const canonicalUrl = parsed.canonicalUrl || fetched.finalUrl
|
|
523
|
+
meta.url = fetched.finalUrl
|
|
524
|
+
meta.canonicalUrl = canonicalUrl
|
|
525
|
+
meta.title = parsed.title
|
|
526
|
+
meta.site = parsed.siteName
|
|
527
|
+
meta.author = parsed.byline
|
|
528
|
+
meta.published = parsed.published
|
|
529
|
+
meta.excerpt = parsed.excerpt
|
|
530
|
+
meta.wordCount = parsed.wordCount
|
|
531
|
+
meta.charCount = parsed.charCount
|
|
532
|
+
meta.lang = parsed.lang
|
|
533
|
+
meta.images = parsed.images
|
|
534
|
+
meta.truncated = parsed.truncated === true
|
|
535
|
+
meta.annotationCount = (await this.readAnnotations(meta)).length
|
|
536
|
+
meta.updatedAt = nowIso()
|
|
537
|
+
meta.contentHash = digest(parsed.markdown, 16)
|
|
538
|
+
if (Array.isArray(options.tags) && options.tags.length > 0) {
|
|
539
|
+
meta.tags = normalizeTags(options.tags)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
await writeFile(join(this.dir, meta.articlePath), `${parsed.markdown.trim()}\n`, 'utf8')
|
|
543
|
+
await writeFile(join(this.dir, meta.htmlPath), fetched.html, 'utf8')
|
|
544
|
+
await writeFile(join(this.dir, meta.metaPath), `${JSON.stringify(meta, null, 2)}\n`, 'utf8')
|
|
545
|
+
|
|
546
|
+
this._byUrl.set(meta.url, meta)
|
|
547
|
+
this._byUrl.set(meta.canonicalUrl, meta)
|
|
548
|
+
try {
|
|
549
|
+
this._search.discard(meta.id)
|
|
550
|
+
} catch {
|
|
551
|
+
// The id may be absent after a manual index edit; adding it back is enough.
|
|
552
|
+
}
|
|
553
|
+
this._search.add(toSearchDoc(meta, parsed.markdown))
|
|
554
|
+
await this.persist()
|
|
555
|
+
|
|
556
|
+
return {
|
|
557
|
+
...publicMeta(meta),
|
|
558
|
+
duplicate: false,
|
|
559
|
+
preview: parsed.markdown.slice(0, 1_500),
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
uniqueId(title, url) {
|
|
564
|
+
const base = `${slugify(title)}-${digest(url, 8)}`
|
|
565
|
+
let id = base
|
|
566
|
+
let counter = 2
|
|
567
|
+
while (this._byId.has(id)) {
|
|
568
|
+
id = `${base}-${counter}`
|
|
569
|
+
counter += 1
|
|
570
|
+
}
|
|
571
|
+
return id
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
async search(query) {
|
|
575
|
+
await this.ready()
|
|
576
|
+
const parsed = parseQuery(query)
|
|
577
|
+
if (parsed.text === '') return null
|
|
578
|
+
try {
|
|
579
|
+
return this._search.search(parsed.text).map(result => ({
|
|
580
|
+
id: result.id,
|
|
581
|
+
score: result.score,
|
|
582
|
+
terms: result.terms ?? [],
|
|
583
|
+
}))
|
|
584
|
+
} catch {
|
|
585
|
+
return []
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async list(options = {}) {
|
|
590
|
+
await this.ready()
|
|
591
|
+
const parsed = parseQuery(options.q)
|
|
592
|
+
const terms = queryTerms(parsed)
|
|
593
|
+
const status = options.status ?? 'all'
|
|
594
|
+
const tag = String(options.tag ?? '').trim().toLowerCase()
|
|
595
|
+
const tags = [tag, ...parsed.tags].filter(value => value !== '')
|
|
596
|
+
const limit = Math.max(1, Math.min(200, Number(options.limit) || 50))
|
|
597
|
+
const offset = Math.max(0, Number(options.offset) || 0)
|
|
598
|
+
|
|
599
|
+
let source
|
|
600
|
+
if (parsed.text !== '') {
|
|
601
|
+
let ranked = []
|
|
602
|
+
try {
|
|
603
|
+
ranked = this._search.search(parsed.text)
|
|
604
|
+
} catch {
|
|
605
|
+
ranked = []
|
|
606
|
+
}
|
|
607
|
+
const scoreById = new Map(ranked.map(item => [item.id, item.score]))
|
|
608
|
+
source = this._clips
|
|
609
|
+
.filter(item => scoreById.has(item.id))
|
|
610
|
+
.sort((a, b) => (scoreById.get(b.id) ?? 0) - (scoreById.get(a.id) ?? 0))
|
|
611
|
+
} else {
|
|
612
|
+
source = [...this._clips].sort((a, b) => String(b.clippedAt).localeCompare(String(a.clippedAt)))
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
const filtered = source.filter(item => {
|
|
616
|
+
if (status !== 'all' && item.status !== status) return false
|
|
617
|
+
if (parsed.statuses.length > 0 && !parsed.statuses.includes(item.status)) return false
|
|
618
|
+
const itemTags = (item.tags ?? []).map(value => String(value).toLowerCase())
|
|
619
|
+
if (tags.some(value => !itemTags.includes(value))) return false
|
|
620
|
+
if (parsed.sites.length > 0) {
|
|
621
|
+
const site = String(item.site ?? '').toLowerCase()
|
|
622
|
+
let host = ''
|
|
623
|
+
try { host = new URL(item.url).hostname.toLowerCase() } catch { /* no host */ }
|
|
624
|
+
if (!parsed.sites.some(value => site.includes(value) || host.includes(value))) return false
|
|
625
|
+
}
|
|
626
|
+
const day = String(item.clippedAt ?? '').slice(0, 10)
|
|
627
|
+
if (parsed.after !== '' && day < parsed.after) return false
|
|
628
|
+
if (parsed.before !== '' && day > parsed.before) return false
|
|
629
|
+
if (parsed.hasHighlight && !(Number.isInteger(item.annotationCount) && item.annotationCount > 0)) return false
|
|
630
|
+
return true
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
const items = filtered.slice(offset, offset + limit).map(publicMeta)
|
|
634
|
+
if (terms.length > 0) {
|
|
635
|
+
for (const item of items) {
|
|
636
|
+
const body = await this.readBody(item.id).catch(() => '')
|
|
637
|
+
const snippet = makeSnippet(`${item.title}\n${renderedText(body)}`, terms)
|
|
638
|
+
if (snippet !== '') item.snippet = snippet
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
return {
|
|
643
|
+
total: filtered.length,
|
|
644
|
+
offset,
|
|
645
|
+
limit,
|
|
646
|
+
items,
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
async get(id) {
|
|
651
|
+
await this.ready()
|
|
652
|
+
const meta = this._byId.get(String(id))
|
|
653
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
654
|
+
const body = await this.readBody(meta.id)
|
|
655
|
+
const annotations = await this.listAnnotations(meta.id, body)
|
|
656
|
+
return {
|
|
657
|
+
...publicMeta(meta),
|
|
658
|
+
annotationCount: annotations.length,
|
|
659
|
+
content: body,
|
|
660
|
+
html: renderMarkdown(body),
|
|
661
|
+
annotations,
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
async read(id, options = {}) {
|
|
666
|
+
await this.ready()
|
|
667
|
+
const meta = this._byId.get(String(id))
|
|
668
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
669
|
+
const body = await this.readBody(meta.id)
|
|
670
|
+
const offset = Math.max(0, Number(options.offset) || 0)
|
|
671
|
+
const limit = Math.max(1, Math.min(40_000, Number(options.limit) || 12_000))
|
|
672
|
+
const content = body.slice(offset, offset + limit)
|
|
673
|
+
const nextOffset = offset + content.length
|
|
674
|
+
return {
|
|
675
|
+
...publicMeta(meta),
|
|
676
|
+
content,
|
|
677
|
+
offset,
|
|
678
|
+
totalChars: body.length,
|
|
679
|
+
nextOffset,
|
|
680
|
+
hasMore: nextOffset < body.length,
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
async update(id, patch = {}) {
|
|
685
|
+
await this.ready()
|
|
686
|
+
const meta = this._byId.get(String(id))
|
|
687
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
688
|
+
if (patch.status !== undefined) {
|
|
689
|
+
if (!STATUSES.has(patch.status)) {
|
|
690
|
+
throw new Error(`无效状态:${patch.status};可选 unread/reading/read/archived`)
|
|
691
|
+
}
|
|
692
|
+
meta.status = patch.status
|
|
693
|
+
}
|
|
694
|
+
if (patch.tags !== undefined) meta.tags = normalizeTags(patch.tags)
|
|
695
|
+
await writeFile(join(this.dir, meta.metaPath), `${JSON.stringify(meta, null, 2)}\n`, 'utf8')
|
|
696
|
+
const body = await this.readBody(meta.id).catch(() => '')
|
|
697
|
+
try {
|
|
698
|
+
this._search.discard(meta.id)
|
|
699
|
+
} catch {
|
|
700
|
+
// MiniSearch throws when the id is absent; a missing doc needs no discard.
|
|
701
|
+
}
|
|
702
|
+
this._search.add(toSearchDoc(meta, body))
|
|
703
|
+
await this.persist()
|
|
704
|
+
return publicMeta(meta)
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
async stats() {
|
|
708
|
+
await this.ready()
|
|
709
|
+
const counts = { all: this._clips.length, unread: 0, reading: 0, read: 0, archived: 0 }
|
|
710
|
+
const tags = new Map()
|
|
711
|
+
for (const item of this._clips) {
|
|
712
|
+
if (counts[item.status] !== undefined) counts[item.status] += 1
|
|
713
|
+
for (const tag of item.tags ?? []) tags.set(tag, (tags.get(tag) ?? 0) + 1)
|
|
714
|
+
}
|
|
715
|
+
return {
|
|
716
|
+
...counts,
|
|
717
|
+
tags: [...tags.entries()]
|
|
718
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
719
|
+
.slice(0, 40)
|
|
720
|
+
.map(([name, count]) => ({ name, count })),
|
|
721
|
+
warnings: this.warnings,
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
async export(id) {
|
|
726
|
+
await this.ready()
|
|
727
|
+
const meta = this._byId.get(String(id))
|
|
728
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
729
|
+
const body = await this.readBody(meta.id)
|
|
730
|
+
const annotations = await this.listAnnotations(meta.id, body)
|
|
731
|
+
return { meta: publicMeta(meta), markdown: exportMarkdown(meta, body, annotations) }
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
async persist() {
|
|
735
|
+
const snapshot = { version: INDEX_VERSION, clips: this._clips }
|
|
736
|
+
this._writeChain = this._writeChain
|
|
737
|
+
.catch(() => {})
|
|
738
|
+
.then(() => writeJsonAtomic(this.indexPath, snapshot))
|
|
739
|
+
await this._writeChain
|
|
740
|
+
}
|
|
741
|
+
}
|