Skip to content

Commit ad671b6

Browse files
perf(web): PDF page scan checks text without mapping characters to runs
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 944f5b9 commit ad671b6

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎apps/web/src/components/files/PdfViewer.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { readBoundedDocument } from "./readBoundedDocument";
1515
import { openPdf } from "./pdfDocument";
1616
import {
1717
currentPdfPage,
18+
pdfFindIncludes,
1819
pdfFindMatchRuns,
1920
pdfPageOffsets,
2021
pdfPageScrollTop,
@@ -247,7 +248,7 @@ export default function PdfViewer(props: { src: string; title: string }) {
247248
throw new Error(
248249
"This PDF has too much text to search. Save it to search in another app.",
249250
);
250-
if (pdfFindMatchRuns([text], query)[0]) found.push(index);
251+
if (pdfFindIncludes(text, query)) found.push(index);
251252
}
252253
if (cancelled) return;
253254
setRatios(sizes);

‎apps/web/src/components/files/pdfLayout.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import {
33
currentPdfPage,
4+
pdfFindIncludes,
45
pdfFindMatchRuns,
56
pdfPageOffsets,
67
pdfPageScrollTop,
@@ -119,6 +120,14 @@ describe("pdfFindMatchRuns", () => {
119120
]);
120121
});
121122

123+
it("finds the same pages the highlights do", () => {
124+
const page = "Q3 quarterly\noutlook ahead, the quar-\nterly report";
125+
expect(pdfFindIncludes(page, "Quarterly OUTLOOK")).toBe(true);
126+
expect(pdfFindIncludes(page, "quarterly report")).toBe(true);
127+
expect(pdfFindIncludes(page, "bud-get")).toBe(false);
128+
expect(pdfFindIncludes(page, " ")).toBe(false);
129+
});
130+
122131
it("highlights nothing for a blank or missing query", () => {
123132
expect(pdfFindMatchRuns(["a b", "c"], " ")).toEqual([false, false]);
124133
expect(pdfFindMatchRuns(["a b", "c"], "")).toEqual([false, false]);

‎apps/web/src/components/files/pdfLayout.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ function pdfFindText(runs: readonly string[]) {
8989
return { text, owner };
9090
}
9191

92+
/** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without
93+
* mapping characters back to runs: the page scan checks every page on each query. */
94+
export function pdfFindIncludes(text: string, query: string) {
95+
const needle = query.toLowerCase().replace(/\s+/g, " ");
96+
if (!needle.trim()) return false;
97+
return text
98+
.toLowerCase()
99+
.replace(/(\p{L})-\n(?=\p{L})/gu, "$1")
100+
.replace(/\s+/g, " ")
101+
.includes(needle);
102+
}
103+
92104
/** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */
93105
export function pdfFindMatchRuns(runs: readonly string[], query: string) {
94106
const { text, owner } = pdfFindText(runs);

0 commit comments

Comments
 (0)