dsh-pdf-workbench 1.4.1 → 1.5.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/README.md +108 -78
- package/assets/screenshot-hero.jpg +0 -0
- package/assets/screenshot-settings-data.jpg +0 -0
- package/assets/screenshot-settings-env.jpg +0 -0
- package/assets/screenshot-settings-mode.jpg +0 -0
- package/assets/screenshot-settings-reading.jpg +0 -0
- package/assets/screenshot-shelf.jpg +0 -0
- package/client.js +4106 -469
- package/docs/LAYOUT.md +56 -0
- package/docs/RELEASE.md +8 -0
- package/docs/TESTING.md +16 -0
- package/docs/ci.md +18 -13
- package/docs/copy/templates.md +4 -4
- package/docs/decisions.md +119 -0
- package/docs/features.md +14 -8
- package/docs/requirements/2026-11-25-v1.5/346/224/271/350/277/233/351/234/200/346/261/202.md +622 -0
- package/docs/roles/{release-manager.md → ci.md} +37 -47
- package/docs/roles/programmer.md +45 -59
- package/docs/roles/requirements.md +68 -0
- package/docs/todo.md +590 -32
- package/index.js +337 -1
- package/package.json +1 -1
- package/screenshots.json +6 -4
- package/src/host/backup.js +172 -33
- package/src/host/book-wizard.js +454 -0
- package/src/host/digest.js +4 -0
- package/src/host/engine.js +88 -0
- package/src/host/exports.js +159 -0
- package/src/host/http.js +28 -1
- package/src/host/imports.js +650 -0
- package/src/host/index-source.js +117 -11
- package/src/host/library-export.js +162 -0
- package/src/host/pdf-source.js +6 -2
- package/src/host/routes.js +1020 -120
- package/src/host/shortcut.js +238 -0
- package/src/host/storage.js +421 -0
- package/src/host/translate-blocks.js +49 -45
- package/src/host/translate-job.js +37 -10
- package/src/host/translate.js +19 -45
- package/src/host/translation-cache.js +209 -0
- package/src/host/typst-install.js +132 -0
- package/src/host/zip.js +486 -4
- package/tools/latex_to_typst.py +1353 -0
- package/tools/md2overlay.py +147 -152
- package/tools/md2overlay_typst.py +312 -0
- package/tools/md2typst.py +818 -0
- package/tools/overlay_layout.py +332 -0
- package/tools/pdf_blocks.py +82 -10
- package/tools/realign_overlay.py +4 -1
- package/tools/rebuild_overlay.py +3 -1
- package/tools/split_pdf.py +158 -0
- package/tools/typst_runtime.py +98 -0
- package/vendor/katex/LICENSE +21 -0
- package/vendor/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/vendor/katex/katex.min.css +1 -0
- package/vendor/katex/katex.min.js +1 -0
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +9 -1
- package/docs/boss-handover.md +0 -84
- package/docs/roles/auditor.md +0 -123
- package/docs/roles/boss.md +0 -162
- package/docs/roles/code-reviewer.md +0 -132
- package/docs/roles/feature-inventory.md +0 -119
- package/docs/roles/feedback-collector.md +0 -105
- package/docs/roles/tester.md +0 -140
- package/docs/roles/ui-copywriter.md +0 -122
- package/src/host/book-import.js +0 -98
package/src/host/index-source.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*
|
|
10
10
|
* @module dsh-pdf-workbench/index-source
|
|
11
11
|
*/
|
|
12
|
-
import { readdir, readFile } from 'node:fs/promises'
|
|
12
|
+
import { readdir, readFile, stat } from 'node:fs/promises'
|
|
13
13
|
import { basename, join } from 'node:path'
|
|
14
14
|
|
|
15
15
|
/** A chapter code is `c` plus two or three digits; the title part is optional. */
|
|
@@ -187,6 +187,9 @@ function normalizeChapters(chapters) {
|
|
|
187
187
|
kind: typeof chapter.kind === 'string' && chapter.kind !== '' ? chapter.kind : kindOf(chapter.code),
|
|
188
188
|
pages: typeof chapter.pages === 'string' ? chapter.pages : '',
|
|
189
189
|
file: typeof chapter.file === 'string' ? chapter.file : '',
|
|
190
|
+
// A file kept only because every PDF is readable, not because its name
|
|
191
|
+
// follows the splitter convention. The browser hides these by default.
|
|
192
|
+
loose: chapter.loose === true,
|
|
190
193
|
}
|
|
191
194
|
if (byCode.has(row.code)) duplicates += 1
|
|
192
195
|
byCode.set(row.code, row)
|
|
@@ -210,6 +213,9 @@ function bookRow(book) {
|
|
|
210
213
|
cover: typeof book.cover === 'string' ? book.cover : '',
|
|
211
214
|
unparsed: typeof book.unparsed === 'number' ? book.unparsed : 0,
|
|
212
215
|
duplicates,
|
|
216
|
+
// A book made entirely of files whose names the splitter did not produce:
|
|
217
|
+
// readable, but hidden until the reader asks for unrecognized books.
|
|
218
|
+
loose: chapters.length > 0 && chapters.every(chapter => chapter.loose === true),
|
|
213
219
|
chapters,
|
|
214
220
|
}
|
|
215
221
|
}
|
|
@@ -333,6 +339,7 @@ async function scanFragments(fragmentDir) {
|
|
|
333
339
|
continue
|
|
334
340
|
}
|
|
335
341
|
const chapters = []
|
|
342
|
+
const keptFiles = []
|
|
336
343
|
let short = ''
|
|
337
344
|
let pdfCount = 0
|
|
338
345
|
let unparsed = 0
|
|
@@ -344,6 +351,8 @@ async function scanFragments(fragmentDir) {
|
|
|
344
351
|
const parsed = parseChapterFile(file)
|
|
345
352
|
if (parsed === null) {
|
|
346
353
|
unparsed += 1
|
|
354
|
+
// Not chapter-shaped, but still a PDF the reader may open.
|
|
355
|
+
keptFiles.push(file)
|
|
347
356
|
continue
|
|
348
357
|
}
|
|
349
358
|
if (short === '') short = parsed.short
|
|
@@ -356,7 +365,7 @@ async function scanFragments(fragmentDir) {
|
|
|
356
365
|
from: parsed.from,
|
|
357
366
|
})
|
|
358
367
|
}
|
|
359
|
-
if (chapters.length === 0) {
|
|
368
|
+
if (chapters.length === 0 && keptFiles.length === 0) {
|
|
360
369
|
if (pdfCount > 0) skipped.push({ dir: entry.name, pdfCount })
|
|
361
370
|
continue
|
|
362
371
|
}
|
|
@@ -384,6 +393,21 @@ async function scanFragments(fragmentDir) {
|
|
|
384
393
|
chapter.kind = kindOf(code)
|
|
385
394
|
taken.add(code)
|
|
386
395
|
}
|
|
396
|
+
// Every PDF is readable even when its name never followed the splitter
|
|
397
|
+
// convention: number the rest after the real chapters and mark them loose,
|
|
398
|
+
// so the browser keeps them out of the shelf until the reader asks for
|
|
399
|
+
// unrecognized books. Nothing is written back to disk or to 索引.json.
|
|
400
|
+
// Code-unit order, not locale order: "第一章" must sort before "第二章", and
|
|
401
|
+
// a pinyin collation would put them the other way round.
|
|
402
|
+
keptFiles.sort((left, right) => (left < right ? -1 : left > right ? 1 : 0))
|
|
403
|
+
for (const name of keptFiles) {
|
|
404
|
+
const stem = name.replace(/\.pdf$/i, '')
|
|
405
|
+
let index = 1
|
|
406
|
+
while (taken.has(`c${String(index).padStart(2, '0')}`)) index += 1
|
|
407
|
+
const code = `c${String(index).padStart(2, '0')}`
|
|
408
|
+
taken.add(code)
|
|
409
|
+
chapters.push({ code, title: stem, kind: 'chapter', pages: '', file: stem, loose: true })
|
|
410
|
+
}
|
|
387
411
|
for (const chapter of chapters) delete chapter.from
|
|
388
412
|
chapters.sort((left, right) => left.code.localeCompare(right.code))
|
|
389
413
|
const { title, author } = splitBookTitle(entry.name)
|
|
@@ -401,17 +425,24 @@ async function scanFragments(fragmentDir) {
|
|
|
401
425
|
}
|
|
402
426
|
|
|
403
427
|
/**
|
|
404
|
-
* Whole-book PDFs: a library may also carry unsplit books under `整本\`,
|
|
405
|
-
*
|
|
406
|
-
* synthetic chapter (`c00`), so reading works but
|
|
428
|
+
* Whole-book PDFs: a library may also carry unsplit books, under `整本\`, loose
|
|
429
|
+
* in the library root, or dropped straight into `分片\` (people do that). Each
|
|
430
|
+
* file is one book with a single synthetic chapter (`c00`), so reading works but
|
|
431
|
+
* there are no chapter rows.
|
|
407
432
|
* @param {string} libraryDir - the library root.
|
|
433
|
+
* @param {string} [fragmentDir] - the `分片` directory, when it is configured elsewhere.
|
|
408
434
|
* @returns {Promise<{ books: object[], readable: boolean }>} one book per whole PDF.
|
|
409
435
|
*/
|
|
410
|
-
async function scanWholeBooks(libraryDir) {
|
|
436
|
+
async function scanWholeBooks(libraryDir, fragmentDir) {
|
|
411
437
|
const found = []
|
|
412
438
|
let readable = false
|
|
413
|
-
// `整本\` first so it wins when the same stem exists
|
|
414
|
-
|
|
439
|
+
// `整本\` first so it wins when the same stem exists elsewhere too, then the
|
|
440
|
+
// library root, then loose PDFs directly inside `分片\`.
|
|
441
|
+
for (const [dir, at] of [
|
|
442
|
+
[join(libraryDir, '整本'), 'whole'],
|
|
443
|
+
[libraryDir, 'root'],
|
|
444
|
+
[typeof fragmentDir === 'string' && fragmentDir !== '' ? fragmentDir : join(libraryDir, '分片'), 'fragment'],
|
|
445
|
+
]) {
|
|
415
446
|
let names
|
|
416
447
|
try {
|
|
417
448
|
names = await readdir(dir)
|
|
@@ -436,6 +467,9 @@ async function scanWholeBooks(libraryDir) {
|
|
|
436
467
|
title: stem,
|
|
437
468
|
author: '',
|
|
438
469
|
whole: true,
|
|
470
|
+
// A whole, unsplit PDF never followed the chapter naming either, so it
|
|
471
|
+
// counts as an unrecognized book and follows the same shelf switch.
|
|
472
|
+
loose: true,
|
|
439
473
|
chapters: [{
|
|
440
474
|
code: 'c00',
|
|
441
475
|
title: '整本',
|
|
@@ -455,10 +489,74 @@ async function scanWholeBooks(libraryDir) {
|
|
|
455
489
|
* @param {{ indexPath: string, fragmentDir: string, libraryDir?: string }} config - resolved paths.
|
|
456
490
|
* @returns {Promise<{ source: 'index.json' | 'scan', indexIgnored: boolean, books: object[], skipped: object[], warnings: string[], problem: { code: string, message: string } | null }>} the catalog, how it was obtained, and any blocking problem.
|
|
457
491
|
*/
|
|
492
|
+
/**
|
|
493
|
+
* Whether a book listed in `索引.json` still has files on disk.
|
|
494
|
+
*
|
|
495
|
+
* The index is a cache: deleting a book moves its folder or PDF into `回收站/`
|
|
496
|
+
* but no code rewrites the index, and the user can also delete files in
|
|
497
|
+
* Explorer. Trusting an entry without checking leaves a ghost book that the
|
|
498
|
+
* shelf lists and the split picker offers, but nothing can open.
|
|
499
|
+
* @param {object} config - resolved paths.
|
|
500
|
+
* @param {object} book - one index entry.
|
|
501
|
+
* @returns {Promise<boolean>} true when at least one layout is still present.
|
|
502
|
+
*/
|
|
503
|
+
async function bookStillPresent(config, book) {
|
|
504
|
+
if (book === null || typeof book !== 'object') return false
|
|
505
|
+
const libraryDir = config.libraryDir
|
|
506
|
+
if (typeof libraryDir !== 'string' || libraryDir === '') return true
|
|
507
|
+
const chapters = Array.isArray(book.chapters) ? book.chapters : []
|
|
508
|
+
const whole = chapters.find(chapter => chapter !== null && typeof chapter === 'object' && chapter.whole === true)
|
|
509
|
+
if (book.whole === true || whole !== undefined) {
|
|
510
|
+
const file = String((whole ?? {}).file ?? book.id ?? '').trim()
|
|
511
|
+
if (file === '') return false
|
|
512
|
+
for (const dir of [join(libraryDir, '整本'), libraryDir, join(libraryDir, '分片')]) {
|
|
513
|
+
try {
|
|
514
|
+
const info = await stat(join(dir, `${file}.pdf`))
|
|
515
|
+
if (info.isFile()) return true
|
|
516
|
+
} catch {
|
|
517
|
+
// try the next layout
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
// A whole entry may also be a folder of chapters that were split later.
|
|
521
|
+
}
|
|
522
|
+
const fragmentRoot = typeof config.fragmentDir === 'string' && config.fragmentDir !== ''
|
|
523
|
+
? config.fragmentDir
|
|
524
|
+
: join(libraryDir, '分片')
|
|
525
|
+
const candidates = [book.dir, book.id]
|
|
526
|
+
.filter(value => typeof value === 'string' && value !== '' && !/[\\/]/.test(value))
|
|
527
|
+
for (const name of candidates) {
|
|
528
|
+
try {
|
|
529
|
+
const info = await stat(join(fragmentRoot, name))
|
|
530
|
+
if (info.isDirectory()) return true
|
|
531
|
+
} catch {
|
|
532
|
+
// try the next candidate
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return false
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Drop the index entries whose files are gone.
|
|
540
|
+
* @param {object} config - resolved paths.
|
|
541
|
+
* @param {object[]} books - index entries.
|
|
542
|
+
* @returns {Promise<{ books: object[], missing: number }>} what survived.
|
|
543
|
+
*/
|
|
544
|
+
async function dropMissingBooks(config, books) {
|
|
545
|
+
const kept = []
|
|
546
|
+
let missing = 0
|
|
547
|
+
for (const book of Array.isArray(books) ? books : []) {
|
|
548
|
+
if (await bookStillPresent(config, book)) kept.push(book)
|
|
549
|
+
else missing += 1
|
|
550
|
+
}
|
|
551
|
+
return { books: kept, missing }
|
|
552
|
+
}
|
|
553
|
+
|
|
458
554
|
export async function loadCatalog(config) {
|
|
459
555
|
const fromIndex = await readIndexFile(config.indexPath)
|
|
460
556
|
const scanned = await scanFragments(config.fragmentDir)
|
|
461
|
-
const whole = config.libraryDir === undefined
|
|
557
|
+
const whole = config.libraryDir === undefined
|
|
558
|
+
? { books: [], readable: false }
|
|
559
|
+
: await scanWholeBooks(config.libraryDir, config.fragmentDir)
|
|
462
560
|
// A library may legitimately hold only whole-book PDFs: `分片` missing is not
|
|
463
561
|
// an error then. It is only a problem when neither layout is readable.
|
|
464
562
|
const readable = scanned.readable || whole.readable
|
|
@@ -468,12 +566,20 @@ export async function loadCatalog(config) {
|
|
|
468
566
|
...(scanned.missing === true && whole.readable ? ['没有分片目录,只显示整本 PDF。'] : []),
|
|
469
567
|
]
|
|
470
568
|
if (fromIndex.status === 'ok') {
|
|
569
|
+
// The index may name books that are no longer on disk (trashed or deleted in
|
|
570
|
+
// Explorer). Verifying costs one stat per book and stops ghost entries from
|
|
571
|
+
// reaching the shelf and the split picker.
|
|
572
|
+
const verified = await dropMissingBooks(config, fromIndex.books)
|
|
471
573
|
return {
|
|
472
574
|
source: 'index.json',
|
|
473
575
|
indexIgnored: false,
|
|
474
|
-
books: assignUniqueIds([...
|
|
576
|
+
books: assignUniqueIds([...verified.books, ...whole.books]),
|
|
475
577
|
skipped: scanned.skipped,
|
|
476
|
-
warnings: [
|
|
578
|
+
warnings: [
|
|
579
|
+
...fromIndex.warnings,
|
|
580
|
+
...(verified.missing > 0 ? [`索引.json 里有 ${verified.missing} 本已经不存在的书,已忽略;重新扫描书库可清理。`] : []),
|
|
581
|
+
...warnings,
|
|
582
|
+
],
|
|
477
583
|
problem: null,
|
|
478
584
|
}
|
|
479
585
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export one library's data as a ZIP that never contains a book PDF.
|
|
3
|
+
*
|
|
4
|
+
* A migration package answers "move my workspace data"; this answers "move my
|
|
5
|
+
* library's own data". The PDFs stay where they are: they are the user's files
|
|
6
|
+
* under `分片/` and `整本/`, and the export is the counterpart the user is
|
|
7
|
+
* expected to pair with their own compression of those two folders (DEC-0014
|
|
8
|
+
* §3, copyright). The translation cache is rebuildable and stays out as well.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-pdf-workbench/library-export
|
|
11
|
+
*/
|
|
12
|
+
import { cp, mkdir, readdir, rm, stat } from 'node:fs/promises'
|
|
13
|
+
import { join } from 'node:path'
|
|
14
|
+
|
|
15
|
+
import { packDirectory } from './backup.js'
|
|
16
|
+
import { PLUGIN_VERSION } from './config.js'
|
|
17
|
+
|
|
18
|
+
/** Manifest `format` value; deliberately not the migration-backup number. */
|
|
19
|
+
export const LIBRARY_EXPORT_FORMAT = 'pdfdesk-library-export'
|
|
20
|
+
|
|
21
|
+
/** Format of the PDF-only package (source PDFs, no plugin data). */
|
|
22
|
+
export const PDF_PACK_FORMAT = 'pdfdesk-pdf-pack'
|
|
23
|
+
|
|
24
|
+
/** What the manifest tells anyone opening the archive. */
|
|
25
|
+
const EXPORT_NOTE = '书库数据包:不含书籍 PDF(版权)。PDF 在 分片/ 与 整本/,请自行压缩携带。'
|
|
26
|
+
|
|
27
|
+
/** A file-name-safe library label. */
|
|
28
|
+
function libraryLabel(library) {
|
|
29
|
+
const raw = String(library.name ?? '').trim() === '' ? String(library.id ?? '') : String(library.name)
|
|
30
|
+
const safe = raw.replace(/[\\/:*?"<>|]/g, '_').trim().slice(0, 40)
|
|
31
|
+
return safe === '' ? 'library' : safe
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One UTC timestamp readable in a file name. */
|
|
35
|
+
function stamp() {
|
|
36
|
+
return new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').slice(0, 15)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Copy a path when it exists.
|
|
41
|
+
* @param {string} source - file or folder.
|
|
42
|
+
* @param {string} target - destination.
|
|
43
|
+
* @returns {Promise<boolean>} whether anything was copied.
|
|
44
|
+
*/
|
|
45
|
+
async function copyIfExists(source, target) {
|
|
46
|
+
const info = await stat(source).catch(() => null)
|
|
47
|
+
if (info === null) return false
|
|
48
|
+
await mkdir(join(target, '..'), { recursive: true })
|
|
49
|
+
await cp(source, target, { recursive: info.isDirectory() })
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Copy a folder's children except the named ones.
|
|
55
|
+
* @param {string} source - folder.
|
|
56
|
+
* @param {string} target - destination folder.
|
|
57
|
+
* @param {Set<string>} skip - child names to leave out.
|
|
58
|
+
* @returns {Promise<boolean>} whether any child was copied.
|
|
59
|
+
*/
|
|
60
|
+
async function copyChildrenExcept(source, target, skip) {
|
|
61
|
+
const info = await stat(source).catch(() => null)
|
|
62
|
+
if (info === null || !info.isDirectory()) return false
|
|
63
|
+
let copied = false
|
|
64
|
+
for (const name of await readdir(source)) {
|
|
65
|
+
if (skip.has(name)) continue
|
|
66
|
+
await mkdir(target, { recursive: true })
|
|
67
|
+
await cp(join(source, name), join(target, name), { recursive: true })
|
|
68
|
+
copied = true
|
|
69
|
+
}
|
|
70
|
+
return copied
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Pack one library's data (index + `.pdfdesk/` without translation cache +
|
|
75
|
+
* optional `产物/`) into `%DSH_HOME%/pdfdesk/backup/library-export-*.zip`.
|
|
76
|
+
* @param {{ library: { id?: string, name?: string, libraryDir: string }, dataDir: string, includeArtifacts?: boolean }} options - library, plugin data folder, and whether produced PDFs travel.
|
|
77
|
+
* @returns {Promise<{ name: string, bytes: number, files: number, included: object }>} the archive summary.
|
|
78
|
+
*/
|
|
79
|
+
export async function exportLibrary(options) {
|
|
80
|
+
const library = options.library
|
|
81
|
+
const staging = join(options.dataDir, 'tmp', `library-export-${process.pid}-${Date.now()}`)
|
|
82
|
+
await rm(staging, { recursive: true, force: true })
|
|
83
|
+
await mkdir(staging, { recursive: true })
|
|
84
|
+
const included = { index: false, libraryData: false, artifacts: false, pdf: false }
|
|
85
|
+
try {
|
|
86
|
+
included.index = await copyIfExists(join(library.libraryDir, '索引.json'), join(staging, '索引.json'))
|
|
87
|
+
included.libraryData = await copyChildrenExcept(
|
|
88
|
+
join(library.libraryDir, '.pdfdesk'),
|
|
89
|
+
join(staging, '.pdfdesk'),
|
|
90
|
+
new Set(['translation']),
|
|
91
|
+
)
|
|
92
|
+
if (options.includeArtifacts === true) {
|
|
93
|
+
included.artifacts = await copyIfExists(join(library.libraryDir, '产物'), join(staging, '产物'))
|
|
94
|
+
}
|
|
95
|
+
const stampValue = stamp()
|
|
96
|
+
const name = `library-export-${libraryLabel(library)}-${stampValue}.zip`
|
|
97
|
+
const outPath = join(options.dataDir, 'backup', name)
|
|
98
|
+
const packed = await packDirectory({
|
|
99
|
+
root: staging,
|
|
100
|
+
outPath,
|
|
101
|
+
compress: true,
|
|
102
|
+
format: LIBRARY_EXPORT_FORMAT,
|
|
103
|
+
meta: {
|
|
104
|
+
kind: 'library-export',
|
|
105
|
+
note: EXPORT_NOTE,
|
|
106
|
+
plugin: { name: 'dsh-pdf-workbench', version: PLUGIN_VERSION },
|
|
107
|
+
library: {
|
|
108
|
+
id: String(library.id ?? ''),
|
|
109
|
+
name: String(library.name ?? ''),
|
|
110
|
+
path: String(library.libraryDir ?? ''),
|
|
111
|
+
},
|
|
112
|
+
included,
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
return { name, bytes: packed.bytes, files: packed.files, included }
|
|
116
|
+
} finally {
|
|
117
|
+
await rm(staging, { recursive: true, force: true })
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Pack the library's PDF zone as its own package: source PDFs only, no plugin
|
|
123
|
+
* data. `scope` picks `分片/`, `整本/` or both.
|
|
124
|
+
* @param {{ library: { id?: string, name?: string, libraryDir: string }, dataDir: string, scope?: 'fragments' | 'whole' | 'both' }} options - library, data folder and what to pack.
|
|
125
|
+
* @returns {Promise<{ name: string, bytes: number, files: number, included: object }>} the archive summary.
|
|
126
|
+
*/
|
|
127
|
+
export async function exportPdfPack(options) {
|
|
128
|
+
const scope = options.scope === 'fragments' || options.scope === 'whole' ? options.scope : 'both'
|
|
129
|
+
const staging = join(options.dataDir, 'tmp', `pdf-pack-${process.pid}-${Date.now()}`)
|
|
130
|
+
await rm(staging, { recursive: true, force: true })
|
|
131
|
+
await mkdir(staging, { recursive: true })
|
|
132
|
+
const included = { pdf: true, fragments: false, whole: false }
|
|
133
|
+
try {
|
|
134
|
+
if (scope !== 'whole') {
|
|
135
|
+
included.fragments = await copyIfExists(join(options.library.libraryDir, '分片'), join(staging, '分片'))
|
|
136
|
+
}
|
|
137
|
+
if (scope !== 'fragments') {
|
|
138
|
+
included.whole = await copyIfExists(join(options.library.libraryDir, '整本'), join(staging, '整本'))
|
|
139
|
+
}
|
|
140
|
+
const name = `pdf-pack-${libraryLabel(options.library)}-${stamp()}.zip`
|
|
141
|
+
const packed = await packDirectory({
|
|
142
|
+
root: staging,
|
|
143
|
+
outPath: join(options.dataDir, 'backup', name),
|
|
144
|
+
compress: true,
|
|
145
|
+
format: PDF_PACK_FORMAT,
|
|
146
|
+
meta: {
|
|
147
|
+
kind: 'pdf-pack',
|
|
148
|
+
note: '书籍 PDF 包:只含用户自己的 PDF(分片/整本),不含标注、疑问、笔记等插件数据。',
|
|
149
|
+
plugin: { name: 'dsh-pdf-workbench', version: PLUGIN_VERSION },
|
|
150
|
+
library: {
|
|
151
|
+
id: String(options.library.id ?? ''),
|
|
152
|
+
name: String(options.library.name ?? ''),
|
|
153
|
+
path: String(options.library.libraryDir ?? ''),
|
|
154
|
+
},
|
|
155
|
+
included,
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
return { name, bytes: packed.bytes, files: packed.files, included }
|
|
159
|
+
} finally {
|
|
160
|
+
await rm(staging, { recursive: true, force: true })
|
|
161
|
+
}
|
|
162
|
+
}
|
package/src/host/pdf-source.js
CHANGED
|
@@ -56,8 +56,12 @@ export function chapterPdfPath(config, book, chapter) {
|
|
|
56
56
|
if (typeof book.id !== 'string' || !isSegment(book.id) || !isSegment(chapter.file)) {
|
|
57
57
|
return { ok: false, reason: 'bad-name' }
|
|
58
58
|
}
|
|
59
|
-
const wholeAt = chapter.wholeAt === 'root'
|
|
60
|
-
|
|
59
|
+
const wholeAt = chapter.wholeAt === 'root'
|
|
60
|
+
? config.libraryDir
|
|
61
|
+
: chapter.wholeAt === 'fragment'
|
|
62
|
+
? config.fragmentDir
|
|
63
|
+
: join(config.libraryDir, '整本')
|
|
64
|
+
const base = wholeAt
|
|
61
65
|
const target = resolve(base, `${chapter.file}.pdf`)
|
|
62
66
|
if (!isInside(base, target)) return { ok: false, reason: 'bad-name' }
|
|
63
67
|
return { ok: true, path: target }
|