Skip to content

Add breadcrumbs to search results #2772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 28, 2025
5 changes: 5 additions & 0 deletions .changeset/giant-carrots-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Add breadcrumbs to search results
52 changes: 47 additions & 5 deletions packages/gitbook/src/components/Search/SearchPageResultItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Icon } from '@gitbook/icons';
import { tcls } from '@/lib/tailwind';
import { Icon, type IconName } from '@gitbook/icons';
import React from 'react';

import { tcls } from '@/lib/tailwind';
import { Link } from '../primitives';
import { HighlightQuery } from './HighlightQuery';
import type { ComputedPageResult } from './server-actions';
Expand All @@ -16,6 +16,23 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
) {
const { query, item, active } = props;

const breadcrumbs: (string | React.ReactNode)[] = item.ancestors.map(
(ancestor) => ancestor.title
);
if (item.spaceTitle) {
breadcrumbs.unshift(item.spaceTitle);
}
if (item.section) {
breadcrumbs.unshift(
<span className="flex items-center gap-1">
{item.section.icon ? (
<Icon className="size-3" icon={item.section.icon as IconName} />
) : null}
{item.section.title}
</span>
);
}

return (
<Link
ref={ref}
Expand Down Expand Up @@ -53,18 +70,43 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
/>
</div>
<div className={tcls('flex', 'flex-col', 'w-full')}>
{item.spaceTitle ? (
{breadcrumbs.length > 0 ? (
<div
className={tcls(
'text-xs',
'opacity-6',
'font-normal',
'uppercase',
'tracking-wider',
'mb-1'
'mb-1',
'flex',
'flex-wrap',
'gap-x-2',
'gap-y-1',
'items-center'
)}
>
{item.spaceTitle}
{(breadcrumbs.length > 3
? [
...breadcrumbs.slice(0, 2),
<Icon key="ellipsis" icon="ellipsis-h" className="size-3" />,
...breadcrumbs.slice(-1),
]
: breadcrumbs
).map((crumb, index) => (
<>
{index !== 0 ? (
<Icon
key={`${index}-icon`}
icon="chevron-right"
className="size-3"
/>
) : null}
<span key={index} className="line-clamp-1">
{crumb}
</span>
</>
))}
</div>
) : null}
<HighlightQuery query={query} text={item.title} />
Expand Down
25 changes: 19 additions & 6 deletions packages/gitbook/src/components/Search/server-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server';

import { resolvePageId } from '@/lib/pages';
import { findSiteSpaceById } from '@/lib/sites';
import { type AncestorRevisionPage, resolvePageId } from '@/lib/pages';
import { findSiteSpaceById, getSiteStructureSections } from '@/lib/sites';
import { filterOutNullable } from '@/lib/typescript';
import { getV1BaseContext } from '@/lib/v1';
import type {
Expand All @@ -10,6 +10,7 @@ import type {
SearchAIRecommendedQuestionStream,
SearchPageResult,
SearchSpaceResult,
SiteSection,
Space,
} from '@gitbook/api';
import type { GitBookBaseContext, GitBookSiteContext } from '@v2/lib/context';
Expand Down Expand Up @@ -48,6 +49,9 @@ export interface ComputedPageResult {

/** When part of a multi-spaces search, the title of the space */
spaceTitle?: string;
section?: SiteSection;

ancestors: AncestorRevisionPage[];
}

export interface AskAnswerSource {
Expand Down Expand Up @@ -258,15 +262,21 @@ async function searchSiteContent(
return (
await Promise.all(
searchResults.map(async (spaceItem) => {
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
const siteSpace = findSiteSpaceById(structure, spaceItem.id);
const siteSection = sections.find((i) => i.id === siteSpace?.section);

return Promise.all(
spaceItem.pages.map((pageItem) =>
transformSitePageResult(context, {
pageItem,
spaceItem,
space: siteSpace?.space,
space:
!siteSection || siteSection.siteSpaces.length > 1
? siteSpace?.space
: undefined,
spaceURL: siteSpace?.urls.published,
siteSection: siteSection ?? undefined,
})
)
);
Expand Down Expand Up @@ -348,9 +358,10 @@ async function transformSitePageResult(
spaceItem: SearchSpaceResult;
space?: Space;
spaceURL?: string;
siteSection?: SiteSection;
}
): Promise<OrderedComputedResult[]> {
const { pageItem, spaceItem, space, spaceURL } = args;
const { pageItem, spaceItem, space, spaceURL, siteSection } = args;
const { linker } = context;

const page: ComputedPageResult = {
Expand All @@ -363,9 +374,11 @@ async function transformSitePageResult(
spaceTitle: space?.title,
pageId: pageItem.id,
spaceId: spaceItem.id,
section: siteSection,
ancestors: [], // @TODO: empty for now until we calculate performantly
};

const sections = await Promise.all(
const pageSections = await Promise.all(
pageItem.sections?.map<Promise<ComputedSectionResult>>(async (section) => ({
type: 'section',
id: `${page.id}/${section.id}`,
Expand All @@ -379,5 +392,5 @@ async function transformSitePageResult(
})) ?? []
);

return [page, ...sections];
return [page, ...pageSections];
}