Skip to content

Commit 3e8a040

Browse files
fix: remove unneeded files
1 parent 7efd234 commit 3e8a040

File tree

8 files changed

+377
-299
lines changed

8 files changed

+377
-299
lines changed

docs/src/content/docs/guides/site-search.mdx

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,36 +199,121 @@ Add translations of the modal UI for your language using Starlight’s built-in
199199

200200
```json title="src/content/i18n/en.json"
201201
{
202-
"docsearch.searchBox.resetButtonTitle": "Clear the query",
203-
"docsearch.searchBox.resetButtonAriaLabel": "Clear the query",
204-
"docsearch.searchBox.cancelButtonText": "Cancel",
205-
"docsearch.searchBox.cancelButtonAriaLabel": "Cancel",
202+
"docsearch.searchBox.clearButtonTitle": "Clear the query",
203+
"docsearch.searchBox.clearButtonAriaLabel": "Clear the query",
204+
"docsearch.searchBox.closeButtonText": "Close",
205+
"docsearch.searchBox.closeButtonAriaLabel": "Close",
206+
"docsearch.searchBox.placeholderText": "Search docs",
207+
"docsearch.searchBox.placeholderTextAskAi": "Ask AI: ",
208+
"docsearch.searchBox.placeholderTextAskAiStreaming": "Answering…",
206209
"docsearch.searchBox.searchInputLabel": "Search",
210+
"docsearch.searchBox.backToKeywordSearchButtonText": "Back to keyword search",
211+
"docsearch.searchBox.backToKeywordSearchButtonAriaLabel": "Back to keyword search",
207212

208213
"docsearch.startScreen.recentSearchesTitle": "Recent",
209214
"docsearch.startScreen.noRecentSearchesText": "No recent searches",
210215
"docsearch.startScreen.saveRecentSearchButtonTitle": "Save this search",
211216
"docsearch.startScreen.removeRecentSearchButtonTitle": "Remove this search from history",
212217
"docsearch.startScreen.favoriteSearchesTitle": "Favorite",
213218
"docsearch.startScreen.removeFavoriteSearchButtonTitle": "Remove this search from favorites",
219+
"docsearch.startScreen.recentConversationsTitle": "Recent conversations",
220+
"docsearch.startScreen.removeRecentConversationButtonTitle": "Remove this conversation from history",
214221

215222
"docsearch.errorScreen.titleText": "Unable to fetch results",
216223
"docsearch.errorScreen.helpText": "You might want to check your network connection.",
217224

218225
"docsearch.footer.selectText": "to select",
226+
"docsearch.footer.submitQuestionText": "Submit question",
219227
"docsearch.footer.selectKeyAriaLabel": "Enter key",
220228
"docsearch.footer.navigateText": "to navigate",
221229
"docsearch.footer.navigateUpKeyAriaLabel": "Arrow up",
222230
"docsearch.footer.navigateDownKeyAriaLabel": "Arrow down",
223231
"docsearch.footer.closeText": "to close",
232+
"docsearch.footer.backToSearchText": "Back to search",
224233
"docsearch.footer.closeKeyAriaLabel": "Escape key",
225-
"docsearch.footer.searchByText": "Search by",
234+
"docsearch.footer.poweredByText": "Search by",
226235

227236
"docsearch.noResultsScreen.noResultsText": "No results for",
228237
"docsearch.noResultsScreen.suggestedQueryText": "Try searching for",
229238
"docsearch.noResultsScreen.reportMissingResultsText": "Believe this query should return results?",
230-
"docsearch.noResultsScreen.reportMissingResultsLinkText": "Let us know."
239+
"docsearch.noResultsScreen.reportMissingResultsLinkText": "Let us know.",
240+
241+
"docsearch.resultsScreen.askAiPlaceholder": "Ask AI: ",
242+
243+
"docsearch.askAiScreen.disclaimerText": "Answers are generated by AI and may be inaccurate.",
244+
"docsearch.askAiScreen.relatedSourcesText": "Related sources",
245+
"docsearch.askAiScreen.thinkingText": "Thinking…",
246+
"docsearch.askAiScreen.copyButtonText": "Copy",
247+
"docsearch.askAiScreen.copyButtonCopiedText": "Copied!",
248+
"docsearch.askAiScreen.copyButtonTitle": "Copy",
249+
"docsearch.askAiScreen.likeButtonTitle": "Like",
250+
"docsearch.askAiScreen.dislikeButtonTitle": "Dislike",
251+
"docsearch.askAiScreen.thanksForFeedbackText": "Thanks for your feedback!",
252+
"docsearch.askAiScreen.preToolCallText": "Searching…",
253+
"docsearch.askAiScreen.duringToolCallText": "Searching ",
254+
"docsearch.askAiScreen.afterToolCallText": "Searched",
255+
"docsearch.askAiScreen.aggregatedToolCallText": "Searched"
231256
}
232257
```
233258

