Skip to content

Commit

Permalink
feat(v2): add search page
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed May 16, 2020
1 parent dd1ad5d commit e699937
Show file tree
Hide file tree
Showing 12 changed files with 688 additions and 38 deletions.
14 changes: 14 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from './types';
import {Configuration} from 'webpack';
import {docsVersion} from './version';
import {VERSIONS_JSON_FILE} from './constants';

const DEFAULT_OPTIONS: PluginOptions = {
path: 'docs', // Path to data on filesystem, relative to site dir.
Expand Down Expand Up @@ -95,6 +96,10 @@ export default function pluginContentDocs(
return {
name: 'docusaurus-plugin-content-docs',

getThemePath() {
return path.resolve(__dirname, './theme');
},

extendCli(cli) {
cli
.command('docs:version')
Expand Down Expand Up @@ -418,7 +423,16 @@ export default function pluginContentDocs(
configureWebpack(_config, isServer, utils) {
const {getBabelLoader, getCacheLoader} = utils;
const {rehypePlugins, remarkPlugins} = options;
// Suppress warnings about non-existing of versions file.
const stats = {
warningsFilter: [VERSIONS_JSON_FILE],
};

return {
stats,
devServer: {
stats,
},
resolve: {
alias: {
'~docs': dataDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

let versions: string[] = [];

try {
versions = require('@site/versions.json');
} catch (e) {}

function useVersioning() {
return {
versioningEnabled: versions.length > 0,
versions,
latestVersion: versions[0],
};
}

export default useVersioning;
6 changes: 5 additions & 1 deletion packages/docusaurus-theme-search-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
},
"license": "MIT",
"dependencies": {
"algoliasearch": "^3.24.5",
"algoliasearch-helper": "^3.1.1",
"classnames": "^2.2.6",
"docsearch.js": "^2.6.3"
"docsearch.js": "^2.6.3",
"eta": "^1.1.1"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
"@docusaurus/utils": "^2.0.0-alpha.54",
"react": "^16.8.4",
"react-dom": "^16.8.4"
},
Expand Down
57 changes: 56 additions & 1 deletion packages/docusaurus-theme-search-algolia/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@
*/

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',

getThemePath() {
return path.resolve(__dirname, './theme');
},

getPathsToWatch() {
return [pagePath];
},

configureWebpack() {
// Ensure that algolia docsearch styles is its own chunk.
return {
Expand All @@ -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

0 comments on commit e699937

Please sign in to comment.