@li-canghai/dsh-paper 0.0.0-stage → 0.8.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/LICENSE +21 -0
- package/README.md +137 -2
- package/cordis.patch.yml +4 -0
- package/lib/annotate-tool.js +334 -0
- package/lib/arxiv-id.js +74 -0
- package/lib/asset-routes.js +210 -0
- package/lib/bibliography-formats.js +126 -0
- package/lib/bibliography-import.js +105 -0
- package/lib/bibliography-service.js +43 -0
- package/lib/bibliography.js +42 -0
- package/lib/block-authors.js +292 -0
- package/lib/block-cache.js +39 -0
- package/lib/blocks-markdown.js +157 -0
- package/lib/blocks-reconcile.js +218 -0
- package/lib/blocks.js +1344 -0
- package/lib/citation-metadata.js +260 -0
- package/lib/citation-styles.js +120 -0
- package/lib/client-parts/01-pure-helpers.js +2607 -0
- package/lib/client.js +9569 -0
- package/lib/code-source.js +68 -0
- package/lib/concurrency-gate.js +91 -0
- package/lib/context.js +233 -0
- package/lib/csl/SOURCES.md +10 -0
- package/lib/csl/gb-t-7714.csl +441 -0
- package/lib/csl/ieee.csl +520 -0
- package/lib/csl/locales-zh-CN.xml +438 -0
- package/lib/csl/nature.csl +190 -0
- package/lib/discussion.js +400 -0
- package/lib/documents.js +67 -0
- package/lib/export.js +848 -0
- package/lib/file-lock.js +177 -0
- package/lib/gloss.js +181 -0
- package/lib/glossary.js +279 -0
- package/lib/index.js +3862 -0
- package/lib/inline-html.js +53 -0
- package/lib/install-dependency.js +120 -0
- package/lib/item-ops.js +83 -0
- package/lib/job-facade.js +58 -0
- package/lib/json-store.js +54 -0
- package/lib/library-model.js +132 -0
- package/lib/library-service.js +180 -0
- package/lib/library-tool.js +982 -0
- package/lib/library.js +814 -0
- package/lib/markdown-assets.js +138 -0
- package/lib/markdown-literals.js +93 -0
- package/lib/metadata-enrichment.js +516 -0
- package/lib/metadata-fetch.js +146 -0
- package/lib/metadata-node.js +158 -0
- package/lib/metadata-proxy.ps1 +35 -0
- package/lib/mineru-config.js +95 -0
- package/lib/mineru-sdk-helper.py +123 -0
- package/lib/mineru-sdk.js +182 -0
- package/lib/mineru.js +268 -0
- package/lib/mirror-export.js +240 -0
- package/lib/outline.js +296 -0
- package/lib/page-image-helper.py +294 -0
- package/lib/page-image.js +454 -0
- package/lib/paper-metadata.js +242 -0
- package/lib/parse-export.js +631 -0
- package/lib/pdf-analysis-tool.js +750 -0
- package/lib/pdf-metadata-helper.py +75 -0
- package/lib/pdf-metadata.js +141 -0
- package/lib/pdf-page.js +183 -0
- package/lib/pdf-scan.js +134 -0
- package/lib/pdf-text.js +68 -0
- package/lib/picker.js +305 -0
- package/lib/portable.js +379 -0
- package/lib/purge.js +170 -0
- package/lib/read-tool.js +660 -0
- package/lib/reader-store.js +497 -0
- package/lib/reading-order.js +207 -0
- package/lib/rename-document.js +480 -0
- package/lib/rename.js +372 -0
- package/lib/renderer-status.js +161 -0
- package/lib/resolver.js +1152 -0
- package/lib/result-archive.js +157 -0
- package/lib/revisions.js +330 -0
- package/lib/search-database.js +62 -0
- package/lib/search-index.js +601 -0
- package/lib/search-scan.js +185 -0
- package/lib/search-source.js +23 -0
- package/lib/search-terms.js +539 -0
- package/lib/section-audit.js +209 -0
- package/lib/selection-message.js +332 -0
- package/lib/serial-tail.js +21 -0
- package/lib/slicing.js +425 -0
- package/lib/source-resolver.js +581 -0
- package/lib/stem-repair.js +189 -0
- package/lib/storage.js +562 -0
- package/lib/store-paths.js +253 -0
- package/lib/table-gfm.js +236 -0
- package/lib/tasks.js +270 -0
- package/lib/tex-align.js +509 -0
- package/lib/tex-blocks.js +653 -0
- package/lib/tex-fetch.js +455 -0
- package/lib/tex-figures.js +140 -0
- package/lib/tex-inline.js +166 -0
- package/lib/tex-merge.js +279 -0
- package/lib/tex-pipeline.js +242 -0
- package/lib/tex-prepare.js +746 -0
- package/lib/tex-tables.js +210 -0
- package/lib/text-similarity.js +85 -0
- package/lib/tool-contract.js +86 -0
- package/lib/tool-host.js +1058 -0
- package/lib/tool-identity.js +214 -0
- package/lib/translatable-text.js +48 -0
- package/lib/translate-prompts.js +709 -0
- package/lib/translate-queue.js +418 -0
- package/lib/translate-task.js +158 -0
- package/lib/translate.js +909 -0
- package/lib/update-check.js +438 -0
- package/lib/update-contracts.js +75 -0
- package/lib/usage.js +282 -0
- package/package.json +55 -4
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { createLibraryModel } from './library-model.js'
|
|
4
|
+
import { readLibrary, writeLibrary, readItem, writeItem, setTrashed, MAX_TAG_LENGTH, MAX_TAGS_PER_ITEM } from './library.js'
|
|
5
|
+
|
|
6
|
+
const model = createLibraryModel()
|
|
7
|
+
const idsOf = value => {
|
|
8
|
+
if (!Array.isArray(value) || !value.length || value.some(id => typeof id !== 'string' || !/^[a-f0-9]{64}$/.test(id))) throw new Error('论文 ID 列表无效')
|
|
9
|
+
return [...new Set(value)]
|
|
10
|
+
}
|
|
11
|
+
const tagOf = value => {
|
|
12
|
+
if (typeof value !== 'string' || !value.trim() || value.trim().length > MAX_TAG_LENGTH) throw new Error(`标签须为 1–${MAX_TAG_LENGTH} 个字符`)
|
|
13
|
+
return value.trim()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Called inside the host's serialized library request channel. Item assets are never rewritten by migration.
|
|
18
|
+
*
|
|
19
|
+
* `withWrite` 是**跨实例**的那一层(进程内队列只管得住本进程):library.json 装着集合、
|
|
20
|
+
* 保存搜索与偏好,两个旧快照互相覆盖整份文件时,连带消失的是用户刚建的集合 ——
|
|
21
|
+
* 实测:`sort` 拿到旧库、集合刚建好、`sort` 回写,集合数从 1 变 0,两次都报成功。
|
|
22
|
+
*/
|
|
23
|
+
export function createLibraryService({ path, items, itemPath, withWrite }) {
|
|
24
|
+
/** 包一层写锁;没有注入时照旧执行(单独使用本模块的测试不需要)。 */
|
|
25
|
+
const locked = operation => (typeof withWrite === 'function' ? withWrite(operation) : operation())
|
|
26
|
+
async function load() {
|
|
27
|
+
const library = await readLibrary(path)
|
|
28
|
+
if (library.collectionsMigrated) return library
|
|
29
|
+
let original
|
|
30
|
+
try { original = await readFile(path, 'utf8') } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
31
|
+
if (original?.trim()) {
|
|
32
|
+
const raw = JSON.parse(original)
|
|
33
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('文献库文件损坏,已停止迁移')
|
|
34
|
+
// Exclusive backup creation keeps the first pre-migration image across retries.
|
|
35
|
+
try { await writeFile(path + '.v1.bak', original, { flag: 'wx' }) } catch (error) { if (error.code !== 'EEXIST') throw error }
|
|
36
|
+
}
|
|
37
|
+
const entries = await items()
|
|
38
|
+
const next = { ...library, collectionsMigrated: true, categories: [],
|
|
39
|
+
hidden: library.hidden.filter(key => !key.startsWith('c:')),
|
|
40
|
+
pinned: library.pinned.filter(key => !key.startsWith('c:')),
|
|
41
|
+
collections: library.collections.length ? library.collections : library.categories.map(name => ({
|
|
42
|
+
id: randomUUID(), name, parentId: null,
|
|
43
|
+
documentIds: entries.filter(entry => entry.item.tags.some(tag => model.lower(tag) === model.lower(name))).map(entry => entry.documentId),
|
|
44
|
+
})) }
|
|
45
|
+
return writeLibrary(path, next)
|
|
46
|
+
}
|
|
47
|
+
async function updateItem(documentId, transform) {
|
|
48
|
+
const target = await itemPath(documentId)
|
|
49
|
+
if (!target) throw new Error('论文没有可写入的条目文件')
|
|
50
|
+
return writeItem(target, transform(await readItem(target)))
|
|
51
|
+
}
|
|
52
|
+
async function batch(ids, transform) {
|
|
53
|
+
const succeeded = [], failures = []
|
|
54
|
+
for (const id of ids) {
|
|
55
|
+
try { await updateItem(id, item => transform(item, id)); succeeded.push(id) }
|
|
56
|
+
catch (error) { failures.push({ documentId: id, error: error.message }) }
|
|
57
|
+
}
|
|
58
|
+
return { succeeded, failures }
|
|
59
|
+
}
|
|
60
|
+
async function handle(body) {
|
|
61
|
+
return locked(() => handleLocked(body))
|
|
62
|
+
}
|
|
63
|
+
async function handleLocked(body) {
|
|
64
|
+
const action = body.action
|
|
65
|
+
if (!action?.startsWith('collection-') && !action?.startsWith('search-') && !action?.startsWith('tags-') && action !== 'library-preferences' && action !== 'items-batch') return null
|
|
66
|
+
const library = await load()
|
|
67
|
+
let next = { ...library }, report = {}
|
|
68
|
+
const getCollection = () => {
|
|
69
|
+
const row = model.collection(library, body.collectionId)
|
|
70
|
+
if (!row) throw new Error('集合不存在')
|
|
71
|
+
return row
|
|
72
|
+
}
|
|
73
|
+
if (action === 'items-batch') {
|
|
74
|
+
if (!['trash', 'restore'].includes(body.operation)) throw new Error('批量条目操作无效')
|
|
75
|
+
return { library, ...await batch(idsOf(body.documentIds), item => setTrashed(item, body.operation === 'trash')) }
|
|
76
|
+
} else if (action === 'collection-create') {
|
|
77
|
+
const row = model.validateCollection(library, { id: randomUUID(), name: body.name, parentId: body.parentId ?? null, documentIds: [] })
|
|
78
|
+
next.collections = [...library.collections, row]; report.collectionId = row.id
|
|
79
|
+
} else if (action === 'collection-update') {
|
|
80
|
+
const row = getCollection()
|
|
81
|
+
const updated = model.validateCollection(library, { ...row, name: body.name ?? row.name, parentId: body.parentId === undefined ? row.parentId : body.parentId })
|
|
82
|
+
next.collections = library.collections.map(entry => entry.id === row.id ? updated : entry)
|
|
83
|
+
} else if (action === 'collection-remove') {
|
|
84
|
+
const row = getCollection()
|
|
85
|
+
const removed = new Set(model.descendants(library, row.id))
|
|
86
|
+
if (body.trashItems === true) {
|
|
87
|
+
if (body.confirm !== true) throw new Error('删除集合并移入回收站需要确认')
|
|
88
|
+
report = await batch(model.memberIds(library, row.id, true), item => setTrashed(item, true))
|
|
89
|
+
// Keep the tree available for retry if any item's trash write failed.
|
|
90
|
+
if (report.failures.length) return { library, ...report }
|
|
91
|
+
}
|
|
92
|
+
next.collections = library.collections.filter(entry => !removed.has(entry.id))
|
|
93
|
+
} else if (action === 'collection-merge') {
|
|
94
|
+
// 把来源集合的全部成员**并入**目标集合(并集),然后删掉来源集合。
|
|
95
|
+
// 语义没有歧义:一篇可以同属多个集合,所以「并入」不是「搬走」—— 目标集合只增不减。
|
|
96
|
+
const target = getCollection()
|
|
97
|
+
const source = model.collection(library, body.sourceCollectionId)
|
|
98
|
+
if (!source) throw new Error('来源集合不存在')
|
|
99
|
+
if (source.id === target.id) throw new Error('不能把集合并入它自己')
|
|
100
|
+
// 目标集合若是来源的后代,并完再删来源会把目标一起删掉(descendants 含自身子树)。
|
|
101
|
+
if (model.descendants(library, source.id).includes(target.id)) throw new Error('目标集合在来源集合的子树里,并入后会被一起删除')
|
|
102
|
+
const merged = [...new Set([...target.documentIds, ...model.memberIds(library, source.id, true)])]
|
|
103
|
+
const removed = new Set(model.descendants(library, source.id))
|
|
104
|
+
next.collections = library.collections
|
|
105
|
+
.filter(row => !removed.has(row.id))
|
|
106
|
+
.map(row => row.id === target.id ? { ...row, documentIds: merged } : row)
|
|
107
|
+
report.merged = merged.length - target.documentIds.length
|
|
108
|
+
} else if (['collection-add-items', 'collection-remove-items', 'collection-move-items'].includes(action)) {
|
|
109
|
+
const target = getCollection(), ids = idsOf(body.documentIds)
|
|
110
|
+
const known = new Set((await items()).map(entry => entry.documentId))
|
|
111
|
+
if (ids.some(id => !known.has(id))) throw new Error('论文不存在')
|
|
112
|
+
const source = action === 'collection-move-items' ? model.collection(library, body.sourceCollectionId) : null
|
|
113
|
+
if (action === 'collection-move-items' && !source) throw new Error('来源集合不存在')
|
|
114
|
+
// keepInSource:move-items 默认「搬走」(从来源移除);给了 true 才是「只加入不移动」。
|
|
115
|
+
// 不写清这一条,一篇同时属于多个集合时实现出来就是错的。
|
|
116
|
+
const keepInSource = body.keepInSource === true
|
|
117
|
+
next.collections = library.collections.map(row => {
|
|
118
|
+
if (row.id === target.id) return { ...row, documentIds: action === 'collection-remove-items' ? row.documentIds.filter(id => !ids.includes(id)) : [...new Set([...row.documentIds, ...ids])] }
|
|
119
|
+
if (source && row.id === source.id && !keepInSource) return { ...row, documentIds: row.documentIds.filter(id => !ids.includes(id)) }
|
|
120
|
+
return row
|
|
121
|
+
})
|
|
122
|
+
} else if (action === 'library-preferences') {
|
|
123
|
+
if (typeof body.showSubcollections !== 'boolean') throw new Error('子集合显示选项无效')
|
|
124
|
+
next.showSubcollections = body.showSubcollections
|
|
125
|
+
} else if (action === 'search-save') {
|
|
126
|
+
const search = model.validateSearch({ ...body.search, id: body.search?.id || randomUUID() }, library)
|
|
127
|
+
if (body.search?.id && !library.savedSearches.some(row => row.id === body.search.id)) throw new Error('保存搜索不存在')
|
|
128
|
+
next.savedSearches = [...library.savedSearches.filter(row => row.id !== search.id), search]
|
|
129
|
+
report.searchId = search.id
|
|
130
|
+
} else if (action === 'search-remove') {
|
|
131
|
+
next.savedSearches = library.savedSearches.filter(row => row.id !== body.searchId)
|
|
132
|
+
} else if (action === 'tags-color') {
|
|
133
|
+
const tag = tagOf(body.tag)
|
|
134
|
+
next.tagColors = library.tagColors.filter(row => model.lower(row.tag) !== model.lower(tag))
|
|
135
|
+
if (body.color !== null) {
|
|
136
|
+
if (typeof body.color !== 'string' || !/^#[0-9a-f]{6}$/i.test(body.color) || !Number.isInteger(body.slot) || body.slot < 1 || body.slot > 9) throw new Error('标签颜色或快捷键无效')
|
|
137
|
+
if (next.tagColors.some(row => row.slot === body.slot)) throw new Error('此数字已分配给其他标签')
|
|
138
|
+
next.tagColors.push({ tag, color: body.color, slot: body.slot })
|
|
139
|
+
}
|
|
140
|
+
} else if (['tags-add', 'tags-remove', 'tags-toggle', 'tags-rename', 'tags-delete'].includes(action)) {
|
|
141
|
+
const tag = tagOf(body.tag), key = model.lower(tag)
|
|
142
|
+
const global = action === 'tags-rename' || action === 'tags-delete'
|
|
143
|
+
const entries = await items()
|
|
144
|
+
const ids = global ? entries.filter(entry => entry.item.tags.some(value => model.lower(value) === key)).map(entry => entry.documentId) : idsOf(body.documentIds)
|
|
145
|
+
const replacement = action === 'tags-rename' ? tagOf(body.next) : null
|
|
146
|
+
const remove = action === 'tags-remove' || action === 'tags-delete' || (action === 'tags-toggle' && ids.every(id => entries.find(entry => entry.documentId === id)?.item.tags.some(value => model.lower(value) === key)))
|
|
147
|
+
report = await batch(ids, item => {
|
|
148
|
+
let tags = item.tags
|
|
149
|
+
if (replacement) tags = tags.map(value => model.lower(value) === key ? replacement : value)
|
|
150
|
+
else if (remove) tags = tags.filter(value => model.lower(value) !== key)
|
|
151
|
+
else if (!tags.some(value => model.lower(value) === key)) {
|
|
152
|
+
if (tags.length >= MAX_TAGS_PER_ITEM) throw new Error(`每篇最多 ${MAX_TAGS_PER_ITEM} 个标签`)
|
|
153
|
+
tags = [...tags, tag]
|
|
154
|
+
}
|
|
155
|
+
return { ...item, tags }
|
|
156
|
+
})
|
|
157
|
+
if (global && report.failures.length === 0) {
|
|
158
|
+
next.tagColors = library.tagColors.flatMap(row => model.lower(row.tag) !== key ? [row]
|
|
159
|
+
: replacement && !library.tagColors.some(other => model.lower(other.tag) === model.lower(replacement) && model.lower(other.tag) !== key) ? [{ ...row, tag: replacement }] : [])
|
|
160
|
+
if (replacement) next.savedSearches = library.savedSearches.map(search => ({ ...search, conditions: search.conditions.map(row => row.field === 'tag' && model.lower(row.value) === key ? { ...row, value: replacement } : row) }))
|
|
161
|
+
}
|
|
162
|
+
} else throw new Error('文献库操作无效')
|
|
163
|
+
return { library: await writeLibrary(path, next), ...report }
|
|
164
|
+
}
|
|
165
|
+
async function forget(ids) {
|
|
166
|
+
return locked(async () => {
|
|
167
|
+
const library = await load()
|
|
168
|
+
await writeLibrary(path, { ...library, collections: library.collections.map(row => ({ ...row, documentIds: row.documentIds.filter(id => !ids.includes(id)) })) })
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
async function attachImported(collectionId, ids) {
|
|
172
|
+
return locked(async () => {
|
|
173
|
+
const library = await load()
|
|
174
|
+
if (!model.collection(library, collectionId)) throw new Error('论文已导入,但目标集合已删除,请重新归类')
|
|
175
|
+
await writeLibrary(path, { ...library, collections: library.collections.map(row => row.id === collectionId
|
|
176
|
+
? { ...row, documentIds: [...new Set([...row.documentIds, ...ids])] } : row) })
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
return { load, handle, forget, attachImported }
|
|
180
|
+
}
|