Skip to content

Commit

Permalink
feat(search-algolia): allow disabling search page and configuring path (
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 authored Feb 16, 2022
1 parent 3629b5a commit 53c2c11
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
56 changes: 36 additions & 20 deletions packages/docusaurus-theme-search-algolia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ 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()),
);

function renderOpenSearchTemplate(data: {
title: string;
url: string;
favicon: string | null;
siteUrl: string;
searchUrl: string;
faviconUrl: string | null;
}) {
const compiled = getCompiledOpenSearchTemplate();
return compiled(data, defaultConfig);
Expand All @@ -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',
Expand All @@ -57,30 +62,41 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
},

async contentLoaded({actions: {addRoute}}) {
addRoute({
path: normalizeUrl([baseUrl, 'search']),
component: '@theme/SearchPage',
exact: true,
});
if (searchPagePath) {
addRoute({
path: normalizeUrl([baseUrl, searchPagePath]),
component: '@theme/SearchPage',
exact: true,
});
}
},

async postBuild({outDir}) {
try {
fs.writeFileSync(
path.join(outDir, OPEN_SEARCH_FILENAME),
renderOpenSearchTemplate({
title,
url: url + baseUrl,
favicon: favicon ? normalizeUrl([url, baseUrl, favicon]) : null,
}),
);
} catch (e) {
logger.error('Generating OpenSearch file failed.');
throw e;
if (searchPagePath) {
const siteUrl = normalizeUrl([url, baseUrl]);

try {
fs.writeFileSync(
path.join(outDir, OPEN_SEARCH_FILENAME),
renderOpenSearchTemplate({
title,
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: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type DocSearchProps = Omit<
> & {
contextualSearch?: string;
externalUrlRegex?: string;
searchPagePath: boolean | string;
};

let DocSearchModal: typeof DocSearchModalType | null = null;
Expand Down Expand Up @@ -256,8 +257,10 @@ function DocSearch({
navigator={navigator}
transformItems={transformItems}
hitComponent={Hit}
resultsFooterComponent={resultsFooterComponent}
transformSearchClient={transformSearchClient}
{...(props.searchPagePath && {
resultsFooterComponent,
})}
{...props}
searchParameters={searchParameters}
/>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const DEFAULT_CONFIG = {
appId: 'BH4D9OD16A',

searchParameters: {},
searchPagePath: 'search',
};

export const Schema = Joi.object({
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion website/docs/api/themes/theme-search-algolia.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions website/docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53c2c11

Please sign in to comment.