mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
feat(v2): add search page (#2756)
This commit is contained in:
parent
1fe2dc192e
commit
3ad4550854
12 changed files with 688 additions and 38 deletions
|
@ -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]),
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue