feat(v2): add search page (#2756)

This commit is contained in:
Alexey Pyltsyn 2020-05-17 10:55:40 +03:00 committed by GitHub
parent 1fe2dc192e
commit 3ad4550854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 688 additions and 38 deletions

View file

@ -6,8 +6,20 @@
*/
const path = require('path');
const fs = require('fs');
const eta = require('eta');
const {normalizeUrl} = require('@docusaurus/utils');
const openSearchTemplate = require('./templates/opensearch');
const OPEN_SEARCH_FILENAME = 'opensearch.xml';
module.exports = function (context) {
const {
baseUrl,
siteConfig: {title, url, favicon},
} = context;
const pagePath = path.resolve(__dirname, './pages/search/index.js');
module.exports = function () {
return {
name: 'docusaurus-theme-search-algolia',
@ -15,6 +27,10 @@ module.exports = function () {
return path.resolve(__dirname, './theme');
},
getPathsToWatch() {
return [pagePath];
},
configureWebpack() {
// Ensure that algolia docsearch styles is its own chunk.
return {
@ -34,5 +50,44 @@ module.exports = function () {
},
};
},
async contentLoaded({actions: {addRoute}}) {
addRoute({
path: normalizeUrl([baseUrl, 'search']),
component: pagePath,
exact: true,
});
},
async postBuild({outDir}) {
try {
fs.writeFileSync(
path.join(outDir, OPEN_SEARCH_FILENAME),
eta.render(openSearchTemplate.trim(), {
title,
url,
favicon: normalizeUrl([url, favicon]),
}),
);
} catch (err) {
throw new Error(`Generating OpenSearch file failed: ${err}`);
}
},
injectHtmlTags() {
return {
headTags: [
{
tagName: 'link',
attributes: {
rel: 'search',
type: 'application/opensearchdescription+xml',
title,
href: normalizeUrl([baseUrl, OPEN_SEARCH_FILENAME]),
},
},
],
};
},
};
};