|
1 | 1 | import { z } from 'astro/zod';
|
2 | 2 |
|
| 3 | +const indexWeightSchema = z.number().nonnegative().optional(); |
| 4 | +const pagefindRankingWeightsSchema = z.object({ |
| 5 | + /** |
| 6 | + * Set Pagefind’s `pageLength` ranking option. |
| 7 | + * |
| 8 | + * The default value is `0.1` and values must be in the range `0` to `1`. |
| 9 | + * |
| 10 | + * @see https://pagefind.app/docs/ranking/#configuring-page-length |
| 11 | + */ |
| 12 | + pageLength: z.number().min(0).max(1).default(0.1), |
| 13 | + /** |
| 14 | + * Set Pagefind’s `termFrequency` ranking option. |
| 15 | + * |
| 16 | + * The default value is `0.1` and values must be in the range `0` to `1`. |
| 17 | + * |
| 18 | + * @see https://pagefind.app/docs/ranking/#configuring-term-frequency |
| 19 | + */ |
| 20 | + termFrequency: z.number().min(0).max(1).default(0.1), |
| 21 | + /** |
| 22 | + * Set Pagefind’s `termSaturation` ranking option. |
| 23 | + * |
| 24 | + * The default value is `2` and values must be in the range `0` to `2`. |
| 25 | + * |
| 26 | + * @see https://pagefind.app/docs/ranking/#configuring-term-saturation |
| 27 | + */ |
| 28 | + termSaturation: z.number().min(0).max(2).default(2), |
| 29 | + /** |
| 30 | + * Set Pagefind’s `termSimilarity` ranking option. |
| 31 | + * |
| 32 | + * The default value is `9` and values must be greater than or equal to `0`. |
| 33 | + * |
| 34 | + * @see https://pagefind.app/docs/ranking/#configuring-term-similarity |
| 35 | + */ |
| 36 | + termSimilarity: z.number().min(0).default(9), |
| 37 | +}); |
| 38 | +const pagefindIndexOptionsSchema = z.object({ |
| 39 | + /** |
| 40 | + * Overrides the URL path that Pagefind uses to load its search bundle |
| 41 | + */ |
| 42 | + basePath: z.string().optional(), |
| 43 | + /** |
| 44 | + * Appends the given baseURL to all search results. May be a path, or a full domain |
| 45 | + */ |
| 46 | + baseUrl: z.string().optional(), |
| 47 | + /** |
| 48 | + * Multiply all rankings for this index by the given weight. |
| 49 | + * |
| 50 | + * @see https://pagefind.app/docs/multisite/#changing-the-weighting-of-individual-indexes |
| 51 | + */ |
| 52 | + indexWeight: indexWeightSchema, |
| 53 | + /** |
| 54 | + * Apply this filter configuration to all search results from this index. |
| 55 | + * |
| 56 | + * Only applies in multisite setups. |
| 57 | + * |
| 58 | + * @see https://pagefind.app/docs/multisite/#filtering-results-by-index |
| 59 | + */ |
| 60 | + mergeFilter: z.record(z.string(), z.string().or(z.array(z.string()).nonempty())).optional(), |
| 61 | + /** |
| 62 | + * Language of this index. |
| 63 | + * |
| 64 | + * @see https://pagefind.app/docs/multisite/#merging-a-specific-language-index |
| 65 | + */ |
| 66 | + language: z.string().optional(), |
| 67 | + /** |
| 68 | + * Configure how search result rankings are calculated by Pagefind. |
| 69 | + */ |
| 70 | + ranking: pagefindRankingWeightsSchema.optional(), |
| 71 | +}); |
| 72 | + |
3 | 73 | const pagefindSchema = z.object({
|
| 74 | + /** |
| 75 | + * Configure how search results from the current website are weighted by Pagefind |
| 76 | + * compared to results from other sites when using the `mergeIndex` option. |
| 77 | + * |
| 78 | + * @see https://pagefind.app/docs/multisite/#changing-the-weighting-of-individual-indexes |
| 79 | + */ |
| 80 | + indexWeight: indexWeightSchema, |
4 | 81 | /** Configure how search result rankings are calculated by Pagefind. */
|
5 |
| - ranking: z |
6 |
| - .object({ |
7 |
| - /** |
8 |
| - * Set Pagefind’s `pageLength` ranking option. |
9 |
| - * |
10 |
| - * The default value is `0.1` and values must be in the range `0` to `1`. |
11 |
| - * |
12 |
| - * @see https://pagefind.app/docs/ranking/#configuring-page-length |
13 |
| - */ |
14 |
| - pageLength: z.number().min(0).max(1).default(0.1), |
15 |
| - /** |
16 |
| - * Set Pagefind’s `termFrequency` ranking option. |
17 |
| - * |
18 |
| - * The default value is `0.1` and values must be in the range `0` to `1`. |
19 |
| - * |
20 |
| - * @see https://pagefind.app/docs/ranking/#configuring-term-frequency |
21 |
| - */ |
22 |
| - termFrequency: z.number().min(0).max(1).default(0.1), |
23 |
| - /** |
24 |
| - * Set Pagefind’s `termSaturation` ranking option. |
25 |
| - * |
26 |
| - * The default value is `2` and values must be in the range `0` to `2`. |
27 |
| - * |
28 |
| - * @see https://pagefind.app/docs/ranking/#configuring-term-saturation |
29 |
| - */ |
30 |
| - termSaturation: z.number().min(0).max(2).default(2), |
| 82 | + ranking: pagefindRankingWeightsSchema.default({}), |
| 83 | + /** |
| 84 | + * Configure how search indexes from different sites are merged by Pagefind. |
| 85 | + * |
| 86 | + * @see https://pagefind.app/docs/multisite/#searching-additional-sites-from-pagefind-ui |
| 87 | + */ |
| 88 | + mergeIndex: z |
| 89 | + .array( |
31 | 90 | /**
|
32 |
| - * Set Pagefind’s `termSimilarity` ranking option. |
33 |
| - * |
34 |
| - * The default value is `9` and values must be greater than or equal to `0`. |
| 91 | + * Each entry of this array represents a `PagefindIndexOptions` from pagefind. |
35 | 92 | *
|
36 |
| - * @see https://pagefind.app/docs/ranking/#configuring-term-similarity |
| 93 | + * @see https://github.com/CloudCannon/pagefind/blob/v1.3.0/pagefind_web_js/lib/coupled_search.ts#L549 |
37 | 94 | */
|
38 |
| - termSimilarity: z.number().min(0).default(9), |
39 |
| - }) |
40 |
| - .default({}), |
| 95 | + pagefindIndexOptionsSchema.extend({ |
| 96 | + /** |
| 97 | + * Set Pagefind’s `bundlePath` mergeIndex option. |
| 98 | + * |
| 99 | + * @see https://pagefind.app/docs/multisite/#searching-additional-sites-from-pagefind-ui |
| 100 | + */ |
| 101 | + bundlePath: z.string(), |
| 102 | + }) |
| 103 | + ) |
| 104 | + .optional(), |
41 | 105 | });
|
42 | 106 |
|
43 | 107 | export const PagefindConfigSchema = () => pagefindSchema;
|
|
0 commit comments