Skip to content

Commit

Permalink
Search: case-insensitive excerpt matching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmezzacappa committed Sep 26, 2023
1 parent bfdeec5 commit 0d53550
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions app/components/layout/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
<strong>{query}</strong>
<strong>{matched}</strong>
{after}...
</>
)
Expand Down
1 change: 1 addition & 0 deletions app/routes/documentation.$product.$ref.actions.search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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})/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})/

Expand Down

0 comments on commit 0d53550

Please sign in to comment.