Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom searchNoResultsText and searchSuggestedQueryText #1114

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion packages/document/docs/en/api/config/config-theme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export default defineConfig({
- Type: `string`
- Default: `Search Docs`

The placeholder text of the search box. for example:
The placeholder text of the search box. For example:

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';
Expand All @@ -306,6 +306,40 @@ export default defineConfig({
});
```

## searchNoResultsText

- Type: `string`
- Default: `No results for`

The text of no search result. For example:

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';

export default defineConfig({
themeConfig: {
searchNoResultsText: 'No results for',
},
});
```

## searchSuggestedQueryText

- Type: `string`
- Default: `Please try again with a different keyword`

The text of suggested query text when no search result. For example:

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';

export default defineConfig({
themeConfig: {
searchSuggestedQueryText: 'Please search again',
},
});
```

## socialLinks

- Type: `Array`
Expand Down Expand Up @@ -449,6 +483,10 @@ export interface LocaleConfig {
nextPageText?: string;
// Search box placeholder text
searchPlaceholderText?: string;
// The text of no search result
searchNoResultsText?: string;
// The text of suggested query text when no search result
searchSuggestedQueryText?: string;
}
```

Expand Down
38 changes: 38 additions & 0 deletions packages/document/docs/zh/api/config/config-theme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,40 @@ export default defineConfig({
});
```

## searchNoResultsText

- Type: `string`
- Default: `No results for`

没有搜索结果时的显示文本。比如:

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';

export default defineConfig({
themeConfig: {
searchNoResultsText: 'No results for',
},
});
```

## searchSuggestedQueryText

- Type: `string`
- Default: `Please try again with a different keyword`

没有搜索结果时的建议查询提示文本。比如:

```ts title="rspress.config.ts"
import { defineConfig } from 'rspress/config';

export default defineConfig({
themeConfig: {
searchSuggestedQueryText: 'Please search again',
},
});
```

## socialLinks

- Type: `Array`
Expand Down Expand Up @@ -435,6 +469,10 @@ export interface LocaleConfig {
nextPageText?: string;
// 搜索框占位符文本
searchPlaceholderText?: string;
// 没有搜索结果时的显示文本
searchNoResultsText?: string;
// 没有搜索结果时的建议查询提示文本
searchSuggestedQueryText?: string;
}
```

Expand Down
2 changes: 2 additions & 0 deletions packages/document/rspress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export default defineConfig({
nextPageText: '下一篇',
outlineTitle: '目录',
searchPlaceholderText: '搜索',
searchNoResultsText: '未搜索到相关结果',
searchSuggestedQueryText: '可更换不同的关键字后重试',
},
{
lang: 'en',
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/types/defaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export interface Config {
* The placeholder of search input
*/
searchPlaceholderText?: string;
/**
* The text of no search result
*/
searchNoResultsText?: string;
/**
* The text of suggested query text when no search result
*/
searchSuggestedQueryText?: string;
/**
* The behavior of hiding navbar
*/
Expand Down Expand Up @@ -126,6 +134,8 @@ export interface LocaleConfig {
sourceCodeText?: string;
langRoutePrefix?: string;
searchPlaceholderText?: string;
searchNoResultsText?: string;
searchSuggestedQueryText?: string;
}
// nav -----------------------------------------------------------------------
export type Nav = NavItem[] | { [key: string]: NavItem[] };
Expand Down
10 changes: 8 additions & 2 deletions packages/theme-default/src/components/Search/NoSearchResult.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import Empty from '@theme-assets/empty';
import { SvgWrapper } from '../SvgWrapper';
import { useLocaleSiteData } from '../../logic';

export function NoSearchResult({ query }: { query: string }) {
const {
searchNoResultsText = 'No results for',
searchSuggestedQueryText = 'Please try again with a different keyword',
} = useLocaleSiteData();

return (
<div className="flex flex-col items-center pt-8 pb-2">
<SvgWrapper icon={Empty} className="mb-4 opacity-80" />
<p className="mb-2">
No results for <b>&quot;{query}&quot;</b>.
{searchNoResultsText} <b>&quot;{query}&quot;</b>
</p>
<p>Please try again with a different keyword.</p>
<p>{searchSuggestedQueryText}</p>
</div>
);
}
2 changes: 2 additions & 0 deletions packages/theme-default/src/logic/useLocaleSiteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function useLocaleSiteData(): NormalizedLocales {
nextPageText: themeConfig.nextPageText,
sourceCodeText: themeConfig.sourceCodeText,
searchPlaceholderText: themeConfig.searchPlaceholderText,
searchNoResultsText: themeConfig.searchNoResultsText,
searchSuggestedQueryText: themeConfig.searchSuggestedQueryText,
} as NormalizedLocales;
}
const localeInfo = locales.find(locale => locale.lang === lang)!;
Expand Down
Loading