@li-canghai/dsh-paper 0.8.1 → 0.8.3

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 (43) hide show
  1. package/README.md +42 -93
  2. package/THIRD_PARTY_NOTICES.md +69 -0
  3. package/lib/annotate-tool.js +58 -10
  4. package/lib/block-cache.js +45 -26
  5. package/lib/blocks-reconcile.js +56 -17
  6. package/lib/client-parts/01-pure-helpers.js +410 -7
  7. package/lib/client-parts/02-author-year.js +111 -24
  8. package/lib/client-parts/03-pdf-stack.js +97 -0
  9. package/lib/client.js +1022 -112
  10. package/lib/code-source.js +8 -8
  11. package/lib/discussion.js +2 -101
  12. package/lib/export.js +228 -14
  13. package/lib/file-lock.js +128 -16
  14. package/lib/gloss.js +1030 -12
  15. package/lib/index.js +193 -68
  16. package/lib/inline-html.js +1 -1
  17. package/lib/item-ops.js +14 -2
  18. package/lib/library-service.js +32 -8
  19. package/lib/library-tool.js +6 -0
  20. package/lib/metadata-enrichment.js +8 -1
  21. package/lib/pdf-analysis-tool.js +72 -0
  22. package/lib/pdf-page.js +609 -13
  23. package/lib/picker.js +19 -5
  24. package/lib/reader-store.js +14 -10
  25. package/lib/rename-document.js +25 -3
  26. package/lib/search-index.js +0 -9
  27. package/lib/selection-message.js +2 -3
  28. package/lib/storage.js +122 -10
  29. package/lib/store-paths.js +0 -13
  30. package/lib/table-gfm.js +42 -14
  31. package/lib/tex-fetch.js +0 -1
  32. package/lib/tex-merge.js +0 -1
  33. package/lib/tex-prepare.js +74 -13
  34. package/lib/text-similarity.js +0 -9
  35. package/lib/tool-host.js +4 -1
  36. package/lib/translate-prompts.js +11 -1
  37. package/lib/translate-queue.js +1 -34
  38. package/lib/translate-task.js +1 -1
  39. package/lib/translate.js +57 -26
  40. package/lib/update-contracts.js +5 -5
  41. package/locale/en.json +6 -0
  42. package/locale/zh.json +6 -0
  43. package/package.json +7 -2
@@ -5,16 +5,31 @@
5
5
  // 🔴 与编号制最大的差别是**没有唯一键**。"姓氏 + 年份"在 AI 领域经常撞车(同一作者同年
6
6
  // 多篇),所以索引是一对多:命中几条就全列,由读者自己判断。宁可显示两条让人选,也不要
7
7
  // 挑一条赌对 —— 猜错时读者无从知道错。
8
- /** `(Surname et al., 2020)` / `(Surname & Other, 2020)` / `(Surname, 2020)`。 */
9
- const AUTHOR_YEAR =
10
- /\(\s*([\p{Lu}][\p{L}'-]+)(?:\s+et\s+al\.?|\s*(?:&|and)\s*[\p{Lu}][\p{L}'-]+)?\s*,\s*(\d{4})[a-z]?\s*\)/gu
11
- // 🔴 三个正则都**必须带 u flag**:`\p{Lu}` / `\p{L}` 是 Unicode 属性转义,没有 u 时
8
+ //
9
+ // 🔴 参考文献有**两种排版**,两种都得认(实测一篇 AAAI 式论文 27 条全灭):
10
+ // · CS 式 `Zhang, Y. Title. 2020.` —— 姓在前,后面跟逗号
11
+ // · AAAI 式 `Parth Sarthi, Salman Abdullah, …, and Christopher D. Manning. 2024.`
12
+ // —— 名在前姓在后,年份在整串作者**之后**
13
+ // 所以不再拿正则硬抠"姓 + 逗号",改成**先按年份切开、再从作者串里取第一个人的最后一个词**。
14
+ /** 整个括号。**不**要求里面只有一条引用:`(A et al., 2024; B et al., 2025)` 是常态。 */
15
+ const AUTHOR_YEAR = /\(([^()]{1,240})\)/gu
16
+ // 🔴 下面的正则都**必须带 u flag**:`\p{Lu}` / `\p{L}` 是 Unicode 属性转义,没有 u 时
12
17
  // 会被当成字面量 p / L / u,正则永远匹配不上而**不报错** —— 表现为索引恒为空、悬停全无反应。
