diff --git a/src/utils/clinicalRetrieval.js b/src/utils/clinicalRetrieval.js index b640041..6eedf1a 100644 --- a/src/utils/clinicalRetrieval.js +++ b/src/utils/clinicalRetrieval.js @@ -224,17 +224,18 @@ function displayTitleForSource(source, fallback) { source = source || {}; var rawTitle = String(source.title || '').trim(); var filePath = source.file_path || source.filePath || source.path || ''; - if (looksLikeInternalPdfTitle(rawTitle)) { - var fromPath = titleFromPath(filePath); - if (fromPath) return cleanTitle(fromPath); + var fromPath = titleFromPath(filePath); + if (fromPath) return cleanTitle(fromPath); + if (looksLikePdfMetadataJunk(rawTitle)) { var mapped = INTERNAL_TITLE_FALLBACKS[String(source.id || '')] || INTERNAL_TITLE_FALLBACKS[rawTitle]; if (mapped) return mapped; } return cleanTitle(rawTitle || filePath || fallback || 'Untitled resource'); } -function looksLikeInternalPdfTitle(title) { - return /\.qxd$/i.test(String(title || '').trim()); +function looksLikePdfMetadataJunk(title) { + title = String(title || '').trim(); + return /\.qxd$/i.test(title) || /^https?:\/\//i.test(title) || /^www\./i.test(title); } function titleFromPath(filePath) { diff --git a/test/clinical-retrieval-title.test.js b/test/clinical-retrieval-title.test.js index e19f032..a90f36a 100644 --- a/test/clinical-retrieval-title.test.js +++ b/test/clinical-retrieval-title.test.js @@ -46,6 +46,34 @@ test('clinical retrieval leaves normal source titles unchanged', () => { assert.equal(results[0].title, 'Nelson Textbook of Pediatrics'); }); +test('clinical retrieval always prefers file basename over PDF metadata title', () => { + const results = normalizeMcpSearchResponse({ + results: [{ + id: 321, + doc_type: 'file', + title: 'Dermatology, Second Edition: 2-Volume Set (Bolognia, Dermatology)', + file_path: 'Medical Library/Dermatology/My%20Named%20Dermatology%20Source.pdf', + excerpt: 'Dermatologic therapy text' + }] + }); + + assert.equal(results[0].title, 'My Named Dermatology Source'); +}); + +test('clinical retrieval prefers file basename for URL PDF metadata titles', () => { + const results = normalizeMcpSearchResponse({ + results: [{ + id: 456, + doc_type: 'file', + title: 'https://bookbaz.ir', + file_path: 'Medical Library/Dermatology/Bolognia%20Dermatology%20Second%20Edition.pdf', + excerpt: 'Dermatologic therapy text' + }] + }); + + assert.equal(results[0].title, 'Bolognia Dermatology Second Edition'); +}); + test('clinical retrieval strips image markdown while preserving OCR table text', () => { const results = normalizeMcpSearchResponse({ results: [{