Skip to content

Commit 2df9d05

Browse files
XREvodelucis
andauthored
Support multisite search on pagefind (#2858)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 9e48513 commit 2df9d05

File tree

3 files changed

+121
-35
lines changed

3 files changed

+121
-35
lines changed

.changeset/dry-geese-pretend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/starlight": minor
3+
---
4+
5+
Adds support for Pagefind’s multisite search features

docs/src/content/docs/reference/configuration.mdx

+19-2
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,10 @@ This will also hide the default search UI if in use.
457457

458458
Pagefind cannot be enabled when the [`prerender`](#prerender) option is set to `false`.
459459

460-
Set `pagefind` to an object to configure the Pagefind search client.
461-
See [“Customize Pagefind's result ranking”](https://pagefind.app/docs/ranking/) in the Pagefind documentation for more details about using the `pagefind.ranking` option to control how search result ranking is calculated.
460+
Set `pagefind` to an object to configure the Pagefind search client:
461+
462+
- See [“Customize Pagefind's result ranking”](https://pagefind.app/docs/ranking/) in the Pagefind documentation for more details about using the `pagefind.ranking` option to control how search result ranking is calculated
463+
- See [“Searching multiple sites”](https://pagefind.app/docs/multisite/) in the Pagefind documentation for more details about using the `pagefind.mergeIndex` option to control how to search accros multiple sites
462464

463465
#### `PagefindOptions`
464466

@@ -470,6 +472,21 @@ interface PagefindOptions {
470472
termSaturation?: number;
471473
termSimilarity?: number;
472474
};
475+
indexWeight?: number;
476+
mergeIndex?: Array<{
477+
bundlePath: string;
478+
indexWeight?: number;
479+
basePath?: string;
480+
baseUrl?: string;
481+
mergeFilter?: Record<string, string | string[]>;
482+
language?: string;
483+
ranking?: {
484+
pageLength?: number;
485+
termFrequency?: number;
486+
termSaturation?: number;
487+
termSimilarity?: number;
488+
};
489+
}>;
473490
}
474491
```
475492

packages/starlight/schemas/pagefind.ts

+97-33
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,107 @@
11
import { z } from 'astro/zod';
22

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+
373
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,
481
/** 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(
3190
/**
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.
3592
*
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
3794
*/
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(),
41105
});
42106

43107
export const PagefindConfigSchema = () => pagefindSchema;

0 commit comments

Comments
 (0)