经过一周的反复试验和 Docsearch 开发人员通过 github 提供的一些非常有价值的帮助后,我想出了一个适用于我的文档站点的解决方案,并在这里分享它,希望 Docusaurus、Docsearch 和 Instantsearch 的其他沮丧用户找到解决方案的方法:
在instantsearch.js
(在网站/静态/js中找到):
// instantSearch on homepage
document.addEventListener('DOMContentLoaded', () => {
// Set initial parameters for instantsearch on the homepage
const mainSearch = instantsearch({
appId: '...',
apiKey: '...',
indexName: 'your-docsearch-index-name',
searchParameters: {
hitsPerPage: 10
},
routing: true
});
mainSearch.addWidget(
instantsearch.widgets.configure({
filters: 'version:1.5.5' // set latest version here
})
);
// initialize SearchBox
mainSearch.addWidget(
instantsearch.widgets.searchBox({
container: '#search_input_main',
placeholder: 'Search the Docs',
autofocus: false,
poweredBy: true
})
);
// add hits cont
mainSearch.addWidget(
instantsearch.widgets.hits({
container: '#search-hits',
templates: {
empty: 'No results for the search term <em>"{{query}}"</em>.',
item: $('#results-template').html() // this is the template at index.js
},
hitsPerPage: 10
})
);
// add pagination
mainSearch.addWidget(
instantsearch.widgets.pagination({
container: '#pagination-container',
maxPages: 20,
// default is to scroll to 'body', here we disable this behavior
scrollTo: '#search-container'
})
);
mainSearch.start();
// Toggle search results
const search_input_main = document.querySelector('#search_input_main');
const searchContainer = document.querySelector('#search-container');
// Bind keyup event on the input
search_input_main.addEventListener('keyup', function() {
if (search_input_main.value.length > 0) {
searchContainer.classList.remove('hidden');
} else {
searchContainer.classList.add('hidden');
}
});
// hide results on reset
document.querySelector('.ais-search-box--reset').addEventListener('click', function() {
searchContainer.classList.add('hidden');
});
});
这是我的 pages/en/index.js:
const React = require('react');
...
// for homepage instantsearch
let resultsTemplate = `
<script type="text/template" id="results-template">
<div class="ais-result">
{{#hierarchy.lvl0}}
<div class="ais-lvl0">
<a title="{{_highlightResult.hierarchy.lvl1.value}}" href="{{{url}}}"><h4>{{{_highlightResult.hierarchy.lvl0.value}}}</h4></a>
</div>
{{/hierarchy.lvl0}}
<div class="ais-lvl1 breadcrumbs">
{{#hierarchy.lvl1}} {{{_highlightResult.hierarchy.lvl1.value}}} {{/hierarchy.lvl1}} {{#hierarchy.lvl2}} > {{{_highlightResult.hierarchy.lvl2.value}}} {{/hierarchy.lvl2}} {{#hierarchy.lvl3}} > {{{_highlightResult.hierarchy.lvl3.value}}} {{/hierarchy.lvl3}}
{{#hierarchy.lvl4}} > {{{_highlightResult.hierarchy.lvl4.value}}} {{/hierarchy.lvl4}}
</div>
<div class="ais-content">
{{{#content}}} {{{_highlightResult.content.value}}} {{{/content}}}
</div>
</div>
</script>
`
...
const SearchInput = () => (
<div>
<div className='productShowcaseSection small-paddingBottom paddingTop' style={{textAlign: 'center'}}>
<h3>Get the most out of Stackery's serverless toolkit</h3>
<Container>
<div className='search_input_div'>
<input id="search_input_main" type="text" className="form-control" placeholder="Search the docs" aria-label="Search" aria-describedby="search"></input>
</div>
</Container>
</div>
</div>
);
const SearchHits = () => {
return (
<Container className='hidden' id='search-container'>
<h3>Search results:</h3>
<div id='search-hits'></div>
<div id='pagination-container'></div>
<div
dangerouslySetInnerHTML={{ __html: resultsTemplate }}
/>
</Container>
)
}
class HomeSplash extends React.Component {
render () {
const language = this.props.language || '';
return (
<SplashContainer>
<div className='inner'>
<ProjectTitle />
<PromoSection>
...
</PromoSection>
<SearchInput />
</div>
</SplashContainer>
);
}
}
...
class Index extends React.Component {
render () {
const language = this.props.language || '';
return (
<div>
<HomeSplash language={language} />
<SearchHits />
<BrowseDocs />
<TryIt />
</div>
);
}
}
module.exports = Index;
注意事项:
- 请务必添加
filters: 'version:your-version'
,否则即时搜索将返回您拥有的每个版本的结果,从而导致大量重复。
- 底部的 JQuery用于根据搜索输入中的 keyup 事件
instantsearch.js
显示和隐藏搜索结果出现的位置<div>
- 我使用的原因
const mainSearch = instantsearch({...
是const search
默认导航栏 Docsearch 已经使用了该search
变量。如果页面上有多个搜索,则需要执行此操作。
- 虽然本指南是特定于 Docusaurus 的,但这个有用的 JSfiddle 展示了如何在普通静态站点上使用 Docsearch 索引实现即时搜索:https ://jsfiddle.net/s_pace/965a4w3o/1/
- 如果您需要帮助在自己的 Docusaurus 网站上实现此功能,请发表评论,我很乐意提供帮助