234259
</Steps>
260+
261+
### Algolia Ask&nbsp;AI
262+
263+
DocSearch v4 introduces an optional **Ask AI** conversational experience. To enable it, pass the `askAi` option to the Starlight DocSearch plugin — either as a plain string (your **assistant ID**) or as an object with overrides:
264+
265+
```ts title="astro.config.mjs" ins={15-20}
266+
import { defineConfig } from 'astro/config';
267+
import starlight from '@astrojs/starlight';
268+
import starlightDocSearch from '@astrojs/starlight-docsearch';
269+
270+
export default defineConfig({
271+
integrations: [
272+
starlight({
273+
plugins: [
274+
starlightDocSearch({
275+
appId: 'YOUR_APP_ID',
276+
apiKey: 'YOUR_SEARCH_API_KEY',
277+
indexName: 'YOUR_INDEX_NAME',
278+
// simplest form — just the assistant ID
279+
// askAi: 'YOUR_ASSISTANT_ID',
280+
281+
// or full form with per-assistant overrides
282+
askAi: {
283+
assistantId: 'YOUR_ASSISTANT_ID',
284+
// apiKey, appId & indexName default to the top-level values
285+
// but can be overridden individually if needed
286+
// apiKey: '...',
287+
// appId: '...',
288+
// indexName: '...',
289+
},
290+
}),
291+
],
292+
}),
293+
],
294+
});
295+
```
296+
297+
If you want to stick with keyword search only, simply omit the `askAi` property.
298+
299+
#### Translating the Ask AI UI
300+
301+
Ask AI adds a few extra UI texts. Add them to your `i18n` JSON alongside the earlier DocSearch strings:
302+
303+
```json title="src/content/i18n/en.json"{8}
304+
{
305+
// …previous DocSearch strings…
306+
"docsearch.resultsScreen.askAiPlaceholder": "Ask AI: ",
307+
"docsearch.searchBox.placeholderTextAskAi": "Ask AI: ",
308+
"docsearch.askAiScreen.disclaimerText": "Answers are generated by AI and may be inaccurate.",
309+
"docsearch.askAiScreen.relatedSourcesText": "Related sources",
310+
"docsearch.askAiScreen.thinkingText": "Thinking…",
311+
"docsearch.askAiScreen.copyButtonText": "Copy",
312+
"docsearch.askAiScreen.copyButtonCopiedText": "Copied!",
313+
"docsearch.askAiScreen.thanksForFeedbackText": "Thanks for your feedback!",
314+
"docsearch.footer.submitQuestionText": "Submit question",
315+
"docsearch.footer.backToSearchText": "Back to search"
316+
}
317+
```
318+
319+
See the [Ask AI documentation](https://docsearch.algolia.com/docs/v4/askai) for full details.

packages/docsearch/DocSearch.astro

Lines changed: 157 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,113 @@
11
---
22
import '@docsearch/css/dist/modal.css';
3-
import type docsearch from '@docsearch/js';
43
import './variables.css';
54
6-
type DocSearchTranslationProps = Pick<
7-
Parameters<typeof docsearch>[0],
8-
'placeholder' | 'translations'
9-
>;
5+
export interface DocSearchTranslations {
6+
button?: ButtonTranslations;
7+
modal?: ModalTranslations;
8+
}
9+
10+
export interface ButtonTranslations {
11+
buttonText?: string;
12+
buttonAriaLabel?: string;
13+
}
14+
15+
export interface ModalTranslations extends ScreenStateTranslations {
16+
searchBox?: SearchBoxTranslations;
17+
footer?: FooterTranslations;
18+
}
19+
20+
export interface ScreenStateTranslations {
21+
errorScreen?: ErrorScreenTranslations;
22+
startScreen?: StartScreenTranslations;
23+
resultsScreen?: ResultsScreenTranslations;
24+
noResultsScreen?: NoResultsScreenTranslations;
25+
askAiScreen?: AskAiScreenTranslations;
26+
}
27+
28+
export interface SearchBoxTranslations {
29+
clearButtonTitle?: string;
30+
clearButtonAriaLabel?: string;
31+
closeButtonText?: string;
32+
closeButtonAriaLabel?: string;
33+
placeholderText?: string;
34+
placeholderTextAskAi?: string;
35+
searchInputLabel?: string;
36+
placeholderTextAskAiStreaming?: string;
37+
backToKeywordSearchButtonText?: string;
38+
backToKeywordSearchButtonAriaLabel?: string;
39+
}
40+
41+
export interface FooterTranslations {
42+
selectText?: string;
43+
submitQuestionText?: string;
44+
selectKeyAriaLabel?: string;
45+
navigateText?: string;
46+
navigateUpKeyAriaLabel?: string;
47+
backToSearchText?: string;
48+
navigateDownKeyAriaLabel?: string;
49+
closeText?: string;
50+
closeKeyAriaLabel?: string;
51+
poweredByText?: string;
52+
}
53+
54+
export interface ErrorScreenTranslations {
55+
titleText?: string;
56+
helpText?: string;
57+
}
58+
59+
export interface StartScreenTranslations {
60+
recentSearchesTitle?: string;
61+
noRecentSearchesText?: string;
62+
saveRecentSearchButtonTitle?: string;
63+
removeRecentSearchButtonTitle?: string;
64+
favoriteSearchesTitle?: string;
65+
removeFavoriteSearchButtonTitle?: string;
66+
recentConversationsTitle?: string;
67+
removeRecentConversationButtonTitle?: string;
68+
}
69+
70+
export interface ResultsScreenTranslations {
71+
askAiPlaceholder?: string;
72+
}
73+
74+
export interface NoResultsScreenTranslations {
75+
noResultsText?: string;
76+
suggestedQueryText?: string;
77+
reportMissingResultsText?: string;
78+
reportMissingResultsLinkText?: string;
79+
}
80+
81+
export interface AskAiScreenTranslations {
82+
disclaimerText?: string;
83+
relatedSourcesText?: string;
84+
thinkingText?: string;
85+
copyButtonText?: string;
86+
copyButtonCopiedText?: string;
87+
copyButtonTitle?: string;
88+
likeButtonTitle?: string;
89+
dislikeButtonTitle?: string;
90+
thanksForFeedbackText?: string;
91+
preToolCallText?: string;
92+
duringToolCallText?: string;
93+
afterToolCallText?: string;
94+
aggregatedToolCallText?: string;
95+
}
96+
97+
export interface DocSearchProps {
98+
appId: string;
99+
apiKey: string;
100+
indexName: string;
101+
placeholder?: string;
102+
searchParameters?: any;
103+
disableUserPersonalization?: boolean;
104+
initialQuery?: string;
105+
insights?: boolean;
106+
translations?: DocSearchTranslations;
107+
askAi?: any;
108+
theme?: 'dark' | 'light';
109+
}
110+
type DocSearchTranslationProps = Pick<DocSearchProps, 'placeholder' | 'translations'>;
10111
11112
const pick = (keyStart: string) =>
12113
Object.fromEntries(
@@ -130,12 +231,59 @@ const docsearchTranslations: DocSearchTranslationProps = {
130231
super();
131232
window.addEventListener('DOMContentLoaded', async () => {
132233
const { default: docsearch } = await import('@docsearch/js');
133-
const options = { ...config, container: 'sl-doc-search' };
234+
let translations;
134235
try {
135-
const translations = JSON.parse(this.dataset.translations || '{}');
136-
Object.assign(options, translations);
236+
translations = JSON.parse(this.dataset.translations || '{}');
137237
} catch {}
138-
docsearch(options);
238+
239+
// Extract the page language (e.g. <html lang="en">) and pass it to DocSearch
240+
// as an Algolia facet filter so that the search results are scoped to the current
241+
// language. If users already defined facet filters we preserve them while appending
242+
// the new one.
243+
let searchParameters: Record<string, any> | undefined;
244+
245+
// Rebuild the askAi prop as an object:
246+
// If the askAi prop is a string, treat it as the assistantId and use
247+
// the default indexName, apiKey and appId from the main options.
248+
// If the askAi prop is an object, spread its explicit values.
249+
const askAiProp = config.askAi;
250+
const isAskAiString = typeof askAiProp === 'string';
251+
252+
const askAi = askAiProp
253+
? {
254+
indexName: isAskAiString ? config.indexName : askAiProp.indexName,
255+
apiKey: isAskAiString ? config.apiKey : askAiProp.apiKey,
256+
appId: isAskAiString ? config.appId : askAiProp.appId,
257+
assistantId: isAskAiString ? askAiProp : askAiProp.assistantId,
258+
searchParameters: {},
259+
}
260+
: undefined;
261+
262+
const pageLang = document.documentElement.getAttribute('lang');
263+
if (pageLang) {
264+
searchParameters = config.searchParameters || {};
265+
const existingFilters = searchParameters?.facetFilters ?? [];
266+
// Ensure facetFilters is always an array of strings for simplicity.
267+
searchParameters.facetFilters = Array.isArray(existingFilters)
268+
? [...existingFilters, `lang:${pageLang}`]
269+
: [existingFilters, `lang:${pageLang}`].filter(Boolean);
270+
271+
if (askAi) {
272+
// Re-use the merged facetFilters from the search parameters so that
273+
// Ask AI uses the same language filtering as the regular search.
274+
askAi.searchParameters = { facetFilters: searchParameters.facetFilters };
275+
}
276+
277+
const options = {
278+
...config,
279+
container: 'sl-doc-search',
280+
translations,
281+
searchParameters,
282+
askAi,
283+
};
284+
285+
docsearch(options);
286+
}
139287
});
140288
}
141289
}

packages/docsearch/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Algolia DocSearch plugin for the [Starlight][starlight] documentation theme for [Astro][astro].
44

5+
Supports DocSearch v4 with optional **Ask AI** conversational search.
6+
57
## Documentation
68

79
See the [Starlight site search guide][docs] for how to use this plugin.

0 commit comments

Comments
 (0)