離線/本地搜尋在防火牆後面工作的 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 開箱即用的文件。
您也可以透過修改<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 檔案以取得更多資訊。
麻省理工學院