Skip to content

Commit e7b7cde

Browse files
committed
Add breadcrumbs to search results
1 parent 1762f85 commit e7b7cde

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

Diff for: .changeset/giant-carrots-divide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Add breadcrumbs to search results

Diff for: packages/gitbook/src/components/Search/SearchPageResultItem.tsx

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Icon } from '@gitbook/icons';
22
import React from 'react';
33

4+
import { AncestorRevisionPage } from '@/lib/pages';
45
import { tcls } from '@/lib/tailwind';
56

67
import { HighlightQuery } from './HighlightQuery';
@@ -17,6 +18,11 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
1718
) {
1819
const { query, item, active } = props;
1920

21+
const breadcrumbs = item.ancestors.map((ancestor) => ancestor.title);
22+
if (item.spaceTitle) {
23+
breadcrumbs.unshift(item.spaceTitle);
24+
}
25+
2026
return (
2127
<Link
2228
ref={ref}
@@ -56,7 +62,7 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
5662
/>
5763
</div>
5864
<div className={tcls('flex', 'flex-col', 'w-full')}>
59-
{item.spaceTitle ? (
65+
{breadcrumbs.length > 0 ? (
6066
<div
6167
className={tcls(
6268
'text-xs',
@@ -65,9 +71,34 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
6571
'uppercase',
6672
'tracking-wider',
6773
'mb-1',
74+
'flex',
75+
'flex-wrap',
76+
'gap-x-2',
77+
'gap-y-1',
78+
'items-center',
6879
)}
6980
>
70-
{item.spaceTitle}
81+
{(breadcrumbs.length > 3
82+
? [
83+
...breadcrumbs.slice(0, 2),
84+
<Icon key="ellipsis" icon="ellipsis-h" className="size-3" />,
85+
...breadcrumbs.slice(-1),
86+
]
87+
: breadcrumbs
88+
).map((crumb, index) => (
89+
<>
90+
{index !== 0 ? (
91+
<Icon
92+
key={index + '-icon'}
93+
icon="chevron-right"
94+
className="size-3"
95+
/>
96+
) : null}
97+
<span key={index} className="line-clamp-1">
98+
{crumb}
99+
</span>
100+
</>
101+
))}
71102
</div>
72103
) : null}
73104
<HighlightQuery query={query} text={item.title} />

Diff for: packages/gitbook/src/components/Search/server-actions.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { captureException } from '@sentry/nextjs';
55
import * as React from 'react';
66
import { assert } from 'ts-essentials';
77

8+
import { fetchPageData } from '@/app/middleware/(site)/fetch';
89
import { streamResponse } from '@/lib/actions';
910
import * as api from '@/lib/api';
1011
import { getAbsoluteHref, getPageHref } from '@/lib/links';
11-
import { resolvePageId } from '@/lib/pages';
12+
import { AncestorRevisionPage, resolvePageId } from '@/lib/pages';
1213
import { filterOutNullable } from '@/lib/typescript';
1314

1415
import { DocumentView } from '../DocumentView';
@@ -31,6 +32,8 @@ export interface ComputedPageResult {
3132

3233
/** When part of a multi-spaces search, the title of the space */
3334
spaceTitle?: string;
35+
36+
ancestors: AncestorRevisionPage[];
3437
}
3538

3639
export interface AskAnswerSource {
@@ -317,12 +320,15 @@ async function transformSectionsAndPage(args: {
317320
})) ?? [],
318321
);
319322

323+
const pageData = await fetchPageData({ pathname: [item.path] });
324+
320325
const page: ComputedPageResult = {
321326
type: 'page',
322327
id: item.id,
323328
title: item.title,
324329
href: await getURL(item.path, spaceURL),
325330
spaceTitle: space?.title,
331+
ancestors: pageData.ancestors,
326332
};
327333

328334
return [page, sections];

0 commit comments

Comments
 (0)