dsh-pdf-workbench 0.0.0-stage → 1.3.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.
- package/LICENSE +21 -0
- package/README.md +192 -3
- package/assets/screenshot-bilingual.jpg +0 -0
- package/assets/screenshot-hero.jpg +0 -0
- package/assets/screenshot-overlay.jpg +0 -0
- package/assets/screenshot-qa.jpg +0 -0
- package/assets/screenshot-reader.jpg +0 -0
- package/assets/screenshot-shelf.jpg +0 -0
- package/client.js +14126 -0
- package/cordis.patch.yml +13 -0
- package/docs/DETAIL.md +69 -0
- package/docs/READER-REWRITE.md +95 -0
- package/docs/RELEASE.md +110 -0
- package/docs/ROADMAP.md +172 -0
- package/docs/SIDE-PREVIEW.md +271 -0
- package/docs/TESTING.md +109 -0
- package/docs/TRANSLATION.md +167 -0
- package/icon.svg +6 -0
- package/index.js +210 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +64 -6
- package/samples/manifest.json +52 -0
- package/samples/test/serre_c01_p2-3.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c00_/345/211/215/347/275/256_/345/260/201/351/235/242/347/233/256/345/275/225.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c01_/347/254/2540/347/253/240_/345/257/274/350/257/273.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c02_/347/254/2541/347/253/240_/345/217/244/351/242/230_/345/255/231/345/255/220/347/256/227/347/273/217/345/210/260/347/247/246/344/271/235/351/237/266.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c03_/347/254/2542/347/253/240_/346/225/264/346/225/260/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c04_/347/254/2543/347/253/240_/347/216/257/350/256/272/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c05_/347/254/2544/347/253/240_/345/244/232/351/241/271/345/274/217/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c06_/347/254/2545/347/253/240_/347/276/244/350/256/272/347/211/210/346/234/254.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c07_/347/254/2546/347/253/240_/347/273/237/344/270/200/347/273/223/346/236/204.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c08_/347/254/2547/347/253/240_/345/233/236/346/234/233.pdf +0 -0
- package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c90_/351/231/204/345/275/225_/346/225/231/346/235/220/351/241/265/347/240/201/347/254/246/345/217/267/350/241/250/350/207/252/346/265/213.pdf +0 -0
- package/screenshots.json +8 -0
- package/src/client/viewer.js +80 -0
- package/src/host/annotations.js +145 -0
- package/src/host/artifacts.js +317 -0
- package/src/host/blocks.js +44 -0
- package/src/host/config.js +112 -0
- package/src/host/digest.js +156 -0
- package/src/host/environment.js +42 -0
- package/src/host/http.js +124 -0
- package/src/host/index-source.js +488 -0
- package/src/host/libraries.js +221 -0
- package/src/host/links.js +201 -0
- package/src/host/pdf-source.js +122 -0
- package/src/host/pending.js +166 -0
- package/src/host/pressure.js +137 -0
- package/src/host/purge.js +100 -0
- package/src/host/render.js +43 -0
- package/src/host/routes.js +1778 -0
- package/src/host/shelf-groups.js +109 -0
- package/src/host/state.js +128 -0
- package/src/host/toolkit.js +54 -0
- package/src/host/translate-blocks.js +132 -0
- package/src/host/translate-job.js +316 -0
- package/src/host/translate.js +94 -0
- package/src/host/update.js +134 -0
- package/tools/AGENTS.md +33 -0
- package/tools/README.md +138 -0
- package/tools/auto_chapters.py +122 -0
- package/tools/dsfile.mjs +198 -0
- package/tools/export_chapter.py +108 -0
- package/tools/export_page.py +91 -0
- package/tools/md2overlay.py +200 -0
- package/tools/md2tex.py +422 -0
- package/tools/pdf_blocks.py +110 -0
- package/tools/pdf_fontsize.py +20 -0
- package/tools/pdf_merge.py +15 -0
- package/tools/pdf_pagesizes.py +8 -0
- package/tools/pdf_raster.py +21 -0
- package/tools/pdfkit.py +896 -0
- package/tools/realign_overlay.py +248 -0
- package/tools/rebuild_overlay.py +55 -0
- package/tools/rename_chapters.py +93 -0
- package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -0
- package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -0
- package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -0
- package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -0
- package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -0
- package/vendor/cmaps/78-EUC-H.bcmap +0 -0
- package/vendor/cmaps/78-EUC-V.bcmap +0 -0
- package/vendor/cmaps/78-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/78-V.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-V.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/vendor/cmaps/B5-H.bcmap +0 -0
- package/vendor/cmaps/B5-V.bcmap +0 -0
- package/vendor/cmaps/B5pc-H.bcmap +0 -0
- package/vendor/cmaps/B5pc-V.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-H.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-V.bcmap +0 -0
- package/vendor/cmaps/CNS1-H.bcmap +0 -0
- package/vendor/cmaps/CNS1-V.bcmap +0 -0
- package/vendor/cmaps/CNS2-H.bcmap +0 -0
- package/vendor/cmaps/CNS2-V.bcmap +3 -0
- package/vendor/cmaps/ETHK-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETHK-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETenms-B5-H.bcmap +3 -0
- package/vendor/cmaps/ETenms-B5-V.bcmap +0 -0
- package/vendor/cmaps/EUC-H.bcmap +0 -0
- package/vendor/cmaps/EUC-V.bcmap +0 -0
- package/vendor/cmaps/Ext-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Ext-V.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GB-H.bcmap +4 -0
- package/vendor/cmaps/GB-V.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBK2K-H.bcmap +0 -0
- package/vendor/cmaps/GBK2K-V.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-H.bcmap +0 -0
- package/vendor/cmaps/GBT-V.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-V.bcmap +0 -0
- package/vendor/cmaps/Hankaku.bcmap +0 -0
- package/vendor/cmaps/Hiragana.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-V.bcmap +0 -0
- package/vendor/cmaps/KSC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-V.bcmap +0 -0
- package/vendor/cmaps/KSC-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/Katakana.bcmap +0 -0
- package/vendor/cmaps/LICENSE.txt +36 -0
- package/vendor/cmaps/NWP-H.bcmap +0 -0
- package/vendor/cmaps/NWP-V.bcmap +0 -0
- package/vendor/cmaps/RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Roman.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/V.bcmap +0 -0
- package/vendor/cmaps/WP-Symbol.bcmap +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
- package/vendor/fonts/excalifont/OFL.txt +98 -0
- package/vendor/fonts/zhhand/OFL.txt +99 -0
- package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
- package/vendor/pdf.min.mjs +29 -0
- package/vendor/pdf.worker.min.mjs +29 -0
- package/vendor/pdfjs-LICENSE.txt +177 -0
- package/vendor/rough/LICENSE +21 -0
- package/vendor/rough/rough.mjs +1 -0
- package/vendor/wasm/LICENSE_JBIG2 +196 -0
- package/vendor/wasm/LICENSE_OPENJPEG +39 -0
- package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -0
- package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
- package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -0
- package/vendor/wasm/LICENSE_QCMS +21 -0
- package/vendor/wasm/jbig2.wasm +0 -0
- package/vendor/wasm/jbig2_nowasm_fallback.js +15 -0
- package/vendor/wasm/openjpeg.wasm +0 -0
- package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -0
- package/vendor/wasm/qcms_bg.wasm +0 -0
- package/vendor/wasm/quickjs-eval.wasm +0 -0
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +54 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Produced files: what a chapter's sessions wrote.
|
|
3
|
+
*
|
|
4
|
+
* The convention is `<artifactDir>/<book id>/<cNN>/` — one directory per
|
|
5
|
+
* chapter, named exactly like the chapter code the session titles use. Listing
|
|
6
|
+
* is deliberately narrow: a chapter that the catalog does not know is refused
|
|
7
|
+
* rather than looked up, so a typo cannot read an unrelated folder, and a file
|
|
8
|
+
* name is only ever joined under that chapter's own directory.
|
|
9
|
+
*
|
|
10
|
+
* The browser never touches the filesystem, so a previewed file arrives here in
|
|
11
|
+
* full, decoded, and bounded: 512 KiB and a hard line cap is enough for notes
|
|
12
|
+
* and extracts, and the response says when it stopped early.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-pdf-workbench/artifacts
|
|
15
|
+
*/
|
|
16
|
+
import { open, readdir, stat } from 'node:fs/promises'
|
|
17
|
+
import { basename, extname, join, resolve, sep } from 'node:path'
|
|
18
|
+
|
|
19
|
+
/** Bytes served for one text preview. */
|
|
20
|
+
export const MAX_TEXT_BYTES = 512 * 1024
|
|
21
|
+
|
|
22
|
+
/** Rows one chapter may list before it says the list is cut. */
|
|
23
|
+
const MAX_ROWS = 300
|
|
24
|
+
|
|
25
|
+
/** A chapter code, canonical form. */
|
|
26
|
+
const CODE = /^c\d{2,3}$/
|
|
27
|
+
|
|
28
|
+
/** One path segment: no separators, no traversal, no drive colon. */
|
|
29
|
+
const SEGMENT = /^[^\\/:*?"<>|]{1,200}$/
|
|
30
|
+
|
|
31
|
+
/** A plain file name as it appears in the chapter's own directory. */
|
|
32
|
+
const FILE_NAME = /^[^\\/:*?"<>|]{1,200}$/
|
|
33
|
+
|
|
34
|
+
/** Extensions rendered as text in the preview. Anything else is listed but not previewed. */
|
|
35
|
+
const TEXT_EXTENSIONS = new Set([
|
|
36
|
+
'md', 'markdown', 'mdx', 'txt', 'text', 'tex', 'latex', 'log', 'rst',
|
|
37
|
+
'json', 'jsonl', 'json5', 'csv', 'tsv', 'yml', 'yaml', 'toml', 'ini', 'conf', 'cfg',
|
|
38
|
+
'py', 'js', 'mjs', 'cjs', 'ts', 'tsx', 'jsx', 'c', 'h', 'cc', 'cpp', 'hpp', 'cs', 'java',
|
|
39
|
+
'rs', 'go', 'rb', 'php', 'sh', 'bash', 'ps1', 'bat', 'cmd',
|
|
40
|
+
'html', 'htm', 'css', 'scss', 'xml', 'svg', 'sql', 'r', 'jl', 'm', 'lua', 'pl', 'swift', 'kt',
|
|
41
|
+
])
|
|
42
|
+
|
|
43
|
+
/** Extensions shown as an image badge; the reader does not draw them yet. */
|
|
44
|
+
const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'avif'])
|
|
45
|
+
|
|
46
|
+
/** Decoders tried in order for a preview; UTF-8 first, then the common Chinese Windows code page. */
|
|
47
|
+
const DECODERS = ['utf-8', 'gb18030']
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The kind shown as a badge and used to decide whether the reader can draw it.
|
|
51
|
+
* @param {string} name - file name.
|
|
52
|
+
* @returns {'pdf' | 'text' | 'image' | 'other'} the kind.
|
|
53
|
+
*/
|
|
54
|
+
export function kindOf(name) {
|
|
55
|
+
const extension = extname(name).replace(/^\./, '').toLowerCase()
|
|
56
|
+
if (extension === 'pdf') return 'pdf'
|
|
57
|
+
if (TEXT_EXTENSIONS.has(extension)) return 'text'
|
|
58
|
+
if (IMAGE_EXTENSIONS.has(extension)) return 'image'
|
|
59
|
+
return 'other'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether a value can be used as one directory segment.
|
|
64
|
+
* @param {unknown} value - candidate.
|
|
65
|
+
* @returns {boolean} true when safe.
|
|
66
|
+
*/
|
|
67
|
+
function isSegment(value) {
|
|
68
|
+
return typeof value === 'string' && SEGMENT.test(value) && value !== '.' && value !== '..'
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a value is a plain file name (not a path).
|
|
73
|
+
* @param {unknown} value - candidate.
|
|
74
|
+
* @returns {boolean} true when safe.
|
|
75
|
+
*/
|
|
76
|
+
function isFileName(value) {
|
|
77
|
+
return typeof value === 'string' && FILE_NAME.test(value) && value !== '.' && value !== '..' && basename(value) === value
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether `target` really lives under `root`.
|
|
82
|
+
* @param {string} root - directory that must contain the target.
|
|
83
|
+
* @param {string} target - resolved candidate.
|
|
84
|
+
* @returns {boolean} true when contained.
|
|
85
|
+
*/
|
|
86
|
+
function isInside(root, target) {
|
|
87
|
+
const normalize = value => value.replace(/[\\/]+$/, '').replace(/\//g, '\\').toLowerCase()
|
|
88
|
+
const parent = normalize(root)
|
|
89
|
+
const child = normalize(target)
|
|
90
|
+
return child === parent || child.startsWith(`${parent}${sep}`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The directory a chapter's produced files live in, whether or not it exists.
|
|
95
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
96
|
+
* @param {string} bookId - catalog book id.
|
|
97
|
+
* @param {string} code - canonical chapter code.
|
|
98
|
+
* @returns {string} the absolute directory.
|
|
99
|
+
*/
|
|
100
|
+
export function chapterArtifactDir(config, bookId, code) {
|
|
101
|
+
return join(config.artifactDir, bookId, code)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Describe one file name for the browser.
|
|
106
|
+
* @param {string} name - file name inside the chapter directory.
|
|
107
|
+
* @returns {{ name: string, kind: 'pdf' | 'text' | 'image' | 'other' }} the descriptor.
|
|
108
|
+
*/
|
|
109
|
+
function descriptorOf(name) {
|
|
110
|
+
return { name, kind: kindOf(name) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Find the catalog row for one chapter, so the listing is scoped to a chapter
|
|
115
|
+
* the workbench actually shows.
|
|
116
|
+
* @param {{ books: object[] }} catalog - the loaded catalog.
|
|
117
|
+
* @param {string} bookId - requested book.
|
|
118
|
+
* @param {string} code - requested chapter.
|
|
119
|
+
* @returns {{ book: object, chapter: object } | null} the rows, or null when either is unknown.
|
|
120
|
+
*/
|
|
121
|
+
export function findChapter(catalog, bookId, code) {
|
|
122
|
+
const book = Array.isArray(catalog.books) ? catalog.books.find(item => item.id === bookId) : undefined
|
|
123
|
+
if (book === undefined || !Array.isArray(book.chapters)) return null
|
|
124
|
+
const chapter = book.chapters.find(item => item.code === code)
|
|
125
|
+
if (chapter === undefined) return null
|
|
126
|
+
return { book, chapter }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* List one chapter's produced files.
|
|
131
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
132
|
+
* @param {object} book - catalog book row.
|
|
133
|
+
* @param {object} chapter - catalog chapter row.
|
|
134
|
+
* @returns {Promise<{ dir: string, artifacts: object[], truncated: boolean, missing: boolean }>} the listing.
|
|
135
|
+
*/
|
|
136
|
+
export async function listArtifacts(config, book, chapter) {
|
|
137
|
+
if (!isSegment(book.id) || !CODE.test(chapter.code)) {
|
|
138
|
+
return { dir: '', artifacts: [], truncated: false, missing: true }
|
|
139
|
+
}
|
|
140
|
+
const dir = chapterArtifactDir(config, book.id, chapter.code)
|
|
141
|
+
const names = await readingNames(dir)
|
|
142
|
+
if (names === null) return { dir, artifacts: [], truncated: false, missing: true }
|
|
143
|
+
const truncated = names.length > MAX_ROWS
|
|
144
|
+
const rows = await Promise.all(names.slice(0, MAX_ROWS).map(async name => {
|
|
145
|
+
const descriptor = descriptorOf(name)
|
|
146
|
+
try {
|
|
147
|
+
const info = await stat(join(dir, name))
|
|
148
|
+
if (!info.isFile()) return null
|
|
149
|
+
return { ...descriptor, size: info.size, mtime: info.mtimeMs }
|
|
150
|
+
} catch {
|
|
151
|
+
// A file that vanished between readdir and stat is not worth a row.
|
|
152
|
+
return null
|
|
153
|
+
}
|
|
154
|
+
}))
|
|
155
|
+
return {
|
|
156
|
+
dir,
|
|
157
|
+
artifacts: rows.filter(row => row !== null),
|
|
158
|
+
truncated,
|
|
159
|
+
missing: false,
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Count the produced files of every chapter of one book without reading any
|
|
165
|
+
* metadata, so a collapsed row can still say how many it holds. One scan of the
|
|
166
|
+
* book's directories replaces a request per chapter.
|
|
167
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
168
|
+
* @param {object} book - catalog book row.
|
|
169
|
+
* @returns {Promise<Record<string, { n: number, truncated: boolean }>>} counts by chapter code.
|
|
170
|
+
*/
|
|
171
|
+
export async function countArtifacts(config, book) {
|
|
172
|
+
const counts = {}
|
|
173
|
+
// An authored index may carry an id that is not a plain directory name; a
|
|
174
|
+
// count is read-only, but it must not walk out of the artifact root either.
|
|
175
|
+
if (!isSegment(book.id)) return counts
|
|
176
|
+
const chapters = Array.isArray(book.chapters) ? book.chapters : []
|
|
177
|
+
await Promise.all(chapters.map(async chapter => {
|
|
178
|
+
if (!CODE.test(chapter.code)) return
|
|
179
|
+
const dir = chapterArtifactDir(config, book.id, chapter.code)
|
|
180
|
+
const names = await readingNames(dir)
|
|
181
|
+
if (names === null || names.length === 0) return
|
|
182
|
+
counts[chapter.code] = { n: Math.min(names.length, MAX_ROWS), truncated: names.length > MAX_ROWS }
|
|
183
|
+
}))
|
|
184
|
+
return counts
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The file names worth listing in one chapter directory.
|
|
189
|
+
* @param {string} dir - the chapter's directory.
|
|
190
|
+
* @returns {Promise<string[] | null>} sorted names, or null when the directory does not exist.
|
|
191
|
+
*/
|
|
192
|
+
async function readingNames(dir) {
|
|
193
|
+
let entries
|
|
194
|
+
try {
|
|
195
|
+
entries = await readdir(dir, { withFileTypes: true })
|
|
196
|
+
} catch (error) {
|
|
197
|
+
const code = error !== null && typeof error === 'object' ? error.code : undefined
|
|
198
|
+
// A chapter nobody has written into yet is a normal, empty answer.
|
|
199
|
+
if (code === 'ENOENT' || code === 'ENOTDIR') return null
|
|
200
|
+
throw error
|
|
201
|
+
}
|
|
202
|
+
return entries
|
|
203
|
+
.filter(entry => entry.isFile() || entry.isSymbolicLink())
|
|
204
|
+
.map(entry => entry.name)
|
|
205
|
+
// Office lock files and editor droppings are noise in a reading list.
|
|
206
|
+
.filter(name => !name.startsWith('.') && !name.startsWith('~$'))
|
|
207
|
+
.sort((left, right) => left.localeCompare(right, 'zh'))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Decode a byte range, preferring UTF-8 but accepting the common Chinese
|
|
212
|
+
* Windows code page when the bytes are not valid UTF-8.
|
|
213
|
+
* @param {Buffer} bytes - the file's leading bytes.
|
|
214
|
+
* @returns {{ text: string, encoding: string }} the decoded text.
|
|
215
|
+
*/
|
|
216
|
+
export function decodeText(bytes) {
|
|
217
|
+
let best = { text: '', encoding: DECODERS[0], loss: Number.POSITIVE_INFINITY }
|
|
218
|
+
for (const encoding of DECODERS) {
|
|
219
|
+
let text
|
|
220
|
+
try {
|
|
221
|
+
text = new TextDecoder(encoding).decode(bytes)
|
|
222
|
+
} catch {
|
|
223
|
+
continue
|
|
224
|
+
}
|
|
225
|
+
// Each replacement character is one byte the decoder could not map.
|
|
226
|
+
const loss = (text.match(/\uFFFD/g) ?? []).length
|
|
227
|
+
if (loss < best.loss) best = { text, encoding, loss }
|
|
228
|
+
if (loss === 0) break
|
|
229
|
+
}
|
|
230
|
+
return { text: best.text, encoding: best.encoding }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Read one produced file for the preview.
|
|
235
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
236
|
+
* @param {object} book - catalog book row.
|
|
237
|
+
* @param {object} chapter - catalog chapter row.
|
|
238
|
+
* @param {string} name - file name inside the chapter directory.
|
|
239
|
+
* @returns {Promise<{ ok: true, name: string, kind: string, size: number, mtime: number, text: string, encoding: string, truncated: boolean } | { ok: false, reason: 'bad-name' | 'not-found' | 'not-text' | 'binary' | 'directory' }>} the preview or why it cannot be drawn.
|
|
240
|
+
*/
|
|
241
|
+
export async function readArtifact(config, book, chapter, name) {
|
|
242
|
+
if (!isFileName(name)) return { ok: false, reason: 'bad-name' }
|
|
243
|
+
if (!isSegment(book.id) || !CODE.test(chapter.code)) return { ok: false, reason: 'bad-name' }
|
|
244
|
+
const dir = chapterArtifactDir(config, book.id, chapter.code)
|
|
245
|
+
const target = resolve(dir, name)
|
|
246
|
+
if (!isInside(config.artifactDir, target) || !isInside(dir, target)) return { ok: false, reason: 'bad-name' }
|
|
247
|
+
const kind = kindOf(name)
|
|
248
|
+
if (kind !== 'text') return { ok: false, reason: 'not-text' }
|
|
249
|
+
|
|
250
|
+
let handle
|
|
251
|
+
let info
|
|
252
|
+
try {
|
|
253
|
+
info = await stat(target)
|
|
254
|
+
if (!info.isFile()) return { ok: false, reason: 'directory' }
|
|
255
|
+
handle = await open(target, 'r')
|
|
256
|
+
} catch {
|
|
257
|
+
return { ok: false, reason: 'not-found' }
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const length = Math.min(info.size, MAX_TEXT_BYTES)
|
|
261
|
+
const buffer = Buffer.alloc(length)
|
|
262
|
+
const { bytesRead } = await handle.read(buffer, 0, length, 0)
|
|
263
|
+
let bytes = buffer.subarray(0, bytesRead)
|
|
264
|
+
const truncated = info.size > bytesRead
|
|
265
|
+
// NUL bytes mean binary; check the raw read so a trimmed tail cannot hide one.
|
|
266
|
+
if (bytes.includes(0)) return { ok: false, reason: 'binary' }
|
|
267
|
+
// A cut inside a multi-byte character leaves an invalid tail; dropping those
|
|
268
|
+
// bytes keeps the encoding probe from mistaking the file for GB18030.
|
|
269
|
+
if (truncated) bytes = trimPartialTail(bytes)
|
|
270
|
+
const { text, encoding } = decodeText(bytes)
|
|
271
|
+
return {
|
|
272
|
+
ok: true,
|
|
273
|
+
name,
|
|
274
|
+
kind,
|
|
275
|
+
size: info.size,
|
|
276
|
+
mtime: info.mtimeMs,
|
|
277
|
+
text,
|
|
278
|
+
encoding,
|
|
279
|
+
truncated,
|
|
280
|
+
}
|
|
281
|
+
} finally {
|
|
282
|
+
await handle.close().catch(() => {})
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Drop an incomplete trailing UTF-8 sequence (at most three bytes).
|
|
288
|
+
* @param {Buffer} bytes - the truncated read.
|
|
289
|
+
* @returns {Buffer} bytes that decode as UTF-8 without a replacement character.
|
|
290
|
+
*/
|
|
291
|
+
function trimPartialTail(bytes) {
|
|
292
|
+
for (let drop = 0; drop <= 3 && drop < bytes.length; drop += 1) {
|
|
293
|
+
const candidate = bytes.subarray(0, bytes.length - drop)
|
|
294
|
+
try {
|
|
295
|
+
new TextDecoder('utf-8', { fatal: true }).decode(candidate)
|
|
296
|
+
return candidate
|
|
297
|
+
} catch {
|
|
298
|
+
// Try cutting one more byte.
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return bytes
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Validate the pair the browser asked about and return the catalog rows.
|
|
306
|
+
* @param {{ books: object[] }} catalog - the loaded catalog.
|
|
307
|
+
* @param {unknown} bookId - requested book.
|
|
308
|
+
* @param {unknown} code - requested chapter.
|
|
309
|
+
* @returns {{ ok: true, book: object, chapter: object } | { ok: false, code: string, message: string }} the match or the refusal.
|
|
310
|
+
*/
|
|
311
|
+
export function resolveChapter(catalog, bookId, code) {
|
|
312
|
+
if (!isSegment(bookId)) return { ok: false, code: 'invalid-book', message: '书 id 不合法' }
|
|
313
|
+
if (typeof code !== 'string' || !CODE.test(code)) return { ok: false, code: 'invalid-code', message: '章号不合法' }
|
|
314
|
+
const match = findChapter(catalog, bookId, code)
|
|
315
|
+
if (match === null) return { ok: false, code: 'unknown-chapter', message: `书库里没有 ${bookId} ${code}` }
|
|
316
|
+
return { ok: true, ...match }
|
|
317
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read one PDF page's layout blocks with PyMuPDF.
|
|
3
|
+
*
|
|
4
|
+
* pdf.js gives text items only; PyMuPDF already groups them into paragraph
|
|
5
|
+
* blocks and reports span sizes, which is what the paragraph/heading/formula
|
|
6
|
+
* boxes need.
|
|
7
|
+
* @module dsh-pdf-workbench/blocks
|
|
8
|
+
*/
|
|
9
|
+
import { execFile } from 'node:child_process'
|
|
10
|
+
|
|
11
|
+
const PYTHONS = [process.env.MATH_DESK_PYTHON, 'python', 'py'].filter(Boolean)
|
|
12
|
+
|
|
13
|
+
function runPython(script, args) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const tryNext = index => {
|
|
16
|
+
if (index >= PYTHONS.length) {
|
|
17
|
+
reject(new Error('没有可用的 Python'))
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
execFile(
|
|
21
|
+
PYTHONS[index],
|
|
22
|
+
[script, ...args],
|
|
23
|
+
{ timeout: 60000, windowsHide: true, maxBuffer: 8 * 1024 * 1024 },
|
|
24
|
+
(error, stdout) => {
|
|
25
|
+
if (error === null) resolve(stdout)
|
|
26
|
+
else if (index + 1 < PYTHONS.length) tryNext(index + 1)
|
|
27
|
+
else reject(new Error(`${PYTHONS[index]}: ${error.message}`))
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
tryNext(0)
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {{ script: string, source: string, pageIndex: number }} options - block reader script and page.
|
|
37
|
+
* @returns {Promise<object[]>} blocks with normalized bbox, kind and text.
|
|
38
|
+
*/
|
|
39
|
+
export async function readPageBlocks(options) {
|
|
40
|
+
const output = await runPython(options.script, [options.source, String(options.pageIndex)])
|
|
41
|
+
const parsed = JSON.parse(output)
|
|
42
|
+
if (!Array.isArray(parsed)) throw new Error('块解析返回格式不对')
|
|
43
|
+
return parsed
|
|
44
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolved plugin paths.
|
|
3
|
+
*
|
|
4
|
+
* Everything the plugin reads or writes is named here once, so the routes and
|
|
5
|
+
* the state store never assemble paths themselves. `libraryDir` comes from the
|
|
6
|
+
* bundle patch (or the user's patch layer); the rest derives from it.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-pdf-workbench/config
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync } from 'node:fs'
|
|
11
|
+
import { homedir } from 'node:os'
|
|
12
|
+
import { dirname, join, resolve } from 'node:path'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
14
|
+
|
|
15
|
+
/** This module's folder, so the shipped vendor files resolve in a link install. */
|
|
16
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
17
|
+
|
|
18
|
+
/** The package version, read once for the update check. */
|
|
19
|
+
export const PLUGIN_VERSION = (() => {
|
|
20
|
+
try {
|
|
21
|
+
return String(JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version ?? '0.0.0')
|
|
22
|
+
} catch {
|
|
23
|
+
return '0.0.0'
|
|
24
|
+
}
|
|
25
|
+
})()
|
|
26
|
+
|
|
27
|
+
/** Default split-library root when the bundle patch names none (a fresh install). */
|
|
28
|
+
export const DEFAULT_LIBRARY_DIR = join(homedir(), '数学书库')
|
|
29
|
+
|
|
30
|
+
/** Folder under the library root that holds the chapter-split PDFs. */
|
|
31
|
+
const FRAGMENT_FOLDER = '分片'
|
|
32
|
+
|
|
33
|
+
/** Folder under the library root that holds the per-chapter produced files. */
|
|
34
|
+
const ARTIFACT_FOLDER = '产物'
|
|
35
|
+
|
|
36
|
+
/** Folder under the library root where the user drops book cover images. */
|
|
37
|
+
const COVER_FOLDER = '封面'
|
|
38
|
+
|
|
39
|
+
/** Index file name under the library root. */
|
|
40
|
+
const INDEX_FILE = '索引.json'
|
|
41
|
+
|
|
42
|
+
/** Plugin data folder name under `$DSH_HOME`. */
|
|
43
|
+
const DATA_FOLDER = 'pdfdesk'
|
|
44
|
+
|
|
45
|
+
/** Share of a session's context window at which "New" starts a continuation. */
|
|
46
|
+
export const DEFAULT_RESUME_RATIO = 0.85
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Expand a leading `~` to the user's home directory.
|
|
50
|
+
* @param {string} value - user-supplied path.
|
|
51
|
+
* @returns {string} the expanded path.
|
|
52
|
+
*/
|
|
53
|
+
function expandHome(value) {
|
|
54
|
+
if (value === '~') return homedir()
|
|
55
|
+
if (value.startsWith('~/') || value.startsWith('~\\')) return join(homedir(), value.slice(2))
|
|
56
|
+
return value
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Read one optional string config field.
|
|
61
|
+
* @param {Record<string, unknown>} source - raw config mapping.
|
|
62
|
+
* @param {string} key - field name.
|
|
63
|
+
* @returns {string | undefined} the trimmed value, or undefined when absent.
|
|
64
|
+
*/
|
|
65
|
+
function textField(source, key) {
|
|
66
|
+
const value = source[key]
|
|
67
|
+
if (typeof value !== 'string') return undefined
|
|
68
|
+
const trimmed = value.trim()
|
|
69
|
+
return trimmed === '' ? undefined : trimmed
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resolve every path the plugin uses.
|
|
74
|
+
* @param {unknown} rawConfig - the Loader row's `config` value.
|
|
75
|
+
* @returns {{ libraryDir: string, fragmentDir: string, artifactDir: string, coverDir: string, indexPath: string, dataDir: string, vendorDir: string, toolsDir: string, resumeRatio: number, statePath: string, pendingPath: string, linksPath: string, pressurePath: string, librariesPath: string }} resolved paths.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveConfig(rawConfig) {
|
|
78
|
+
const source = rawConfig !== null && typeof rawConfig === 'object' ? rawConfig : {}
|
|
79
|
+
const libraryDir = resolve(expandHome(textField(source, 'libraryDir') ?? DEFAULT_LIBRARY_DIR))
|
|
80
|
+
const fragmentDir = resolve(expandHome(textField(source, 'fragmentDir') ?? join(libraryDir, FRAGMENT_FOLDER)))
|
|
81
|
+
const artifactDir = resolve(expandHome(textField(source, 'artifactDir') ?? join(libraryDir, ARTIFACT_FOLDER)))
|
|
82
|
+
const coverDir = resolve(expandHome(textField(source, 'coverDir') ?? join(libraryDir, COVER_FOLDER)))
|
|
83
|
+
const indexPath = resolve(expandHome(textField(source, 'indexPath') ?? join(libraryDir, INDEX_FILE)))
|
|
84
|
+
const dshHome = textField(source, 'dshHome') ?? process.env.DSH_HOME ?? join(homedir(), '.dsh')
|
|
85
|
+
const dataDir = resolve(expandHome(textField(source, 'dataDir') ?? join(dshHome, DATA_FOLDER)))
|
|
86
|
+
// The reader's pdf.js build ships inside this package, next to `src/`.
|
|
87
|
+
const vendorDir = resolve(expandHome(textField(source, 'vendorDir') ?? join(HERE, '..', '..', 'vendor')))
|
|
88
|
+
const toolsDir = resolve(expandHome(textField(source, 'toolsDir') ?? join(HERE, '..', '..', 'tools')))
|
|
89
|
+
const samplesDir = resolve(expandHome(textField(source, 'samplesDir') ?? join(HERE, '..', '..', 'samples')))
|
|
90
|
+
const ratio = Number(source.resumeRatio)
|
|
91
|
+
const resumeRatio = Number.isFinite(ratio) && ratio > 0 && ratio < 1 ? ratio : DEFAULT_RESUME_RATIO
|
|
92
|
+
return {
|
|
93
|
+
libraryDir,
|
|
94
|
+
fragmentDir,
|
|
95
|
+
artifactDir,
|
|
96
|
+
coverDir,
|
|
97
|
+
indexPath,
|
|
98
|
+
dataDir,
|
|
99
|
+
vendorDir,
|
|
100
|
+
toolsDir,
|
|
101
|
+
samplesDir,
|
|
102
|
+
resumeRatio,
|
|
103
|
+
updateProxy: textField(source, 'updateProxy'),
|
|
104
|
+
deepseekApiKey: textField(source, 'deepseekApiKey') ?? process.env.DEEPSEEK_API_KEY,
|
|
105
|
+
statePath: join(dataDir, 'state.json'),
|
|
106
|
+
pendingPath: join(dataDir, 'pending.json'),
|
|
107
|
+
linksPath: join(dataDir, 'links.json'),
|
|
108
|
+
pressurePath: join(dataDir, 'pressure.json'),
|
|
109
|
+
shelvesPath: join(dataDir, 'shelves.json'),
|
|
110
|
+
librariesPath: join(dataDir, 'libraries.json'),
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chapter digest: summarize a chapter's conversations into one outline.
|
|
3
|
+
*
|
|
4
|
+
* The workbench creates one conversation per chapter and mode and continues it
|
|
5
|
+
* until the context fills. Before that point, a digest of everything discussed
|
|
6
|
+
* under the chapter is worth carrying forward: it costs one model call over the
|
|
7
|
+
* chapter's transcripts, and saves far more than it spends in a continuation.
|
|
8
|
+
*
|
|
9
|
+
* The Host owns this because only it can read session logs and reach the model.
|
|
10
|
+
* The browser sends the session ids it shows under the chapter; the Host folds
|
|
11
|
+
* their text, asks the default model for an outline, and saves the result next to
|
|
12
|
+
* the chapter's produced files so it is readable and reusable.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-pdf-workbench/digest
|
|
15
|
+
*/
|
|
16
|
+
import { mkdir, writeFile } from 'node:fs/promises'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
|
|
19
|
+
/** Characters of transcript sent to the model; longer chapters are cut. */
|
|
20
|
+
export const MAX_TRANSCRIPT_CHARS = 120000
|
|
21
|
+
|
|
22
|
+
/** The outline the model is asked for. */
|
|
23
|
+
const SYSTEM = [
|
|
24
|
+
'你是一位数学学习助教,负责把一段学习对话压缩成可继续使用的提纲。',
|
|
25
|
+
'只输出 Markdown,不要客套话。结构固定为:',
|
|
26
|
+
'## 本章范围(读了哪几页 / 哪些主题)',
|
|
27
|
+
'## 已解决的问题(要点式,保留关键结论与公式)',
|
|
28
|
+
'## 仍未解决的问题(明确列出)',
|
|
29
|
+
'## 下一步建议(简短)',
|
|
30
|
+
'忠实于对话,不要编造对话里没有的结论;公式用 $...$ 或 $$...$$。',
|
|
31
|
+
].join('\n')
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Pull readable text out of one stored session log.
|
|
35
|
+
* @param {{ events?: object[] }} log - `sessionQuery.readSession` result.
|
|
36
|
+
* @returns {string} the transcript, oldest first.
|
|
37
|
+
*/
|
|
38
|
+
export function transcriptOf(log) {
|
|
39
|
+
if (log === null || typeof log !== 'object' || !Array.isArray(log.events)) return ''
|
|
40
|
+
const lines = []
|
|
41
|
+
for (const event of log.events) {
|
|
42
|
+
if (event === null || typeof event !== 'object') continue
|
|
43
|
+
const role = event.type === 'user/message' ? '用户' : event.type === 'assistant/message' ? '助手' : ''
|
|
44
|
+
if (role === '') continue
|
|
45
|
+
const content = event.message !== null && typeof event.message === 'object' && Array.isArray(event.message.content)
|
|
46
|
+
? event.message.content
|
|
47
|
+
: Array.isArray(event.content) ? event.content : []
|
|
48
|
+
const text = content
|
|
49
|
+
.filter(block => block !== null && typeof block === 'object' && block.type === 'text' && typeof block.text === 'string')
|
|
50
|
+
.map(block => block.text.trim())
|
|
51
|
+
.filter(text => text !== '')
|
|
52
|
+
.join('\n')
|
|
53
|
+
if (text === '') continue
|
|
54
|
+
lines.push(`${role}:${text}`)
|
|
55
|
+
}
|
|
56
|
+
return lines.join('\n\n')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Fold several sessions into one bounded transcript.
|
|
61
|
+
* @param {string[]} sessionIds - sessions to read.
|
|
62
|
+
* @param {(id: string) => Promise<{ events?: object[] }>} readSession - host reader.
|
|
63
|
+
* @param {number} [maxChars] - character budget.
|
|
64
|
+
* @returns {Promise<{ text: string, used: string[], dropped: number }>} the transcript and what fit.
|
|
65
|
+
*/
|
|
66
|
+
export async function collectTranscript(sessionIds, readSession, maxChars = MAX_TRANSCRIPT_CHARS) {
|
|
67
|
+
const parts = []
|
|
68
|
+
const used = []
|
|
69
|
+
let total = 0
|
|
70
|
+
let dropped = 0
|
|
71
|
+
for (const id of sessionIds) {
|
|
72
|
+
let log
|
|
73
|
+
try {
|
|
74
|
+
log = await readSession(id)
|
|
75
|
+
} catch {
|
|
76
|
+
dropped += 1
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
const text = transcriptOf(log)
|
|
80
|
+
if (text === '') {
|
|
81
|
+
dropped += 1
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
if (total + text.length > maxChars) {
|
|
85
|
+
// Keep a head slice of the overflow rather than dropping it whole: early
|
|
86
|
+
// messages usually define the chapter's question.
|
|
87
|
+
const room = Math.max(0, maxChars - total)
|
|
88
|
+
if (room > 2000) {
|
|
89
|
+
parts.push(text.slice(0, room))
|
|
90
|
+
used.push(id)
|
|
91
|
+
}
|
|
92
|
+
dropped += 1
|
|
93
|
+
break
|
|
94
|
+
}
|
|
95
|
+
parts.push(text)
|
|
96
|
+
used.push(id)
|
|
97
|
+
total += text.length
|
|
98
|
+
}
|
|
99
|
+
return { text: parts.join('\n\n---\n\n'), used, dropped }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Ask the model for the outline.
|
|
104
|
+
* @param {{ provider: string, model: string }} selection - default model.
|
|
105
|
+
* @param {(request: object) => AsyncIterable<object>} stream - `llm.stream`.
|
|
106
|
+
* @param {{ bookTitle: string, code: string, chapterTitle: string, mode?: string, transcript: string }} input - what is being summarized.
|
|
107
|
+
* @returns {Promise<string>} the outline text.
|
|
108
|
+
*/
|
|
109
|
+
export async function summarize(selection, stream, input) {
|
|
110
|
+
const scope = input.mode === undefined || input.mode === ''
|
|
111
|
+
? '本章全部对话'
|
|
112
|
+
: `本章「${input.mode}」这一组对话`
|
|
113
|
+
const prompt = [
|
|
114
|
+
`书:${input.bookTitle}`,
|
|
115
|
+
`章节:${input.code} ${input.chapterTitle}`,
|
|
116
|
+
`范围:${scope}`,
|
|
117
|
+
'',
|
|
118
|
+
'以下是这些对话的原文(可能被截断):',
|
|
119
|
+
'',
|
|
120
|
+
input.transcript,
|
|
121
|
+
].join('\n')
|
|
122
|
+
let text = ''
|
|
123
|
+
const chunks = stream({
|
|
124
|
+
provider: selection.provider,
|
|
125
|
+
model: selection.model,
|
|
126
|
+
system: SYSTEM,
|
|
127
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
|
|
128
|
+
maxTokens: 2400,
|
|
129
|
+
})
|
|
130
|
+
for await (const chunk of chunks) {
|
|
131
|
+
if (chunk === null || typeof chunk !== 'object') continue
|
|
132
|
+
if (chunk.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text
|
|
133
|
+
else if (chunk.type === 'finish' && chunk.reason !== undefined && chunk.reason.kind === 'error') {
|
|
134
|
+
const failure = chunk.reason.failure
|
|
135
|
+
throw new Error(failure !== undefined && typeof failure.message === 'string' ? failure.message : '模型调用失败')
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return text.trim()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Save the outline beside the chapter's produced files.
|
|
143
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
144
|
+
* @param {string} bookId - catalog book id.
|
|
145
|
+
* @param {string} code - chapter code.
|
|
146
|
+
* @param {string} mode - `all` or a mode key, used in the file name.
|
|
147
|
+
* @param {string} text - outline Markdown.
|
|
148
|
+
* @returns {Promise<string>} the absolute file path.
|
|
149
|
+
*/
|
|
150
|
+
export async function saveDigest(config, bookId, code, mode, text) {
|
|
151
|
+
const dir = join(config.artifactDir, bookId, code)
|
|
152
|
+
await mkdir(dir, { recursive: true })
|
|
153
|
+
const path = join(dir, `提纲-${mode}.md`)
|
|
154
|
+
await writeFile(path, `${text}\n`, 'utf8')
|
|
155
|
+
return path
|
|
156
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect the optional typesetting environment once per bootstrap.
|
|
3
|
+
*
|
|
4
|
+
* The plugin never installs TeX or Python: Markdown-to-PDF is an optional
|
|
5
|
+
* feature, so the browser hides its buttons until both are present.
|
|
6
|
+
* @module dsh-pdf-workbench/environment
|
|
7
|
+
*/
|
|
8
|
+
import { accessSync, constants } from 'node:fs'
|
|
9
|
+
import { delimiter, join } from 'node:path'
|
|
10
|
+
|
|
11
|
+
/** Where TinyTeX/TeX Live commonly land when not on PATH. */
|
|
12
|
+
const TEX_DIRS = [
|
|
13
|
+
'C:\\texlive\\2025\\bin\\windows',
|
|
14
|
+
'C:\\texlive\\2024\\bin\\windows',
|
|
15
|
+
'C:\\Program Files\\MiKTeX\\miktex\\bin\\x64',
|
|
16
|
+
join(process.env.LOCALAPPDATA ?? '', 'Programs', 'MiKTeX', 'miktex', 'bin', 'x64'),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
function onPath(names, extraDirs = []) {
|
|
20
|
+
const dirs = [...(process.env.PATH ?? '').split(delimiter), ...extraDirs].filter(Boolean)
|
|
21
|
+
for (const dir of dirs) {
|
|
22
|
+
for (const name of names) {
|
|
23
|
+
try {
|
|
24
|
+
accessSync(join(dir, name), constants.X_OK)
|
|
25
|
+
return true
|
|
26
|
+
} catch {
|
|
27
|
+
// try the next candidate
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @returns {{ python: boolean, tex: boolean }} which optional tools exist.
|
|
36
|
+
*/
|
|
37
|
+
export function detectEnvironment() {
|
|
38
|
+
return {
|
|
39
|
+
python: onPath(['python.exe', 'python', 'py.exe', 'py']),
|
|
40
|
+
tex: onPath(['xelatex.exe', 'xelatex'], TEX_DIRS),
|
|
41
|
+
}
|
|
42
|
+
}
|