The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 15:45 UTC
fix(web): PDF search finds phrases that wrap across lines (#25702)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 18:02 UTC
becf853defb6127dbe4447860383777d69346b6f
3 files changed
+92
-13
apps/web/src/components/files/PdfViewer.tsx
+2
-1
@@ -15,6 +15,7 @@ import { readBoundedDocument } from "./readBoundedDocument";
15
import { openPdf } from "./pdfDocument";
16
import {
17
currentPdfPage,
18
+ pdfFindIncludes,
19
pdfFindMatchRuns,
20
pdfPageOffsets,
21
pdfPageScrollTop,
@@ -247,7 +248,7 @@ export default function PdfViewer(props: { src: string; title: string }) {
248
throw new Error(
249
"This PDF has too much text to search. Save it to search in another app.",
250
);
250
- if (text.toLowerCase().includes(query.toLowerCase())) found.push(index);
251
+ if (pdfFindIncludes(text, query)) found.push(index);
252
}
253
if (cancelled) return;
254
setRatios(sizes);
apps/web/src/components/files/pdfLayout.test.ts
+31
@@ -1,6 +1,7 @@
1
import { describe, expect, it } from "vite-plus/test";
2
import {
3
currentPdfPage,
4
+ pdfFindIncludes,
5
pdfFindMatchRuns,
6
pdfPageOffsets,
7
pdfPageScrollTop,
@@ -105,6 +106,36 @@ describe("pdfFindMatchRuns", () => {
106
]);
107
});
108
109
+ it("matches a space in the query across a line end", () => {
110
+ expect(
111
+ pdfFindMatchRuns(["Q3 quarterly", "\n", "outlook", " ahead"], "quarterly outlook"),
112
+ ).toEqual([true, true, true, false]);
113
+ });
114
+
115
+ it("matches a word hyphenated across a line end", () => {
116
+ expect(pdfFindMatchRuns(["the quar-", "\n", "terly report"], "quarterly")).toEqual([
117
+ true,
118
+ false,
119
+ true,
120
+ ]);
121
+ });
122
+
123
+ it("drops a line-end hyphen after a letter outside the BMP", () => {
124
+ expect(pdfFindMatchRuns(["\u{10400}-", "\n", "\u{10401}"], "\u{10400}\u{10401}")).toEqual([
125
+ true,
126
+ false,
127
+ true,
128
+ ]);
129
+ });
130
+
131
+ it("finds the same pages the highlights do", () => {
132
+ const page = "Q3 quarterly\noutlook ahead, the quar-\nterly report";
133
+ expect(pdfFindIncludes(page, "Quarterly OUTLOOK")).toBe(true);
134
+ expect(pdfFindIncludes(page, "quarterly report")).toBe(true);
135
+ expect(pdfFindIncludes(page, "bud-get")).toBe(false);
136
+ expect(pdfFindIncludes(page, " ")).toBe(false);
137
+ });
138
+
139
it("highlights nothing for a blank or missing query", () => {
140
expect(pdfFindMatchRuns(["a b", "c"], " ")).toEqual([false, false]);
141
expect(pdfFindMatchRuns(["a b", "c"], "")).toEqual([false, false]);
apps/web/src/components/files/pdfLayout.ts
+59
-12
@@ -56,18 +56,65 @@ export function pdfPageText(
56
return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join("");
57
}
58
59
+/**
60
+ * Lowercased search text with each whitespace run, line ends included, read as one space, and a
61
+ * hyphen that ends a line between letters dropped, as pdf.js's own find does. `owner` maps each
62
+ * character back to the run it came from.
63
+ */
64
+function pdfFindText(runs: readonly string[]) {
65
+ const chars = runs.flatMap((run, index) =>
66
+ Array.from(run.toLowerCase(), (char) => ({ char, run: index })),
67
+ );
68
+ let text = "";
69
+ let last = "";
70
+ const owner: number[] = [];
71
+ for (let index = 0; index < chars.length; index++) {
72
+ const { char, run } = chars[index]!;
73
+ if (/\s/.test(char)) {
74
+ if (!text.endsWith(" ")) {
75
+ text += " ";
76
+ last = " ";
77
+ owner.push(run);
78
+ }
79
+ } else if (
80
+ char === "-" &&
81
+ chars[index + 1]?.char === "\n" &&
82
+ /\p{L}/u.test(last) &&
83
+ /\p{L}/u.test(chars[index + 2]?.char ?? "")
84
+ ) {
85
+ index++;
86
+ } else {
87
+ text += char;
88
+ last = char;
89
+ for (let unit = 0; unit < char.length; unit++) owner.push(run);
90
+ }
91
+ }
92
+ return { text, owner };
93
+}
94
+
95
+/** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without
96
+ * mapping characters back to runs: the page scan checks every page on each query. */
97
+export function pdfFindIncludes(text: string, query: string) {
98
+ const needle = query.toLowerCase().replace(/\s+/g, " ");
99
+ if (!needle.trim()) return false;
100
+ return text
101
+ .toLowerCase()
102
+ .replace(/(\p{L})-\n(?=\p{L})/gu, "$1")
103
+ .replace(/\s+/g, " ")
104
+ .includes(needle);
105
+}
106
+
107
/** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */
108
export function pdfFindMatchRuns(runs: readonly string[], query: string) {
61
- const texts = runs.map((run) => run.toLowerCase());
62
- const text = texts.join("");
63
- const needle = query.toLowerCase();
64
- let match = needle.trim() ? text.indexOf(needle) : -1;
65
- let offset = 0;
66
- return texts.map((run) => {
67
- while (match !== -1 && match + needle.length <= offset)
68
- match = text.indexOf(needle, match + needle.length);
69
- const hit = match !== -1 && match < offset + run.length;
70
- offset += run.length;
71
- return hit;
72
- });
109
+ const { text, owner } = pdfFindText(runs);
110
+ const needle = query.toLowerCase().replace(/\s+/g, " ");
111
+ const hits = runs.map(() => false);
112
+ if (!needle.trim()) return hits;
113
+ for (
114
+ let match = text.indexOf(needle);
115
+ match !== -1;
116
+ match = text.indexOf(needle, match + needle.length)
117
+ )
118
+ for (let index = match; index < match + needle.length; index++) hits[owner[index]!] = true;
119
+ return hits;
120
}