feat(search-algolia): allow disabling search page and configuring path (#6692)

This commit is contained in:
Alexey Pyltsyn 2022-02-16 21:07:01 +03:00 committed by GitHub
parent 3629b5ab39
commit 53c2c118c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 27 deletions

View file

@ -15,6 +15,7 @@ import openSearchTemplate from './templates/opensearch';
import {memoize} from 'lodash';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {ThemeConfig} from '@docusaurus/theme-search-algolia';
const getCompiledOpenSearchTemplate = memoize(() =>
compile(openSearchTemplate.trim()),
@ -22,8 +23,9 @@ const getCompiledOpenSearchTemplate = memoize(() =>
function renderOpenSearchTemplate(data: {
title: string;
url: string;
favicon: string | null;
siteUrl: string;
searchUrl: string;
faviconUrl: string | null;
}) {
const compiled = getCompiledOpenSearchTemplate();
return compiled(data, defaultConfig);
@ -34,9 +36,12 @@ const OPEN_SEARCH_FILENAME = 'opensearch.xml';
export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
const {
baseUrl,
siteConfig: {title, url, favicon},
siteConfig: {title, url, favicon, themeConfig},
i18n: {currentLocale},
} = context;
const {
algolia: {searchPagePath},
} = themeConfig as ThemeConfig;
return {
name: 'docusaurus-theme-search-algolia',
@ -57,30 +62,41 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
},
async contentLoaded({actions: {addRoute}}) {
if (searchPagePath) {
addRoute({
path: normalizeUrl([baseUrl, 'search']),
path: normalizeUrl([baseUrl, searchPagePath]),
component: '@theme/SearchPage',
exact: true,
});
}
},
async postBuild({outDir}) {
if (searchPagePath) {
const siteUrl = normalizeUrl([url, baseUrl]);
try {
fs.writeFileSync(
path.join(outDir, OPEN_SEARCH_FILENAME),
renderOpenSearchTemplate({
title,
url: url + baseUrl,
favicon: favicon ? normalizeUrl([url, baseUrl, favicon]) : null,
siteUrl,
searchUrl: normalizeUrl([siteUrl, searchPagePath]),
faviconUrl: favicon ? normalizeUrl([siteUrl, favicon]) : null,
}),
);
} catch (e) {
logger.error('Generating OpenSearch file failed.');
throw e;
}
}
},
injectHtmlTags() {
if (!searchPagePath) {
return {};
}
return {
headTags: [
{

View file

@ -12,11 +12,11 @@ export default `
<ShortName><%= it.title %></ShortName>
<Description>Search <%= it.title %></Description>
<InputEncoding>UTF-8</InputEncoding>
<% if (it.favicon) { _%>
<Image width="16" height="16" type="image/x-icon"><%= it.favicon %></Image>
<% if (it.faviconUrl) { _%>
<Image width="16" height="16" type="image/x-icon"><%= it.faviconUrl %></Image>
<% } _%>
<Url type="text/html" method="get" template="<%= it.url %>search?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" rel="self" template="<%= it.url %>opensearch.xml" />
<moz:SearchForm><%= it.url %></moz:SearchForm>
<Url type="text/html" method="get" template="<%= it.searchUrl %>?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" rel="self" template="<%= it.siteUrl %>opensearch.xml" />
<moz:SearchForm><%= it.siteUrl %></moz:SearchForm>
</OpenSearchDescription>
`;

View file

@ -16,6 +16,7 @@ declare module '@docusaurus/theme-search-algolia' {
apiKey: string;
indexName: string;
searchParameters: Record<string, unknown>;
searchPagePath: string | false | null;
};
};
export type UserThemeConfig = DeepPartial<ThemeConfig>;

View file

@ -34,6 +34,7 @@ type DocSearchProps = Omit<
> & {
contextualSearch?: string;
externalUrlRegex?: string;
searchPagePath: boolean | string;
};
let DocSearchModal: typeof DocSearchModalType | null = null;
@ -256,8 +257,10 @@ function DocSearch({
navigator={navigator}
transformItems={transformItems}
hitComponent={Hit}
resultsFooterComponent={resultsFooterComponent}
transformSearchClient={transformSearchClient}
{...(props.searchPagePath && {
resultsFooterComponent,
})}
{...props}
searchParameters={searchParameters}
/>,

View file

@ -18,6 +18,7 @@ export const DEFAULT_CONFIG = {
appId: 'BH4D9OD16A',
searchParameters: {},
searchPagePath: 'search',
};
export const Schema = Joi.object({
@ -32,6 +33,10 @@ export const Schema = Joi.object({
searchParameters: Joi.object()
.default(DEFAULT_CONFIG.searchParameters)
.unknown(),
searchPagePath: Joi.alternatives()
.try(Joi.boolean().invalid(true), Joi.string())
.allow(null)
.default(DEFAULT_CONFIG.searchPagePath),
})
.label('themeConfig.algolia')
.required()

View file

@ -11,7 +11,7 @@ This theme provides a `@theme/SearchBar` component that integrates with Algolia
npm install --save @docusaurus/theme-search-algolia
```
This theme also adds search page available at `/search` (as swizzlable `SearchPage` component) path with OpenSearch support.
This theme also adds search page available at `/search` (as swizzlable `SearchPage` component) path with OpenSearch support. You can this default path via `themeConfig.algolia.searchPagePath`. Use `false` to disable search page.
:::tip

View file

@ -107,6 +107,9 @@ module.exports = {
// Optional: Algolia search parameters
searchParameters: {},
// Optional: path for search page that enabled by default (`false` to disable it)
searchPagePath: 'search',
//... other Algolia params
},
// highlight-end