Skip to content

Commit 42d88da

Browse files
authored
Fix UX issue about highlighting the search term in search result sections (#3323)
1 parent 2863fe0 commit 42d88da

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

.changeset/afraid-gifts-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix UX issue about highlighting the search term in search result sections

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ git clone https://github.com/gitbookIO/gitbook.git
5656
bun install
5757
```
5858

59-
4. Start your local development server.
59+
4. Run build.
60+
61+
```
62+
bun build:v2
63+
```
64+
65+
5. Start your local development server.
6066

6167
```
6268
bun dev:v2
6369
```
6470

65-
5. Open a published GitBook space in your web browser, prefixing it with `http://localhost:3000/`.
71+
6. Open a published GitBook space in your web browser, prefixing it with `http://localhost:3000/`.
6672

6773
examples:
6874

packages/gitbook/src/components/Search/SearchSectionResultItem.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ export const SearchSectionResultItem = React.forwardRef(function SearchSectionRe
6666
<HighlightQuery query={query} text={item.title} />
6767
</p>
6868
) : null}
69-
{item.body ? (
70-
<p className={tcls('text-sm', 'line-clamp-3', 'relative')}>
71-
<HighlightQuery query={query} text={item.body} />
72-
</p>
73-
) : null}
69+
{item.body ? highlightQueryInBody(item.body, query) : null}
7470
</div>
7571
<div
7672
className={tcls(
@@ -90,3 +86,14 @@ export const SearchSectionResultItem = React.forwardRef(function SearchSectionRe
9086
</Link>
9187
);
9288
});
89+
90+
function highlightQueryInBody(body: string, query: string) {
91+
const idx = body.indexOf(query);
92+
93+
// Ensure the query to be highlighted is visible in the body.
94+
return (
95+
<p className={tcls('text-sm', 'line-clamp-3', 'relative')}>
96+
<HighlightQuery query={query} text={idx < 20 ? body : `...${body.slice(idx - 15)}`} />
97+
</p>
98+
);
99+
}

0 commit comments

Comments
 (0)