@intentic/fileq 1.431.0 → 1.433.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.
- package/NOTICE +58 -1
- package/README.md +22 -1
- package/bin/fileq-rg-pre +6 -2
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +4 -1
- package/dist/app.js.map +1 -1
- package/dist/commands/check.command.d.ts.map +1 -1
- package/dist/commands/check.command.js +12 -5
- package/dist/commands/check.command.js.map +1 -1
- package/dist/commands/derive.command.d.ts.map +1 -1
- package/dist/commands/derive.command.js +3 -2
- package/dist/commands/derive.command.js.map +1 -1
- package/dist/commands/read.command.d.ts.map +1 -1
- package/dist/commands/read.command.js +42 -7
- package/dist/commands/read.command.js.map +1 -1
- package/dist/commands/render.command.d.ts.map +1 -1
- package/dist/commands/render.command.js +23 -10
- package/dist/commands/render.command.js.map +1 -1
- package/dist/lib/derivers/docx-math-symbols.d.ts +14 -0
- package/dist/lib/derivers/docx-math-symbols.d.ts.map +1 -0
- package/dist/lib/derivers/docx-math-symbols.js +160 -0
- package/dist/lib/derivers/docx-math-symbols.js.map +1 -0
- package/dist/lib/derivers/docx-math.d.ts +15 -0
- package/dist/lib/derivers/docx-math.d.ts.map +1 -0
- package/dist/lib/derivers/docx-math.js +252 -0
- package/dist/lib/derivers/docx-math.js.map +1 -0
- package/dist/lib/derivers/docx.d.ts.map +1 -1
- package/dist/lib/derivers/docx.js +3 -2
- package/dist/lib/derivers/docx.js.map +1 -1
- package/dist/lib/derivers/pdf-furniture.d.ts.map +1 -1
- package/dist/lib/derivers/pdf-furniture.js +3 -0
- package/dist/lib/derivers/pdf-furniture.js.map +1 -1
- package/dist/lib/derivers/pdf-tables.d.ts +9 -0
- package/dist/lib/derivers/pdf-tables.d.ts.map +1 -0
- package/dist/lib/derivers/pdf-tables.js +319 -0
- package/dist/lib/derivers/pdf-tables.js.map +1 -0
- package/dist/lib/derivers/pdf.d.ts.map +1 -1
- package/dist/lib/derivers/pdf.js +3 -6
- package/dist/lib/derivers/pdf.js.map +1 -1
- package/dist/skill.d.ts.map +1 -1
- package/dist/skill.js +7 -2
- package/dist/skill.js.map +1 -1
- package/package.json +6 -6
- package/src/app.ts +4 -1
- package/src/cli.integration.test.ts +30 -8
- package/src/commands/check.command.ts +20 -5
- package/src/commands/derive.command.ts +3 -2
- package/src/commands/read.command.ts +56 -8
- package/src/commands/render.command.ts +33 -11
- package/src/derivers.integration.test.ts +104 -0
- package/src/lib/derivers/docx-math-symbols.ts +197 -0
- package/src/lib/derivers/docx-math.test.ts +131 -0
- package/src/lib/derivers/docx-math.ts +355 -0
- package/src/lib/derivers/docx.ts +5 -3
- package/src/lib/derivers/pdf-furniture.test.ts +9 -0
- package/src/lib/derivers/pdf-furniture.ts +5 -0
- package/src/lib/derivers/pdf-tables.test.ts +292 -0
- package/src/lib/derivers/pdf-tables.ts +465 -0
- package/src/lib/derivers/pdf.ts +5 -9
- package/src/render.integration.test.ts +5 -4
- package/src/skill.ts +7 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* `fileq derive` derives metadata from one input file. */
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
|
+
import { failure } from "@intentic/agent-cli/errors";
|
|
3
4
|
import { countParser } from "@intentic/agent-cli/flags";
|
|
4
5
|
import { buildCommand, type CommandContext } from "@stricli/core";
|
|
5
6
|
import { ensureSidecar, type Outcome } from "../lib/derive.js";
|
|
@@ -40,12 +41,12 @@ export const deriveCommand = buildCommand({
|
|
|
40
41
|
async func(this: CommandContext, flags: DeriveFlags, ...files: string[]) {
|
|
41
42
|
const root = workspaceRoot();
|
|
42
43
|
if (root === undefined) {
|
|
43
|
-
this.process.stdout.write("fileq
|
|
44
|
+
this.process.stdout.write(failure("fileq", "usage", "derive needs a workspace (WORKSPACE_ROOT is not set)", "fileq read <file> reads a one-off file"));
|
|
44
45
|
process.exitCode = 2;
|
|
45
46
|
return;
|
|
46
47
|
}
|
|
47
48
|
if (files.length === 0) {
|
|
48
|
-
this.process.stdout.write("fileq
|
|
49
|
+
this.process.stdout.write(failure("fileq", "usage", "derive takes file paths", "fileq derive <file…>"));
|
|
49
50
|
process.exitCode = 2;
|
|
50
51
|
return;
|
|
51
52
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/* `fileq read <file>` (also the default command): one file as markdown — a capsule line saying what happened, the content up to a token budget; `--plain` is the body alone, whole, for a program (git's textconv) rather than an agent. */
|
|
2
|
-
import {
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { basename, dirname, resolve } from "node:path";
|
|
5
|
+
import { type ErrorCode, failure, shellWord } from "@intentic/agent-cli/errors";
|
|
3
6
|
import { countParser } from "@intentic/agent-cli/flags";
|
|
4
|
-
import { capsule, clip } from "@intentic/agent-cli/output";
|
|
7
|
+
import { capsule, clip, type Whole } from "@intentic/agent-cli/output";
|
|
5
8
|
import { estimateTokens } from "@intentic/base/format";
|
|
6
9
|
import { buildCommand, type CommandContext } from "@stricli/core";
|
|
7
10
|
import { deriverStamp } from "../lib/derivers/deriver.js";
|
|
@@ -52,9 +55,9 @@ export const readCommand = buildCommand({
|
|
|
52
55
|
this.process.stdout.write(`saved: ${result.savedPath}\n`);
|
|
53
56
|
}
|
|
54
57
|
if (flags.budget > 0 && result.body !== "") {
|
|
55
|
-
const
|
|
58
|
+
const { body, whole } = await printable(result, absPath);
|
|
56
59
|
this.process.stdout.write("---\n");
|
|
57
|
-
this.process.stdout.write(withFinalNewline(clip(
|
|
60
|
+
this.process.stdout.write(withFinalNewline(clip(body, flags.budget, whole)));
|
|
58
61
|
}
|
|
59
62
|
},
|
|
60
63
|
});
|
|
@@ -79,10 +82,10 @@ const readInWorkspace = async (root: string, absPath: string): Promise<ReadResul
|
|
|
79
82
|
if (outcome.kind === "skipped" && (outcome.reason === "outside-workspace" || outcome.reason === "ignored-path")) {
|
|
80
83
|
return readOutsideWorkspace(root, absPath);
|
|
81
84
|
}
|
|
82
|
-
return fromOutcome(outcome);
|
|
85
|
+
return fromOutcome(outcome, absPath);
|
|
83
86
|
};
|
|
84
87
|
|
|
85
|
-
const fromOutcome = (outcome: Outcome): ReadResult | undefined => {
|
|
88
|
+
const fromOutcome = async (outcome: Outcome, absPath: string): Promise<ReadResult | undefined> => {
|
|
86
89
|
switch (outcome.kind) {
|
|
87
90
|
case "derived":
|
|
88
91
|
return {
|
|
@@ -108,18 +111,48 @@ const fromOutcome = (outcome: Outcome): ReadResult | undefined => {
|
|
|
108
111
|
case "removed":
|
|
109
112
|
case "skipped": {
|
|
110
113
|
const reason = outcome.kind === "removed" ? "missing" : outcome.reason;
|
|
111
|
-
process.stdout.write(
|
|
114
|
+
process.stdout.write(await refusal(outcome.relPath, absPath, reason));
|
|
112
115
|
return undefined;
|
|
113
116
|
}
|
|
114
117
|
}
|
|
115
118
|
};
|
|
116
119
|
|
|
120
|
+
// Why a read failed, as a code and the step that gets further. The reasons are derive.ts's: `missing`, `unsupported`,
|
|
121
|
+
// `too-large (N MB)`, `unreadable (EACCES)`, `derive-failed (docx): …`. The daemon reads the reason back off the
|
|
122
|
+
// error line's `cannot read <path>: ` (_sandbox/sandbox/src/derived/derived-blob.ts), so that wording stays.
|
|
123
|
+
const refusal = async (shown: string, absPath: string, reason: string): Promise<string> => {
|
|
124
|
+
const [code, next] = await refusalKind(absPath, reason);
|
|
125
|
+
return failure("fileq", code, `cannot read ${shown}: ${reason}`, next);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const refusalKind = async (absPath: string, reason: string): Promise<readonly [ErrorCode, string | undefined]> => {
|
|
129
|
+
if (reason === "missing") {
|
|
130
|
+
return ["not_found", `ls ${shellWord(dirname(absPath))}`];
|
|
131
|
+
}
|
|
132
|
+
if (reason === "unsupported") {
|
|
133
|
+
return ["unsupported", "fileq --help lists the formats it reads; read a text file directly"];
|
|
134
|
+
}
|
|
135
|
+
if (reason.startsWith("too-large")) {
|
|
136
|
+
return ["too_large", undefined];
|
|
137
|
+
}
|
|
138
|
+
if (reason.startsWith("unreadable")) {
|
|
139
|
+
return ["unreadable", `ls -l ${shellWord(absPath)}`];
|
|
140
|
+
}
|
|
141
|
+
// A document that will not open: a check names what is wrong with it (damaged, password-protected), and is loaded
|
|
142
|
+
// only on this path.
|
|
143
|
+
const failed = /^derive-failed \((\w+)\)/.exec(reason)?.[1];
|
|
144
|
+
const { CHECKABLE } = await import("../lib/check/check.js");
|
|
145
|
+
return ["unreadable", CHECKABLE.some((format) => format === failed) ? `fileq check ${shellWord(absPath)}` : undefined];
|
|
146
|
+
};
|
|
147
|
+
|
|
117
148
|
// A file outside the workspace has no sidecar: the content cache alone keeps its rendering, and its entry is what `saved:`
|
|
118
149
|
// names, so git's textconv reading the same blob twice derives it once.
|
|
119
150
|
const readOutsideWorkspace = async (root: string | undefined, absPath: string): Promise<ReadResult | undefined> => {
|
|
120
151
|
const outcome = await renderByContent(root, absPath);
|
|
121
152
|
if (outcome.kind === "failed") {
|
|
122
|
-
|
|
153
|
+
// The content cache cannot tell a missing file from one in no format it reads; the file system can.
|
|
154
|
+
const reason = outcome.reason === "unsupported or missing" ? (existsSync(absPath) ? "unsupported" : "missing") : outcome.reason;
|
|
155
|
+
process.stdout.write(await refusal(absPath, absPath, reason));
|
|
123
156
|
return undefined;
|
|
124
157
|
}
|
|
125
158
|
return {
|
|
@@ -134,5 +167,20 @@ const readOutsideWorkspace = async (root: string | undefined, absPath: string):
|
|
|
134
167
|
};
|
|
135
168
|
};
|
|
136
169
|
|
|
170
|
+
// The body to print and where its continuation is read: the saved rendering's own text below its front matter, so the
|
|
171
|
+
// line a cut names is the line that file shows there; `--plain`'s output when nothing was saved or it cannot be read back.
|
|
172
|
+
const printable = async (result: ReadResult, absPath: string): Promise<{ body: string; whole: Whole }> => {
|
|
173
|
+
// allow(silent-catch): a rendering that cannot be read back is still printed, from memory, with `--plain` as its rest.
|
|
174
|
+
const saved = result.savedPath === undefined ? undefined : await readFile(result.savedPath, "utf8").catch(() => undefined);
|
|
175
|
+
if (result.savedPath === undefined || saved === undefined) {
|
|
176
|
+
return { body: result.body, whole: { command: `fileq read --plain ${shellWord(absPath)}` } };
|
|
177
|
+
}
|
|
178
|
+
const fence = saved.startsWith("---\n") ? saved.indexOf("\n---\n", 4) : -1;
|
|
179
|
+
const start = fence === -1 ? 0 : fence + "\n---\n".length;
|
|
180
|
+
// The front matter's lines, plus one: the line the body starts on.
|
|
181
|
+
const firstLine = saved.slice(0, start).split("\n").length;
|
|
182
|
+
return { body: saved.slice(start), whole: { file: result.savedPath, firstLine } };
|
|
183
|
+
};
|
|
184
|
+
|
|
137
185
|
// A document's last line is content, and a sidecar body need not end in a newline; a clipped one already does.
|
|
138
186
|
const withFinalNewline = (text: string): string => (text.endsWith("\n") ? text : `${text}\n`);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* `fileq render <file>`: a document's pages (a deck's slides) as PNG files, one path per line, for an agent to look at with its image reader. */
|
|
2
2
|
import { basename, resolve } from "node:path";
|
|
3
|
+
import { type ErrorCode, failure } from "@intentic/agent-cli/errors";
|
|
3
4
|
import { countParser } from "@intentic/agent-cli/flags";
|
|
4
5
|
import { capsule } from "@intentic/agent-cli/output";
|
|
5
|
-
import { errorMessage } from "@intentic/base/errors";
|
|
6
|
+
import { errnoCode, errorMessage } from "@intentic/base/errors";
|
|
6
7
|
import { buildCommand, type CommandContext } from "@stricli/core";
|
|
7
8
|
import { workspaceRoot } from "../lib/env.js";
|
|
8
9
|
import { ConvertFailed, ToolMissing } from "../lib/render/convert.js";
|
|
@@ -16,16 +17,37 @@ interface RenderFlags {
|
|
|
16
17
|
readonly json: boolean;
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
interface Failure {
|
|
21
|
+
readonly exit: number;
|
|
22
|
+
readonly code: ErrorCode;
|
|
23
|
+
readonly message: string;
|
|
24
|
+
readonly next?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
19
27
|
// What went wrong, as the exit code the agent-CLI contract gives it: a missing tool or a bad flag is the invocation's
|
|
20
|
-
// fault (2); a document that cannot be drawn is "nothing here" (1).
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
// fault (2); a document that cannot be drawn is "nothing here" (1). Anything else is fileq's own failure (2).
|
|
29
|
+
const failureOf = (cause: unknown): Failure => {
|
|
30
|
+
const message = errorMessage(cause);
|
|
31
|
+
if (cause instanceof ToolMissing) {
|
|
32
|
+
// convert.ts words it `<what is missing> … here: <how to get it>`; the second half is the step.
|
|
33
|
+
const at = message.indexOf(" here: ");
|
|
34
|
+
return at === -1
|
|
35
|
+
? { exit: 2, code: "missing_tool", message }
|
|
36
|
+
: { exit: 2, code: "missing_tool", message: message.slice(0, at + " here".length), next: message.slice(at + " here: ".length) };
|
|
37
|
+
}
|
|
38
|
+
if (cause instanceof BadPages) {
|
|
39
|
+
return { exit: 2, code: "usage", message };
|
|
40
|
+
}
|
|
41
|
+
if (cause instanceof NotRenderable) {
|
|
42
|
+
return { exit: 1, code: "unsupported", message };
|
|
24
43
|
}
|
|
25
|
-
if (cause instanceof
|
|
26
|
-
return {
|
|
44
|
+
if (cause instanceof ConvertFailed) {
|
|
45
|
+
return { exit: 1, code: "unreadable", message };
|
|
27
46
|
}
|
|
28
|
-
|
|
47
|
+
// A file system's refusal (an --out it cannot write) names itself; only a bug is worth reporting.
|
|
48
|
+
return errnoCode(cause) === undefined
|
|
49
|
+
? { exit: 2, code: "internal", message, next: "this is fileq's own failure, not an empty answer: report it with the command that was run" }
|
|
50
|
+
: { exit: 2, code: "internal", message };
|
|
29
51
|
};
|
|
30
52
|
|
|
31
53
|
const summary = (file: string, result: RenderResult): string => {
|
|
@@ -62,9 +84,9 @@ export const renderCommand = buildCommand({
|
|
|
62
84
|
const size = Math.max(64, Math.round(flags.size));
|
|
63
85
|
result = await renderDocument({ absPath, pages, outDir: flags.out === undefined ? undefined : resolve(flags.out), size }, workspaceRoot());
|
|
64
86
|
} catch (cause) {
|
|
65
|
-
const
|
|
66
|
-
this.process.stdout.write(
|
|
67
|
-
process.exitCode =
|
|
87
|
+
const failed = failureOf(cause);
|
|
88
|
+
this.process.stdout.write(failure("fileq", failed.code, `cannot render ${absPath}: ${failed.message}`, failed.next));
|
|
89
|
+
process.exitCode = failed.exit;
|
|
68
90
|
return;
|
|
69
91
|
}
|
|
70
92
|
process.exitCode = result.images.length > 0 ? 0 : 1;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/* Every deriver against a real file its real parser accepts, in a temp tree. */
|
|
2
2
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
3
|
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
4
5
|
import { tmpdir } from "node:os";
|
|
5
6
|
import { join } from "node:path";
|
|
6
7
|
import { requires } from "@intentic/testing/requires";
|
|
7
8
|
import { onPath } from "./lib/tools.js";
|
|
8
9
|
import { archiveDeriver } from "./lib/derivers/archive.js";
|
|
10
|
+
import { mammothInput } from "./lib/derivers/docx-math.js";
|
|
9
11
|
import { docxDeriver } from "./lib/derivers/docx.js";
|
|
10
12
|
import { epubDeriver } from "./lib/derivers/epub.js";
|
|
11
13
|
import { htmlDeriver } from "./lib/derivers/html.js";
|
|
@@ -22,6 +24,7 @@ import { rtfDeriver, rtfParagraphs } from "./lib/derivers/rtf.js";
|
|
|
22
24
|
import { xlsxDeriver } from "./lib/derivers/xlsx.js";
|
|
23
25
|
import { detectFormat } from "./lib/formats.js";
|
|
24
26
|
import {
|
|
27
|
+
docxBodyBytes,
|
|
25
28
|
docxBytes,
|
|
26
29
|
epubBytes,
|
|
27
30
|
gzipBytes,
|
|
@@ -68,6 +71,32 @@ describe("docx", () => {
|
|
|
68
71
|
const path = fixture("plan2.docx", docxBytes("T", ["x"]));
|
|
69
72
|
expect(await detectFormat(path)).toBe("docx");
|
|
70
73
|
});
|
|
74
|
+
|
|
75
|
+
// The equations of markitdown's equations.docx (tests/test_files, MIT), one inline and two display, as Word writes
|
|
76
|
+
// them, its run properties left out.
|
|
77
|
+
const r = (text: string): string => `<m:r><m:t>${text}</m:t></m:r>`;
|
|
78
|
+
const fraction = (num: string, den: string): string => `<m:f><m:fPr/><m:num>${num}</m:num><m:den>${den}</m:den></m:f>`;
|
|
79
|
+
const EQUATIONS = [
|
|
80
|
+
`<w:p><w:r><w:t xml:space="preserve">For </w:t></w:r><m:oMath>${r("m")}${r("=1")}</m:oMath><w:r><w:t>,</w:t></w:r></w:p>`,
|
|
81
|
+
`<w:p><m:oMathPara><m:oMath>${r("sin\u2061")}${r("θ")}${r("=")}${fraction(r("mλ"), r("a"))}${r("=")}${fraction(
|
|
82
|
+
`${r("(1)")}<m:d><m:dPr/><m:e>${r("550×")}<m:sSup><m:e>${r("10")}</m:e><m:sup>${r("-6")}</m:sup></m:sSup>${r(" mm")}</m:e></m:d>`,
|
|
83
|
+
r("2.5 mm"),
|
|
84
|
+
)}</m:oMath></m:oMathPara></w:p>`,
|
|
85
|
+
`<w:p><w:r><w:t xml:space="preserve">The angle is </w:t></w:r><m:oMath>${r("θ=")}<m:sSup><m:e>${r("sin")}</m:e><m:sup>${r("-1")}</m:sup></m:sSup>${r("(2.2×10)")}</m:oMath><w:r><w:t>.</w:t></w:r></w:p>`,
|
|
86
|
+
].join("");
|
|
87
|
+
|
|
88
|
+
test("equations read as LaTeX, $…$ inline and $$…$$ on their own, where mammoth alone dropped them", async () => {
|
|
89
|
+
const doc = await docxDeriver.derive(fixture("equations.docx", docxBodyBytes(EQUATIONS)));
|
|
90
|
+
expect(doc.markdown).toContain("For $m=1$,");
|
|
91
|
+
expect(doc.markdown).toContain("$$sinθ=\\frac{mλ}{a}=\\frac{(1)\\left(550×10^{-6} mm\\right)}{2.5 mm}$$");
|
|
92
|
+
expect(doc.markdown).toContain("The angle is $θ=sin^{-1}(2.2×10)$.");
|
|
93
|
+
expect(doc.notes.join(" ")).not.toContain("oMath");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("a document without an equation reaches mammoth by its path, so it converts exactly as it always did", async () => {
|
|
97
|
+
const path = fixture("no-math.docx", docxBytes("Plain", ["No equations, though m=1 is written here."]));
|
|
98
|
+
expect(await mammothInput(path)).toEqual({ path });
|
|
99
|
+
});
|
|
71
100
|
});
|
|
72
101
|
|
|
73
102
|
describe("pptx", () => {
|
|
@@ -117,6 +146,81 @@ describe("xlsx", () => {
|
|
|
117
146
|
});
|
|
118
147
|
});
|
|
119
148
|
|
|
149
|
+
// markitdown's own PDF test files (fixtures/markitdown/ATTRIBUTION), read against the expectations of its
|
|
150
|
+
// tests/test_pdf_tables.py and test_pdf_masterformat.py.
|
|
151
|
+
const markitdown = (name: string): string => fileURLToPath(new URL(`../fixtures/markitdown/${name}`, import.meta.url));
|
|
152
|
+
|
|
153
|
+
// Each of `needles` found, in the order given.
|
|
154
|
+
const inOrder = (text: string, needles: readonly string[]): number[] => needles.map((needle) => text.indexOf(needle));
|
|
155
|
+
|
|
156
|
+
describe("pdf tables (markitdown's fixtures)", () => {
|
|
157
|
+
test("a borderless table keeps every value in its own column, between the prose around it", async () => {
|
|
158
|
+
const { markdown } = await pdfDeriver.derive(markitdown("SPARSE-2024-INV-1234_borderless_table.pdf"));
|
|
159
|
+
for (const header of ["Report ID: SPARSE-2024-INV-1234", "Warehouse: Distribution Center East", "Prepared By: Sarah Martinez"]) {
|
|
160
|
+
expect(markdown).toContain(header);
|
|
161
|
+
}
|
|
162
|
+
// Page 1's sparse rows: which column a value sits in is the whole of what it means.
|
|
163
|
+
expect(markdown).toContain(
|
|
164
|
+
[
|
|
165
|
+
"| Product Code | Location | Expected | Actual | Variance | Status |",
|
|
166
|
+
"| --- | --- | --- | --- | --- | --- |",
|
|
167
|
+
"| SKU-8847 | A-12 | 450 | | | |",
|
|
168
|
+
"| | B-07 | | 289 | -23 | |",
|
|
169
|
+
"| SKU-9201 | | 780 | 778 | | OK |",
|
|
170
|
+
"| | C-15 | | | +15 | |",
|
|
171
|
+
].join("\n"),
|
|
172
|
+
);
|
|
173
|
+
// Page 2: "Critical" is a note, not a last audit date.
|
|
174
|
+
expect(markdown).toContain("| SKU-4563 | Software | $120.00 | $18,720.00 | | Critical |");
|
|
175
|
+
expect(markdown).toContain("| SKU-6678 | Software | $200.00 | $42,000.00 | 2024-10-25 | High Value |");
|
|
176
|
+
const positions = inOrder(markdown, [
|
|
177
|
+
"INVENTORY RECONCILIATION REPORT",
|
|
178
|
+
"| Product Code | Location",
|
|
179
|
+
"Variance Analysis:",
|
|
180
|
+
"## Page 2",
|
|
181
|
+
"Extended Inventory Review:",
|
|
182
|
+
"| Product Code | Category",
|
|
183
|
+
"Recommendations:",
|
|
184
|
+
]);
|
|
185
|
+
expect(positions.every((position) => position >= 0)).toBe(true);
|
|
186
|
+
expect(positions).toEqual(positions.toSorted((a, b) => a - b));
|
|
187
|
+
expect(markdown.split("SKU-8847").length - 1).toBeLessThanOrEqual(4);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("a booking order's tables come out whole, a wrapped title and a footer drawn across rows included", async () => {
|
|
191
|
+
const { markdown } = await pdfDeriver.derive(markitdown("movie-theater-booking-2024.pdf"));
|
|
192
|
+
for (const expected of ["BOOKING ORDER", "2024-12-5678", "Holiday Movie Marathon Package", "12/20/2024 - 12/31/2024", "CUST-98765", "December 2024"]) {
|
|
193
|
+
expect(markdown).toContain(expected);
|
|
194
|
+
}
|
|
195
|
+
expect(markdown).toContain("| Start Date | End Date | # Shows | Gross Amount | Net Amount |\n| --- | --- | --- | --- | --- |\n| 12/20/24 | 12/31/24 | 48 | $12,500.00 | $11,250.00 |");
|
|
196
|
+
expect(markdown).toContain("| 1 | SCR-1 | 12/20/24 | 12/25/24 | Holiday Spectacular | IMAX 3D | 7:00 PM | Daily | 12 | $250 | PM | $3,000 |");
|
|
197
|
+
expect(markdown).toContain("| | | | | (Runtime: 142 min); Holiday Season Premium | | | | | | | |");
|
|
198
|
+
expect(markdown).toContain(
|
|
199
|
+
[
|
|
200
|
+
"| 1 | SCR-1 | 12/20-12/25 | Holiday Spectacular | 7:00 PM | Daily | PM | $250 | $3,000 |",
|
|
201
|
+
"| 2 | SCR-2 | 12/20-12/31 | Winter Wonderland | 4:30 PM | Daily | MT | $150 | $3,600 |",
|
|
202
|
+
"| 3 | SCR-1 | 12/26-12/31 | New Year Mystery | 9:30 PM | Daily | PM | $300 | $3,600 |",
|
|
203
|
+
].join("\n"),
|
|
204
|
+
);
|
|
205
|
+
expect(markdown).toContain("This booking order is subject to cinema availability and standard terms.");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a receipt's formatted text stays text, in reading order", async () => {
|
|
209
|
+
const { markdown } = await pdfDeriver.derive(markitdown("RECEIPT-2024-TXN-98765_retail_purchase.pdf"));
|
|
210
|
+
expect(markdown).not.toContain("|");
|
|
211
|
+
const positions = inOrder(markdown, ["TECHMART ELECTRONICS", "TXN: TXN-98765-2024", "Wireless Noise-Cancelling", "SUBTOTAL", "PAYMENT METHOD", "REWARDS MEMBER", "RETURN POLICY"]);
|
|
212
|
+
expect(positions.every((position) => position >= 0)).toBe(true);
|
|
213
|
+
expect(positions).toEqual(positions.toSorted((a, b) => a - b));
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("partial numbering stays on the line it numbers", async () => {
|
|
217
|
+
const { markdown } = await pdfDeriver.derive(markitdown("masterformat_partial_numbering.pdf"));
|
|
218
|
+
expect(markdown).toContain(".1 The intent of this Request for Proposal");
|
|
219
|
+
expect(markdown.split("\n").filter((each) => /^\.\d+$/.test(each.trim()))).toEqual([]);
|
|
220
|
+
expect(markdown).not.toContain("|");
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
120
224
|
describe("pdf", () => {
|
|
121
225
|
test("the text layer is the markdown", async () => {
|
|
122
226
|
const path = fixture("hello.pdf", pdfBytes("Hello from the text layer of this fixture document"));
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// The symbol tables docx-math.ts converts Office Math with. Ported from markitdown's
|
|
2
|
+
// packages/markitdown/src/markitdown/converter_utils/docx/math/latex_dict.py (https://github.com/microsoft/markitdown,
|
|
3
|
+
// MIT, Copyright (c) Microsoft Corporation), itself adapted from dwml's dwml/latex_dict.py
|
|
4
|
+
// (https://github.com/xiilei/dwml, Apache-2.0, by xilei; ../../../NOTICE). Kept: every mapping and markitdown's fixes to them
|
|
5
|
+
// (U+20EE is \underleftarrow, the math italic h is U+210E). Changed: a template is a macro name applied as
|
|
6
|
+
// `\macro{…}` rather than a str.format string, so no table entry can carry an unbalanced brace.
|
|
7
|
+
|
|
8
|
+
/** Characters LaTeX reads as syntax, escaped with a backslash when they come from an equation's text. */
|
|
9
|
+
export const LATEX_SPECIALS = new Set(["{", "}", "_", "^", "#", "&", "$", "%", "~"]);
|
|
10
|
+
|
|
11
|
+
/** Accent and over/under characters (`m:chr` of an accent, bar or group character) to the macro that sets them. */
|
|
12
|
+
export const ACCENTS: ReadonlyMap<string, string> = new Map([
|
|
13
|
+
// Top accents
|
|
14
|
+
["\u0300", "\\grave"],
|
|
15
|
+
["\u0301", "\\acute"],
|
|
16
|
+
["\u0302", "\\hat"],
|
|
17
|
+
["\u0303", "\\tilde"],
|
|
18
|
+
["\u0304", "\\bar"],
|
|
19
|
+
["\u0305", "\\overbar"],
|
|
20
|
+
["\u0306", "\\breve"],
|
|
21
|
+
["\u0307", "\\dot"],
|
|
22
|
+
["\u0308", "\\ddot"],
|
|
23
|
+
["\u0309", "\\ovhook"],
|
|
24
|
+
["\u030a", "\\ocirc"],
|
|
25
|
+
["\u030c", "\\check"],
|
|
26
|
+
["\u0310", "\\candra"],
|
|
27
|
+
["\u0312", "\\oturnedcomma"],
|
|
28
|
+
["\u0315", "\\ocommatopright"],
|
|
29
|
+
["\u031a", "\\droang"],
|
|
30
|
+
["\u0338", "\\not"],
|
|
31
|
+
["\u20d0", "\\leftharpoonaccent"],
|
|
32
|
+
["\u20d1", "\\rightharpoonaccent"],
|
|
33
|
+
["\u20d2", "\\vertoverlay"],
|
|
34
|
+
["\u20d6", "\\overleftarrow"],
|
|
35
|
+
["\u20d7", "\\vec"],
|
|
36
|
+
["\u20db", "\\dddot"],
|
|
37
|
+
["\u20dc", "\\ddddot"],
|
|
38
|
+
["\u20e1", "\\overleftrightarrow"],
|
|
39
|
+
["\u20e7", "\\annuity"],
|
|
40
|
+
["\u20e9", "\\widebridgeabove"],
|
|
41
|
+
["\u20f0", "\\asteraccent"],
|
|
42
|
+
// Bottom accents
|
|
43
|
+
["\u0330", "\\wideutilde"],
|
|
44
|
+
["\u0331", "\\underbar"],
|
|
45
|
+
["\u20e8", "\\threeunderdot"],
|
|
46
|
+
["\u20ec", "\\underrightharpoondown"],
|
|
47
|
+
["\u20ed", "\\underleftharpoondown"],
|
|
48
|
+
["\u20ee", "\\underleftarrow"],
|
|
49
|
+
["\u20ef", "\\underrightarrow"],
|
|
50
|
+
// Over and under groups
|
|
51
|
+
["\u23b4", "\\overbracket"],
|
|
52
|
+
["\u23dc", "\\overparen"],
|
|
53
|
+
["\u23de", "\\overbrace"],
|
|
54
|
+
["\u23b5", "\\underbracket"],
|
|
55
|
+
["\u23dd", "\\underparen"],
|
|
56
|
+
["\u23df", "\\underbrace"],
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
/** An accent with no `m:chr` is a hat; a group character with none is an underbrace. */
|
|
60
|
+
export const DEFAULT_ACCENT = "\\hat";
|
|
61
|
+
export const DEFAULT_GROUP_CHR = "\\underbrace";
|
|
62
|
+
|
|
63
|
+
/** A bar's `m:pos`; with none it is an overline. */
|
|
64
|
+
export const BAR_POSITIONS: ReadonlyMap<string, string> = new Map([
|
|
65
|
+
["top", "\\overline"],
|
|
66
|
+
["bot", "\\underline"],
|
|
67
|
+
]);
|
|
68
|
+
export const DEFAULT_BAR = "\\overline";
|
|
69
|
+
|
|
70
|
+
/** Big operators (`m:chr` of an n-ary); an n-ary with no `m:chr` is an integral, as Word draws it. */
|
|
71
|
+
export const BIG_OPERATORS: ReadonlyMap<string, string> = new Map([
|
|
72
|
+
["\u2140", "\\Bbbsum"],
|
|
73
|
+
["\u220f", "\\prod"],
|
|
74
|
+
["\u2210", "\\coprod"],
|
|
75
|
+
["\u2211", "\\sum"],
|
|
76
|
+
["\u222b", "\\int"],
|
|
77
|
+
["\u22c0", "\\bigwedge"],
|
|
78
|
+
["\u22c1", "\\bigvee"],
|
|
79
|
+
["\u22c2", "\\bigcap"],
|
|
80
|
+
["\u22c3", "\\bigcup"],
|
|
81
|
+
["\u2a00", "\\bigodot"],
|
|
82
|
+
["\u2a01", "\\bigoplus"],
|
|
83
|
+
["\u2a02", "\\bigotimes"],
|
|
84
|
+
]);
|
|
85
|
+
export const DEFAULT_BIG_OPERATOR = "\\int";
|
|
86
|
+
|
|
87
|
+
// Mathematical italic letters back to ASCII. Lowercase has a hole at U+1D455: that letter is U+210E (Planck's h).
|
|
88
|
+
const italic = (first: number, letters: string, skip?: number): [string, string][] =>
|
|
89
|
+
[...letters].flatMap((letter, index): [string, string][] => (first + index === skip ? [] : [[String.fromCodePoint(first + index), letter]]));
|
|
90
|
+
|
|
91
|
+
const GREEK = [
|
|
92
|
+
"alpha",
|
|
93
|
+
"beta",
|
|
94
|
+
"gamma",
|
|
95
|
+
"delta",
|
|
96
|
+
"epsilon",
|
|
97
|
+
"zeta",
|
|
98
|
+
"eta",
|
|
99
|
+
"theta",
|
|
100
|
+
"iota",
|
|
101
|
+
"kappa",
|
|
102
|
+
"lambda",
|
|
103
|
+
"mu",
|
|
104
|
+
"nu",
|
|
105
|
+
"xi",
|
|
106
|
+
"omicron",
|
|
107
|
+
"pi",
|
|
108
|
+
"rho",
|
|
109
|
+
"varsigma",
|
|
110
|
+
"sigma",
|
|
111
|
+
"tau",
|
|
112
|
+
"upsilon",
|
|
113
|
+
"phi",
|
|
114
|
+
"chi",
|
|
115
|
+
"psi",
|
|
116
|
+
"omega",
|
|
117
|
+
"partial",
|
|
118
|
+
"varepsilon",
|
|
119
|
+
"vartheta",
|
|
120
|
+
"varkappa",
|
|
121
|
+
"varphi",
|
|
122
|
+
"varrho",
|
|
123
|
+
"varpi",
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
/** Characters of an equation's text to LaTeX; a macro keeps a trailing space so the next letter cannot extend it. */
|
|
127
|
+
export const SYMBOLS: ReadonlyMap<string, string> = new Map<string, string>([
|
|
128
|
+
// Mathematical italic Greek, U+1D6FC onwards
|
|
129
|
+
...GREEK.map((name, index): [string, string] => [String.fromCodePoint(0x1d6fc + index), `\\${name} `]),
|
|
130
|
+
// Arrows and relations
|
|
131
|
+
["\u2190", "\\leftarrow "],
|
|
132
|
+
["\u2191", "\\uparrow "],
|
|
133
|
+
["\u2192", "\\rightarrow "],
|
|
134
|
+
["\u2193", "\\downarrow "],
|
|
135
|
+
["\u2194", "\\leftrightarrow "],
|
|
136
|
+
["\u2195", "\\updownarrow "],
|
|
137
|
+
["\u2196", "\\nwarrow "],
|
|
138
|
+
["\u2197", "\\nearrow "],
|
|
139
|
+
["\u2198", "\\searrow "],
|
|
140
|
+
["\u2199", "\\swarrow "],
|
|
141
|
+
["\u22ee", "\\vdots "],
|
|
142
|
+
["\u22ef", "\\cdots "],
|
|
143
|
+
["\u22f0", "\\adots "],
|
|
144
|
+
["\u22f1", "\\ddots "],
|
|
145
|
+
["\u2260", "\\ne "],
|
|
146
|
+
["\u2264", "\\leq "],
|
|
147
|
+
["\u2265", "\\geq "],
|
|
148
|
+
["\u2266", "\\leqq "],
|
|
149
|
+
["\u2267", "\\geqq "],
|
|
150
|
+
["\u2268", "\\lneqq "],
|
|
151
|
+
["\u2269", "\\gneqq "],
|
|
152
|
+
["\u226a", "\\ll "],
|
|
153
|
+
["\u226b", "\\gg "],
|
|
154
|
+
["\u2208", "\\in "],
|
|
155
|
+
["\u2209", "\\notin "],
|
|
156
|
+
["\u220b", "\\ni "],
|
|
157
|
+
["\u220c", "\\nni "],
|
|
158
|
+
["\u221e", "\\infty "],
|
|
159
|
+
["\u00b1", "\\pm "],
|
|
160
|
+
["\u2213", "\\mp "],
|
|
161
|
+
// Mathematical italic Latin
|
|
162
|
+
...italic(0x1d434, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
|
|
163
|
+
...italic(0x1d44e, "abcdefghijklmnopqrstuvwxyz", 0x1d455),
|
|
164
|
+
["\u210e", "h"],
|
|
165
|
+
// The invisible function-application, times, separator and plus operators carry nothing a reader sees.
|
|
166
|
+
["\u2061", ""],
|
|
167
|
+
["\u2062", ""],
|
|
168
|
+
["\u2063", ""],
|
|
169
|
+
["\u2064", ""],
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
/** Function names (`m:fName`) with a LaTeX operator of their own; any other name is set with \operatorname. */
|
|
173
|
+
export const FUNCTIONS: ReadonlyMap<string, string> = new Map([
|
|
174
|
+
...["sin", "cos", "tan", "arcsin", "arccos", "arctan", "sinh", "cosh", "tanh", "coth", "sec", "csc", "log", "ln", "exp"].map(
|
|
175
|
+
(name): [string, string] => [name, `\\${name}`],
|
|
176
|
+
),
|
|
177
|
+
...["det", "gcd", "lim", "max", "min", "sup", "inf", "dim", "ker", "hom", "deg", "arg"].map((name): [string, string] => [name, `\\${name}`]),
|
|
178
|
+
["arccot", "\\operatorname{arccot}"],
|
|
179
|
+
["lcm", "\\operatorname{lcm}"],
|
|
180
|
+
]);
|
|
181
|
+
|
|
182
|
+
/** Lower-limit objects (`m:limLow`) whose base is one of these are set as that operator with a subscript. */
|
|
183
|
+
export const LIMIT_OPERATORS: ReadonlyMap<string, string> = new Map([
|
|
184
|
+
["lim", "\\lim"],
|
|
185
|
+
["max", "\\max"],
|
|
186
|
+
["min", "\\min"],
|
|
187
|
+
]);
|
|
188
|
+
|
|
189
|
+
/** A fraction's `m:type` to how it is set; with none it is a stacked \frac. */
|
|
190
|
+
export const FRACTIONS: ReadonlyMap<string, (num: string, den: string) => string> = new Map([
|
|
191
|
+
["bar", (num: string, den: string) => `\\frac{${num}}{${den}}`],
|
|
192
|
+
["skw", (num: string, den: string) => `^{${num}}/_{${den}}`],
|
|
193
|
+
["noBar", (num: string, den: string) => `\\genfrac{}{}{0pt}{}{${num}}{${den}}`],
|
|
194
|
+
["lin", (num: string, den: string) => `{${num}}/{${den}}`],
|
|
195
|
+
]);
|
|
196
|
+
|
|
197
|
+
export const LINE_BREAK = "\\\\";
|