@li-canghai/dsh-paper 0.0.0-stage → 0.8.1

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 (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +150 -2
  3. package/cordis.patch.yml +4 -0
  4. package/lib/annotate-tool.js +334 -0
  5. package/lib/arxiv-id.js +74 -0
  6. package/lib/asset-routes.js +210 -0
  7. package/lib/bibliography-formats.js +126 -0
  8. package/lib/bibliography-import.js +105 -0
  9. package/lib/bibliography-service.js +43 -0
  10. package/lib/bibliography.js +42 -0
  11. package/lib/block-authors.js +292 -0
  12. package/lib/block-cache.js +39 -0
  13. package/lib/blocks-markdown.js +157 -0
  14. package/lib/blocks-reconcile.js +218 -0
  15. package/lib/blocks.js +1344 -0
  16. package/lib/citation-metadata.js +260 -0
  17. package/lib/citation-styles.js +120 -0
  18. package/lib/client-parts/01-pure-helpers.js +2762 -0
  19. package/lib/client-parts/02-author-year.js +95 -0
  20. package/lib/client.js +10381 -0
  21. package/lib/code-source.js +68 -0
  22. package/lib/concurrency-gate.js +91 -0
  23. package/lib/context.js +233 -0
  24. package/lib/csl/SOURCES.md +10 -0
  25. package/lib/csl/gb-t-7714.csl +441 -0
  26. package/lib/csl/ieee.csl +520 -0
  27. package/lib/csl/locales-zh-CN.xml +438 -0
  28. package/lib/csl/nature.csl +190 -0
  29. package/lib/discussion.js +400 -0
  30. package/lib/documents.js +67 -0
  31. package/lib/export.js +848 -0
  32. package/lib/file-lock.js +177 -0
  33. package/lib/gloss.js +181 -0
  34. package/lib/glossary.js +279 -0
  35. package/lib/index.js +3862 -0
  36. package/lib/inline-html.js +53 -0
  37. package/lib/install-dependency.js +120 -0
  38. package/lib/item-ops.js +83 -0
  39. package/lib/job-facade.js +58 -0
  40. package/lib/json-store.js +54 -0
  41. package/lib/library-model.js +132 -0
  42. package/lib/library-service.js +180 -0
  43. package/lib/library-tool.js +982 -0
  44. package/lib/library.js +814 -0
  45. package/lib/markdown-assets.js +138 -0
  46. package/lib/markdown-literals.js +93 -0
  47. package/lib/metadata-enrichment.js +516 -0
  48. package/lib/metadata-fetch.js +146 -0
  49. package/lib/metadata-node.js +158 -0
  50. package/lib/metadata-proxy.ps1 +35 -0
  51. package/lib/mineru-config.js +95 -0
  52. package/lib/mineru-sdk-helper.py +123 -0
  53. package/lib/mineru-sdk.js +182 -0
  54. package/lib/mineru.js +268 -0
  55. package/lib/mirror-export.js +240 -0
  56. package/lib/outline.js +296 -0
  57. package/lib/page-image-helper.py +308 -0
  58. package/lib/page-image.js +454 -0
  59. package/lib/paper-metadata.js +242 -0
  60. package/lib/parse-export.js +631 -0
  61. package/lib/pdf-analysis-tool.js +750 -0
  62. package/lib/pdf-metadata-helper.py +75 -0
  63. package/lib/pdf-metadata.js +141 -0
  64. package/lib/pdf-page.js +261 -0
  65. package/lib/pdf-scan.js +134 -0
  66. package/lib/pdf-text.js +68 -0
  67. package/lib/picker.js +305 -0
  68. package/lib/portable.js +379 -0
  69. package/lib/purge.js +170 -0
  70. package/lib/read-tool.js +660 -0
  71. package/lib/reader-store.js +497 -0
  72. package/lib/reading-order.js +207 -0
  73. package/lib/rename-document.js +480 -0
  74. package/lib/rename.js +372 -0
  75. package/lib/renderer-status.js +161 -0
  76. package/lib/resolver.js +1152 -0
  77. package/lib/result-archive.js +157 -0
  78. package/lib/revisions.js +330 -0
  79. package/lib/search-database.js +62 -0
  80. package/lib/search-index.js +601 -0
  81. package/lib/search-scan.js +185 -0
  82. package/lib/search-source.js +23 -0
  83. package/lib/search-terms.js +539 -0
  84. package/lib/section-audit.js +209 -0
  85. package/lib/selection-message.js +332 -0
  86. package/lib/serial-tail.js +21 -0
  87. package/lib/slicing.js +425 -0
  88. package/lib/source-resolver.js +581 -0
  89. package/lib/stem-repair.js +189 -0
  90. package/lib/storage.js +562 -0
  91. package/lib/store-paths.js +253 -0
  92. package/lib/table-gfm.js +236 -0
  93. package/lib/tasks.js +270 -0
  94. package/lib/tex-align.js +509 -0
  95. package/lib/tex-blocks.js +653 -0
  96. package/lib/tex-fetch.js +455 -0
  97. package/lib/tex-figures.js +140 -0
  98. package/lib/tex-inline.js +166 -0
  99. package/lib/tex-merge.js +279 -0
  100. package/lib/tex-pipeline.js +242 -0
  101. package/lib/tex-prepare.js +746 -0
  102. package/lib/tex-tables.js +210 -0
  103. package/lib/text-similarity.js +85 -0
  104. package/lib/tool-contract.js +86 -0
  105. package/lib/tool-host.js +1058 -0
  106. package/lib/tool-identity.js +214 -0
  107. package/lib/translatable-text.js +48 -0
  108. package/lib/translate-prompts.js +709 -0
  109. package/lib/translate-queue.js +418 -0
  110. package/lib/translate-task.js +158 -0
  111. package/lib/translate.js +909 -0
  112. package/lib/update-check.js +438 -0
  113. package/lib/update-contracts.js +75 -0
  114. package/lib/usage.js +282 -0
  115. package/package.json +55 -4
@@ -0,0 +1,218 @@
1
+ /**
2
+ * 重解析时的块对齐:尽量保留旧块 id,让挂在其上的笔记与划线不错位。
3
+ *
4
+ * 为什么需要它:块 id 由「页码 + 页内同类型序号」派生(见 blocks.js)。重解析后只要
5
+ * 某页多出或少了**任何一个同类型块**,该块之后的同类块序号全部平移 —— 实测一次插入
6
+ * 会让 92% 的 id 变化(样本 329:274 块 → 275 块,253 个 id 变化)。笔记锚在 id 上,
7
+ * 不做事后对齐就等于每次重解析都把笔记甩到别处。
8
+ *
9
+ * 旧 id 从哪来:旧 Markdown 里的 `<!-- b:id -->` 标记(见 markBlocks)。不落盘块列表。
10
+ * 标记只能恢复 id 与文本;但 **id 自带 page 与 type**(`p1-h1` → 第 1 页的 heading),
11
+ * 所以「page + type + 文本」三要素齐全,**唯独 bbox 恢复不了** —— 算法因此不依赖几何匹配。
12
+ *
13
+ * 纯函数模块:无 IO、无时钟、无随机。同一对输入两次调用结果逐字节一致。
14
+ *
15
+ * @module dsh-paper/blocks-reconcile
16
+ */
17
+ import { stripBlockMarkers } from './blocks.js'
18
+
19
+ /** 标记行本身;与 blocks.js 的 markBlocks 同格式。 */
20
+ const MARKER_LINE = /^<!-- b:(\S+) -->$/
21
+
22
+ /**
23
+ * id 里的类型字母 → 块类型名。
24
+ * 必须与 blocks.js 的 LETTER_BY_KIND 互为逆映射:锚点的 type 从 id 字母解出,
25
+ * 而新块的 type 是完整类型名,两边不归一就永远匹配不上(首版的实际缺陷)。
26
+ */
27
+ const KIND_BY_LETTER = { h: 'heading', p: 'para', l: 'list', e: 'math', t: 'table', f: 'figure', c: 'code', r: 'references', n: 'note' }
28
+
29
+ /** 相似度判定的最小归一化长度:太短的文本(作者名、页码)靠前缀匹配没有区分度。 */
30
+ const MIN_SIMILARITY_TEXT = 24
31
+
32
+ /** 顺序约束下允许向前回看的窗口(块数)。重解析的漂移通常只影响局部。 */
33
+ const LOOKBACK = 8
34
+
35
+ /** Markdown 标题前缀(`#`/`##`…)。heading 块在 Markdown 里带它,而块的 sourceText 不带。 */
36
+ const HEADING_PREFIX = /^#{1,6}\s+/
37
+
38
+ /**
39
+ * 归一化文本:剥掉 Markdown 标题前缀、折叠空白、去首尾。
40
+ *
41
+ * 剥前缀是必需的:Markdown 里标题写作 `## Abstract`,而 MinerU 给的块 sourceText 是
42
+ * `Abstract`。不剥就永远匹配不上 —— 实测会让**全部 heading** 落进 tombstones
43
+ * (样本 329:47 个未认领里 44 个是 heading)。
44
+ */
45
+ function normalizeText(value) {
46
+ return String(value ?? '').replace(/\s+/g, ' ').trim().replace(HEADING_PREFIX, '').trim()
47
+ }
48
+
49
+ /**
50
+ * 从一份带标记的 Markdown 反解出旧锚点。
51
+ *
52
+ * 每个锚点带 id、page、type 与它标记的那段文本。id 形如 `p3-p4`:`p` 后面是页码,
53
+ * 连字符后是类型字母与页内序号 —— page 与 type 因此无需额外存储。
54
+ *
55
+ * 解析不出页码/类型(手工改过标记、旧格式)的条目**跳过**:拿不准的锚点不参与对齐,
56
+ * 比让它错配到一个无辜的块上好。
57
+ *
58
+ * @param {string} markdown 带 `<!-- b:id -->` 标记的 Markdown。
59
+ * @returns {Array<{id: string, page: number, type: string, text: string}>} 按文档顺序的锚点。
60
+ */
61
+ export function anchorsOf(markdown) {
62
+ const text = stripBlockMarkers(markdown)
63
+ const lines = String(markdown ?? '').split('\n')
64
+ const anchors = []
65
+ for (let index = 0; index < lines.length; index += 1) {
66
+ const matched = MARKER_LINE.exec(lines[index].trim())
67
+ if (matched === null) continue
68
+ const id = matched[1]
69
+ const parsed = /^p(\d+)-([a-z])(\d+)$/.exec(id)
70
+ if (parsed === null) continue
71
+ const type = KIND_BY_LETTER[parsed[2]]
72
+ if (type === undefined) continue
73
+ let body = ''
74
+ for (let next = index + 1; next < lines.length; next += 1) {
75
+ if (MARKER_LINE.test(lines[next].trim())) break
76
+ body += `${lines[next]}\n`
77
+ }
78
+ anchors.push({ id, page: Number(parsed[1]), type, text: normalizeText(body) })
79
+ }
80
+ // text 参数只用于「这份 Markdown 确实不含标记时快速返回」;真正的文本取自 lines。
81
+ if (text === '' && anchors.length === 0) return []
82
+ return anchors
83
+ }
84
+
85
+ /** 两个归一化文本的相似度:前缀命中给满分,否则按最长公共前缀比例给分。 */
86
+ function similarity(left, right) {
87
+ if (left === '' || right === '') return 0
88
+ if (left === right) return 1
89
+ if (left.startsWith(right) || right.startsWith(left)) {
90
+ return Math.min(left.length, right.length) / Math.max(left.length, right.length)
91
+ }
92
+ const limit = Math.min(left.length, right.length)
93
+ let shared = 0
94
+ while (shared < limit && left[shared] === right[shared]) shared += 1
95
+ return shared / Math.max(left.length, right.length)
96
+ }
97
+
98
+ /**
99
+ * 为一个让路的新块找一个**同方案**的空闲 id:`p1-p2` 被占就试 `p1-p3`、`p1-p4`…
100
+ *
101
+ * 不能用 `p1-p2#2` 这类后缀 —— anchorsOf 的正则只认 `p<页>-<字母><序号>`,
102
+ * 带后缀的 id 下一轮反解不出来,那个块会永远被当成新块(id 不稳定)。
103
+ *
104
+ * @param {string} id 原机械 id。 @param {Set<string>} taken 已被占用的 id。
105
+ * @returns {string} 可用的同方案 id。
106
+ */
107
+ function freeIdNear(id, taken) {
108
+ const parsed = /^p(\d+)-([a-z])(\d+)$/.exec(id)
109
+ if (parsed === null) {
110
+ let suffix = 2
111
+ while (taken.has(`${id}#${suffix}`)) suffix += 1
112
+ return `${id}#${suffix}`
113
+ }
114
+ let index = Number(parsed[3]) + 1
115
+ for (;;) {
116
+ const candidate = `p${parsed[1]}-${parsed[2]}${index}`
117
+ if (!taken.has(candidate)) return candidate
118
+ index += 1
119
+ }
120
+ }
121
+
122
+ /** 相似度是否够格认作同一个块:要么前缀关系,要么足够长的公共前缀。 */
123
+ function isSameBlock(anchor, block) {
124
+ const left = normalizeText(anchor.text)
125
+ const right = normalizeText(block.sourceText)
126
+ if (left === '' || right === '') return false
127
+ if (left === right) return true
128
+ if (left.startsWith(right) || right.startsWith(left)) return true
129
+ if (Math.min(left.length, right.length) < MIN_SIMILARITY_TEXT) return false
130
+ return similarity(left, right) >= 0.6
131
+ }
132
+
133
+ /**
134
+ * 把新切出的块与旧锚点对齐,尽量沿用旧 id。
135
+ *
136
+ * 策略按优先级:
137
+ * 1. 精确/前缀文本匹配 —— 最可靠,优先在 `page + type` 相同的候选里找;
138
+ * 2. 顺序约束的相似度匹配 —— 在游标附近的窗口里取最佳,容忍局部重排;
139
+ * 3. 未匹配的旧锚点记入 `tombstones`(只报告消失,不动数据);
140
+ * 4. 未匹配的新块保留 `splitBlocks` 给它的新 id。
141
+ *
142
+ * 一个旧 id 至多被认领一次;认领即定,不再因后续匹配而改(顺序扫描,一次决定)。
143
+ *
144
+ * @param {Array<object>} blocks 新切出的块(`splitBlocks` 的产物)。
145
+ * @param {Array<object>} anchors 旧锚点(`anchorsOf` 的产物)。
146
+ * @returns {{blocks: Array<object>, tombstones: string[], stats: object}} 对齐结果。
147
+ */
148
+ export function reconcileBlocks(blocks, anchors) {
149
+ const list = Array.isArray(blocks) ? blocks : []
150
+ const old = Array.isArray(anchors) ? anchors : []
151
+ if (list.length === 0) return { blocks: [], tombstones: old.map(anchor => anchor.id), stats: emptyStats(old.length, 0) }
152
+ if (old.length === 0) return { blocks: list.map(block => ({ ...block })), tombstones: [], stats: emptyStats(0, list.length) }
153
+
154
+ const claimed = new Set()
155
+ const usedIds = new Set(list.map(block => block.id))
156
+ const resolved = []
157
+ let cursor = 0
158
+
159
+ for (const block of list) {
160
+ const page = block.page
161
+ const type = block.type
162
+ let match = null
163
+
164
+ // 1) 同页同类型的精确/前缀匹配:从游标起先向后找,找不到再在窗口内回看。
165
+ for (let index = cursor; index < old.length; index += 1) {
166
+ const anchor = old[index]
167
+ if (claimed.has(anchor.id)) continue
168
+ if (anchor.page !== page || anchor.type !== type) continue
169
+ if (isSameBlock(anchor, block)) { match = anchor; break }
170
+ }
171
+ if (match === null) {
172
+ for (let index = Math.max(0, cursor - LOOKBACK); index < cursor; index += 1) {
173
+ const anchor = old[index]
174
+ if (claimed.has(anchor.id)) continue
175
+ if (anchor.page !== page || anchor.type !== type) continue
176
+ if (isSameBlock(anchor, block)) { match = anchor; break }
177
+ }
178
+ }
179
+
180
+ if (match !== null) {
181
+ claimed.add(match.id)
182
+ resolved.push({ ...block, id: match.id, reconciled: 'kept', mechanicalId: block.id })
183
+ const position = old.indexOf(match)
184
+ if (position >= cursor) cursor = position + 1
185
+ continue
186
+ }
187
+ resolved.push({ ...block, reconciled: 'new', mechanicalId: block.id })
188
+ }
189
+
190
+ // 认领的旧 id 可能与某个新块的机械 id 撞车(旧块消失、新块恰好被分到同名 id)。
191
+ // 让路的是**新块**,不是认领者 —— 认领者身上挂着笔记,改名等于把笔记甩到别处。
192
+ const taken = new Set(resolved.filter(block => block.reconciled === 'kept').map(block => block.id))
193
+ for (const block of resolved) {
194
+ if (block.reconciled === 'kept') continue
195
+ if (!taken.has(block.id)) { taken.add(block.id); continue }
196
+ block.id = freeIdNear(block.mechanicalId, taken)
197
+ block.reconciled = 'renamed'
198
+ taken.add(block.id)
199
+ }
200
+
201
+ const tombstones = old.filter(anchor => !claimed.has(anchor.id)).map(anchor => anchor.id)
202
+ return {
203
+ blocks: resolved,
204
+ tombstones,
205
+ stats: {
206
+ anchors: old.length,
207
+ blocks: list.length,
208
+ kept: resolved.filter(block => block.reconciled === 'kept').length,
209
+ created: resolved.filter(block => block.reconciled === 'new').length,
210
+ renamed: resolved.filter(block => block.reconciled === 'renamed').length,
211
+ tombstones: tombstones.length,
212
+ },
213
+ }
214
+ }
215
+
216
+ function emptyStats(anchors, blocks) {
217
+ return { anchors, blocks, kept: 0, created: blocks, renamed: 0, tombstones: anchors }
218
+ }