| 54 | let loading: Promise<Loaded> | undefined |
| 55 | |
| 56 | export const load = (): Promise<Loaded> => |
| 57 | (loading ??= fetch(withBase("/search.json")) |
| 58 | .then(res => res.json() as Promise<SearchIndex>) |
| 59 | .then(index => { |
| 60 | const names = index.pages.map(page => ({ |
| 61 | terms: termsOf( |
| 62 | index.pages.some(other => other.url.startsWith(`${page.url}/`)) ? |
| 63 | index.sections[page.section] |
| 64 | : page.title |
| 65 | ), |
| 66 | keys: termsOf( |
| 67 | `${compact(page.title)} ${compact(page.url.split("/").at(-1)!)}` |
| 68 | ) |
| 69 | })) |
| 70 | // every page gets an entry of its own, even one that opens on a heading |
| 71 | const covered = new Set( |
| 72 | index.entries.filter(entry => !entry.id).map(entry => entry.page) |
| 73 | ) |
| 74 | for (const page of index.pages.keys()) { |
| 75 | if (!covered.has(page)) |
| 76 | index.entries.push({ page, heading: "", id: "", text: "" }) |
| 77 | } |
| 78 | const search = new MiniSearch({ |
| 79 | fields: ["heading", "text", "title", "section", "key", "description"], |
| 80 | storeFields: ["page", "top", "name"], |
| 81 | processTerm: fold, |
| 82 | searchOptions: { |
| 83 | prefix: true, |
| 84 | fuzzy: 0.15, |
| 85 | combineWith: "AND", |
| 86 | boost: { |
| 87 | heading: 3, |
| 88 | key: 3, |
| 89 | description: 1.5, |
| 90 | title: 1.5 |
| 91 | } |
| 92 | } |
| 93 | }) |
| 94 | search.addAll( |
| 95 | index.entries.map((entry, id) => { |
| 96 | const page = index.pages[entry.page] |
| 97 | const top = !entry.id |
| 98 | return { |
| 99 | id, |
| 100 | page: entry.page, |
| 101 | top, |
| 102 | name: termsOf(entry.heading.split(" · ").at(-1)!).join(" "), |
| 103 | heading: entry.heading, |
| 104 | text: entry.text, |
| 105 | title: page.title, |
| 106 | section: index.sections[page.section], |
| 107 | key: top ? names[entry.page].keys.join(" ") : "", |
| 108 | description: top ? page.description : "" |
| 109 | } |
| 110 | }) |
| 111 | ) |
| 112 | return { index, search, names } |
| 113 | })) |