Skip to content

A fast, helpful, and open-source document parser

fix(node): preserve text page boundaries

tgolob74e142b3898d
diff --git a/packages/node/src/cli-text.ts b/packages/node/src/cli-text.ts
new file mode 100644
index 0000000..8241b28
--- /dev/null
+++ b/packages/node/src/cli-text.ts
@@ -0,0 +1,10 @@
+import type { ParsedPage } from "./lib.js";
+
+/** Format parsed pages as plain text with page headers. */
+export function formatText(
+ pages: readonly Pick<ParsedPage, "pageNum" | "text">[],
+): string {
+ return pages
+ .map((page) => `\n--- Page ${page.pageNum} ---\n${page.text}`)
+ .join("\n\n");
+}
diff --git a/packages/node/src/cli.ts b/packages/node/src/cli.ts
index 55bc59e..88d9007 100644
--- a/packages/node/src/cli.ts
+++ b/packages/node/src/cli.ts
@@ -3,6 +3,7 @@
import { program } from "commander";
import { LiteParse, type LiteParseConfig } from "./lib.js";
import { parseResultToCliJson } from "./cli-json.js";
+import { formatText } from "./cli-text.js";
import { readFileSync, writeFileSync, mkdirSync, readdirSync, statSync } from "node:fs";
import { join, relative, parse as parsePath } from "node:path";
@@ -187,7 +188,9 @@ program
null,
2,
)
- : result.text;
+ : config.outputFormat === "text"
+ ? formatText(result.pages)
+ : result.text;
if (opts.output) {
@@ -461,7 +464,9 @@ program
null,
2,
)
- : result.text;
+ : format === "text"
+ ? formatText(result.pages)
+ : result.text;
writeFileSync(outPath, output, "utf-8");
success++;
if (!opts.quiet) {
diff --git a/packages/node/tests/cli-text.test.mjs b/packages/node/tests/cli-text.test.mjs
new file mode 100644
index 0000000..fa5dd2e
--- /dev/null
+++ b/packages/node/tests/cli-text.test.mjs
@@ -0,0 +1,77 @@
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import {
+ copyFileSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const repoRoot = resolve(packageDir, "..", "..");
+const cliPath = join(packageDir, "dist", "cli.js");
+const samplePdf = join(repoRoot, "demo", "docs", "apple-10k-2024.pdf");
+
+function pageHeaders(output) {
+ return output.match(/^--- Page \d+ ---$/gm) ?? [];
+}
+
+test("parse preserves page boundaries in text output", () => {
+ const output = execFileSync(
+ process.execPath,
+ [
+ cliPath,
+ "parse",
+ samplePdf,
+ "--no-ocr",
+ "--max-pages",
+ "2",
+ "--format",
+ "text",
+ "--quiet",
+ ],
+ { encoding: "utf8" },
+ );
+
+ assert.deepEqual(pageHeaders(output), ["--- Page 1 ---", "--- Page 2 ---"]);
+});
+
+test("batch-parse preserves page boundaries in text output", () => {
+ const testDir = mkdtempSync(join(tmpdir(), "liteparse-cli-text-"));
+ const inputDir = join(testDir, "input");
+ const outputDir = join(testDir, "output");
+ mkdirSync(inputDir);
+ mkdirSync(outputDir);
+ copyFileSync(samplePdf, join(inputDir, "sample.pdf"));
+
+ try {
+ execFileSync(
+ process.execPath,
+ [
+ cliPath,
+ "batch-parse",
+ inputDir,
+ outputDir,
+ "--no-ocr",
+ "--max-pages",
+ "2",
+ "--format",
+ "text",
+ "--quiet",
+ ],
+ { encoding: "utf8" },
+ );
+ const output = readFileSync(join(outputDir, "sample.txt"), "utf8");
+ assert.deepEqual(pageHeaders(output), [
+ "--- Page 1 ---",
+ "--- Page 2 ---",
+ ]);
+ } finally {
+ rmSync(testDir, { recursive: true, force: true });
+ }
+});