From 0d53550d8e01fa2ece2932a78d0702fe26953326 Mon Sep 17 00:00:00 2001 From: Joey Mezzacappa <20446836+jmezzacappa@users.noreply.github.com> Date: Tue, 26 Sep 2023 15:02:12 -0400 Subject: [PATCH] Search: case-insensitive excerpt matching Before, excerpts varied, depending on query casing. For example, the excerpts for "olm" and "OLM" were not the same. This change uses case-insensitive methods to make excerpts consistent. --- app/components/layout/Search.tsx | 10 +++++++--- .../documentation.$product.$ref.actions.search.tsx | 1 + ...umentation.private.$product.$ref.actions.search.tsx | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/components/layout/Search.tsx b/app/components/layout/Search.tsx index aac65bb..fd8a4b1 100644 --- a/app/components/layout/Search.tsx +++ b/app/components/layout/Search.tsx @@ -15,13 +15,17 @@ import { function showExcerpt(body: string | null, query: string) { if (body === null) return '...' - if (!body.includes(query)) return `${body}...` + const startIndex = body.toLowerCase().indexOf(query.toLowerCase()) + if (startIndex === -1) return `${body}...` - const [before, after] = body.split(query) + const endIndex = startIndex + query.length + const before = body.slice(0, startIndex) + const matched = body.slice(startIndex, endIndex) + const after = body.slice(endIndex) return ( <> ...{before} - {query} + {matched} {after}... ) diff --git a/app/routes/documentation.$product.$ref.actions.search.tsx b/app/routes/documentation.$product.$ref.actions.search.tsx index f5856ef..a9d0e1d 100644 --- a/app/routes/documentation.$product.$ref.actions.search.tsx +++ b/app/routes/documentation.$product.$ref.actions.search.tsx @@ -7,6 +7,7 @@ function getBodyContext(body: string, term: string) { const numContextWords = 2 const searchTermRegex = new RegExp( `(?:\\s?(?:[\\w]+)\\s?){0,${numContextWords}}${term}(?:\\s?(?:[\\w]+)\\s?){0,${numContextWords}}`, + 'i', ) const excerptRegex = /^(\w+(?:\s+\w+){0,5})/ diff --git a/app/routes/documentation.private.$product.$ref.actions.search.tsx b/app/routes/documentation.private.$product.$ref.actions.search.tsx index e9859ff..15c8d40 100644 --- a/app/routes/documentation.private.$product.$ref.actions.search.tsx +++ b/app/routes/documentation.private.$product.$ref.actions.search.tsx @@ -7,6 +7,7 @@ function getBodyContext(body: string, term: string) { const numContextWords = 2 const searchTermRegex = new RegExp( `(?:\\s?(?:[\\w]+)\\s?){0,${numContextWords}}${term}(?:\\s?(?:[\\w]+)\\s?){0,${numContextWords}}`, + 'i', ) const excerptRegex = /^(\w+(?:\s+\w+){0,5})/