离线/本地搜索在防火墙后面工作的 Docusaurus v2+ 。
功能亮点:
注意:我们为搜索框使用开源 algolia/autocomplete 库。该库仅用作前端,并且与默认的 Docusaurus 搜索体验(algolia/docsearch)不同,它不连接到任何 Algolia 或第三方服务器。
npm install @cmfcmf/docusaurus-search-local
或者
yarn add @cmfcmf/docusaurus-search-local
将此插件添加到docusaurus.config.js
中的plugins
数组中。
module . exports = {
// ...
plugins : [ require . resolve ( "@cmfcmf/docusaurus-search-local" ) ] ,
// or, if you want to specify options:
// ...
plugins : [
[
require . resolve ( "@cmfcmf/docusaurus-search-local" ) ,
{
// Options here
} ,
] ,
] ,
} ;
可以使用以下选项(默认值如下所示):
{
// whether to index docs pages
indexDocs : true ,
// Whether to also index the titles of the parent categories in the sidebar of a doc page.
// 0 disables this feature.
// 1 indexes the direct parent category in the sidebar of a doc page
// 2 indexes up to two nested parent categories of a doc page
// 3...
//
// Do _not_ use Infinity, the value must be a JSON-serializable integer.
indexDocSidebarParentCategories : 0 ,
// Includes parent categories path in search result
includeParentCategoriesInPageTitle : false ,
// whether to index blog pages
indexBlog : true ,
// whether to index static pages
// /404.html is never indexed
indexPages : false ,
// language of your documentation, see next section
language : "en" ,
// setting this to "none" will prevent the default CSS to be included. The default CSS
// comes from autocomplete-theme-classic, which you can read more about here:
// https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-theme-classic/
// When you want to overwrite CSS variables defined by the default theme, make sure to suffix your
// overwrites with `!important`, because they might otherwise not be applied as expected. See the
// following comment for more information: https://github.com/cmfcmf/docusaurus-search-local/issues/107#issuecomment-1119831938.
style : undefined ,
// The maximum number of search results shown to the user. This does _not_ affect performance of
// searches, but simply does not display additional search results that have been found.
maxSearchResults : 8 ,
// lunr.js-specific settings
lunr : {
// When indexing your documents, their content is split into "tokens".
// Text entered into the search box is also tokenized.
// This setting configures the separator used to determine where to split the text into tokens.
// By default, it splits the text at whitespace and dashes.
//
// Note: Does not work for "ja" and "th" languages, since these use a different tokenizer.
tokenizerSeparator : / [s-]+ / ,
// https://lunrjs.com/guides/customising.html#similarity-tuning
//
// This parameter controls the importance given to the length of a document and its fields. This
// value must be between 0 and 1, and by default it has a value of 0.75. Reducing this value
// reduces the effect of different length documents on a term’s importance to that document.
b : 0.75 ,
// This controls how quickly the boost given by a common word reaches saturation. Increasing it
// will slow down the rate of saturation and lower values result in quicker saturation. The
// default value is 1.2. If the collection of documents being indexed have high occurrences
// of words that are not covered by a stop word filter, these words can quickly dominate any
// similarity calculation. In these cases, this value can be reduced to get more balanced results.
k1 : 1.2 ,
// By default, we rank pages where the search term appears in the title higher than pages where
// the search term appears in just the text. This is done by "boosting" title matches with a
// higher value than content matches. The concrete boosting behavior can be controlled by changing
// the following settings.
titleBoost : 5 ,
contentBoost : 1 ,
tagsBoost : 3 ,
parentCategoriesBoost : 2 , // Only used when indexDocSidebarParentCategories > 0
}
}
您现在可以使用搜索栏来搜索您的文档。
重要提示:搜索仅适用于静态构建的文档(即,在文档文件夹中运行npm run docusaurus build
之后)。
搜索在开发中不起作用(即运行npm run docusaurus start
时)。如果您想在本地测试搜索,请首先使用npm run docusaurus build
构建文档,然后通过npm run docusaurus serve
提供服务。
如果您的文档不是用英语编写的,请使用language
选项。您可以指定一种语言或多种语言的数组。可以使用以下语言:
ar, da, de, en, es, fi, fr, hi, hu, it, ja, nl, no, pt, ro, ru, sv, th, tr, vi, zh
重要提示:对于中文支持 ( zh
),您还必须安装位于^2.5.0 || ^3.0.0
的nodejieba
npm 包^2.5.0 || ^3.0.0
。
支持使用官方 Docusaurus 文档插件创建的文档版本。当不在文档页面上时(例如,在查看博客文章或静态页面时),搜索栏默认为最新版本(不是next
,而是versions.json
中定义的最新版本)。如果用户访问文档页面,则会从该页面中提取版本,并且搜索将仅搜索该版本的文档。搜索栏占位符文本始终反映当前检测到的文档版本。
该插件支持使用 Docusaurus i18n 开箱即用的文档。请通过在 codeTranslations 子文件夹中创建新的翻译文件并提交 PR 来贡献其他翻译。
您还可以通过修改<yourfolder>/i18n/<locale>/code.json
中以cmfcmf/dsl.
。了解更多信息:https://docusaurus.io/docs/i18n/tutorial#translate-json-files
如果构建文档产生错误,您可以在调试模式下构建它以找出导致该错误的页面。为此,只需在构建文档时设置DEBUG
环境变量: DEBUG=1 npm run docusaurus build
。
请参阅 CONTRIBUTING.md 文件以获取更多信息。
麻省理工学院