@li-canghai/dsh-paper 0.8.6 → 0.8.7
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 +1 -0
- package/THIRD_PARTY_NOTICES.md +41 -30
- package/lib/asset-routes.js +49 -2
- package/lib/client-parts/01-pure-helpers.js +113 -9
- package/lib/client.js +127 -10
- package/lib/fonts/NotoSansSC-common.woff2 +0 -0
- package/lib/fonts/NotoSerifSC-common.woff2 +0 -0
- package/lib/fonts/OFL.txt +93 -0
- package/lib/gloss.js +21 -6
- package/lib/index.js +20 -1
- package/lib/translate-prompts.js +279 -12
- package/lib/translate.js +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
| 启动器 | `uvx`(页面图、PDF 元数据、MinerU 解析三条链共用;缺失时详情页自检卡给安装命令)|
|
|
33
33
|
| 解析语言 | `ch`(中英);扫描件可开 `parseOcr` |
|
|
34
34
|
| 自动翻译 | 开,范围「前 20 页」,模型跟随宿主默认 |
|
|
35
|
+
| 翻译模型 | 设置页可钉到某个 provider/model;本地专用 MT(如 `tencent/Hy-MT3-7B-GGUF`)请把「输出」选成 `plain` 或 `lines`。三档都能用,但 `plain` 最稳、`lines` 最快;`json` 档需要模型严守 JSON 转义规则,含 LaTeX 的论文容易整批失败 |
|
|
35
36
|
|
|
36
37
|
完整配置项表见 [SPEC.md 的配置项](docs/SPEC.md#配置项)。
|
|
37
38
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -10,46 +10,57 @@ dsh-paper 以 [MIT](LICENSE) 发布。它依赖下列第三方软件,每个项
|
|
|
10
10
|
|
|
11
11
|
插件在运行时直接 `import` 的包,随插件一起被安装。
|
|
12
12
|
|
|
13
|
-
| 包
|
|
14
|
-
|
|
|
15
|
-
| `@citation-js/core`
|
|
16
|
-
| `@citation-js/plugin-bibtex` | 0.9.0
|
|
17
|
-
| `@citation-js/plugin-csl`
|
|
18
|
-
| `@citation-js/plugin-ris`
|
|
19
|
-
| `@xmldom/xmldom`
|
|
20
|
-
| `https-proxy-agent`
|
|
21
|
-
| `katex`
|
|
22
|
-
| `socks`
|
|
13
|
+
| 包 | 版本 | 许可 | 上游 |
|
|
14
|
+
| ---------------------------- | ------- | ---- | ----------------------------------------------------------------------- |
|
|
15
|
+
| `@citation-js/core` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
|
|
16
|
+
| `@citation-js/plugin-bibtex` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
|
|
17
|
+
| `@citation-js/plugin-csl` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
|
|
18
|
+
| `@citation-js/plugin-ris` | 0.9.0 | MIT | [citation-js/citation-js](https://github.com/citation-js/citation-js) |
|
|
19
|
+
| `@xmldom/xmldom` | 0.9.12 | MIT | [xmldom/xmldom](https://github.com/xmldom/xmldom) |
|
|
20
|
+
| `https-proxy-agent` | 9.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
|
|
21
|
+
| `katex` | 0.16.47 | MIT | [KaTeX/KaTeX](https://github.com/KaTeX/KaTeX) |
|
|
22
|
+
| `socks` | 2.8.10 | MIT | [JoshGlazebrook/socks](https://github.com/JoshGlazebrook/socks) |
|
|
23
23
|
|
|
24
24
|
## 运行时传递依赖
|
|
25
25
|
|
|
26
26
|
由上面那些包引入,同样随插件一起被安装。
|
|
27
27
|
|
|
28
|
-
| 包
|
|
29
|
-
|
|
|
30
|
-
| `@citation-js/date`
|
|
31
|
-
| `@citation-js/name`
|
|
32
|
-
| `agent-base`
|
|
33
|
-
| `citeproc`
|
|
34
|
-
| `commander`
|
|
35
|
-
| `debug`
|
|
36
|
-
| `ip-address`
|
|
37
|
-
| `moo`
|
|
38
|
-
| `ms`
|
|
39
|
-
| `proxy-agent-negotiate` | 1.1.0
|
|
40
|
-
| `smart-buffer`
|
|
41
|
-
| `sync-fetch-undici`
|
|
42
|
-
| `whatwg-mimetype`
|
|
28
|
+
| 包 | 版本 | 许可 | 上游 |
|
|
29
|
+
| ----------------------- | ------ | -------------------- | ----------------------------------------------------------------------------- |
|
|
30
|
+
| `@citation-js/date` | 0.5.1 | MIT | [citation-js/date](https://github.com/citation-js/date) |
|
|
31
|
+
| `@citation-js/name` | 0.4.2 | MIT | [citation-js/name](https://github.com/citation-js/name) |
|
|
32
|
+
| `agent-base` | 9.0.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
|
|
33
|
+
| `citeproc` | 2.4.63 | CPAL-1.0 OR AGPL-1.0 | [juris-m/citeproc-js](https://github.com/juris-m/citeproc-js) |
|
|
34
|
+
| `commander` | 8.3.0 | MIT | [tj/commander.js](https://github.com/tj/commander.js) |
|
|
35
|
+
| `debug` | 4.4.3 | MIT | [debug-js/debug](https://github.com/debug-js/debug) |
|
|
36
|
+
| `ip-address` | 10.7.5 | MIT | [beaugunderson/ip-address](https://github.com/beaugunderson/ip-address) |
|
|
37
|
+
| `moo` | 0.5.3 | BSD-3-Clause | [tjvr/moo](https://github.com/tjvr/moo) |
|
|
38
|
+
| `ms` | 2.1.3 | MIT | [vercel/ms](https://github.com/vercel/ms) |
|
|
39
|
+
| `proxy-agent-negotiate` | 1.1.0 | MIT | [TooTallNate/proxy-agents](https://github.com/TooTallNate/proxy-agents) |
|
|
40
|
+
| `smart-buffer` | 4.2.0 | MIT | [JoshGlazebrook/smart-buffer](https://github.com/JoshGlazebrook/smart-buffer) |
|
|
41
|
+
| `sync-fetch-undici` | 0.7.1 | MIT | [larsgw/sync-fetch](https://github.com/larsgw/sync-fetch) |
|
|
42
|
+
| `whatwg-mimetype` | 5.0.0 | MIT | [jsdom/whatwg-mimetype](https://github.com/jsdom/whatwg-mimetype) |
|
|
43
|
+
|
|
44
|
+
## 随包分发的字体
|
|
45
|
+
|
|
46
|
+
插件自带两款中文兜底字体(D207),位于 `lib/fonts/`。它们**不是 npm 依赖**,是从 Google Fonts 取完整字体后自行子集化的产物(GB2312 一级常用字 3755 + ASCII + 常用标点,共 3880 字)。
|
|
47
|
+
|
|
48
|
+
| 字体 | 文件 | 大小 | 许可 | 上游 |
|
|
49
|
+
| ------------- | -------------------------- | ------ | ----------- | ----------------------------------------------------------- |
|
|
50
|
+
| Noto Serif SC | `NotoSerifSC-common.woff2` | 679 KB | SIL OFL 1.1 | [notofonts/noto-cjk](https://github.com/notofonts/noto-cjk) |
|
|
51
|
+
| Noto Sans SC | `NotoSansSC-common.woff2` | 525 KB | SIL OFL 1.1 | [notofonts/noto-cjk](https://github.com/notofonts/noto-cjk) |
|
|
52
|
+
|
|
53
|
+
**OFL 的要求**:保留版权声明与许可原文;若修改了字体,不得使用原作者的保留字体名作为自己的产品名。因此,本插件把子集声明为 `Paper Noto Serif SC` / `Paper Noto Sans SC` 两个自有族名,字体本位图的派生也未被用于宣传。
|
|
43
54
|
|
|
44
55
|
## 由宿主提供(不随本包安装)
|
|
45
56
|
|
|
46
57
|
这些包由 DSH 宿主在运行时供给,本插件的 `package.json` 只在 `peerDependencies` 里声明版本区间,不把副本装进自己的安装树。
|
|
47
58
|
|
|
48
|
-
| 包
|
|
49
|
-
|
|
|
50
|
-
| `@deepseek-ai/schemastery` | `^3.18.1`
|
|
51
|
-
| `@deepseek-ai/cosmokit`
|
|
52
|
-
| `@standard-schema/spec`
|
|
59
|
+
| 包 | 版本区间 | 许可 | 上游 |
|
|
60
|
+
| -------------------------- | -------------- | ---- | ------------------------------------------------------------------------------------- |
|
|
61
|
+
| `@deepseek-ai/schemastery` | `^3.18.1` | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
|
|
62
|
+
| `@deepseek-ai/cosmokit` | 随 schemastery | MIT | [deepseek-ai/deepseek-harness](https://github.com/deepseek-ai/deepseek-harness) |
|
|
63
|
+
| `@standard-schema/spec` | 随 schemastery | MIT | [standard-schema/standard-schema](https://github.com/standard-schema/standard-schema) |
|
|
53
64
|
|
|
54
65
|
## 需要单独说明的许可
|
|
55
66
|
|
package/lib/asset-routes.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises'
|
|
2
|
-
import { dirname, extname, isAbsolute, relative, resolve, sep } from 'node:path'
|
|
2
|
+
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
3
3
|
import { isPageImageUnavailable } from './page-image.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -27,6 +27,7 @@ export function createAssetRoutes(deps = {}) {
|
|
|
27
27
|
const renderPageImages = typeof deps.renderPageImages === 'function' ? deps.renderPageImages : null
|
|
28
28
|
const cacheDir = typeof deps.cacheDir === 'string' ? deps.cacheDir.trim() : ''
|
|
29
29
|
const command = typeof deps.command === 'string' && deps.command.trim() !== '' ? deps.command : undefined
|
|
30
|
+
const fontDir = typeof deps.fontDir === 'string' ? deps.fontDir.trim() : ''
|
|
30
31
|
|
|
31
32
|
/** `GET /analysis-asset?documentId=&path=`:结果目录内的图片。 */
|
|
32
33
|
async function analysisAsset(request, url) {
|
|
@@ -97,7 +98,40 @@ export function createAssetRoutes(deps = {}) {
|
|
|
97
98
|
return byteResponse(image.data, PAGE_IMAGE_CONTENT_TYPE)
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
/**
|
|
102
|
+
* `GET /font-file?name=`:随包自带的中文分片字体(D207)。
|
|
103
|
+
*
|
|
104
|
+
* 🔴 文件名走**白名单正则**而不是路径拼接:`..`、分隔符、绝对路径一个都进不来,
|
|
105
|
+
* 也不需要 relative() 那种越界判定 —— 正则本身就把范围钉死在"编号或语种 + 数字"的形状上。
|
|
106
|
+
*
|
|
107
|
+
* 🔴 缓存一年:文件名里带着分片编号,字体内容变了编号就变了,所以长缓存安全。
|
|
108
|
+
* 这一点与图片那两条不同 —— 图片是私有一小时(按 documentId 变),字体是包内静态资产。
|
|
109
|
+
*
|
|
110
|
+
* 缺文件回 404 而不是 500:客户端据此判断"这个分片没有",静默跳过即可 ——
|
|
111
|
+
* 分片缺一片只会让那几个字落到下一档字体,不该在控制台留 500 的红字。
|
|
112
|
+
*/
|
|
113
|
+
async function fontFile(request, url) {
|
|
114
|
+
if (fontDir === '') return textResponse('字体目录未接线', 503)
|
|
115
|
+
const params = searchParamsOf(request, url)
|
|
116
|
+
const name = (params.get('name') ?? '').trim()
|
|
117
|
+
if (!FONT_FILE_PATTERN.test(name)) return textResponse('字体文件名非法', 400)
|
|
118
|
+
let data
|
|
119
|
+
try {
|
|
120
|
+
data = await readFile(join(fontDir, name))
|
|
121
|
+
} catch {
|
|
122
|
+
return textResponse('字体分片不存在', 404)
|
|
123
|
+
}
|
|
124
|
+
return new Response(data, {
|
|
125
|
+
status: 200,
|
|
126
|
+
headers: {
|
|
127
|
+
'Content-Type': 'font/woff2',
|
|
128
|
+
'Content-Length': String(data.byteLength),
|
|
129
|
+
'Cache-Control': FONT_CACHE_CONTROL,
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return { 'analysis-asset': analysisAsset, 'page-image': pageImage, 'font-file': fontFile }
|
|
101
135
|
}
|
|
102
136
|
|
|
103
137
|
/** 扩展名 → Content-Type。**不猜**:表里没有的一律 application/octet-stream。 */
|
|
@@ -111,6 +145,19 @@ const OCTET_STREAM = 'application/octet-stream'
|
|
|
111
145
|
const PAGE_IMAGE_CONTENT_TYPE = 'image/webp'
|
|
112
146
|
/** 结果目录内的图片与内容寻址的页图都属于「私有一小时的静态资源」。 */
|
|
113
147
|
const CACHE_CONTROL = 'private, max-age=3600'
|
|
148
|
+
/** 字体是包内静态资产:文件名即内容标识,缓存一年。 */
|
|
149
|
+
const FONT_CACHE_CONTROL = 'public, max-age=31536000, immutable'
|
|
150
|
+
/**
|
|
151
|
+
* 字体文件名白名单。只认随包自带的这两款 Noto 的命名形状:
|
|
152
|
+
* `NotoSerifSC-common.woff2` / `NotoSansSC-common.woff2`(当前实际发布的子集),
|
|
153
|
+
* 以及 @fontsource 那套 `noto-<serif|sans>-sc-<编号>-400-normal.woff2` 切片 ——
|
|
154
|
+
* 后者留作将来换回切片方案的余地,两种形态都钉在这条正则里。
|
|
155
|
+
*
|
|
156
|
+
* 🔴 用**闭集正则**而不是"不含 `..`"这类黑名单:黑名单永远漏一种写法,
|
|
157
|
+
* 正则可以反过来把整个命名空间钉死。哪天要加第三款,是改这一行加一条分支,
|
|
158
|
+
* 而不是放宽成通配。
|
|
159
|
+
*/
|
|
160
|
+
const FONT_FILE_PATTERN = /^(?:Noto(?:Serif|Sans)SC-common|noto-(?:serif|sans)-sc-(?:\d{1,3}|latin|cyrillic|vietnamese)(?:-ext)?-400-normal)\.woff2$/
|
|
114
161
|
/** UI 高清变体;翻译侧走 896,两档在缓存键里分开(D49),互不干扰。 */
|
|
115
162
|
const DEFAULT_PAGE_WIDTH = 1200
|
|
116
163
|
/** width 直接决定 uvx 渲染出的位图大小,不设上限就是一个内存放大器。 */
|
|
@@ -2668,26 +2668,130 @@
|
|
|
2668
2668
|
*
|
|
2669
2669
|
* 固定清单,不给自由输入 —— 允许任意字符串等于允许读者把正文变成豆腐块,
|
|
2670
2670
|
* 而浏览器又不提供"列出系统字体"的接口(字体枚举要用户授权且各浏览器行为不一)。
|
|
2671
|
+
*
|
|
2672
|
+
* 🔴 **每一档都必须是"换台电脑也还在"的字体**(用户裁定:这些字体要在别的机器上也可用)。
|
|
2673
|
+
* 选名依据是官方清单,不是本机 `C:\Windows\Fonts` 里有什么:
|
|
2674
|
+
*
|
|
2675
|
+
* · Windows 11 核心字体清单(learn.microsoft.com/typography/fonts/windows_11_font_list)
|
|
2676
|
+
* —— 任何语言版 Windows 都带的那批;
|
|
2677
|
+
* · W3C clreq《Recommended System Font Mappings for Generic Chinese Font Families》
|
|
2678
|
+
* —— 逐款标了 Built-in / OS optional / Downloadable,还给了 macOS 与 Linux 的名字。
|
|
2679
|
+
*
|
|
2680
|
+
* 于是栈的排法是**跨平台优先级**而非"本机有什么排前面":每档第一个名字是至少一个主流平台
|
|
2681
|
+
* 一定有的,后面依次是另外两个平台的对应款,末尾永远是 CSS 通用族。换一台没有第一款字体
|
|
2682
|
+
* 的机器,浏览器顺着往下找,最差落到通用族 —— 仍是完整字形,不会是豆腐块。
|
|
2683
|
+
*
|
|
2684
|
+
* 🔴 特意**没有**收进来的(它们在本机上确实存在,但不保证别人也有):
|
|
2685
|
+
* · Noto Sans/Serif SC、HarmonyOS Sans SC —— 微软核心清单里没有,是第三方装上去的;
|
|
2686
|
+
* · 方正系列(FZShuTi / FZYaoTi)、华文系列(STSong / STKaiti / STFangsong / STXingkai /
|
|
2687
|
+
* STLiti / STXihei…)—— Windows 上来自 Office,macOS 上才是系统自带,
|
|
2688
|
+
* 所以它们只作为**栈中间的一环**出现,绝不单独成为某一档的唯一希望;
|
|
2689
|
+
* · 隶书 LiSu、幼圆 YouYuan —— 同上,早期 Office 内置,不是 Windows 自带。
|
|
2690
|
+
* 这些名字留在栈里是有用的(装了 Office 的机器能命中),但**没有任何一档以它们打头**。
|
|
2691
|
+
*
|
|
2692
|
+
* ⚠️ 楷体在 Windows 上是 simkai.ttf,英文族名 `KaiTi`;`STKaiti` 是另一款
|
|
2693
|
+
* (STKAITI.TTF,华文楷体),两者不是同一款字,所以栈里是两个名字、不能说谁代替谁。
|
|
2671
2694
|
*/
|
|
2672
2695
|
const SRC_FONT_FAMILIES = Object.freeze([
|
|
2673
2696
|
Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
|
|
2674
|
-
Object.freeze({
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2697
|
+
Object.freeze({
|
|
2698
|
+
value: 'serif', label: '衬线', group: '衬线体',
|
|
2699
|
+
stack: "Georgia,'Times New Roman',Times,'Source Serif Pro',serif",
|
|
2700
|
+
}),
|
|
2701
|
+
Object.freeze({
|
|
2702
|
+
value: 'classic', label: '古典', group: '衬线体',
|
|
2703
|
+
stack: "'Iowan Old Style',Charter,Constantia,Cambria,Georgia,serif",
|
|
2704
|
+
}),
|
|
2705
|
+
Object.freeze({
|
|
2706
|
+
value: 'book', label: '书籍', group: '衬线体',
|
|
2707
|
+
stack: "'Palatino Linotype',Palatino,'Book Antiqua',Garamond,Georgia,serif",
|
|
2708
|
+
}),
|
|
2709
|
+
Object.freeze({
|
|
2710
|
+
value: 'sans', label: '无衬线', group: '无衬线体',
|
|
2711
|
+
stack: "'Segoe UI','Helvetica Neue',Helvetica,Arial,'Liberation Sans',sans-serif",
|
|
2712
|
+
}),
|
|
2713
|
+
Object.freeze({
|
|
2714
|
+
value: 'humanist', label: '人文', group: '无衬线体',
|
|
2715
|
+
stack: "Verdana,Tahoma,'Trebuchet MS','Gill Sans MT','Gill Sans',sans-serif",
|
|
2716
|
+
}),
|
|
2717
|
+
Object.freeze({
|
|
2718
|
+
value: 'mono', label: '等宽',
|
|
2719
|
+
stack: "'Cascadia Mono',Consolas,Menlo,'DejaVu Sans Mono','Liberation Mono',monospace",
|
|
2720
|
+
}),
|
|
2721
|
+
Object.freeze({
|
|
2722
|
+
value: 'kai', label: '手写',
|
|
2723
|
+
stack: "'Segoe Script','Segoe Print','Bradley Hand','Comic Sans MS',cursive",
|
|
2724
|
+
}),
|
|
2678
2725
|
])
|
|
2679
2726
|
const ZH_FONT_FAMILIES = Object.freeze([
|
|
2680
2727
|
Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
//
|
|
2684
|
-
|
|
2685
|
-
|
|
2728
|
+
// 🔴 顺序就是「系统优先、自带兜底」(用户裁定,D207):SimSun / 微软雅黑 排在最前,
|
|
2729
|
+
// 机器上有原版就用原版;没有时落到随包自带的 Paper Noto(D207),任何平台都渲染得出。
|
|
2730
|
+
// 浏览器从栈左往右找第一个**存在**的字体,不需要任何运行时检测。
|
|
2731
|
+
Object.freeze({
|
|
2732
|
+
value: 'serif', label: '宋体', group: '印刷体',
|
|
2733
|
+
stack: "SimSun,'Songti SC',STSong,'Paper Noto Serif SC','Noto Serif CJK SC','Source Han Serif SC',serif",
|
|
2734
|
+
}),
|
|
2735
|
+
Object.freeze({
|
|
2736
|
+
value: 'sans', label: '黑体', group: '印刷体',
|
|
2737
|
+
stack: "'Microsoft YaHei','Heiti SC','PingFang SC','Paper Noto Sans SC','Noto Sans CJK SC','Source Han Sans SC','WenQuanYi Zen Hei',sans-serif",
|
|
2738
|
+
}),
|
|
2739
|
+
Object.freeze({
|
|
2740
|
+
value: 'kai', label: '楷体', group: '印刷体',
|
|
2741
|
+
stack: "KaiTi,'Kaiti SC',STKaiti,'LXGW WenKai',serif",
|
|
2742
|
+
}),
|
|
2743
|
+
Object.freeze({
|
|
2744
|
+
value: 'fangsong', label: '仿宋', group: '印刷体',
|
|
2745
|
+
stack: "FangSong,STFangsong,'Noto Serif CJK SC',serif",
|
|
2746
|
+
}),
|
|
2747
|
+
Object.freeze({
|
|
2748
|
+
value: 'yuan', label: '圆体', group: '印刷体',
|
|
2749
|
+
stack: "'Yuanti SC',YouYuan,'Microsoft YaHei',sans-serif",
|
|
2750
|
+
}),
|
|
2751
|
+
Object.freeze({
|
|
2752
|
+
value: 'xingkai', label: '行楷', group: '书体',
|
|
2753
|
+
stack: "STXingkai,'Xingkai SC',KaiTi,cursive",
|
|
2754
|
+
}),
|
|
2755
|
+
Object.freeze({
|
|
2756
|
+
value: 'lishu', label: '隶书', group: '书体',
|
|
2757
|
+
stack: "LiSu,'Libian SC',STLiti,serif",
|
|
2758
|
+
}),
|
|
2686
2759
|
])
|
|
2687
2760
|
/** 某一侧的字体清单。side 是 'src' / 'zh'。 */
|
|
2688
2761
|
function fontFamiliesOf(side) {
|
|
2689
2762
|
return side === 'src' ? SRC_FONT_FAMILIES : ZH_FONT_FAMILIES
|
|
2690
2763
|
}
|
|
2764
|
+
/**
|
|
2765
|
+
* 字体菜单的 items(宿主 Menu 的三态条目:普通项 / 分组标题 / 分隔线)。
|
|
2766
|
+
*
|
|
2767
|
+
* 🔴 分组标题只在**同组两项以上**时插:一项底下顶个标题只是噪声。
|
|
2768
|
+
* 中文那侧「印刷体(五项)/ 书体(两项)」的分法是有意义的 —— 前者拿来读,后者拿来摆;
|
|
2769
|
+
* 英文那侧衬线组三项、无衬线组两项也够格,等宽与手写各一项,于是不出标题。
|
|
2770
|
+
*
|
|
2771
|
+
* ⚠️ 组名**必须与所有档位名错开**:两者撞名时菜单上会出现两个一模一样的词,
|
|
2772
|
+
* 一个点得动一个点不动(第一版英文组名写成「衬线」,与档位「衬线」撞了个正着)。
|
|
2773
|
+
*
|
|
2774
|
+
* 标题的 id 加 group: 前缀,纯粹为了不与某个档位的 value 撞车 —— 宿主拿它当 key。
|
|
2775
|
+
*/
|
|
2776
|
+
function fontMenuItemsOf(side) {
|
|
2777
|
+
const list = fontFamiliesOf(side)
|
|
2778
|
+
const counts = new Map()
|
|
2779
|
+
for (const entry of list) {
|
|
2780
|
+
if (entry.group === undefined) continue
|
|
2781
|
+
counts.set(entry.group, (counts.get(entry.group) ?? 0) + 1)
|
|
2782
|
+
}
|
|
2783
|
+
const items = []
|
|
2784
|
+
let current
|
|
2785
|
+
for (const entry of list) {
|
|
2786
|
+
const group = entry.group
|
|
2787
|
+
if (group !== current && group !== undefined && (counts.get(group) ?? 0) > 1) {
|
|
2788
|
+
items.push({ type: 'label', id: 'group:' + group, text: group })
|
|
2789
|
+
}
|
|
2790
|
+
if (group !== undefined) current = group
|
|
2791
|
+
items.push({ id: entry.value, label: entry.label })
|
|
2792
|
+
}
|
|
2793
|
+
return items
|
|
2794
|
+
}
|
|
2691
2795
|
/**
|
|
2692
2796
|
* 把存下来的 family 解析成某一侧真正要用的栈。**每侧各用各的清单**:
|
|
2693
2797
|
* 旧数据里译文存的 'serif' 拿到原文那侧仍然叫 'serif',但栈换成了拉丁字体。
|
package/lib/client.js
CHANGED
|
@@ -2819,26 +2819,130 @@ function createLibraryModel() {
|
|
|
2819
2819
|
*
|
|
2820
2820
|
* 固定清单,不给自由输入 —— 允许任意字符串等于允许读者把正文变成豆腐块,
|
|
2821
2821
|
* 而浏览器又不提供"列出系统字体"的接口(字体枚举要用户授权且各浏览器行为不一)。
|
|
2822
|
+
*
|
|
2823
|
+
* 🔴 **每一档都必须是"换台电脑也还在"的字体**(用户裁定:这些字体要在别的机器上也可用)。
|
|
2824
|
+
* 选名依据是官方清单,不是本机 `C:\Windows\Fonts` 里有什么:
|
|
2825
|
+
*
|
|
2826
|
+
* · Windows 11 核心字体清单(learn.microsoft.com/typography/fonts/windows_11_font_list)
|
|
2827
|
+
* —— 任何语言版 Windows 都带的那批;
|
|
2828
|
+
* · W3C clreq《Recommended System Font Mappings for Generic Chinese Font Families》
|
|
2829
|
+
* —— 逐款标了 Built-in / OS optional / Downloadable,还给了 macOS 与 Linux 的名字。
|
|
2830
|
+
*
|
|
2831
|
+
* 于是栈的排法是**跨平台优先级**而非"本机有什么排前面":每档第一个名字是至少一个主流平台
|
|
2832
|
+
* 一定有的,后面依次是另外两个平台的对应款,末尾永远是 CSS 通用族。换一台没有第一款字体
|
|
2833
|
+
* 的机器,浏览器顺着往下找,最差落到通用族 —— 仍是完整字形,不会是豆腐块。
|
|
2834
|
+
*
|
|
2835
|
+
* 🔴 特意**没有**收进来的(它们在本机上确实存在,但不保证别人也有):
|
|
2836
|
+
* · Noto Sans/Serif SC、HarmonyOS Sans SC —— 微软核心清单里没有,是第三方装上去的;
|
|
2837
|
+
* · 方正系列(FZShuTi / FZYaoTi)、华文系列(STSong / STKaiti / STFangsong / STXingkai /
|
|
2838
|
+
* STLiti / STXihei…)—— Windows 上来自 Office,macOS 上才是系统自带,
|
|
2839
|
+
* 所以它们只作为**栈中间的一环**出现,绝不单独成为某一档的唯一希望;
|
|
2840
|
+
* · 隶书 LiSu、幼圆 YouYuan —— 同上,早期 Office 内置,不是 Windows 自带。
|
|
2841
|
+
* 这些名字留在栈里是有用的(装了 Office 的机器能命中),但**没有任何一档以它们打头**。
|
|
2842
|
+
*
|
|
2843
|
+
* ⚠️ 楷体在 Windows 上是 simkai.ttf,英文族名 `KaiTi`;`STKaiti` 是另一款
|
|
2844
|
+
* (STKAITI.TTF,华文楷体),两者不是同一款字,所以栈里是两个名字、不能说谁代替谁。
|
|
2822
2845
|
*/
|
|
2823
2846
|
const SRC_FONT_FAMILIES = Object.freeze([
|
|
2824
2847
|
Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
|
|
2825
|
-
Object.freeze({
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2848
|
+
Object.freeze({
|
|
2849
|
+
value: 'serif', label: '衬线', group: '衬线体',
|
|
2850
|
+
stack: "Georgia,'Times New Roman',Times,'Source Serif Pro',serif",
|
|
2851
|
+
}),
|
|
2852
|
+
Object.freeze({
|
|
2853
|
+
value: 'classic', label: '古典', group: '衬线体',
|
|
2854
|
+
stack: "'Iowan Old Style',Charter,Constantia,Cambria,Georgia,serif",
|
|
2855
|
+
}),
|
|
2856
|
+
Object.freeze({
|
|
2857
|
+
value: 'book', label: '书籍', group: '衬线体',
|
|
2858
|
+
stack: "'Palatino Linotype',Palatino,'Book Antiqua',Garamond,Georgia,serif",
|
|
2859
|
+
}),
|
|
2860
|
+
Object.freeze({
|
|
2861
|
+
value: 'sans', label: '无衬线', group: '无衬线体',
|
|
2862
|
+
stack: "'Segoe UI','Helvetica Neue',Helvetica,Arial,'Liberation Sans',sans-serif",
|
|
2863
|
+
}),
|
|
2864
|
+
Object.freeze({
|
|
2865
|
+
value: 'humanist', label: '人文', group: '无衬线体',
|
|
2866
|
+
stack: "Verdana,Tahoma,'Trebuchet MS','Gill Sans MT','Gill Sans',sans-serif",
|
|
2867
|
+
}),
|
|
2868
|
+
Object.freeze({
|
|
2869
|
+
value: 'mono', label: '等宽',
|
|
2870
|
+
stack: "'Cascadia Mono',Consolas,Menlo,'DejaVu Sans Mono','Liberation Mono',monospace",
|
|
2871
|
+
}),
|
|
2872
|
+
Object.freeze({
|
|
2873
|
+
value: 'kai', label: '手写',
|
|
2874
|
+
stack: "'Segoe Script','Segoe Print','Bradley Hand','Comic Sans MS',cursive",
|
|
2875
|
+
}),
|
|
2829
2876
|
])
|
|
2830
2877
|
const ZH_FONT_FAMILIES = Object.freeze([
|
|
2831
2878
|
Object.freeze({ value: 'system', label: '系统默认', stack: '' }),
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
//
|
|
2835
|
-
|
|
2836
|
-
|
|
2879
|
+
// 🔴 顺序就是「系统优先、自带兜底」(用户裁定,D207):SimSun / 微软雅黑 排在最前,
|
|
2880
|
+
// 机器上有原版就用原版;没有时落到随包自带的 Paper Noto(D207),任何平台都渲染得出。
|
|
2881
|
+
// 浏览器从栈左往右找第一个**存在**的字体,不需要任何运行时检测。
|
|
2882
|
+
Object.freeze({
|
|
2883
|
+
value: 'serif', label: '宋体', group: '印刷体',
|
|
2884
|
+
stack: "SimSun,'Songti SC',STSong,'Paper Noto Serif SC','Noto Serif CJK SC','Source Han Serif SC',serif",
|
|
2885
|
+
}),
|
|
2886
|
+
Object.freeze({
|
|
2887
|
+
value: 'sans', label: '黑体', group: '印刷体',
|
|
2888
|
+
stack: "'Microsoft YaHei','Heiti SC','PingFang SC','Paper Noto Sans SC','Noto Sans CJK SC','Source Han Sans SC','WenQuanYi Zen Hei',sans-serif",
|
|
2889
|
+
}),
|
|
2890
|
+
Object.freeze({
|
|
2891
|
+
value: 'kai', label: '楷体', group: '印刷体',
|
|
2892
|
+
stack: "KaiTi,'Kaiti SC',STKaiti,'LXGW WenKai',serif",
|
|
2893
|
+
}),
|
|
2894
|
+
Object.freeze({
|
|
2895
|
+
value: 'fangsong', label: '仿宋', group: '印刷体',
|
|
2896
|
+
stack: "FangSong,STFangsong,'Noto Serif CJK SC',serif",
|
|
2897
|
+
}),
|
|
2898
|
+
Object.freeze({
|
|
2899
|
+
value: 'yuan', label: '圆体', group: '印刷体',
|
|
2900
|
+
stack: "'Yuanti SC',YouYuan,'Microsoft YaHei',sans-serif",
|
|
2901
|
+
}),
|
|
2902
|
+
Object.freeze({
|
|
2903
|
+
value: 'xingkai', label: '行楷', group: '书体',
|
|
2904
|
+
stack: "STXingkai,'Xingkai SC',KaiTi,cursive",
|
|
2905
|
+
}),
|
|
2906
|
+
Object.freeze({
|
|
2907
|
+
value: 'lishu', label: '隶书', group: '书体',
|
|
2908
|
+
stack: "LiSu,'Libian SC',STLiti,serif",
|
|
2909
|
+
}),
|
|
2837
2910
|
])
|
|
2838
2911
|
/** 某一侧的字体清单。side 是 'src' / 'zh'。 */
|
|
2839
2912
|
function fontFamiliesOf(side) {
|
|
2840
2913
|
return side === 'src' ? SRC_FONT_FAMILIES : ZH_FONT_FAMILIES
|
|
2841
2914
|
}
|
|
2915
|
+
/**
|
|
2916
|
+
* 字体菜单的 items(宿主 Menu 的三态条目:普通项 / 分组标题 / 分隔线)。
|
|
2917
|
+
*
|
|
2918
|
+
* 🔴 分组标题只在**同组两项以上**时插:一项底下顶个标题只是噪声。
|
|
2919
|
+
* 中文那侧「印刷体(五项)/ 书体(两项)」的分法是有意义的 —— 前者拿来读,后者拿来摆;
|
|
2920
|
+
* 英文那侧衬线组三项、无衬线组两项也够格,等宽与手写各一项,于是不出标题。
|
|
2921
|
+
*
|
|
2922
|
+
* ⚠️ 组名**必须与所有档位名错开**:两者撞名时菜单上会出现两个一模一样的词,
|
|
2923
|
+
* 一个点得动一个点不动(第一版英文组名写成「衬线」,与档位「衬线」撞了个正着)。
|
|
2924
|
+
*
|
|
2925
|
+
* 标题的 id 加 group: 前缀,纯粹为了不与某个档位的 value 撞车 —— 宿主拿它当 key。
|
|
2926
|
+
*/
|
|
2927
|
+
function fontMenuItemsOf(side) {
|
|
2928
|
+
const list = fontFamiliesOf(side)
|
|
2929
|
+
const counts = new Map()
|
|
2930
|
+
for (const entry of list) {
|
|
2931
|
+
if (entry.group === undefined) continue
|
|
2932
|
+
counts.set(entry.group, (counts.get(entry.group) ?? 0) + 1)
|
|
2933
|
+
}
|
|
2934
|
+
const items = []
|
|
2935
|
+
let current
|
|
2936
|
+
for (const entry of list) {
|
|
2937
|
+
const group = entry.group
|
|
2938
|
+
if (group !== current && group !== undefined && (counts.get(group) ?? 0) > 1) {
|
|
2939
|
+
items.push({ type: 'label', id: 'group:' + group, text: group })
|
|
2940
|
+
}
|
|
2941
|
+
if (group !== undefined) current = group
|
|
2942
|
+
items.push({ id: entry.value, label: entry.label })
|
|
2943
|
+
}
|
|
2944
|
+
return items
|
|
2945
|
+
}
|
|
2842
2946
|
/**
|
|
2843
2947
|
* 把存下来的 family 解析成某一侧真正要用的栈。**每侧各用各的清单**:
|
|
2844
2948
|
* 旧数据里译文存的 'serif' 拿到原文那侧仍然叫 'serif',但栈换成了拉丁字体。
|
|
@@ -3899,6 +4003,19 @@ button.paper-select>span{min-width:0;overflow:hidden;text-overflow:ellipsis;whit
|
|
|
3899
4003
|
原文档 → 整档用 src 那套;译文档 → 整档用 zh 那套;双语档 → 原文段用 src、译文段用 zh。
|
|
3900
4004
|
⚠️ 兜底必须与源码常量同口径(DEFAULT_FONT_SIZE / DEFAULT_LINE_HEIGHT),
|
|
3901
4005
|
client-css.test.js 会拿源码里的常量来对,改常量时这里不改就会红。 ---- */
|
|
4006
|
+
/* ---- 随包自带的中文兜底字体(D207)----
|
|
4007
|
+
🔴 **系统字体优先,这两款垫底**:字体栈里 SimSun / 微软雅黑 排在 Paper Noto 前面,
|
|
4008
|
+
读者机器上有原版就用原版,没有才落到这里。不需要任何运行时检测 ——
|
|
4009
|
+
浏览器解析 font-family 时从左往右找第一个已安装或已声明的字体,天然就是这个语义。
|
|
4010
|
+
|
|
4011
|
+
文件是子集化的(GB2312 一级常用字 3755 + ASCII + 常用标点,共 3880 字):
|
|
4012
|
+
宋 679KB / 黑 525KB。生僻字超出这个集合时继续沿栈回落到系统字体,不会豆腐块。
|
|
4013
|
+
|
|
4014
|
+
为什么不用 @fontsource 的 200 多个 unicode-range 切片:切片省的是下载量,
|
|
4015
|
+
而我们本来就要打进 npm 包 —— 202 条 @font-face 的规则表本身 187KB,
|
|
4016
|
+
抵不过省下的流量,还让客户端源码膨胀。单文件子集只有两条规则。 */
|
|
4017
|
+
@font-face{font-family:'Paper Noto Serif SC';src:url('/api/dsh-paper/font-file?name=NotoSerifSC-common.woff2') format('woff2');font-weight:400;font-style:normal;font-display:swap}
|
|
4018
|
+
@font-face{font-family:'Paper Noto Sans SC';src:url('/api/dsh-paper/font-file?name=NotoSansSC-common.woff2') format('woff2');font-weight:400;font-style:normal;font-display:swap}
|
|
3902
4019
|
.paper-reader{--reader-src-font-size:18px;--reader-src-line-height:1.7;
|
|
3903
4020
|
--reader-zh-font-size:18px;--reader-zh-line-height:1.7;
|
|
3904
4021
|
--reader-paper:#f6f3ec;--reader-ink:#23211d}
|
|
@@ -6423,7 +6540,7 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
6423
6540
|
const typeFamily = side => h(Menu, {
|
|
6424
6541
|
open: typePop === 'family',
|
|
6425
6542
|
onClose: () => setTypePop(''),
|
|
6426
|
-
items:
|
|
6543
|
+
items: fontMenuItemsOf(side),
|
|
6427
6544
|
selectedId: readerType[side].family,
|
|
6428
6545
|
onSelect: id => { setTypePop(''); patchType(side, { family: fontOf(side, id) }) },
|
|
6429
6546
|
portal: true,
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Google Inc.
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
http://scripts.sil.org/OFL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
package/lib/gloss.js
CHANGED
|
@@ -1031,14 +1031,29 @@ export async function translateSelection(input = {}, deps = {}) {
|
|
|
1031
1031
|
// 🔴 但**不能一律要求「只抄第一句」**(2026-11-20 实测,用户截图):选区跨三句时模型照办只抄
|
|
1032
1032
|
// 一句,另一列只亮起开头 44 字(English 298 字 → 中文 44 字,而对应内容有 101 字)。
|
|
1033
1033
|
// 选区的句数由用户决定,提示词不能替它拍板。现在两头都点明:该抄的整段,抄到内容结束为止。
|
|
1034
|
+
//
|
|
1035
|
+
// 🔴 **「答案只能来自 Paragraph」必须单独成句**(2026-11-27 实测 Hy-MT3)。
|
|
1036
|
+
//
|
|
1037
|
+
// 原措辞是 `Output the English text from the paragraph that corresponds to the Chinese.`,
|
|
1038
|
+
// 在 tencent/Hy-MT3-7B-GGUF 上两个方向都**原样回显输入**:英文选区回英文、中文选区回中文。
|
|
1039
|
+
// 逐条 A/B 之后根因不是「没说清语言」,而是这一句式被读成了「把选区转写一遍」——
|
|
1040
|
+
// 只要把约束换成 `Your answer must be copied from the Paragraph, never from the target.`
|
|
1041
|
+
// 单独占一行,再用 `The Paragraph is in X and the target is its Y translation.` 交代两侧语言,
|
|
1042
|
+
// 整句、多句、表格行三个方向全部命中(4/4);仅把语言名换个位置(`The Paragraph is Chinese
|
|
1043
|
+
// and the Target is English. Copy the Chinese portion…`)仍会回显,可见起作用的是这一句约束。
|
|
1044
|
+
//
|
|
1045
|
+
// ⚠️ 剩下的已知短板:选区是**单个词**时(`注意力`)Hy-MT3 仍会把它当翻译任务而不是定位任务。
|
|
1046
|
+
// 结构档 {@link structuralSpan} 在能给出跨度时并不依赖这一档的回复,所以影响面只剩
|
|
1047
|
+
// 「结构档给不出跨度、且用户只划了一个词」的降级路径。
|
|
1034
1048
|
system: '',
|
|
1035
|
-
user: '
|
|
1036
|
-
+
|
|
1037
|
-
+ '
|
|
1038
|
-
+
|
|
1039
|
-
+ '
|
|
1049
|
+
user: 'Do not translate. Copy verbatim.\n\n'
|
|
1050
|
+
+ 'Your answer must be copied from the Paragraph, never from the target.\n'
|
|
1051
|
+
+ 'The Paragraph is in ' + otherLanguageName(counterpart, counterpartKind) + ' and the target is its '
|
|
1052
|
+
+ quoteLanguageName(counterpart, counterpartKind) + ' translation.\n'
|
|
1053
|
+
+ 'Output the ' + otherLanguageName(counterpart, counterpartKind) + ' substring of the Paragraph that the target translates — '
|
|
1054
|
+
+ 'the whole span, which may cover several sentences. Copy it character for character.\n\n'
|
|
1040
1055
|
+ 'Paragraph:\n' + counterpart + '\n\n'
|
|
1041
|
-
+ quoteLanguageName(counterpart, counterpartKind) + ':\n' + quote,
|
|
1056
|
+
+ quoteLanguageName(counterpart, counterpartKind) + ' target:\n' + quote,
|
|
1042
1057
|
} : buildSelectionPrompt({
|
|
1043
1058
|
quote,
|
|
1044
1059
|
context: context.slice(0, GLOSS_CONTEXT_LIMIT),
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto'
|
|
|
3
3
|
import { mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'
|
|
4
4
|
import { homedir } from 'node:os'
|
|
5
5
|
import { basename, dirname, extname, isAbsolute, join, resolve } from 'node:path'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
6
7
|
import { visibleHeadingIndexes } from './blocks.js'
|
|
7
8
|
import { normalizeBlockId } from './slicing.js'
|
|
8
9
|
import { buildContextMessage, deliveredPaperState, isProducerSource } from './context.js'
|
|
@@ -562,6 +563,23 @@ export function apply(ctx, config = {}) {
|
|
|
562
563
|
const document = hydrateDocument(raw)
|
|
563
564
|
return { markdownPath: document.markdownPath, pdfPath: document.pdfPath }
|
|
564
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* 随包自带的中文字体分片目录(D207)。
|
|
568
|
+
*
|
|
569
|
+
* `lib/fonts/` 与 `lib/index.js` 是**同一个发布包里的兄弟目录**,所以用
|
|
570
|
+
* `import.meta.url` 反推而不是 `process.cwd()` —— 后者是宿主进程的工作目录,
|
|
571
|
+
* 与插件包在哪毫无关系。桌面端把插件装在 `~/.dsh/profiles/<面>/local-plugins/<名>/`,
|
|
572
|
+
* 开发目录则指向仓库里那份,两种部署都靠这条相对关系找到字体。
|
|
573
|
+
*/
|
|
574
|
+
function bundledFontDir() {
|
|
575
|
+
try {
|
|
576
|
+
return fileURLToPath(new URL('./fonts', import.meta.url))
|
|
577
|
+
} catch {
|
|
578
|
+
// 拿不到自身 URL 的打包形态(罕见):返回空串,路由回 503。
|
|
579
|
+
// 字体缺席只会让正文落到系统字体,绝不阻塞阅读。
|
|
580
|
+
return ''
|
|
581
|
+
}
|
|
582
|
+
}
|
|
565
583
|
// 与 tool 那一路同样**惰性构造**:apply 期 ctx.get('jobs') 之类还没注册,构造时做断言会污染日志。
|
|
566
584
|
let assetRoutesInstance
|
|
567
585
|
function assetRoutesFor() {
|
|
@@ -570,6 +588,7 @@ export function apply(ctx, config = {}) {
|
|
|
570
588
|
renderPageImages,
|
|
571
589
|
cacheDir: pageImageCacheDir,
|
|
572
590
|
command: config.mineruCommand,
|
|
591
|
+
fontDir: bundledFontDir(),
|
|
573
592
|
})
|
|
574
593
|
return assetRoutesInstance
|
|
575
594
|
}
|
|
@@ -3975,7 +3994,7 @@ function hasMeaningfulValue(value) {
|
|
|
3975
3994
|
: path === '/marks' ? request => serializeMarks(() => dispatch(request)) : dispatch,
|
|
3976
3995
|
})
|
|
3977
3996
|
// 阅读器资源(字节响应,不经 JSON dispatch)。
|
|
3978
|
-
for (const [path, name] of [['/analysis-asset', 'analysis-asset'], ['/page-image', 'page-image']]) {
|
|
3997
|
+
for (const [path, name] of [['/analysis-asset', 'analysis-asset'], ['/page-image', 'page-image'], ['/font-file', 'font-file']]) {
|
|
3979
3998
|
connectionCtx.connection.registerFetchRoute(ctx, {
|
|
3980
3999
|
path: API_BASE + path,
|
|
3981
4000
|
methods: ['GET'],
|
package/lib/translate-prompts.js
CHANGED
|
@@ -97,8 +97,8 @@ function idOf(block) {
|
|
|
97
97
|
// ============ 导出 1:构造提示词 ============
|
|
98
98
|
|
|
99
99
|
// 输出要求按**协议**分岔。json 是默认(通用模型),与加这个开关之前逐字节一致;
|
|
100
|
-
// lines 给专用机器翻译模型(如本地 hy-mt2)用 —— 它们不吐 JSON
|
|
101
|
-
//
|
|
100
|
+
// lines 给专用机器翻译模型(如本地 hy-mt2 / Hy-MT3)用 —— 它们不吐 JSON 数组,只吐「每行一条」
|
|
101
|
+
// 的方言,详见 parseLineDialect 与 parseNdjsonDialect 里记的实测。
|
|
102
102
|
const OUTPUT_RULES = {
|
|
103
103
|
json: [
|
|
104
104
|
'只输出一个 JSON 数组,不要任何解释文字,不要 Markdown 围栏。',
|
|
@@ -469,15 +469,13 @@ const LINE_TAIL = /^[\]"'\s]+/
|
|
|
469
469
|
function parseLineDialect(text, expected) {
|
|
470
470
|
const translations = []
|
|
471
471
|
const seen = new Set()
|
|
472
|
-
/**
|
|
472
|
+
/** 最近一条**已成条**的译文;表格行与公式块往它身上接续行。 */
|
|
473
473
|
let current = null
|
|
474
474
|
for (const line of String(text).split(/\r?\n/)) {
|
|
475
475
|
const rest = line.replace(LINE_HEAD, '')
|
|
476
476
|
const match = LINE_ID.exec(rest)
|
|
477
|
-
// 续行只认表格行(行首 `|`):表格译文本身就是多行,第一行跟在 id 后面,其余行紧跟其后。
|
|
478
|
-
// 🔴 只收 `|` 行 —— 其他多行内容照旧丢弃,这条改动不能放宽「每行一条」的既有语义。
|
|
479
477
|
if (match === null || !expected.set.has(match[0].trim())) {
|
|
480
|
-
if (current !== null &&
|
|
478
|
+
if (current !== null && continuesCurrent(line, current.zh)) current.zh += '\n' + line.trim()
|
|
481
479
|
continue
|
|
482
480
|
}
|
|
483
481
|
// 正则没有捕获组:匹配到的整段就是 id 本体(`[^\[\]"]+`)。
|
|
@@ -489,6 +487,121 @@ function parseLineDialect(text, expected) {
|
|
|
489
487
|
current = { id, zh }
|
|
490
488
|
translations.push(current)
|
|
491
489
|
}
|
|
490
|
+
for (const item of translations) item.zh = stripStrayCloseBracket(item.zh)
|
|
491
|
+
return translations
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* 这一行是不是**上一条译文的续行**。
|
|
496
|
+
*
|
|
497
|
+
* 两条判据,都不放宽「每行一条」:一条没接完的译文才收续行,接完就停。
|
|
498
|
+
*
|
|
499
|
+
* 1. **表格行**(行首 `|`):表格译文本身就是多行,第一行跟在 id 后面,其余行紧跟其后。
|
|
500
|
+
* 2. **显示公式块**(`$$` 未配平):实测 2026-11-27 真实论文(AgentPoison,Hy-MT3)——
|
|
501
|
+
* `["p4-e1"] $$` 之后公式体与收尾 `$$` 各占一行:
|
|
502
|
+
*
|
|
503
|
+
* ```text
|
|
504
|
+
* ["p4-e1"] $$
|
|
505
|
+
* \mathbb {E} _ {q \sim \pi_ {q}} [ \mathbb {1} (… = a _ {m}) ],\tag{1}
|
|
506
|
+
* $$
|
|
507
|
+
* ```
|
|
508
|
+
*
|
|
509
|
+
* 🔴 只认 `|` 行时这三行只剩第一行,译文变成字面上的 `$$` —— 而它非空、结构校验也过得去,
|
|
510
|
+
* 于是被当成**成功译文**写进 translation.json。那不是「翻译失败」,是静默的数据损坏:
|
|
511
|
+
* 读者拿到一对空公式符,而原文里是一整条公式。公式块必须按未配平的 `$$` 收全。
|
|
512
|
+
*
|
|
513
|
+
* 判据用 `$$` 出现次数的**奇偶**而不是「行首是不是 `$$`」:模型也会写成
|
|
514
|
+
* `["p4-e1"] $$
|
|
515
|
+
…
|
|
516
|
+
$$` 或整条公式挤在一行(那行 `$$` 成对,不需要续行)。
|
|
517
|
+
*
|
|
518
|
+
* @param {string} line 原始行(未剥壳)
|
|
519
|
+
* @param {string} sofar 该条已经收下的译文
|
|
520
|
+
* @returns {boolean}
|
|
521
|
+
*/
|
|
522
|
+
function continuesCurrent(line, sofar) {
|
|
523
|
+
if (/^\s*\|/.test(line)) return true
|
|
524
|
+
return mathOpen(sofar)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** 显示公式(`$$`)是否还没收尾 —— 奇偶即可,不必解析内容。 */
|
|
528
|
+
function mathOpen(text) {
|
|
529
|
+
return (String(text).split('$$').length - 1) % 2 === 1
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* 剥掉译文末尾**多出来的**收尾方括号。
|
|
534
|
+
*
|
|
535
|
+
* 实测(同上,Hy-MT3 行方言):模型把行首标记的收尾写到了正文后面 ——
|
|
536
|
+
* `["p4-p5"] 其中 $a _ { b }$ 表示…不同。]`,正文末尾多一个 `]`。
|
|
537
|
+
* 它与行首的 `["p4-p5"]` 是配对的,剥行首时只剥掉了一半。
|
|
538
|
+
*
|
|
539
|
+
* 🔴 只在**确实多出一个**时才剥(`]` 比 `[` 多、且串尾就是 `]`)。正文自身成对的方括号
|
|
540
|
+
* 必须原样保留:引用编号 `[39]`、占位符 `[Your definition here]` 都在译文里正常出现。
|
|
541
|
+
*
|
|
542
|
+
* @param {string} zh
|
|
543
|
+
* @returns {string}
|
|
544
|
+
*/
|
|
545
|
+
function stripStrayCloseBracket(zh) {
|
|
546
|
+
let text = String(zh)
|
|
547
|
+
while (text.endsWith(']')) {
|
|
548
|
+
const open = (text.match(/\[/g) ?? []).length
|
|
549
|
+
const close = (text.match(/\]/g) ?? []).length
|
|
550
|
+
if (close <= open) break
|
|
551
|
+
text = text.slice(0, -1).trimEnd()
|
|
552
|
+
}
|
|
553
|
+
return text
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* 按 **NDJSON 行方言**解析回复:每行一个完整的 `{"id":…,"zh":…}` 对象。
|
|
558
|
+
*
|
|
559
|
+
* 🔴 专用机器翻译模型在「要 JSON 数组」的指令下常常**省掉外层方括号**,改成一行一条记录。
|
|
560
|
+
* 实测(本地 tencent/Hy-MT3-7B-GGUF,Unsloth Studio,2026-11-27)7 块的整批回复是
|
|
561
|
+
*
|
|
562
|
+
* ```text
|
|
563
|
+
* {"id":"p1-h1","zh":"Attention Is All You Need"}
|
|
564
|
+
* {"id":"p1-p1","zh":"主流序列转换模型基于复杂的循环或卷积神经网络…"}
|
|
565
|
+
* ```
|
|
566
|
+
*
|
|
567
|
+
* 逐行合法,合起来却不是 JSON 数组 —— `parseArraySlice` 只能报 `no array found`,
|
|
568
|
+
* 于是整批判失败、译文一条都收不回来。
|
|
569
|
+
*
|
|
570
|
+
* 收它**不会**削弱 JSON 路径:调用方先试数组、再试行 id 方言、最后才到这里;三条路径汇合到
|
|
571
|
+
* 同一段收敛逻辑,重复 id / 空译文 / 不在期望列表的判据只有一份。
|
|
572
|
+
*
|
|
573
|
+
* 只认**整行就是一个对象**的行(前后可有空白):模型顺手加的解释文字、围栏标记、行首 `[id]`
|
|
574
|
+
* 前缀都不吞进来 —— 逐行 `JSON.parse` 只要有一处宽松解析就会把正文里的花括号当成条目。
|
|
575
|
+
*
|
|
576
|
+
* 🔴 与 {@link parseLineDialect} 不同,这里**不按期望 id 过滤**:一条合法 NDJSON 记录必须同时有
|
|
577
|
+
* 字符串 id 与 zh,正文里的花括号短语凑不出这个形状;而过滤放给 {@link collectTranslations}
|
|
578
|
+
* 才能把「id 不在期望列表」记进 unexpected(行方言靠期望集合挡引用编号,两者判据不同源)。
|
|
579
|
+
*
|
|
580
|
+
* @param {string} text
|
|
581
|
+
* @returns {Array<{id: string, zh: string}>} 按出现顺序
|
|
582
|
+
*/
|
|
583
|
+
function parseNdjsonDialect(text) {
|
|
584
|
+
const translations = []
|
|
585
|
+
// 同一处字节问题在 NDJSON 里照样存在(`{"zh":"$\m…"}`),逐行修再解析。
|
|
586
|
+
const source = repairIllegalJsonEscapes(text)
|
|
587
|
+
for (const line of source.split(/\r?\n/)) {
|
|
588
|
+
// 🔴 **行尾逗号要剥**:同一模型同一档会吐出两种形态 —— 不带逗号(7 块测试批次)
|
|
589
|
+
// 与带逗号(6 块批次:`{…},\n{…},\n{…}`,只差外层方括号)。后者若不剥,
|
|
590
|
+
// 除最后一行外全部认不出(实测 6 块只认领到 1 块)。
|
|
591
|
+
const trimmed = line.trim().replace(/,$/, '')
|
|
592
|
+
// 围栏行(```json / ```)与解释文字不是条目;只有整行对象才进解析。
|
|
593
|
+
if (!trimmed.startsWith('{') || !trimmed.endsWith('}')) continue
|
|
594
|
+
let parsed
|
|
595
|
+
try {
|
|
596
|
+
parsed = JSON.parse(trimmed)
|
|
597
|
+
} catch {
|
|
598
|
+
continue
|
|
599
|
+
}
|
|
600
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) continue
|
|
601
|
+
if (typeof parsed.id !== 'string' || typeof parsed.zh !== 'string') continue
|
|
602
|
+
// NDJSON 同样经过 JSON.parse,同样会被解码 —— 与 JSON 数组那条路共用同一个还原。
|
|
603
|
+
translations.push({ id: parsed.id, zh: restoreDecodedLatex(parsed.zh) })
|
|
604
|
+
}
|
|
492
605
|
return translations
|
|
493
606
|
}
|
|
494
607
|
|
|
@@ -521,6 +634,148 @@ function normalizeExpected(expectedIds) {
|
|
|
521
634
|
return { list, set }
|
|
522
635
|
}
|
|
523
636
|
|
|
637
|
+
/**
|
|
638
|
+
* 把 `JSON.parse` 顺手解码掉的 LaTeX 反斜杠还原回来。
|
|
639
|
+
*
|
|
640
|
+
* 🔴 这是**静默的数据损坏**,比解析失败严重得多(2026-11-27 真实论文实测)。
|
|
641
|
+
*
|
|
642
|
+
* JSON 字符串里 `\t` `\b` `\f` 是**合法转义**,而模型在 zh 字段里写 LaTeX 时不会做二次转义
|
|
643
|
+
* (它以为那是纯文本)。于是下面三条全部「解析成功、译文非空、状态 ok」,公式却已经烂了:
|
|
644
|
+
*
|
|
645
|
+
* ```text
|
|
646
|
+
* $\top$ → 制表符 + "op" ($\top$ 是转置符号)
|
|
647
|
+
* $\bar{x}$ → 退格符 + "ar{x}" (\bar 是上划线)
|
|
648
|
+
* $\frac{1}{2}$ → 换页符 + "rac{1}{2}"(\frac 是分式)
|
|
649
|
+
* ```
|
|
650
|
+
*
|
|
651
|
+
* 渲染出来是乱码,而日志里一个字都不会提 —— 只有把译文与原文逐字对拍才发现得了。
|
|
652
|
+
*
|
|
653
|
+
* ## 为什么只还原这三个
|
|
654
|
+
*
|
|
655
|
+
* 判据是「这个控制字符在译文里**永远不可能是真的**」:
|
|
656
|
+
*
|
|
657
|
+
* - 制表符、退格、换页在论文译文里没有任何合法用途(GFM 表格也不用制表符对齐);
|
|
658
|
+
* - 而 `\n` 与 `\r` 是**真实需要**的:公式块的 `$$\n…\n$$` 就是真换行,还原成字面量反而毁掉它。
|
|
659
|
+
* ⚠️ 代价是 `\nabla` `\nu` `\neq` `\rho` `\right` 这类以 n / r 开头的命令仍会被解码。
|
|
660
|
+
* 这一条**没有安全的自动修法** —— `\n` 既可能是换行也可能是 `\nabla`,两种解释都合法,
|
|
661
|
+
* 猜错就是把公式改成另一条公式。宁可留着这个已知缺口,也不做不可靠的猜测。
|
|
662
|
+
*
|
|
663
|
+
* 实测依据:AgentPoison 那篇 264 个块里,原文含控制字符的是 **0 个** —— 所以「译文里出现
|
|
664
|
+
* 这三个控制字符」本身就是一个干净的损坏信号,不会误伤正常内容。
|
|
665
|
+
*
|
|
666
|
+
* @param {string} zh
|
|
667
|
+
* @returns {string}
|
|
668
|
+
*/
|
|
669
|
+
function restoreDecodedLatex(zh) {
|
|
670
|
+
if (typeof zh !== 'string' || zh === '') return zh
|
|
671
|
+
if (!DECODED_LATEX_CONTROL.test(zh)) return zh
|
|
672
|
+
let text = zh
|
|
673
|
+
for (const [from, to] of DECODED_LATEX) text = text.split(from).join(to)
|
|
674
|
+
return text
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* 把模型回复里**不合法的 JSON 转义**补成合法转义,让 `JSON.parse` 能读。
|
|
679
|
+
*
|
|
680
|
+
* 🔴 这不是「容错解析」,是**修字面 bug**:模型在 zh 字段里写 LaTeX 时**不做二次转义**,
|
|
681
|
+
* 而 LaTeX 命令全是反斜杠开头。真实回复(AgentPoison,Hy-MT3,2026-11-27)统计:
|
|
682
|
+
*
|
|
683
|
+
* ```text
|
|
684
|
+
* \m 27 处 \c 5 处 \h 4 处 \s 3 处 \p 3 处
|
|
685
|
+
* \{ 2 处 \} 2 处 … 合计 52 处非法转义
|
|
686
|
+
* ```
|
|
687
|
+
*
|
|
688
|
+
* `\m` `\c` `\h` 这些**根本不是合法的 JSON 转义**,`JSON.parse` 当场抛
|
|
689
|
+
* `Bad escaped character`。于是整批 19 个块判失败,一条译文都收不回来 —— 而模型其实
|
|
690
|
+
* 全都译对了,只是那一处字节不合规。
|
|
691
|
+
*
|
|
692
|
+
* ## 两件事,判据都无歧义
|
|
693
|
+
*
|
|
694
|
+
* 1. **非法转义**(`\m` `\{` 这类):除了「补一个反斜杠」没有别种解释。合法转义只有
|
|
695
|
+
* `" \\ / b f n r t u`,其余一律是写坏的 LaTeX。`u` 只在后四位是十六进制时才放行。
|
|
696
|
+
* 2. **字符串里的裸控制字符**(模型直接写了个真换行,而 JSON 要求写成 `\n`):
|
|
697
|
+
* 它只能是「本来就想表达这个控制字符」,按 JSON 规则转义回去。
|
|
698
|
+
*
|
|
699
|
+
* ## 为什么敢动字节
|
|
700
|
+
*
|
|
701
|
+
* 只修**确定非法**的位置;合法转义一个都不碰,所以对正常回复是恒等变换(有测试钉住)。
|
|
702
|
+
* 修完仍解析不出的,照旧 Fail Closed —— 这条路径不会让坏数据混进来。
|
|
703
|
+
*
|
|
704
|
+
* ⚠️ `b f n r t` 这五个**合法但歧义**的转义**不在这里处理**:`\top` 和「制表符+op」是同一
|
|
705
|
+
* 字节序列,改它等于猜。它们由 {@link restoreDecodedLatex} 在解析后按「译文里不可能有控制
|
|
706
|
+
* 字符」这条依据还原。
|
|
707
|
+
*
|
|
708
|
+
* @param {string} text
|
|
709
|
+
* @returns {string}
|
|
710
|
+
*/
|
|
711
|
+
function repairIllegalJsonEscapes(text) {
|
|
712
|
+
const source = String(text)
|
|
713
|
+
let out = ''
|
|
714
|
+
let inString = false
|
|
715
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
716
|
+
const ch = source[index]
|
|
717
|
+
const code = ch.charCodeAt(0)
|
|
718
|
+
if (!inString) {
|
|
719
|
+
out += ch
|
|
720
|
+
if (ch === '"') inString = true
|
|
721
|
+
continue
|
|
722
|
+
}
|
|
723
|
+
if (ch === '"') {
|
|
724
|
+
inString = false
|
|
725
|
+
out += ch
|
|
726
|
+
continue
|
|
727
|
+
}
|
|
728
|
+
// 字符串字面量里的裸控制字符:JSON 要求转义书写,补上。
|
|
729
|
+
if (code < 0x20) {
|
|
730
|
+
if (ch === '\n') out += '\\n'
|
|
731
|
+
else if (ch === '\r') out += '\\r'
|
|
732
|
+
else if (ch === '\t') out += '\\t'
|
|
733
|
+
else out += '\\u' + code.toString(16).padStart(4, '0')
|
|
734
|
+
continue
|
|
735
|
+
}
|
|
736
|
+
if (ch !== '\\') {
|
|
737
|
+
out += ch
|
|
738
|
+
continue
|
|
739
|
+
}
|
|
740
|
+
const next = source[index + 1]
|
|
741
|
+
if (next === undefined) {
|
|
742
|
+
out += ch
|
|
743
|
+
continue
|
|
744
|
+
}
|
|
745
|
+
// 🔴 转义的**成对反斜杠要一起吃掉**:`\\\\` 是合法转义,它的第二个反斜杠不是新转义的
|
|
746
|
+
// 开头。只消费第一个的话,第二个会被判成「非法转义」再补一个,把 `\\\\mathbb` 变成
|
|
747
|
+
// `\\\\\\mathbb` —— 一个完全合法的回复反而被改坏。
|
|
748
|
+
if (next === '\\') {
|
|
749
|
+
out += ch + next
|
|
750
|
+
index += 1
|
|
751
|
+
continue
|
|
752
|
+
}
|
|
753
|
+
if (next === 'u' && /^[0-9a-fA-F]{4}$/.test(source.slice(index + 2, index + 6))) {
|
|
754
|
+
out += ch
|
|
755
|
+
continue
|
|
756
|
+
}
|
|
757
|
+
if (JSON_ESCAPE_LETTERS.has(next)) {
|
|
758
|
+
out += ch
|
|
759
|
+
continue
|
|
760
|
+
}
|
|
761
|
+
// 非法转义:补一个反斜杠,让它变成字面量的反斜杠 + 原字符。
|
|
762
|
+
out += ch + ch
|
|
763
|
+
}
|
|
764
|
+
return out
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** JSON 规定的单字符转义字母。`u` 另判(要跟四位十六进制)。 */
|
|
768
|
+
const JSON_ESCAPE_LETTERS = new Set(['"', '\\', '/', 'b', 'f', 'n', 'r', 't'])
|
|
769
|
+
|
|
770
|
+
/** 三个「译文里永远不可能是真的」控制字符:退格、换页、制表。 */
|
|
771
|
+
const DECODED_LATEX_CONTROL = /[\u0008\u000c\u0009]/
|
|
772
|
+
/** 还原表。顺序无关:替换出来的都是反斜杠 + 字母,不会再撞上控制字符。 */
|
|
773
|
+
const DECODED_LATEX = [
|
|
774
|
+
['\u0008', '\\b'],
|
|
775
|
+
['\u000c', '\\f'],
|
|
776
|
+
['\u0009', '\\t'],
|
|
777
|
+
]
|
|
778
|
+
|
|
524
779
|
function failure(reason, expected, notes) {
|
|
525
780
|
notes.push(reason)
|
|
526
781
|
return {
|
|
@@ -550,7 +805,11 @@ export function parseBatchReply(text, expectedIds) {
|
|
|
550
805
|
|
|
551
806
|
let parsed = null
|
|
552
807
|
let reason = 'no array found'
|
|
553
|
-
|
|
808
|
+
// 🔴 先修非法转义再解析:模型写 LaTeX 时不给反斜杠做二次转义,`\m` `\{` 这类在 JSON 里
|
|
809
|
+
// 直接抛 Bad escaped character,整批 19 块判失败 —— 而模型其实全都译对了。
|
|
810
|
+
// 只动**确定非法**的位置,合法转义一个不碰,对正常回复是恒等变换。
|
|
811
|
+
const repaired = repairIllegalJsonEscapes(text)
|
|
812
|
+
for (const candidate of candidateTexts(repaired)) {
|
|
554
813
|
const attempt = parseArraySlice(candidate)
|
|
555
814
|
if (attempt.items) {
|
|
556
815
|
parsed = attempt.items
|
|
@@ -558,12 +817,17 @@ export function parseBatchReply(text, expectedIds) {
|
|
|
558
817
|
}
|
|
559
818
|
reason = attempt.reason
|
|
560
819
|
}
|
|
561
|
-
// JSON
|
|
562
|
-
//
|
|
820
|
+
// JSON 数组解析不出来时,依次退一步认两种**行方言**(专用翻译模型):
|
|
821
|
+
// 1. `["<块id>"] 译文`(Hy-MT2 的自然方言,见 parseLineDialect);
|
|
822
|
+
// 2. 每行一个 `{"id":…,"zh":…}` 对象(Hy-MT3 在「要数组」指令下的真实形态,见 parseNdjsonDialect)。
|
|
823
|
+
// 三种路径的条目形状一致,汇合到同一段收敛逻辑 —— 重复 id、不在期望列表、
|
|
824
|
+
// id/zh 非字符串的判据只有一份。
|
|
563
825
|
if (!parsed) {
|
|
564
826
|
const lines = parseLineDialect(text, expected)
|
|
565
|
-
if (lines.length
|
|
566
|
-
|
|
827
|
+
if (lines.length > 0) return collectTranslations(lines, expected, notes)
|
|
828
|
+
const records = parseNdjsonDialect(text)
|
|
829
|
+
if (records.length === 0) return failure(reason, expected.list, notes)
|
|
830
|
+
return collectTranslations(records, expected, notes)
|
|
567
831
|
}
|
|
568
832
|
return collectTranslations(parsed, expected, notes)
|
|
569
833
|
}
|
|
@@ -675,7 +939,10 @@ function collectTranslations(parsed, expected, notes) {
|
|
|
675
939
|
if (!unexpected.includes(item.id)) unexpected.push(item.id)
|
|
676
940
|
continue
|
|
677
941
|
}
|
|
678
|
-
|
|
942
|
+
// 🔴 JSON 数组与 NDJSON 两条路径都在这里汇合,而两条都经过 `JSON.parse` ——
|
|
943
|
+
// 被解码成控制字符的 LaTeX 反斜杠必须在**唯一出口**还原,否则 json 档会静默写坏公式。
|
|
944
|
+
// 行方言不经过 JSON 解码,还原函数对它无副作用(没有控制字符就直接返回)。
|
|
945
|
+
translations.push({ id: item.id, zh: restoreDecodedLatex(item.zh) })
|
|
679
946
|
}
|
|
680
947
|
|
|
681
948
|
const missing = expected.list.filter((id) => !seen.has(id))
|
package/lib/translate.js
CHANGED
|
@@ -477,9 +477,16 @@ async function attemptBatch(batch, deps, acquireImages, messages) {
|
|
|
477
477
|
// (实测:终止原因明确是 max-tokens、正文只有半句,结果仍是 translated:1 / status:ok)。
|
|
478
478
|
// 终止原因为空(适配器不发 finish)时不作判断,保持与加这条规则之前一致。
|
|
479
479
|
if (finish !== '' && finish !== 'stop') {
|
|
480
|
+
// max-tokens 与其余终止原因分开说:这一条几乎总是**模型没在答**(或块太长),
|
|
481
|
+
// 而不是网络或额度问题 —— 实测(本地 Hy-MT3,6000 字符的单块)它会把预算全花在
|
|
482
|
+
// 续写与复述上,吐回 28 770 字符(原文的 4.8 倍)后才被截断,这一个块空跑 65 秒。
|
|
483
|
+
// 用户看到「终止原因 length」不知道下一步做什么,这里直接把可操作的两条写出来。
|
|
484
|
+
const hint = finish === 'length' || finish === 'max-tokens'
|
|
485
|
+
? ';模型把输出预算用光了:多半是它在复述或续写而不是作答。把设置页的「范围」改成「前 N 页」缩小单批原文,或换一个更守格式的模型'
|
|
486
|
+
: ''
|
|
480
487
|
return {
|
|
481
488
|
translations: [],
|
|
482
|
-
detail: `回复没有正常结束(终止原因 ${finish}
|
|
489
|
+
detail: `回复没有正常结束(终止原因 ${finish}),不收取被截断的译文${hint};${replyDigest(raw, finish)}`,
|
|
483
490
|
malformed: true,
|
|
484
491
|
missing: [...batch.blockIds],
|
|
485
492
|
usage,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li-canghai/dsh-paper",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"description": "PDF transcription with stable block ids, folder scanning up to two sub-levels, and session-scoped paper context with drag-select batch injection and removal for DSH.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|