Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add search page 🔎 #2756

Merged
merged 1 commit into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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