mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 12:22:45 +02:00
feat(v2): Algolia search theme (#1440)
* feat(v2): Algolia search plugin * patch PR #1440 (#1441) * alternative implementation * typo * refactor noop * rename SearchAlgolia -> SearchBar * changes.md
This commit is contained in:
parent
8aef4ec791
commit
384fd5708f
14 changed files with 116 additions and 44 deletions
27
packages/docusaurus-theme-search-algolia/src/index.js
Normal file
27
packages/docusaurus-theme-search-algolia/src/index.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
|
||||
const DEFAULT_OPTIONS = {};
|
||||
|
||||
class DocusaurusThemeSearchAlgolia {
|
||||
constructor(context, opts) {
|
||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
getName() {
|
||||
return 'docusaurus-theme-search-algolia';
|
||||
}
|
||||
|
||||
getThemePath() {
|
||||
return path.resolve(__dirname, './theme');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DocusaurusThemeSearchAlgolia;
|
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import DocusaurusContext from '@docusaurus/context';
|
||||
|
||||
import './styles.css';
|
||||
|
||||
class Search extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
enabled: true,
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const {siteConfig = {}, metadata = {}} = this.context;
|
||||
const {version: thisVersion, language: thisLanguage} = metadata;
|
||||
const {
|
||||
themeConfig: {algolia},
|
||||
} = siteConfig;
|
||||
|
||||
// https://github.com/algolia/docsearch/issues/352
|
||||
const isClient = typeof window !== 'undefined';
|
||||
if (isClient) {
|
||||
import('docsearch.js').then(({default: docsearch}) => {
|
||||
docsearch({
|
||||
appId: algolia.appId,
|
||||
apiKey: algolia.apiKey,
|
||||
indexName: algolia.indexName,
|
||||
inputSelector: '#search_input_react',
|
||||
algoliaOptions: JSON.parse(
|
||||
JSON.stringify(algolia.algoliaOptions)
|
||||
.replace('VERSION', thisVersion)
|
||||
.replace('LANGUAGE', thisLanguage),
|
||||
),
|
||||
});
|
||||
});
|
||||
} else {
|
||||
console.warn('Search has failed to load and now is being disabled');
|
||||
this.setState({enabled: false});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {enabled} = this.state;
|
||||
|
||||
return enabled ? (
|
||||
<input
|
||||
id="search_input_react"
|
||||
type="search"
|
||||
placeholder="Search docs"
|
||||
aria-label="Search docs"
|
||||
/>
|
||||
) : null;
|
||||
}
|
||||
}
|
||||
|
||||
Search.contextType = DocusaurusContext;
|
||||
|
||||
export default Search;
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue