pdfxtract 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +32 -3
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -6,14 +6,43 @@ import { parseArgs } from "node:util";
6
6
  import { extractText, getDocumentProxy } from "unpdf";
7
7
 
8
8
  //#region src/converter.ts
9
+ const VERBOSITY_ERRORS = 0;
10
+ const LINE_BREAK_HYPHEN = /([A-Za-z]{2,})-\n([A-Za-z]{2,})/g;
11
+ const WORD_SEPARATOR = /[^A-Za-z'-]+/;
12
+ /**
13
+ * Words the document spells out without crossing a line break. Fragments left
14
+ * by hyphenation are dropped rather than flattened, so they cannot vouch for
15
+ * themselves when `dehyphenate` consults this set.
16
+ */
17
+ function buildVocabulary(text) {
18
+ return new Set(text.replace(LINE_BREAK_HYPHEN, "\n").split(WORD_SEPARATOR).filter(Boolean).map((word) => word.toLowerCase()));
19
+ }
20
+ /**
21
+ * PDF.js reports every hyphen as U+002D, so one sitting before a line break is
22
+ * ambiguous: it may be typesetting hyphenation ("prob-\nlem") or part of a real
23
+ * compound ("machine-\nlearning"). Resolve each occurrence against the rest of
24
+ * the document, preferring whichever spelling is attested elsewhere. When
25
+ * neither is, keep the hyphen only if both halves stand alone as words.
26
+ */
27
+ function dehyphenate(text) {
28
+ const vocabulary = buildVocabulary(text);
29
+ return text.replace(LINE_BREAK_HYPHEN, (_match, head, tail) => {
30
+ const joined = `${head}${tail}`;
31
+ const hyphenated = `${head}-${tail}`;
32
+ if (vocabulary.has(joined.toLowerCase())) return joined;
33
+ if (vocabulary.has(hyphenated.toLowerCase())) return hyphenated;
34
+ const halvesAreWords = vocabulary.has(head.toLowerCase()) && vocabulary.has(tail.toLowerCase());
35
+ return halvesAreWords ? hyphenated : joined;
36
+ });
37
+ }
9
38
  async function convertPdf(pdfPath, txtPath) {
10
39
  const buffer = await readFile(pdfPath);
11
- const pdf = await getDocumentProxy(new Uint8Array(buffer));
40
+ const pdf = await getDocumentProxy(new Uint8Array(buffer), { verbosity: VERBOSITY_ERRORS });
12
41
  try {
13
42
  const { text } = await extractText(pdf, { mergePages: true });
14
- await writeFile(txtPath, text, "utf-8");
43
+ await writeFile(txtPath, dehyphenate(text), "utf-8");
15
44
  } finally {
16
- pdf.destroy();
45
+ await pdf.loadingTask.destroy();
17
46
  }
18
47
  }
19
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfxtract",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Recursively convert PDF files to TXT in a directory",
5
5
  "type": "module",
6
6
  "bin": {
@@ -38,7 +38,7 @@
38
38
  "url": "https://github.com/NaokiYazawa/pdfxtract/issues"
39
39
  },
40
40
  "dependencies": {
41
- "unpdf": "^0.12.0"
41
+ "unpdf": "^1.8.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@biomejs/biome": "2.4.6",