|
1 | 1 | --- |
2 | 2 | import { render } from "astro:content"; |
3 | 3 | import { getCollection } from "astro:content"; |
| 4 | +import path from "path"; |
4 | 5 | import type { searchItem } from "~/services/search"; |
5 | 6 | import { searchTerms } from "~/services/search"; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Infers the static URL of a wiki article from its file path. |
9 | | - * @param path filepath of article |
| 10 | + * @param filepath filepath of article |
10 | 11 | * @param target optional target heading |
11 | 12 | */ |
12 | | -const getContentStaticURL = (path: string, target?: string): string => { |
13 | | - const rgx = /src\/.+?\/(.+)\..*?/g; |
| 13 | +const getContentStaticURL = (filepath: string, target?: string): string => { |
| 14 | + const relative = path.relative("src/content", filepath); |
| 15 | + const extension = path.extname(relative); |
14 | 16 |
|
15 | | - console.log(path); |
16 | | -
|
17 | | - const result = rgx.exec(path); |
18 | | - if (result === null) throw new Error(`Failed to infer static URL of ${path}`); |
| 17 | + const absolute = `/${relative.replace(extension, "")}/`; |
19 | 18 |
|
20 | 19 | if (target) { |
21 | | - return `/${result[1]}/#${target}`; |
| 20 | + return absolute + "#" + target |
22 | 21 | } else { |
23 | | - return `/${result[1]}/`; |
| 22 | + return absolute |
24 | 23 | } |
25 | 24 | }; |
26 | 25 |
|
27 | | -const searchTags: searchItem[] = [...searchTerms]; |
| 26 | +const needles: searchItem[] = [...searchTerms]; |
28 | 27 |
|
29 | | -if (searchTags.length === searchTerms.length) { |
| 28 | +const loadNeedles = async () => { |
30 | 29 | const pages = await getCollection("wiki"); |
31 | 30 |
|
| 31 | + const promises = []; |
| 32 | +
|
32 | 33 | for (const page of pages) { |
33 | | - if (!page.filePath) { |
34 | | - throw new Error(`Page ${page.id} does not have a file path}`); |
35 | | - } |
| 34 | + promises.push( |
| 35 | + new Promise(async (resolve, reject) => { |
| 36 | + if (!page.filePath) { |
| 37 | + return reject( |
| 38 | + `Page ${page.id} does not have a file path. This shouldn't ever happen unless something has gone wrong with Astro.` |
| 39 | + ); |
| 40 | + } |
36 | 41 |
|
37 | | - const { headings } = await render(page); |
| 42 | + const { headings } = await render(page); |
38 | 43 |
|
39 | | - const href = getContentStaticURL(page.filePath); |
| 44 | + const href = getContentStaticURL(page.filePath); |
40 | 45 |
|
41 | | - // Add article title to search options |
42 | | - searchTags.push({ text: page.data.title, href }); |
| 46 | + // Article title entry |
| 47 | + needles.push({ text: page.data.title, href }); |
43 | 48 |
|
44 | | - // Add custom article tags to search options |
45 | | - for (const tag of page.data.tags ?? []) { |
46 | | - searchTags.push({ text: tag, href }); |
47 | | - } |
| 49 | + // Article tags |
| 50 | + for (const tag of page.data.tags ?? []) { |
| 51 | + needles.push({ text: tag, href }); |
| 52 | + } |
48 | 53 |
|
49 | | - // Add article headings to search options |
50 | | - for (const heading of headings) { |
51 | | - searchTags.push({ |
52 | | - text: heading.text, |
53 | | - href: getContentStaticURL(page.filePath, heading.slug), |
54 | | - }); |
55 | | - } |
| 54 | + // Article headings |
| 55 | + for (const heading of headings) { |
| 56 | + needles.push({ |
| 57 | + text: heading.text, |
| 58 | + href: getContentStaticURL(page.filePath, heading.slug), |
| 59 | + }); |
| 60 | + } |
| 61 | +
|
| 62 | + resolve(true); |
| 63 | + }) |
| 64 | + ); |
56 | 65 | } |
57 | | -} |
| 66 | +
|
| 67 | + await Promise.all(promises); |
| 68 | +}; |
| 69 | +
|
| 70 | +await loadNeedles(); |
58 | 71 | --- |
59 | 72 |
|
60 | 73 | <div id="site-search"> |
61 | 74 | <dialog> |
62 | 75 | <input type="search" placeholder="Search..." autofocus /> |
63 | 76 | <div class="suggestions" tabindex="-1"> |
64 | 77 | { |
65 | | - searchTags.map((entry) => ( |
| 78 | + needles.map((entry) => ( |
66 | 79 | <a |
67 | 80 | href={entry.href} |
68 | 81 | target={entry.href.startsWith("/") ? undefined : "_blank"} |
|
0 commit comments