1
1
---
2
+ import { render } from " astro:content" ;
3
+ import { getCollection } from " astro:content" ;
2
4
import type { searchItem } from " ~/services/search" ;
3
5
import { searchTerms } from " ~/services/search" ;
4
6
5
- const getContentStaticURL = (path : string , target ? : string ): string | null => {
6
- const rgx = / \/ src\/ . +? \/ (. + )\. . *? / g ;
7
+ /**
8
+ * Infers the static URL of a wiki article from its file path.
9
+ * @param path filepath of article
10
+ * @param target optional target heading
11
+ */
12
+ const getContentStaticURL = (path : string , target ? : string ): string => {
13
+ const rgx = / src\/ . +? \/ (. + )\. . *? / g ;
14
+
15
+ console .log (path );
7
16
8
17
const result = rgx .exec (path );
9
- if (result === null ) return null ;
18
+ if (result === null ) throw new Error ( ` Failed to infer static URL of ${ path } ` ) ;
10
19
11
20
if (target ) {
12
21
return ` /${result [1 ]}/#${target } ` ;
@@ -15,38 +24,36 @@ const getContentStaticURL = (path: string, target?: string): string | null => {
15
24
}
16
25
};
17
26
18
- let searchTags: searchItem [] = [];
27
+ const searchTags: searchItem [] = [... searchTerms ];
19
28
20
- if (searchTags .length === 0 ) {
21
- const pages = await Astro . glob ( " ../../content/**/*.mdx " );
29
+ if (searchTags .length === searchTerms . length ) {
30
+ const pages = await getCollection ( " wiki " );
22
31
23
32
for (const page of pages ) {
24
- const frontmatter = page .frontmatter ;
25
- const headings = page .getHeadings ();
26
-
27
- const href = getContentStaticURL (page .file );
28
- if (! href ) {
29
- throw new Error (` Failed to get static url of ${page .url } ` );
33
+ if (! page .filePath ) {
34
+ throw new Error (` Page ${page .id } does not have a file path} ` );
30
35
}
31
36
32
- // Add article title to search options
33
- searchTags .push ({ text: page .frontmatter .title , href });
37
+ const { headings } = await render (page );
38
+
39
+ const href = getContentStaticURL (page .filePath );
40
+
41
+ // Add article title to search options
42
+ searchTags .push ({ text: page .data .title , href });
34
43
35
44
// Add custom article tags to search options
36
- for (const tag of frontmatter .tags ?? []) {
45
+ for (const tag of page . data .tags ?? []) {
37
46
searchTags .push ({ text: tag , href });
38
47
}
39
48
40
49
// Add article headings to search options
41
50
for (const heading of headings ) {
42
- const href = getContentStaticURL (page .file , heading .slug );
43
- if (! href ) {
44
- throw new Error (` Failed to get static url of ${page .url } ` );
45
- }
46
- searchTags .push ({ text: heading .text , href });
51
+ searchTags .push ({
52
+ text: heading .text ,
53
+ href: getContentStaticURL (page .filePath , heading .slug ),
54
+ });
47
55
}
48
56
}
49
- searchTags = searchTags .concat (searchTerms );
50
57
}
51
58
---
52
59
0 commit comments