Skip to content
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

Add breadcrumbs to search results #2772

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 33 additions & 2 deletions packages/gitbook/src/components/Search/SearchPageResultItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Icon } from '@gitbook/icons';
import React from 'react';

import { AncestorRevisionPage } from '@/lib/pages';
import { tcls } from '@/lib/tailwind';

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

const breadcrumbs = item.ancestors.map((ancestor) => ancestor.title);
if (item.spaceTitle) {
breadcrumbs.unshift(item.spaceTitle);
}

return (
<Link
ref={ref}
Expand Down Expand Up @@ -46,7 +52,7 @@ 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',
Expand All @@ -55,9 +61,34 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
'uppercase',
'tracking-wider',
'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
8 changes: 7 additions & 1 deletion packages/gitbook/src/components/Search/server-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
import * as React from 'react';
import { assert } from 'ts-essentials';

import { fetchPageData } from '@/app/middleware/(site)/fetch';
import { streamResponse } from '@/lib/actions';
import * as api from '@/lib/api';
import { getAbsoluteHref, getPageHref } from '@/lib/links';
import { resolvePageId } from '@/lib/pages';
import { AncestorRevisionPage, resolvePageId } from '@/lib/pages';
import { filterOutNullable } from '@/lib/typescript';
import { getSiteStructureSections } from '@/lib/utils';

Expand All @@ -38,6 +39,8 @@ export interface ComputedPageResult {

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

ancestors: AncestorRevisionPage[];
}

export interface AskAnswerSource {
Expand Down Expand Up @@ -320,12 +323,15 @@ async function transformSectionsAndPage(args: {
})) ?? [],
);

const pageData = await fetchPageData({ pathname: [item.path] });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function correctly gets the page data when the page is in the same space as we're currently searching in, but doesn't work when we need to search across sections/variants. Any suggestions to make it work are welcome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use this function as it's tied to the current context, and generally, it'll make things slower by fetching a lot of data to render search results.

We need to think about a different approach, potentially by changing our API, because otherwise, this PR could badly impact performances.


const page: ComputedPageResult = {
type: 'page',
id: item.id,
title: item.title,
href: await getURLWithSections(item.path, spaceURL),
spaceTitle: space?.title,
ancestors: pageData.ancestors,
};

return [page, sections];
Expand Down
Loading