diff --git a/.changeset/rnd-11356-remove-gbo-bestsection.md b/.changeset/rnd-11356-remove-gbo-bestsection.md new file mode 100644 index 000000000..d0adb74b5 --- /dev/null +++ b/.changeset/rnd-11356-remove-gbo-bestsection.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Remove GBO's redundant re-selection of the best-scoring search section. The search API now returns a single highest-scoring section per page (and orders sections highest-score-first), so GBO no longer needs its own `getBestScoredResult` helper to pick the best section for the search and MCP previews. No user-visible change. diff --git a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts index fa319c0f2..1e98525cd 100644 --- a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts +++ b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/mcp/handler.ts @@ -11,7 +11,6 @@ import { getExposableError, throwIfDataError } from '@/lib/data'; import { fromPageMarkdown, getMarkdownForPageInSpace, toPageMarkdown } from '@/lib/markdownPage'; import { resolvePagePath } from '@/lib/pages'; import { joinPathWithBaseURL } from '@/lib/paths'; -import { getBestScoredResult } from '@/lib/search'; import { findSiteSpaceBy, findSiteSpaceByUrl } from '@/lib/sites'; import { trackServerInsightsEvents } from '@/lib/tracking'; import { waitUntil } from '@/lib/waitUntil'; @@ -135,8 +134,10 @@ export async function handleMcpRequest( ) ); - const body = getBestScoredResult( - (pageResult.sections ?? []).filter((section) => section.body) + // The search API returns sections ordered highest-score-first, so + // the first section with a body is the best-scoring preview. + const body = (pageResult.sections ?? []).find( + (section) => section.body )?.body; return { diff --git a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/route.ts b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/route.ts index e55e729d7..92f36e83b 100644 --- a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/route.ts +++ b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/search/route.ts @@ -8,7 +8,6 @@ import { throwIfDataError } from '@/lib/data'; import { toEmbeddableLinkForPublishedContent } from '@/lib/embeddable-linker'; import { getSiteURLDataFromMiddleware } from '@/lib/middleware'; import { joinPathWithBaseURL } from '@/lib/paths'; -import { getBestScoredResult } from '@/lib/search'; import { getServerActionBaseContext } from '@/lib/server-actions'; import { findSiteSpaceBy, getLocalizedTitle } from '@/lib/sites'; import type { @@ -187,8 +186,9 @@ function transformSitePageResult(args: { }; }) ?? []; - // Find the best-scoring section to use as a body preview on the page result. - const bestSection = getBestScoredResult(pageSections); + // The search API returns each page's sections ordered highest-score-first and caps them at one + // per page, so the first section is the best-scoring one to use as a body preview. + const bestSection = pageSections[0]; if (bestSection) { page.bestSection = { href: bestSection.href, diff --git a/packages/gitbook/src/lib/search.ts b/packages/gitbook/src/lib/search.ts deleted file mode 100644 index 14acd8e24..000000000 --- a/packages/gitbook/src/lib/search.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Return the highest-scoring item in the list, or undefined when empty. - */ -export function getBestScoredResult( - items: readonly T[] -): T | undefined { - return items.reduce( - (best, item) => (best === undefined || item.score > best.score ? item : best), - undefined - ); -}