Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

refactor(llm-parse): use the shared tag stripper instead of a second copy

Maurice071f516c0fe7
diff --git a/server/src/nest/llm-parse/text-extract.ts b/server/src/nest/llm-parse/text-extract.ts
index cbb6eb08c..05657be02 100644
--- a/server/src/nest/llm-parse/text-extract.ts
+++ b/server/src/nest/llm-parse/text-extract.ts
@@ -1,6 +1,7 @@
import { parseEmail } from './mime-email';
import { extname } from 'node:path';
import { PDFParse } from 'pdf-parse';
+import { stripHtmlTags } from '../common/stripHtmlTags';
/** File extensions whose bytes are inherently text and can be decoded directly. */
const TEXT_LIKE = new Set(['.txt', '.html', '.htm', '.eml']);
@@ -86,32 +87,18 @@ function decodeEntities(s: string): string {
}
/**
- * Replace every tag with a space. Hand-rolled rather than a /<[^>]+>/g replace: a
- * match can only start at a "<", so a run of brackets with no ">" behind it — an
- * uploaded .eml or .html is free to carry one — makes the engine rescan the tail
- * once per bracket. Character for character the same result, including the two
- * cases the regex leaves alone: a "<" that nothing closes, and an empty "<>",
- * which [^>]+ cannot match.
+ * Strip HTML/XML tags, resolve entities and collapse whitespace for a cleaner
+ * LLM prompt.
+ *
+ * The tag stripping is stripHtmlTags rather than a `/<[^>]+>/g` replace: a match
+ * can only start at a `<`, so a run of brackets with nothing closing them — and
+ * an uploaded .eml or .html is free to carry one — makes the engine rescan the
+ * tail once per bracket.
*/
-function stripTags(s: string): string {
- let out = '';
- let i = 0;
- for (;;) {
- const lt = s.indexOf('<', i);
- if (lt === -1) break;
- const gt = s.indexOf('>', lt + 1);
- if (gt === -1) break; // nothing closes it, so the rest of the string is text
- // An empty "<>" has nothing for [^>]+ to match, so both brackets stay literal.
- out += s.slice(i, lt) + (gt === lt + 1 ? '<>' : ' ');
- i = gt + 1;
- }
- return out + s.slice(i);
-}
-
-/** Strip HTML/XML tags, resolve entities and collapse whitespace for a cleaner LLM prompt. */
function stripMarkup(s: string): string {
- const withoutTags = stripTags(
+ const withoutTags = stripHtmlTags(
s.replace(/<script[\s\S]*?<\/script>/gi, ' ').replace(/<style[\s\S]*?<\/style>/gi, ' '),
+ ' ',
);
// Entities come last so that a decoded `&lt;` cannot turn into a tag the
// stripper would then eat.
@@ -161,8 +148,8 @@ async function extractPdfText(buffer: Buffer): Promise<string> {
/** Exactly the code points JS `\s` matches — the class the page-marker pattern walks over. */
const SPACE_CODES = new Set([
- 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x20, 0xa0, 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
- 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff,
+ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x20, 0xa0, 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007,
+ 0x2008, 0x2009, 0x200a, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff,
]);
/** The line breaks a multiline `^` starts after and `$` ends before. */
diff --git a/server/tests/unit/nest/llm-parse/text-extract.test.ts b/server/tests/unit/nest/llm-parse/text-extract.test.ts
index 3682f9c6d..9396a0d95 100644
--- a/server/tests/unit/nest/llm-parse/text-extract.test.ts
+++ b/server/tests/unit/nest/llm-parse/text-extract.test.ts
@@ -226,4 +226,75 @@ describe('text-extract', () => {
expect(out).toContain('Zimmer für zwei – 120,00 €');
});
});
+
+ describe('pdf page markers', () => {
+ const fromPdf = async (text: string) => {
+ getText.mockResolvedValueOnce({ text });
+ return extractText(Buffer.from('%PDF-1.4'), 'a.pdf');
+ };
+
+ it('drops a marker together with the blank lines around it', async () => {
+ expect(await fromPdf('Hotel Ibis\n\n\n--- 3 of 12 ---\n\n\nCheck-in 04.08.')).toBe(
+ 'Hotel Ibis\n\nCheck-in 04.08.',
+ );
+ });
+
+ it('drops a marker that opens or closes the document', async () => {
+ expect(await fromPdf('--- 1 of 2 ---\nBoarding pass')).toBe('Boarding pass');
+ expect(await fromPdf('Rechnung\n--- 4 of 4 ---')).toBe('Rechnung');
+ });
+
+ it('drops an indented marker, spaces or non-breaking ones', async () => {
+ expect(await fromPdf('Gate 12\n --- 2 of 12 ---\nSeat 4A')).toBe('Gate 12\n\nSeat 4A');
+ expect(await fromPdf('Gate 12\n  --- 2 of 12 ---\nSeat 4A')).toBe('Gate 12\n\nSeat 4A');
+ });
+
+ it('reads the marker case-insensitively and across CRLF line ends', async () => {
+ expect(await fromPdf('Zimmer 12\r\n\r\n-- 1 OF 9 --\r\nAnreise')).toBe('Zimmer 12\r\nAnreise');
+ });
+
+ it('clears every marker of a multi-page document and still de-kerns the text', async () => {
+ const pages = 'A M S T E R D A M\n--- 1 of 3 ---\nGate 12\n-- 2 of 3 --\nSeat 4A\n--- 3 of 3 ---';
+ expect(await fromPdf(pages)).toBe('AMSTERDAM\n\nGate 12\n\nSeat 4A');
+ });
+
+ it('keeps dashed lines that only look like a marker', async () => {
+ const kept = [
+ 'Total -- 3 of 4 --', // not at the start of its line
+ 'Summe\n--- 3 of 4 --- netto', // something else follows on the line
+ 'Summe\n--- 3of 4 ---\nX', // no gap behind the page number
+ 'Summe\n--- 3 on 4 ---\nX',
+ 'Summe\n--- 3 of4 ---\nX',
+ 'Summe\n--- 3 of vier ---\nX',
+ 'Summe\n--- 3 of 4\nX', // nothing closes it
+ 'Summe\n--- keine Zahl ---\nX',
+ 'Summe\n----------\nX',
+ ];
+ for (const text of kept) expect(await fromPdf(text)).toBe(text);
+ });
+
+ it('stays linear on a page that is nothing but whitespace', async () => {
+ const started = Date.now();
+ expect(await fromPdf(' \n'.repeat(50000))).toBe('');
+ // The pattern this replaced walked the whole run again from every line
+ // start and needed ~4 s for these 100k characters.
+ expect(Date.now() - started).toBeLessThan(1000);
+ });
+ });
+ describe('tag stripping keeps the old regex edge cases', () => {
+ it('leaves an empty `<>` alone, because `[^>]+` had nothing to match', async () => {
+ expect(await extractText(Buffer.from('a<>b<p>c</p>'), 'a.html')).toBe('a<>b c');
+ });
+
+ it('keeps a `<` that nothing closes at all as literal text', async () => {
+ expect(await extractText(Buffer.from('3 < 4'), 'a.html')).toBe('3 < 4');
+ });
+
+ it('treats a `<` inside a tag as content, the way the regex did', async () => {
+ // `[^>]` matches `<` too, so `< 4 and <b>` is ONE tag, not a stray `<`
+ // followed by a `<b>`. Narrowing the class would split it and leave the
+ // first `<` behind — a different sanitiser output on malformed markup.
+ expect(await extractText(Buffer.from('3 < 4 and <b>5</b>'), 'a.html')).toBe('3 5');
+ });
+ });
});