Skip to content

Commit 9163724

Browse files
committed
[search] Override Algolia DocSearch config with lang facetFilters
1 parent 69755f3 commit 9163724

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

astro.config.mjs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-check
2-
import { defineConfig } from "astro/config";
2+
import { defineConfig, envField } from "astro/config";
33
import starlight from "@astrojs/starlight";
44
import tailwindcss from "@tailwindcss/vite";
55
import starlightOpenAPI, { openAPISidebarGroups } from "starlight-openapi";
@@ -71,9 +71,7 @@ export default defineConfig({
7171
...(hasAlgoliaConfig
7272
? [
7373
starlightDocSearch({
74-
appId: ALGOLIA_APP_ID,
75-
apiKey: ALGOLIA_SEARCH_API_KEY,
76-
indexName: ALGOLIA_INDEX_NAME,
74+
clientOptionsModule: "./src/config/docsearch.ts",
7775
}),
7876
]
7977
: []),
@@ -128,4 +126,14 @@ export default defineConfig({
128126
remarkPlugins: [remarkMath],
129127
rehypePlugins: [rehypeKatex],
130128
},
129+
env: {
130+
schema: {
131+
ALGOLIA_APP_ID: envField.string({ context: "client", access: "public" }),
132+
ALGOLIA_SEARCH_API_KEY: envField.string({
133+
context: "client",
134+
access: "public",
135+
}),
136+
ALGOLIA_INDEX_NAME: envField.string({ context: "client", access: "public" }),
137+
},
138+
},
131139
});

src/config/docsearch.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { DocSearchClientOptions } from "@astrojs/starlight-docsearch";
2+
import { ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX_NAME } from "astro:env/client";
3+
4+
const getFacetFilters = (): string[] =>
5+
typeof document !== "undefined" ? [`lang:${document.documentElement.lang}`] : [];
6+
7+
const getEnvVar = (key: string, value: string | undefined): string => {
8+
if (!value) {
9+
throw new Error(`Missing environment variable: ${key}`);
10+
}
11+
return value;
12+
};
13+
14+
export default Object.freeze({
15+
appId: getEnvVar("ALGOLIA_APP_ID", ALGOLIA_APP_ID as string | undefined),
16+
apiKey: getEnvVar("ALGOLIA_SEARCH_API_KEY", ALGOLIA_SEARCH_API_KEY as string | undefined),
17+
indexName: getEnvVar("ALGOLIA_INDEX_NAME", ALGOLIA_INDEX_NAME as string | undefined),
18+
searchParameters: {
19+
get facetFilters() {
20+
return getFacetFilters();
21+
},
22+
},
23+
} satisfies DocSearchClientOptions);

src/starlight-overrides/Head.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Default from "@astrojs/starlight/components/Head.astro";
44
import AnalyticsComponent from "@vercel/analytics/astro";
55
import type { AnalyticsProps } from "@vercel/analytics";
66
import type { JSX } from "astro/jsx-runtime";
7+
const { lang } = Astro.props;
78
89
// This is workaround for wrongly typed Analytics component from @vercel/analytics/astro
910
const Analytics = AnalyticsComponent as unknown as (
@@ -13,3 +14,5 @@ const Analytics = AnalyticsComponent as unknown as (
1314

1415
<Default {...Astro.props}><slot /></Default>
1516
<Analytics />
17+
<!-- Algolia docsearch language facet -->
18+
<meta name="docsearch:language" content={lang} />

0 commit comments

Comments
 (0)