GeezerVison is showing github.com — fix(web): PDF search finds phrases that wrap across lines (#25702) · pingdotgg/t3code@becf853
File treeExpand file treeCollapse file tree Open diff view settings apps/web/src/components/files
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/PdfViewer.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ import { readBoundedDocument } from "./readBoundedDocument"; | 15 | 15 | import { openPdf } from "./pdfDocument"; | 16 | 16 | import { | 17 | 17 | currentPdfPage, | | 18 | + pdfFindIncludes, | 18 | 19 | pdfFindMatchRuns, | 19 | 20 | pdfPageOffsets, | 20 | 21 | pdfPageScrollTop, | @@ -247,7 +248,7 @@ export default function PdfViewer(props: { src: string; title: string }) { | 247 | 248 | throw new Error( | 248 | 249 | "This PDF has too much text to search. Save it to search in another app.", | 249 | 250 | ); | 250 | | - if (text.toLowerCase().includes(query.toLowerCase())) found.push(index); | | 251 | + if (pdfFindIncludes(text, query)) found.push(index); | 251 | 252 | } | 252 | 253 | if (cancelled) return; | 253 | 254 | setRatios(sizes); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/pdfLayout.test.ts +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, it } from "vite-plus/test"; | 2 | 2 | import { | 3 | 3 | currentPdfPage, | | 4 | + pdfFindIncludes, | 4 | 5 | pdfFindMatchRuns, | 5 | 6 | pdfPageOffsets, | 6 | 7 | pdfPageScrollTop, | @@ -105,6 +106,36 @@ describe("pdfFindMatchRuns", () => { | 105 | 106 | ]); | 106 | 107 | }); | 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 | + | 108 | 139 | it("highlights nothing for a blank or missing query", () => { | 109 | 140 | expect(pdfFindMatchRuns(["a b", "c"], " ")).toEqual([false, false]); | 110 | 141 | expect(pdfFindMatchRuns(["a b", "c"], "")).toEqual([false, false]); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/pdfLayout.ts +59-12Lines changed: 59 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,18 +56,65 @@ export function pdfPageText( | 56 | 56 | return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join(""); | 57 | 57 | } | 58 | 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 | + | 59 | 107 | /** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */ | 60 | 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; | 73 | 120 | } |
|