mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-17 02:02:38 +02:00
feat(search-algolia): allow disabling search page and configuring path (#6692)
This commit is contained in:
parent
3629b5ab39
commit
53c2c118c4
7 changed files with 55 additions and 27 deletions
|
@ -15,6 +15,7 @@ import openSearchTemplate from './templates/opensearch';
|
||||||
import {memoize} from 'lodash';
|
import {memoize} from 'lodash';
|
||||||
|
|
||||||
import type {LoadContext, Plugin} from '@docusaurus/types';
|
import type {LoadContext, Plugin} from '@docusaurus/types';
|
||||||
|
import type {ThemeConfig} from '@docusaurus/theme-search-algolia';
|
||||||
|
|
||||||
const getCompiledOpenSearchTemplate = memoize(() =>
|
const getCompiledOpenSearchTemplate = memoize(() =>
|
||||||
compile(openSearchTemplate.trim()),
|
compile(openSearchTemplate.trim()),
|
||||||
|
@ -22,8 +23,9 @@ const getCompiledOpenSearchTemplate = memoize(() =>
|
||||||
|
|
||||||
function renderOpenSearchTemplate(data: {
|
function renderOpenSearchTemplate(data: {
|
||||||
title: string;
|
title: string;
|
||||||
url: string;
|
siteUrl: string;
|
||||||
favicon: string | null;
|
searchUrl: string;
|
||||||
|
faviconUrl: string | null;
|
||||||
}) {
|
}) {
|
||||||
const compiled = getCompiledOpenSearchTemplate();
|
const compiled = getCompiledOpenSearchTemplate();
|
||||||
return compiled(data, defaultConfig);
|
return compiled(data, defaultConfig);
|
||||||
|
@ -34,9 +36,12 @@ const OPEN_SEARCH_FILENAME = 'opensearch.xml';
|
||||||
export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
|
export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
|
||||||
const {
|
const {
|
||||||
baseUrl,
|
baseUrl,
|
||||||
siteConfig: {title, url, favicon},
|
siteConfig: {title, url, favicon, themeConfig},
|
||||||
i18n: {currentLocale},
|
i18n: {currentLocale},
|
||||||
} = context;
|
} = context;
|
||||||
|
const {
|
||||||
|
algolia: {searchPagePath},
|
||||||
|
} = themeConfig as ThemeConfig;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'docusaurus-theme-search-algolia',
|
name: 'docusaurus-theme-search-algolia',
|
||||||
|
@ -57,30 +62,41 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
|
||||||
},
|
},
|
||||||
|
|
||||||
async contentLoaded({actions: {addRoute}}) {
|
async contentLoaded({actions: {addRoute}}) {
|
||||||
|
if (searchPagePath) {
|
||||||
addRoute({
|
addRoute({
|
||||||
path: normalizeUrl([baseUrl, 'search']),
|
path: normalizeUrl([baseUrl, searchPagePath]),
|
||||||
component: '@theme/SearchPage',
|
component: '@theme/SearchPage',
|
||||||
exact: true,
|
exact: true,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async postBuild({outDir}) {
|
async postBuild({outDir}) {
|
||||||
|
if (searchPagePath) {
|
||||||
|
const siteUrl = normalizeUrl([url, baseUrl]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join(outDir, OPEN_SEARCH_FILENAME),
|
path.join(outDir, OPEN_SEARCH_FILENAME),
|
||||||
renderOpenSearchTemplate({
|
renderOpenSearchTemplate({
|
||||||
title,
|
title,
|
||||||
url: url + baseUrl,
|
siteUrl,
|
||||||
favicon: favicon ? normalizeUrl([url, baseUrl, favicon]) : null,
|
searchUrl: normalizeUrl([siteUrl, searchPagePath]),
|
||||||
|
faviconUrl: favicon ? normalizeUrl([siteUrl, favicon]) : null,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Generating OpenSearch file failed.');
|
logger.error('Generating OpenSearch file failed.');
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
injectHtmlTags() {
|
injectHtmlTags() {
|
||||||
|
if (!searchPagePath) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
headTags: [
|
headTags: [
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,11 +12,11 @@ export default `
|
||||||
<ShortName><%= it.title %></ShortName>
|
<ShortName><%= it.title %></ShortName>
|
||||||
<Description>Search <%= it.title %></Description>
|
<Description>Search <%= it.title %></Description>
|
||||||
<InputEncoding>UTF-8</InputEncoding>
|
<InputEncoding>UTF-8</InputEncoding>
|
||||||
<% if (it.favicon) { _%>
|
<% if (it.faviconUrl) { _%>
|
||||||
<Image width="16" height="16" type="image/x-icon"><%= it.favicon %></Image>
|
<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="text/html" method="get" template="<%= it.searchUrl %>?q={searchTerms}"/>
|
||||||
<Url type="application/opensearchdescription+xml" rel="self" template="<%= it.url %>opensearch.xml" />
|
<Url type="application/opensearchdescription+xml" rel="self" template="<%= it.siteUrl %>opensearch.xml" />
|
||||||
<moz:SearchForm><%= it.url %></moz:SearchForm>
|
<moz:SearchForm><%= it.siteUrl %></moz:SearchForm>
|
||||||
</OpenSearchDescription>
|
</OpenSearchDescription>
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -16,6 +16,7 @@ declare module '@docusaurus/theme-search-algolia' {
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
indexName: string;
|
indexName: string;
|
||||||
searchParameters: Record<string, unknown>;
|
searchParameters: Record<string, unknown>;
|
||||||
|
searchPagePath: string | false | null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
export type UserThemeConfig = DeepPartial<ThemeConfig>;
|
export type UserThemeConfig = DeepPartial<ThemeConfig>;
|
||||||
|
|
|
@ -34,6 +34,7 @@ type DocSearchProps = Omit<
|
||||||
> & {
|
> & {
|
||||||
contextualSearch?: string;
|
contextualSearch?: string;
|
||||||
externalUrlRegex?: string;
|
externalUrlRegex?: string;
|
||||||
|
searchPagePath: boolean | string;
|
||||||
};
|
};
|
||||||
|
|
||||||
let DocSearchModal: typeof DocSearchModalType | null = null;
|
let DocSearchModal: typeof DocSearchModalType | null = null;
|
||||||
|
@ -256,8 +257,10 @@ function DocSearch({
|
||||||
navigator={navigator}
|
navigator={navigator}
|
||||||
transformItems={transformItems}
|
transformItems={transformItems}
|
||||||
hitComponent={Hit}
|
hitComponent={Hit}
|
||||||
resultsFooterComponent={resultsFooterComponent}
|
|
||||||
transformSearchClient={transformSearchClient}
|
transformSearchClient={transformSearchClient}
|
||||||
|
{...(props.searchPagePath && {
|
||||||
|
resultsFooterComponent,
|
||||||
|
})}
|
||||||
{...props}
|
{...props}
|
||||||
searchParameters={searchParameters}
|
searchParameters={searchParameters}
|
||||||
/>,
|
/>,
|
||||||
|
|
|
@ -18,6 +18,7 @@ export const DEFAULT_CONFIG = {
|
||||||
appId: 'BH4D9OD16A',
|
appId: 'BH4D9OD16A',
|
||||||
|
|
||||||
searchParameters: {},
|
searchParameters: {},
|
||||||
|
searchPagePath: 'search',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Schema = Joi.object({
|
export const Schema = Joi.object({
|
||||||
|
@ -32,6 +33,10 @@ export const Schema = Joi.object({
|
||||||
searchParameters: Joi.object()
|
searchParameters: Joi.object()
|
||||||
.default(DEFAULT_CONFIG.searchParameters)
|
.default(DEFAULT_CONFIG.searchParameters)
|
||||||
.unknown(),
|
.unknown(),
|
||||||
|
searchPagePath: Joi.alternatives()
|
||||||
|
.try(Joi.boolean().invalid(true), Joi.string())
|
||||||
|
.allow(null)
|
||||||
|
.default(DEFAULT_CONFIG.searchPagePath),
|
||||||
})
|
})
|
||||||
.label('themeConfig.algolia')
|
.label('themeConfig.algolia')
|
||||||
.required()
|
.required()
|
||||||
|
|
|
@ -11,7 +11,7 @@ This theme provides a `@theme/SearchBar` component that integrates with Algolia
|
||||||
npm install --save @docusaurus/theme-search-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
|
:::tip
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,9 @@ module.exports = {
|
||||||
// Optional: Algolia search parameters
|
// Optional: Algolia search parameters
|
||||||
searchParameters: {},
|
searchParameters: {},
|
||||||
|
|
||||||
|
// Optional: path for search page that enabled by default (`false` to disable it)
|
||||||
|
searchPagePath: 'search',
|
||||||
|
|
||||||
//... other Algolia params
|
//... other Algolia params
|
||||||
},
|
},
|
||||||
// highlight-end
|
// highlight-end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue