|
1 | | -import type { DocsPage } from '@/lib/content/types' |
| 1 | +import { findNavigationGroup } from '@/lib/content/navigation' |
| 2 | +import type { ContentCatalog, DocsPage } from '@/lib/content/types' |
2 | 3 | import { pageSearchChunks } from '@/lib/search/chunks' |
3 | | -import type { SearchIndex } from '@/lib/search/types' |
| 4 | +import type { SearchIndex, SearchPage } from '@/lib/search/types' |
4 | 5 | import { normalizeSearchText, searchCatalogHash } from '@/lib/search/utils' |
5 | 6 |
|
6 | 7 | const MAX_SEARCH_INDEX_BYTES = 64 * 1024 * 1024 |
| 8 | +const MAX_SEARCH_CATALOG_BYTES = 4 * 1024 * 1024 |
| 9 | +const SECTION_LABELS = { docs: 'Documentation', api: 'API reference', cli: 'CLI', mcp: 'MCP' } |
| 10 | + |
| 11 | +/** Search citations need document identity and navigation, without loading document bodies. */ |
| 12 | +export function buildSearchCatalog(catalog: ContentCatalog): SearchPage[] { |
| 13 | + const pages: SearchPage[] = [] |
| 14 | + let bytes = 0 |
| 15 | + for (const page of catalog.pages) { |
| 16 | + if (page.url === '/') continue |
| 17 | + const section = SECTION_LABELS[page.section] |
| 18 | + const group = findNavigationGroup(catalog.navigation[page.section], page.url) |
| 19 | + const entry: SearchPage = { |
| 20 | + id: page.id, |
| 21 | + url: page.url, |
| 22 | + title: page.title, |
| 23 | + contentHash: page.contentHash, |
| 24 | + headings: page.headings.map((heading) => heading.id), |
| 25 | + breadcrumbs: [ |
| 26 | + section, |
| 27 | + ...(group && group !== section && group !== page.title ? [group] : []), |
| 28 | + page.title, |
| 29 | + ], |
| 30 | + } |
| 31 | + bytes += Buffer.byteLength(JSON.stringify(entry)) + 1 |
| 32 | + if (bytes > MAX_SEARCH_CATALOG_BYTES - 2) |
| 33 | + throw new Error('Generated search catalog exceeds the 4 MiB size budget') |
| 34 | + pages.push(entry) |
| 35 | + } |
| 36 | + return pages |
| 37 | +} |
7 | 38 |
|
8 | 39 | /** Build-time parsing keeps the public search cold path independent of corpus Markdown size. */ |
9 | 40 | export function buildSearchIndex(pages: DocsPage[]): SearchIndex { |
|
0 commit comments