Skip to content

Commit

Permalink
Merge pull request #1 from jmezzacappa/joey/excerpts
Browse files Browse the repository at this point in the history
  • Loading branch information
wking-io authored Sep 26, 2023
2 parents bfdeec5 + 0d53550 commit c9c4db2
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 c9c4db2

Please sign in to comment.