dsh-plugin-zotero 0.2.0 → 0.4.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.md +39 -24
- package/client/client.js +28119 -2121
- package/client/pdfjs/cmaps/78-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/78-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/78-H.bcmap +0 -0
- package/client/pdfjs/cmaps/78-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/78-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/78-V.bcmap +0 -0
- package/client/pdfjs/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Add-H.bcmap +0 -0
- package/client/pdfjs/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Add-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/client/pdfjs/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/client/pdfjs/cmaps/B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/B5pc-H.bcmap +0 -0
- package/client/pdfjs/cmaps/B5pc-V.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS1-H.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS1-V.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS2-H.bcmap +0 -0
- package/client/pdfjs/cmaps/CNS2-V.bcmap +3 -0
- package/client/pdfjs/cmaps/ETHK-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/ETHK-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/ETen-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/ETen-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/ETenms-B5-H.bcmap +3 -0
- package/client/pdfjs/cmaps/ETenms-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Ext-H.bcmap +0 -0
- package/client/pdfjs/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Ext-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GB-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GB-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GB-H.bcmap +4 -0
- package/client/pdfjs/cmaps/GB-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBK-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBK-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBK2K-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBK2K-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBT-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBT-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBT-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBT-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKdla-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKdla-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/HKm314-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKm314-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/HKm471-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKm471-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/HKscs-B5-H.bcmap +0 -0
- package/client/pdfjs/cmaps/HKscs-B5-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Hankaku.bcmap +0 -0
- package/client/pdfjs/cmaps/Hiragana.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-Johab-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-Johab-V.bcmap +0 -0
- package/client/pdfjs/cmaps/KSC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/client/pdfjs/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Katakana.bcmap +0 -0
- package/client/pdfjs/cmaps/LICENSE +36 -0
- package/client/pdfjs/cmaps/NWP-H.bcmap +0 -0
- package/client/pdfjs/cmaps/NWP-V.bcmap +0 -0
- package/client/pdfjs/cmaps/RKSJ-H.bcmap +0 -0
- package/client/pdfjs/cmaps/RKSJ-V.bcmap +0 -0
- package/client/pdfjs/cmaps/Roman.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/client/pdfjs/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/client/pdfjs/cmaps/V.bcmap +0 -0
- package/client/pdfjs/cmaps/WP-Symbol.bcmap +0 -0
- package/client/pdfjs/pdf.worker.min.mjs +29 -0
- package/client/pdfjs/standard_fonts/FoxitDingbats.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitFixed.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitFixedBold.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitFixedBoldItalic.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitFixedItalic.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitSerif.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitSerifBold.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitSerifBoldItalic.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitSerifItalic.pfb +0 -0
- package/client/pdfjs/standard_fonts/FoxitSymbol.pfb +0 -0
- package/client/pdfjs/standard_fonts/LICENSE_FOXIT +27 -0
- package/client/pdfjs/standard_fonts/LICENSE_LIBERATION +363 -0
- package/client/pdfjs/standard_fonts/LiberationSans-Bold.ttf +0 -0
- package/client/pdfjs/standard_fonts/LiberationSans-BoldItalic.ttf +0 -0
- package/client/pdfjs/standard_fonts/LiberationSans-Italic.ttf +0 -0
- package/client/pdfjs/standard_fonts/LiberationSans-Regular.ttf +0 -0
- package/client/pdfjs/wasm/LICENSE_JBIG2 +196 -0
- package/client/pdfjs/wasm/LICENSE_OPENJPEG +39 -0
- package/client/pdfjs/wasm/LICENSE_PDFJS_JBIG2 +13 -0
- package/client/pdfjs/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
- package/client/pdfjs/wasm/LICENSE_PDFJS_QCMS +19 -0
- package/client/pdfjs/wasm/LICENSE_QCMS +21 -0
- package/client/pdfjs/wasm/jbig2.wasm +0 -0
- package/client/pdfjs/wasm/jbig2_nowasm_fallback.js +15 -0
- package/client/pdfjs/wasm/openjpeg.wasm +0 -0
- package/client/pdfjs/wasm/openjpeg_nowasm_fallback.js +17 -0
- package/client/pdfjs/wasm/qcms_bg.wasm +0 -0
- package/client/pdfjs/wasm/quickjs-eval.js +16 -0
- package/client/pdfjs/wasm/quickjs-eval.wasm +0 -0
- package/lib/host/router.d.ts +1 -0
- package/lib/host/router.js +194 -5
- package/lib/host/static.d.ts +9 -0
- package/lib/host/static.js +100 -0
- package/lib/index.js +7 -1
- package/lib/zotero/annotations.d.ts +6 -1
- package/lib/zotero/annotations.js +6 -1
- package/lib/zotero/client.d.ts +51 -2
- package/lib/zotero/client.js +239 -15
- package/lib/zotero/config.d.ts +4 -0
- package/lib/zotero/config.js +4 -0
- package/lib/zotero/types.d.ts +34 -2
- package/package.json +3 -2
package/lib/zotero/client.js
CHANGED
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
* 要拿整目录引用必须走 `/items?itemKey=a,b&format=bib`(每批 ≤ 50)。
|
|
13
13
|
* - `?include=bib` 会让响应**丢掉 `data`**,因此条目详情与 bib 必须分两次请求。
|
|
14
14
|
*/
|
|
15
|
+
import { lstat } from 'node:fs/promises';
|
|
15
16
|
import { connect } from 'node:net';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
16
18
|
import { anchorAnnotations } from './annotations.js';
|
|
17
19
|
import { getSettings, libraryBase, MCP_CALL_TIMEOUT_MS, zoteroProbeUrl, } from './config.js';
|
|
18
20
|
/** 归一化后的 Zotero 访问错误。 */
|
|
@@ -86,6 +88,7 @@ function toAttachment(raw) {
|
|
|
86
88
|
return {
|
|
87
89
|
key: raw.key,
|
|
88
90
|
title: attachmentTitle(raw),
|
|
91
|
+
filename: raw.data?.filename ?? '',
|
|
89
92
|
contentType: raw.data?.contentType ?? '',
|
|
90
93
|
// 本地 API 只在 links.enclosure.length 上暴露大小;缺失时记 0。
|
|
91
94
|
size: raw.links?.enclosure?.length ?? 0,
|
|
@@ -339,6 +342,132 @@ export async function listCollections() {
|
|
|
339
342
|
};
|
|
340
343
|
});
|
|
341
344
|
}
|
|
345
|
+
/** 条目详情里解析分类路径用;60s 内复用,避免每开一条文献都多打一次 Zotero。 */
|
|
346
|
+
let collectionCache = null;
|
|
347
|
+
const COLLECTION_CACHE_MS = 60_000;
|
|
348
|
+
async function cachedCollections() {
|
|
349
|
+
if (collectionCache !== null && Date.now() - collectionCache.at < COLLECTION_CACHE_MS) {
|
|
350
|
+
return collectionCache.collections;
|
|
351
|
+
}
|
|
352
|
+
const collections = await listCollections();
|
|
353
|
+
collectionCache = { at: Date.now(), collections };
|
|
354
|
+
return collections;
|
|
355
|
+
}
|
|
356
|
+
/** 分类 key → 完整路径名(父分类在前,用 ` / ` 连接)。 */
|
|
357
|
+
function collectionPaths(collections, keys) {
|
|
358
|
+
const byKey = new Map(collections.map((collection) => [collection.key, collection]));
|
|
359
|
+
const paths = [];
|
|
360
|
+
for (const key of keys) {
|
|
361
|
+
const parts = [];
|
|
362
|
+
const seen = new Set();
|
|
363
|
+
let current = byKey.get(key);
|
|
364
|
+
while (current !== undefined && !seen.has(current.key)) {
|
|
365
|
+
seen.add(current.key);
|
|
366
|
+
parts.unshift(current.name);
|
|
367
|
+
current = current.parentKey === false ? undefined : byKey.get(current.parentKey);
|
|
368
|
+
}
|
|
369
|
+
if (parts.length > 0)
|
|
370
|
+
paths.push(parts.join(' / '));
|
|
371
|
+
}
|
|
372
|
+
return paths;
|
|
373
|
+
}
|
|
374
|
+
// ---- 标签 -------------------------------------------------------------------
|
|
375
|
+
/** 标签分页大小(本地 API 上限 1000)与总量上限。 */
|
|
376
|
+
const TAG_PAGE_SIZE = 1000;
|
|
377
|
+
const TAG_MAX = 5000;
|
|
378
|
+
/**
|
|
379
|
+
* 文库级标签列表。
|
|
380
|
+
*
|
|
381
|
+
* 本地 API 没有「按分类的标签」端点(`/tags` 上的 `collectionKey` / `q` 实测被忽略),
|
|
382
|
+
* 所以计数是**全库**的;范围用量另走 {@link listTagUsage}。
|
|
383
|
+
* 按名称去重(同名标签只可能来自分页重复),`meta.type` / `numItems` 缺失时
|
|
384
|
+
* 分别按手动标签与 0 处理,避免 UI 出现 `NaN`。
|
|
385
|
+
*/
|
|
386
|
+
export async function listTags() {
|
|
387
|
+
const seen = new Map();
|
|
388
|
+
let total = 0;
|
|
389
|
+
for (let start = 0; start < TAG_MAX; start += TAG_PAGE_SIZE) {
|
|
390
|
+
const { body, total: pageTotal } = await requestJson(`${libraryBase()}/tags?limit=${TAG_PAGE_SIZE}&start=${start}`);
|
|
391
|
+
const page = Array.isArray(body) ? body : [];
|
|
392
|
+
if (pageTotal !== null)
|
|
393
|
+
total = pageTotal;
|
|
394
|
+
for (const raw of page) {
|
|
395
|
+
const name = raw.tag?.trim() ?? '';
|
|
396
|
+
if (name === '' || seen.has(name))
|
|
397
|
+
continue;
|
|
398
|
+
seen.set(name, {
|
|
399
|
+
name,
|
|
400
|
+
count: raw.meta?.numItems ?? 0,
|
|
401
|
+
type: raw.meta?.type ?? 1,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
if (page.length < TAG_PAGE_SIZE)
|
|
405
|
+
break;
|
|
406
|
+
if (total > 0 && start + page.length >= total)
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
return { total: total || seen.size, tags: [...seen.values()] };
|
|
410
|
+
}
|
|
411
|
+
/** 标签用量扫描的分页大小(本地 API 单页上限 100)与扫描条目上限。 */
|
|
412
|
+
const USAGE_PAGE_SIZE = 100;
|
|
413
|
+
const USAGE_SCAN_MAX = 3000;
|
|
414
|
+
/**
|
|
415
|
+
* 当前范围(分类 / 虚拟分类 + 搜索词)内的标签用量。
|
|
416
|
+
*
|
|
417
|
+
* 只能扫条目现算:逐页取范围内的顶层条目,读每条的 `data.tags` 累加计数。
|
|
418
|
+
* 传多个 `collectionKeys` 时分目录扫描,并按条目 key 去重——同一篇条目可能
|
|
419
|
+
* 同时挂在父分类与子分类里,不能重复计数。超过 `USAGE_SCAN_MAX` 条的大范围
|
|
420
|
+
* 按前若干页截断,超大目录可能漏掉靠后的标签(本地 API 没有按分类列标签的端点)。
|
|
421
|
+
*/
|
|
422
|
+
export async function listTagUsage(options) {
|
|
423
|
+
const search = options.q?.trim() ?? '';
|
|
424
|
+
const counts = new Map();
|
|
425
|
+
const seenItems = new Set();
|
|
426
|
+
const bases = options.scope === 'publications'
|
|
427
|
+
? [`${libraryBase()}/publications/items`]
|
|
428
|
+
: options.scope === 'trash'
|
|
429
|
+
? [`${libraryBase()}/items/trash`]
|
|
430
|
+
: (options.collectionKeys ?? []).length > 0
|
|
431
|
+
? options.collectionKeys.map((key) => `${libraryBase()}/collections/${encode(key)}/items/top`)
|
|
432
|
+
: [`${libraryBase()}/items/top`];
|
|
433
|
+
let scanned = 0;
|
|
434
|
+
for (const base of bases) {
|
|
435
|
+
for (let start = 0; scanned < USAGE_SCAN_MAX; start += USAGE_PAGE_SIZE) {
|
|
436
|
+
const params = new URLSearchParams({ limit: String(USAGE_PAGE_SIZE), start: String(start) });
|
|
437
|
+
if (search !== '')
|
|
438
|
+
params.set('q', search);
|
|
439
|
+
const { body } = await requestJson(`${base}?${params.toString()}`);
|
|
440
|
+
const page = Array.isArray(body) ? body : [];
|
|
441
|
+
if (page.length === 0)
|
|
442
|
+
break;
|
|
443
|
+
scanned += page.length;
|
|
444
|
+
for (const raw of page) {
|
|
445
|
+
if (seenItems.has(raw.key))
|
|
446
|
+
continue;
|
|
447
|
+
seenItems.add(raw.key);
|
|
448
|
+
// 「未分类条目」没有专用端点,与列表一样靠 data.collections 筛。
|
|
449
|
+
if (options.scope === 'unfiled' && (raw.data?.collections ?? []).length > 0)
|
|
450
|
+
continue;
|
|
451
|
+
for (const tag of raw.data?.tags ?? []) {
|
|
452
|
+
const name = tag.tag?.trim() ?? '';
|
|
453
|
+
if (name === '')
|
|
454
|
+
continue;
|
|
455
|
+
const existing = counts.get(name);
|
|
456
|
+
if (existing === undefined)
|
|
457
|
+
counts.set(name, { name, count: 1, type: tag.type ?? 1 });
|
|
458
|
+
else
|
|
459
|
+
existing.count += 1;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (page.length < USAGE_PAGE_SIZE)
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
if (scanned >= USAGE_SCAN_MAX)
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
const tags = [...counts.values()];
|
|
469
|
+
return { total: tags.length, tags };
|
|
470
|
+
}
|
|
342
471
|
/** 「未分类条目」推导时最多扫描的顶层条目数(避免超大口库长时间扫描)。 */
|
|
343
472
|
const UNFILED_SCAN_MAX = 1000;
|
|
344
473
|
/** 一页扫描条数(本地 API 上限 100)。 */
|
|
@@ -379,31 +508,50 @@ async function itemLastRead(key) {
|
|
|
379
508
|
return null;
|
|
380
509
|
}
|
|
381
510
|
}
|
|
382
|
-
/** 把
|
|
383
|
-
function
|
|
384
|
-
|
|
511
|
+
/** 把 `q` 与标签过滤写进查询参数(列表与未分类扫描共用)。 */
|
|
512
|
+
function appendItemFilters(params, options) {
|
|
513
|
+
if (options.q && options.q.trim() !== '')
|
|
514
|
+
params.set('q', options.q.trim());
|
|
515
|
+
const tags = (options.tags ?? []).map((tag) => tag.trim()).filter((tag) => tag !== '');
|
|
516
|
+
if (tags.length === 0)
|
|
517
|
+
return;
|
|
518
|
+
if (options.tagMode === 'any') {
|
|
519
|
+
params.set('tag', tags.join(' || '));
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
for (const tag of tags)
|
|
523
|
+
params.append('tag', tag);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/** 范围内的顶层条目端点基址(条目列表与标签用量共用同一套范围规则)。 */
|
|
527
|
+
function itemListBase(options) {
|
|
528
|
+
return options.scope === 'publications'
|
|
385
529
|
? `${libraryBase()}/publications/items`
|
|
386
530
|
: options.scope === 'trash'
|
|
387
531
|
? `${libraryBase()}/items/trash`
|
|
388
532
|
: options.collectionKey
|
|
389
533
|
? `${libraryBase()}/collections/${encode(options.collectionKey)}/items/top`
|
|
390
534
|
: `${libraryBase()}/items/top`;
|
|
535
|
+
}
|
|
536
|
+
/** 把 options 折成「顶层条目」端点基址 + 查询参数。 */
|
|
537
|
+
function itemListQuery(options) {
|
|
391
538
|
const params = new URLSearchParams({
|
|
392
539
|
limit: String(options.limit),
|
|
393
540
|
start: String(options.offset),
|
|
394
541
|
});
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return { base, params };
|
|
542
|
+
appendItemFilters(params, options);
|
|
543
|
+
return { base: itemListBase(options), params };
|
|
398
544
|
}
|
|
399
545
|
/**
|
|
400
546
|
* 「未分类条目」:本地 API 没有专用端点,改为取顶层条目后筛掉已归类的
|
|
401
547
|
* (`data.collections` 非空即已归类),分页在筛完后作用于结果集。
|
|
402
548
|
*/
|
|
403
|
-
async function listUnfiled(
|
|
549
|
+
async function listUnfiled(options) {
|
|
404
550
|
const all = [];
|
|
405
551
|
for (let start = 0; start < UNFILED_SCAN_MAX; start += PAGE_SIZE) {
|
|
406
|
-
const
|
|
552
|
+
const params = new URLSearchParams({ limit: String(PAGE_SIZE), start: String(start) });
|
|
553
|
+
appendItemFilters(params, options);
|
|
554
|
+
const { body } = await requestJson(`${libraryBase()}/items/top?${params.toString()}`);
|
|
407
555
|
if (!Array.isArray(body) || body.length === 0)
|
|
408
556
|
break;
|
|
409
557
|
for (const raw of body) {
|
|
@@ -413,13 +561,13 @@ async function listUnfiled(offset, limit) {
|
|
|
413
561
|
if (body.length < PAGE_SIZE)
|
|
414
562
|
break;
|
|
415
563
|
}
|
|
416
|
-
const page = all.slice(offset, offset + limit);
|
|
564
|
+
const page = all.slice(options.offset, options.offset + options.limit);
|
|
417
565
|
return { total: all.length, items: await toSummaries(page) };
|
|
418
566
|
}
|
|
419
567
|
/** 顶层条目列表(按虚拟分类、真实分类、或全库,或关键字搜索)。 */
|
|
420
568
|
export async function listItems(options) {
|
|
421
569
|
if (options.scope === 'unfiled')
|
|
422
|
-
return listUnfiled(options
|
|
570
|
+
return listUnfiled(options);
|
|
423
571
|
const { base, params } = itemListQuery(options);
|
|
424
572
|
const { body, total } = await requestJson(`${base}?${params.toString()}`);
|
|
425
573
|
const raws = Array.isArray(body) ? body : [];
|
|
@@ -430,12 +578,42 @@ export async function getItemChildren(key) {
|
|
|
430
578
|
const { body } = await requestJson(`${libraryBase()}/items/${encode(key)}/children`);
|
|
431
579
|
return Array.isArray(body) ? body : [];
|
|
432
580
|
}
|
|
433
|
-
/**
|
|
434
|
-
|
|
581
|
+
/**
|
|
582
|
+
* 展示的标注类型。`ink` 没有 rects,只能进列表;其余类型都可在 PDF 上叠加。
|
|
583
|
+
*/
|
|
584
|
+
const ANNOTATION_TYPES = new Set(['highlight', 'underline', 'note', 'text', 'image', 'ink']);
|
|
435
585
|
/** 标注分页大小(本地 API 单页上限 100)与总量上限。 */
|
|
436
586
|
const ANNOTATION_PAGE_SIZE = 100;
|
|
437
587
|
const ANNOTATION_MAX = 1000;
|
|
438
|
-
|
|
588
|
+
function isRect(value) {
|
|
589
|
+
return Array.isArray(value)
|
|
590
|
+
&& value.length === 4
|
|
591
|
+
&& value.every((item) => typeof item === 'number' && Number.isFinite(item));
|
|
592
|
+
}
|
|
593
|
+
/** `annotationPosition` 是 JSON 字符串;结构不符按「无位置」处理。 */
|
|
594
|
+
function toPosition(value) {
|
|
595
|
+
if (typeof value !== 'string' || value === '')
|
|
596
|
+
return null;
|
|
597
|
+
let parsed;
|
|
598
|
+
try {
|
|
599
|
+
parsed = JSON.parse(value);
|
|
600
|
+
}
|
|
601
|
+
catch {
|
|
602
|
+
return null;
|
|
603
|
+
}
|
|
604
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
605
|
+
return null;
|
|
606
|
+
const { pageIndex, rects } = parsed;
|
|
607
|
+
if (typeof pageIndex !== 'number' || !Number.isInteger(pageIndex) || pageIndex < 0)
|
|
608
|
+
return null;
|
|
609
|
+
if (!Array.isArray(rects))
|
|
610
|
+
return null;
|
|
611
|
+
const valid = rects.filter(isRect);
|
|
612
|
+
if (valid.length === 0)
|
|
613
|
+
return null;
|
|
614
|
+
return { pageIndex, rects: valid };
|
|
615
|
+
}
|
|
616
|
+
/** 原始标注 → 展示用标注;未知类型返回 null。 */
|
|
439
617
|
function toAnnotation(raw) {
|
|
440
618
|
const data = raw.data ?? {};
|
|
441
619
|
const type = data.annotationType ?? '';
|
|
@@ -451,10 +629,11 @@ function toAnnotation(raw) {
|
|
|
451
629
|
color: data.annotationColor ?? '',
|
|
452
630
|
pageLabel: data.annotationPageLabel ?? '',
|
|
453
631
|
sortIndex: data.annotationSortIndex ?? '',
|
|
632
|
+
position: toPosition(data.annotationPosition),
|
|
454
633
|
};
|
|
455
634
|
}
|
|
456
635
|
/**
|
|
457
|
-
* 条目下所有 PDF
|
|
636
|
+
* 条目下所有 PDF 附件上的标注。
|
|
458
637
|
*
|
|
459
638
|
* 必须显式带 `itemType=annotation`:实测 `/children` 不带参数时不返回标注。
|
|
460
639
|
* 失败降级为空数组——标注是详情的附加信息,不能拖垮整个条目。
|
|
@@ -506,6 +685,48 @@ export async function getItemFulltext(attachmentKey) {
|
|
|
506
685
|
fallback: false,
|
|
507
686
|
};
|
|
508
687
|
}
|
|
688
|
+
/**
|
|
689
|
+
* 解析附件在磁盘上的真实路径。
|
|
690
|
+
*
|
|
691
|
+
* 路径只来自 Zotero 条目 JSON 的 `links.enclosure.href`(`file://` URL),
|
|
692
|
+
* 客户端传来的任何路径都不接受。`lstat` 拒绝目录与非普通文件;
|
|
693
|
+
* 链接附件(`linkMode: linked_file`)同样走这里,不限制必须在 storage 下。
|
|
694
|
+
*/
|
|
695
|
+
export async function getAttachmentFile(key) {
|
|
696
|
+
const { body: raw } = await requestJson(`${libraryBase()}/items/${encode(key)}`);
|
|
697
|
+
const data = raw.data ?? {};
|
|
698
|
+
if (data.itemType !== 'attachment') {
|
|
699
|
+
throw new ZoteroError('bad_request', `条目 ${key} 不是附件`);
|
|
700
|
+
}
|
|
701
|
+
const href = raw.links?.enclosure?.href ?? '';
|
|
702
|
+
if (href === '') {
|
|
703
|
+
throw new ZoteroError('zotero_not_found', `附件 ${key} 没有可读取的本地文件`);
|
|
704
|
+
}
|
|
705
|
+
let path;
|
|
706
|
+
try {
|
|
707
|
+
path = fileURLToPath(href);
|
|
708
|
+
}
|
|
709
|
+
catch {
|
|
710
|
+
throw new ZoteroError('zotero_not_found', `附件 ${key} 的文件地址无法解析`);
|
|
711
|
+
}
|
|
712
|
+
let stats;
|
|
713
|
+
try {
|
|
714
|
+
stats = await lstat(path);
|
|
715
|
+
}
|
|
716
|
+
catch {
|
|
717
|
+
throw new ZoteroError('zotero_not_found', `附件文件不存在:${path}`);
|
|
718
|
+
}
|
|
719
|
+
if (!stats.isFile()) {
|
|
720
|
+
throw new ZoteroError('zotero_not_found', `附件路径不是普通文件:${path}`);
|
|
721
|
+
}
|
|
722
|
+
return {
|
|
723
|
+
key,
|
|
724
|
+
path,
|
|
725
|
+
size: stats.size,
|
|
726
|
+
contentType: data.contentType ?? '',
|
|
727
|
+
filename: data.filename ?? attachmentTitle(raw),
|
|
728
|
+
};
|
|
729
|
+
}
|
|
509
730
|
/**
|
|
510
731
|
* 依次尝试候选附件的全文,返回第一份可用的。
|
|
511
732
|
*
|
|
@@ -538,9 +759,10 @@ export async function getItemDetail(key, style) {
|
|
|
538
759
|
const itemType = data.itemType ?? '';
|
|
539
760
|
const isAttachment = itemType === 'attachment';
|
|
540
761
|
// 顶层条目可能是附件本身(直接导入的 PDF),此时把自己也算作附件。
|
|
541
|
-
const [children, rawAnnotations] = await Promise.all([
|
|
762
|
+
const [children, rawAnnotations, collections] = await Promise.all([
|
|
542
763
|
isAttachment ? Promise.resolve([]) : getItemChildren(key),
|
|
543
764
|
itemType === 'note' ? Promise.resolve([]) : getItemAnnotations(key),
|
|
765
|
+
cachedCollections().catch(() => []),
|
|
544
766
|
]);
|
|
545
767
|
const childAttachments = children.filter((c) => c.data?.itemType === 'attachment');
|
|
546
768
|
const attachments = isAttachment ? [toAttachment(raw), ...childAttachments.map(toAttachment)] : childAttachments.map(toAttachment);
|
|
@@ -571,6 +793,7 @@ export async function getItemDetail(key, style) {
|
|
|
571
793
|
comment: annotation.comment,
|
|
572
794
|
color: annotation.color,
|
|
573
795
|
pageLabel: annotation.pageLabel,
|
|
796
|
+
position: annotation.position,
|
|
574
797
|
range: annotation.range,
|
|
575
798
|
}));
|
|
576
799
|
return {
|
|
@@ -585,6 +808,7 @@ export async function getItemDetail(key, style) {
|
|
|
585
808
|
abstract: data.abstractNote ?? '',
|
|
586
809
|
tags: (data.tags ?? []).map((tag) => tag.tag ?? '').filter((tag) => tag !== ''),
|
|
587
810
|
collections: data.collections ?? [],
|
|
811
|
+
collectionNames: collectionPaths(collections, data.collections ?? []),
|
|
588
812
|
attachments,
|
|
589
813
|
notes,
|
|
590
814
|
annotations,
|
package/lib/zotero/config.d.ts
CHANGED
|
@@ -31,6 +31,10 @@ export declare const ROUTE_PREFIX = "/dsh-zotero/api/";
|
|
|
31
31
|
* 匹配,带尾斜杠会变成 `.../api//health`,永远匹配不上。
|
|
32
32
|
*/
|
|
33
33
|
export declare const ROUTE_MATCH = "/dsh-zotero/api";
|
|
34
|
+
/** PDF.js 静态资产路由;与 API 路由分开注册。 */
|
|
35
|
+
export declare const STATIC_ROUTE_MATCH = "/dsh-zotero/static";
|
|
36
|
+
/** 静态资产路由的剥离基准(带尾斜杠)。 */
|
|
37
|
+
export declare const STATIC_ROUTE_PREFIX = "/dsh-zotero/static/";
|
|
34
38
|
/** 可在 dsh「插件」页里编辑的字段;默认值即上面的常量。 */
|
|
35
39
|
export interface ZoteroSettings {
|
|
36
40
|
zoteroBaseUrl: string;
|
package/lib/zotero/config.js
CHANGED
|
@@ -31,6 +31,10 @@ export const ROUTE_PREFIX = '/dsh-zotero/api/';
|
|
|
31
31
|
* 匹配,带尾斜杠会变成 `.../api//health`,永远匹配不上。
|
|
32
32
|
*/
|
|
33
33
|
export const ROUTE_MATCH = '/dsh-zotero/api';
|
|
34
|
+
/** PDF.js 静态资产路由;与 API 路由分开注册。 */
|
|
35
|
+
export const STATIC_ROUTE_MATCH = '/dsh-zotero/static';
|
|
36
|
+
/** 静态资产路由的剥离基准(带尾斜杠)。 */
|
|
37
|
+
export const STATIC_ROUTE_PREFIX = `${STATIC_ROUTE_MATCH}/`;
|
|
34
38
|
/** 默认读取器:入口未注入时回落到常量。 */
|
|
35
39
|
let readSettings = () => ({
|
|
36
40
|
zoteroBaseUrl: ZOTERO_BASE,
|
package/lib/zotero/types.d.ts
CHANGED
|
@@ -41,9 +41,19 @@ export interface ZoteroItemSummary {
|
|
|
41
41
|
export interface ZoteroAttachment {
|
|
42
42
|
key: string;
|
|
43
43
|
title: string;
|
|
44
|
+
/** 磁盘文件名,如 `paper.pdf`;缺失时为空串。 */
|
|
45
|
+
filename: string;
|
|
44
46
|
contentType: string;
|
|
45
47
|
size: number;
|
|
46
48
|
}
|
|
49
|
+
/** Host 侧解析出的附件文件(路径只来自 Zotero 条目 JSON)。 */
|
|
50
|
+
export interface ZoteroAttachmentFile {
|
|
51
|
+
key: string;
|
|
52
|
+
path: string;
|
|
53
|
+
size: number;
|
|
54
|
+
contentType: string;
|
|
55
|
+
filename: string;
|
|
56
|
+
}
|
|
47
57
|
/** 条目下的一条笔记(子条目 `itemType: "note"`)。 */
|
|
48
58
|
export interface ZoteroNote {
|
|
49
59
|
key: string;
|
|
@@ -58,13 +68,13 @@ export interface ZoteroNote {
|
|
|
58
68
|
/**
|
|
59
69
|
* 条目下的一条 PDF 标注(子条目 `itemType: "annotation"`)。
|
|
60
70
|
*
|
|
61
|
-
*
|
|
71
|
+
* 覆盖 Zotero 的六种标注类型;高亮与划线可互转,展示时以当前类型为准。
|
|
62
72
|
*/
|
|
63
73
|
export interface ZoteroAnnotation {
|
|
64
74
|
key: string;
|
|
65
75
|
/** 所属附件 key;用于判断标注是否落在当前展示全文的那份 PDF 上。 */
|
|
66
76
|
attachmentKey: string;
|
|
67
|
-
/** `highlight
|
|
77
|
+
/** `highlight | underline | note | text | image | ink`。 */
|
|
68
78
|
type: string;
|
|
69
79
|
/** PDF 中选中的原文。 */
|
|
70
80
|
text: string;
|
|
@@ -77,6 +87,14 @@ export interface ZoteroAnnotation {
|
|
|
77
87
|
color: string;
|
|
78
88
|
/** PDF 印刷页码标签;可能为空串。 */
|
|
79
89
|
pageLabel: string;
|
|
90
|
+
/**
|
|
91
|
+
* PDF 中的位置:`pageIndex` 从 0 开始,`rects` 为 PDF 用户空间(左下原点)的
|
|
92
|
+
* `[x1, y1, x2, y2]`。旧版 Zotero 未提供或墨迹标注时为 `null`。
|
|
93
|
+
*/
|
|
94
|
+
position: {
|
|
95
|
+
pageIndex: number;
|
|
96
|
+
rects: [number, number, number, number][];
|
|
97
|
+
} | null;
|
|
80
98
|
/** 在 `fulltext.content` 中的 UTF-16 区间;`null` 表示未能在全文里定位。 */
|
|
81
99
|
range: {
|
|
82
100
|
start: number;
|
|
@@ -97,6 +115,8 @@ export interface ZoteroItemDetail {
|
|
|
97
115
|
tags: string[];
|
|
98
116
|
/** 所属分类 key 列表。 */
|
|
99
117
|
collections: string[];
|
|
118
|
+
/** 所属分类的完整路径(如「博士论文 / 方法」),由 Host 解析好。 */
|
|
119
|
+
collectionNames: string[];
|
|
100
120
|
attachments: ZoteroAttachment[];
|
|
101
121
|
/** 该条目下的笔记(子条目 `itemType: "note"`)。 */
|
|
102
122
|
notes: ZoteroNote[];
|
|
@@ -140,6 +160,18 @@ export interface ZoteroItems {
|
|
|
140
160
|
total: number;
|
|
141
161
|
items: ZoteroItemSummary[];
|
|
142
162
|
}
|
|
163
|
+
/** 文库级标签(计数是全库的)。 */
|
|
164
|
+
export interface ZoteroTag {
|
|
165
|
+
name: string;
|
|
166
|
+
count: number;
|
|
167
|
+
/** 0 自动标签,1 手动标签。 */
|
|
168
|
+
type: number;
|
|
169
|
+
}
|
|
170
|
+
/** 标签列表数据。 */
|
|
171
|
+
export interface ZoteroTags {
|
|
172
|
+
total: number;
|
|
173
|
+
tags: ZoteroTag[];
|
|
174
|
+
}
|
|
143
175
|
/** 分类引用导出数据。 */
|
|
144
176
|
export interface ZoteroCollectionBib {
|
|
145
177
|
bib: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-zotero",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Install this plugin and DSH can operate your Zotero for you: search the library, read PDF full text, summarize highlights and notes, organize collections by topic, import your downloaded papers, and attach a translated version back to an item. The right sidebar also gains a Zotero library panel that brings an item, an excerpt or a whole collection into the conversation with one click.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc -p tsconfig.json",
|
|
40
|
-
"build:client": "tsdown",
|
|
40
|
+
"build:client": "tsdown && node scripts/copy-pdf-assets.mjs",
|
|
41
41
|
"typecheck:client": "tsc -p tsconfig.client.json --noEmit",
|
|
42
42
|
"prepack": "pnpm build && pnpm build:client"
|
|
43
43
|
},
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"@deepseek-ai/schemastery": "3.18.5",
|
|
53
53
|
"@types/node": "^24.19.2",
|
|
54
54
|
"@types/react": "~18.3.1",
|
|
55
|
+
"pdfjs-dist": "6.3.289",
|
|
55
56
|
"react": "^18.3.1",
|
|
56
57
|
"tsdown": "^0.22.0",
|
|
57
58
|
"typescript": "^5.9.0"
|