| 400 | length ? ((n % length) + length) % length : n |
| 401 | |
| 402 | const resultsOf = ( |
| 403 | loaded: Loaded, |
| 404 | query: string, |
| 405 | scope: number | undefined |
| 406 | ): Results => { |
| 407 | const { index } = loaded |
| 408 | const inScope = (page: number) => |
| 409 | scope === undefined || index.pages[page].section === scope |
| 410 | if (!query.trim()) { |
| 411 | // before typing, the scope's pages |
| 412 | const groups = new Map<number, Group>() |
| 413 | let count = 0 |
| 414 | for (const [i, page] of index.pages.entries()) { |
| 415 | if ( |
| 416 | scope === undefined ? |
| 417 | unscoped.has(index.sections[page.section]) |
| 418 | : page.section !== scope |
| 419 | ) |
| 420 | continue |
| 421 | count++ |
| 422 | const group = groups.get(page.section) ?? { |
| 423 | label: index.sections[page.section], |
| 424 | hits: [] |
| 425 | } |
| 426 | groups.set(page.section, group) |
| 427 | if (group.hits.length < hitsPerGroup * 2) { |
| 428 | group.hits.push({ |
| 429 | key: `search-page-${i}`, |
| 430 | href: page.url, |
| 431 | page: true, |
| 432 | title: page.title, |
| 433 | snippet: page.description |
| 434 | }) |
| 435 | } |
| 436 | } |
| 437 | return { groups: [...groups.values()], count } |
| 438 | } |
| 439 | const results = loaded.search.search(query, { |
| 440 | filter: result => inScope(result.page as number), |
| 441 | boostDocument: boostOf(loaded, query, homeSectionOf(loaded)) |
| 442 | }) |
| 443 | const groups = new Map<number, Group>() |
| 444 | for (const result of results) { |
| 445 | const entry = index.entries[result.id as number] |
| 446 | const page = index.pages[entry.page] |
| 447 | let group = groups.get(entry.page) |
| 448 | if (!group) { |
| 449 | if (groups.size === maxGroups) continue |
| 450 | group = { |
| 451 | label: `${index.sections[page.section]} / ${page.title}`, |
| 452 | hits: [] |
| 453 | } |
| 454 | groups.set(entry.page, group) |
| 455 | } |
| 456 | if (group.hits.length === hitsPerGroup) continue |
| 457 | group.hits.push({ |
| 458 | key: `search-hit-${result.id}`, |
| 459 | href: entry.id ? `${page.url}#${entry.id}` : page.url, |