13
18
  // 这条踩过一次(首版漏了 u,4 条作者-年份用例全挂)。
14
- /** 参考文献条目开头的姓:surname, Initial. */
15
- const REFERENCE_SURNAME = /^\s*(?:\[\s*\d{1,3}\s*\]\s*)?([\p{Lu}][\p{L}'-]+)\s*,/u
16
- /** 条目里出现的第一个四位数年份。 */
17
- const REFERENCE_YEAR = /\b(\d{4})\b/
19
+ /** `e.g.,` / `i.e.,` / `cf.` / `see` 这些前缀不属于引用本身,逐条匹配前先剥掉。 */
20
+ const CITE_PREFIX = /^\s*(?:e\.g\.|i\.e\.|cf\.|see|also)\s*,?\s*/i
21
+ /** 并列里的一条:`Surname, 2020` / `Surname et al., 2020` / `Surname and Other, 2020`。 */
22
+ const CITE_ITEM =
23
+ /^\s*([\p{Lu}][\p{L}'-]+)(?:\s+et\s+al\.?|\s*(?:&|and)\s+[\p{Lu}][\p{L}'-]+(?:\s+[\p{Lu}][\p{L}'-]+)?)?\s*,\s*((?:19|20)\d{2})[a-z]?\s*$/u
24
+ /**
25
+ * 条目里第一个年份。
26
+ *
27
+ * 🔴 尾巴上的年份后缀要一起吃掉:`\b\d{4}\b` 匹配不了 `2024a`,那种条目会整条掉出索引。
28
+ * 以 `19` / `20` 开头也顺带挡掉了页码 `1234` 之类误判。
29
+ */
30
+ const REFERENCE_YEAR = /\b((?:19|20)\d{2})[a-z]?\b/
31
+ /** 作者串里一个像姓的词。`O'Brien` / `Gutiérrez` / `Liu-Lin` 都要能过。 */
32
+ const NAME_WORD = /^[\p{Lu}][\p{L}'-]*$/u
18
33
 
19
34
  /** 匹配键:姓氏折叠成小写 + 年份。作者大小写与变音符号不在这一层处理。 */
20
35
  function authorYearKey(surname, year) {
@@ -23,10 +38,59 @@
23
38
  : ''
24
39
  }
25
40
 
41
+ /**
42
+ * 参考文献条目 → 第一作者的姓。
43
+ *
44
+ * 两步,顺序不能反:
45
+ * 1. **先按年份切开**。年份之后是标题、期刊、卷号,里面全是首字母大写的词 ——
46
+ * 不先切掉就会取到 `Cognition` / `Psychological` 这种,索引里凭空多出一条假条目。
47
+ * 2. 再从作者串里取**第一个人的最后一个词**。第一个人的边界是 `and` / `&` / 逗号 ——
48
+ * 三种排版都用它们分隔作者。
49
+ *
50
+ * 逗号在两种排版里含义不同(`Smith, John` 是"姓, 名",而 `Ge Bai, Jie Liu` 是作者分隔),
51
+ * 但两种情况下第一个逗号之前都正好是第一作者的姓氏串,所以先切逗号两边都对。
52
+ *
53
+ * @param {unknown} source 参考文献原文。
54
+ * @returns {string} 姓氏;认不出时为空串(那条引用只是悬停没反应,不会错配到别人)。
55
+ */
56
+ function referenceSurname(source) {
57
+ const lead = String(source ?? '').replace(/^\s*\[\s*\d{1,5}\s*\]\s*/, '')
58
+ if (lead === '') return ''
59
+ const year = REFERENCE_YEAR.exec(lead)
60
+ const authors = year === null ? lead : lead.slice(0, year.index)
61
+ const pick = zone => {
62
+ const first = zone.split(/\s+(?:and|&)\s+|,/)[0]
63
+ // 🔴 尾部句点必须剥掉:`Leonard Talmy.` 里 `Talmy.` 带点,不剥就过不了 NAME_WORD,
64
+ // 于是只剩 `Leonard` 当姓 —— 索引里多出一个"Leonard 1975",正文的 `Talmy, 1975` 全落空。
65
+ const words = first.trim().replace(/[.,;:]+$/, '').split(/\s+/).filter(word => NAME_WORD.test(word))
66
+ return words.length === 0 ? '' : words[words.length - 1]
67
+ }
68
+ // 年份在最前(极少数期刊把年份编进卷首)时作者串是空的,退回整条开头再试一次。
69
+ return pick(authors) || pick(lead)
70
+ }
71
+
72
+ /**
73
+ * 一个括号里解析出的全部引用。`;` 分隔并列,逐条剥掉 `e.g.,` 之类前缀。
74
+ *
75
+ * 🔴 任一条认不出就**整段作废**,与编号制同一口径(那个编号不在本篇就整段按纯文本):
76
+ * 只标出认得的一半,读者会以为括号里只有那一半。
77
+ *
78
+ * @param {string} inner 括号内部(不含两侧圆括号)。
79
+ * @returns {Array<{surname: string, year: string}>} 一条都认不出时为空数组。
80
+ */
81
+ function authorYearItems(inner) {
82
+ const items = []
83
+ for (const chunk of String(inner).split(';')) {
84
+ const match = CITE_ITEM.exec(chunk.replace(CITE_PREFIX, ''))
85
+ if (match === null) return []
86
+ items.push({ surname: match[1], year: match[2] })
87
+ }
88
+ return items
89
+ }
90
+
26
91
  /**
27
92
  * 从参考文献块建「姓氏|年份 → 块」的一对多索引。
28
93
  *
29
- * 姓氏取条目开头(可带 `[n]` 前缀)的第一个大写词,年份取条目里第一个四位数 ——
30
94
  * 学术期刊格式千差万别,这是**启发式**而不是解析器。认不出来的条目直接跳过:
31
95
  * 索引里少一条只是那条引用悬停没反应,不会错配到别的条目上。
32
96
  *
@@ -39,10 +103,10 @@
39
103
  if (block === null || typeof block !== 'object' || typeof block.id !== 'string') continue
40
104
  if (readerKindOf(block) !== 'references') continue
41
105
  const source = typeof block.sourceText === 'string' ? block.sourceText : ''
42
- const surname = REFERENCE_SURNAME.exec(source)
106
+ const surname = referenceSurname(source)
43
107
  const year = REFERENCE_YEAR.exec(source)
44
- if (surname === null || year === null) continue
45
- const key = authorYearKey(surname[1], year[1])
108
+ if (surname === '' || year === null) continue
109
+ const key = authorYearKey(surname, year[1])
46
110
  if (key === '') continue
47
111
  const entry = { blockId: block.id, text: source.replace(REFERENCE_LEAD, '').trim() }
48
112
  const list = index.get(key)
@@ -58,11 +122,15 @@
58
122
  * 只产出**索引里真有**的标记:查不到的括号原样留在文本里。这一条与编号制同一口径 ——
59
123
  * 点了没反应/悬停了没内容,比没有标记更坏。
60
124
  *
61
- * 标记带 `ambiguous`:命中多条时为真,渲染层据此提示"共 N 条可能匹配"。
125
+ * 括号里并列多条时,**整段合成一个标记**(而不是拆成几个可点区域):读者看到的是
126
+ * 一个括号,悬停展开全部条目才符合直觉;拆开还会让"哪几个字属于同一条引用"变得无法判断。
127
+ * 标记上带 `items`(每条的 `姓氏|年份` 键),形状与编号制的多标号一致。
128
+ *
129
+ * 标记带 `ambiguous`:全部条目命中总数 > 1 时为真,渲染层据此提示"共 N 条可能匹配"。
62
130
  *
63
131
  * @param {unknown} text 段落文本。
64
132
  * @param {unknown} index {@link authorYearIndex} 的产物。
65
- * @returns {Array<{type: string, value: string, surname: string, year: string, ambiguous?: boolean, count?: number} | {type: 'text', text: string}>}
133
+ * @returns {Array<{type: string, value: string, surname?: string, year?: string, items?: string[], ambiguous?: boolean, count?: number} | {type: 'text', text: string}>}
66
134
  */
67
135
  function inlineAuthors(text, index) {
68
136
  if (typeof text !== 'string' || text === '' || !(index instanceof Map) || index.size === 0) return []
@@ -75,18 +143,37 @@
75
143
  let cursor = 0
76
144
  AUTHOR_YEAR.lastIndex = 0
77
145
  for (let match = AUTHOR_YEAR.exec(text); match !== null; match = AUTHOR_YEAR.exec(text)) {
78
- const key = authorYearKey(match[1], match[2])
79
- const hits = key === '' ? undefined : index.get(key)
80
- if (hits === undefined || hits.length === 0) continue
146
+ const items = authorYearItems(match[1])
147
+ if (items.length === 0) continue
148
+ // 先确认**每一条**都查得到,再决定要不要出标记:查到一半就放弃,才不会出现
149
+ // "括号里两条、只标了一条"这种让人误解的部分标记。
150
+ const keys = []
151
+ let total = 0
152
+ let known = true
153
+ for (const item of items) {
154
+ const key = authorYearKey(item.surname, item.year)
155
+ const hits = key === '' ? undefined : index.get(key)
156
+ if (hits === undefined || hits.length === 0) { known = false; break }
157
+ keys.push(key)
158
+ total += hits.length
159
+ }
160
+ if (!known) continue
81
161
  plain += text.slice(cursor, match.index)
82
162
  flush()
83
- parts.push({
84
- type: 'author',
85
- value: match[0],
86
- surname: match[1],
87
- year: match[2],
88
- ...(hits.length > 1 ? { ambiguous: true, count: hits.length } : {}),
89
- })
163
+ const single = items.length === 1
164
+ const part = { type: 'author', value: match[0] }
165
+ // 单条的字段形状**保持不变**:既有 4 条用例按字段全等断言,多一个字段就红。
166
+ if (single) {
167
+ part.surname = items[0].surname
168
+ part.year = items[0].year
169
+ } else {
170
+ part.items = keys
171
+ }
172
+ if (total > 1) {
173
+ part.ambiguous = true
174
+ part.count = total
175
+ }
176
+ parts.push(part)
90
177
  cursor = match.index + match[0].length
91
178
  }
92
179
  plain += text.slice(cursor)
@@ -0,0 +1,97 @@
1
+ // ---- PDF 原页页栈(连续滚动)----
2
+ /**
3
+ * 一篇论文的页身份键:`文档|页|裁边档`。
4
+ *
5
+ * 🔴 **裁边是身份的一部分**:同一页裁与不裁拿到的坐标不是一套(宿主 readPdfPage 按满页口径
6
+ * 缓存、cropPdfGroups 在读出后换算),拿裁边的文字层去配整页的位图,选字框会整片偏出页面。
7
+ * 所以 key 里必须带它,改 key 就等于换一份数据,与旧实现在 pdfData.cropped 上做的判定同源。
8
+ *
9
+ * @param {unknown} documentId 本篇 documentId
10
+ * @param {unknown} page 页码(1 起)
11
+ * @param {unknown} crop 是否裁边
12
+ * @returns {string} 空串 = 这个身份不成立(文档 id 为空或页码不是正整数)
13
+ */
14
+ function pdfStackKey(documentId, page, crop) {
15
+ const id = typeof documentId === 'string' ? documentId : ''
16
+ const number = Number(page)
17
+ if (id === '' || !Number.isSafeInteger(number) || number < 1) return ''
18
+ return id + '|' + number + '|' + (crop ? '1' : '0')
19
+ }
20
+
21
+ /**
22
+ * 该挂载哪几页:当前页 ± radius,并夹在 [1, pageCount] 里。
23
+ *
24
+ * 🔴 **这是连续滚动唯一的性能闸门**。一页的文字层约 4300 个 DOM 节点(逐字符对齐的代价,
25
+ * 见 client.js 里 pdfPageView 的说明),12 页全挂就是 5 万个节点 —— 滚动与重涂都会被拖死。
26
+ * 只挂「当前页 ± 半径」把常驻节点数钉在 (2·radius+1) 页的量级,滚出窗口的页整页卸载。
27
+ *
28
+ * radius = 1 是**滑动手感**的下限:滚到一页底部时下一页必须已经在 DOM 里,否则会先看到
29
+ * 一屏骨架再等 JSON。再大只是白烧节点,滚一页用不到第三页。
30
+ *
31
+ * @param {unknown} page 当前页
32
+ * @param {unknown} pageCount 总页数
33
+ * @param {unknown} radius 前后各挂几页
34
+ * @returns {number[]} 升序页码;参数不成立时空数组(**不编一圈默认页**)
35
+ */
36
+ function pdfPageWindow(page, pageCount, radius = 1) {
37
+ const current = Number(page)
38
+ const total = Number(pageCount)
39
+ if (!Number.isSafeInteger(current) || current < 1) return []
40
+ if (!Number.isSafeInteger(total) || total < 1) return []
41
+ const span = Number.isSafeInteger(Number(radius)) && Number(radius) >= 0 ? Number(radius) : 1
42
+ const from = Math.max(1, current - span)
43
+ const to = Math.min(total, current + span)
44
+ const pages = []
45
+ for (let number = from; number <= to; number++) pages.push(number)
46
+ return pages
47
+ }
48
+
49
+ /**
50
+ * 页框宽高比,**按 CSS `aspect-ratio` 的口径给(宽 / 高)**(纯函数)。
51
+ *
52
+ * 🔴 为什么可以篇内统一:实测同一篇论文各页尺寸逐页相同(PDF 页面框固定),所以一篇之内
53
+ * 用同一个比例不会让任何一页变形;换来的是**不必等每页位图加载完就能定高度** ——
54
+ * 否则滚动条会随着每一页加载完毕而跳动,而连续滚动最刺眼的就是这种跳。
55
+ *
56
+ * 🔴 方向别搞反:CSS 的 `aspect-ratio` 写的是**宽 / 高**。给成高 / 宽时,
57
+ * A4 那种竖版页会被排成横版(0.77 与 1.29 差了 67%),整篇高度全错。
58
+ * 换算不出正数时返回 null,调用方退回「按图片自然尺寸」而不是编一个比例。
59
+ *
60
+ * @param {unknown} width 页面宽(PDF 点)
61
+ * @param {unknown} height 页面高(PDF 点)
62
+ * @returns {number|null} 宽 / 高;无效是 null
63
+ */
64
+ function pdfPageAspectOf(width, height) {
65
+ const w = Number(width)
66
+ const h = Number(height)
67
+ if (!Number.isFinite(w) || !Number.isFinite(h) || !(w > 0) || !(h > 0)) return null
68
+ return w / h
69
+ }
70
+
71
+ /**
72
+ * 从滚动位置反推"现在是第几页"(纯函数)。
73
+ *
74
+ * 🔴 用**页顶坐标**而不是"视口中心落在谁身上":连续滚动下用户可能正停在两页交界处,
75
+ * 中心判据在那一瞬间会来回跳(页码跟着闪),而"最后一个页顶已经滚过视口上沿的页"
76
+ * 单调、稳定,滚过一页才前进一页。
77
+ *
78
+ * @param {number[]} tops 各页页顶的 scrollTop 偏移(升序)
79
+ * @param {unknown} pages 与 tops 一一对应的页码
80
+ * @param {unknown} scrollTop 当前滚动位置
81
+ * @returns {number|null} 页码;参数不成立是 null
82
+ */
83
+ function pdfPageAt(tops, pages, scrollTop) {
84
+ if (!Array.isArray(tops) || !Array.isArray(pages) || tops.length !== pages.length) return null
85
+ if (tops.length === 0) return null
86
+ const position = Number(scrollTop)
87
+ if (!Number.isFinite(position)) return null
88
+ // 留 2px 容差:滚动容器的 scrollTop 是整数化的,页顶算出来可能带小数,
89
+ // 差这一线就会让"刚滚到下一页"判成还在上一页。
90
+ let found = Number(pages[0])
91
+ for (let index = 0; index < tops.length; index++) {
92
+ if (Number(tops[index]) <= position + 2) found = Number(pages[index])
93
+ else break
94
+ }
95
+ return Number.isSafeInteger(found) && found > 0 ? found : null
96
+ }
97
+