mirror of
https://github.com/mkdocs/mkdocs.git
synced 2026-03-27 09:58:31 +07:00
Add minimum search length configuration parameter.
Allow users to set their own minimum search term length. Fixes #2014.
This commit is contained in:
@@ -25,6 +25,7 @@ The current and past members of the MkDocs team.
|
||||
|
||||
Bugfix: Ensure wheel is Python 3 only.
|
||||
Bugfix: Clean up `dev_addr` validation and disallow `0.0.0.0`.
|
||||
Add support for `min_search_length` parameter for search plugin (#2014).
|
||||
|
||||
## Version 1.1 (2020-02-22)
|
||||
|
||||
|
||||
@@ -502,6 +502,22 @@ plugins:
|
||||
|
||||
**default**: `'[\s\-]+'`
|
||||
|
||||
##### **min_search_length**
|
||||
|
||||
An integer value that defines the minimum length for a search query. By default
|
||||
searches shorter than 3 chars in length are ignored as search result quality with
|
||||
short search terms is poor. However, for some use cases (such as documentation
|
||||
about Message Queues which might generate searches for 'MQ') it may be preferable
|
||||
to set a shorter limit.
|
||||
|
||||
```yaml
|
||||
plugins:
|
||||
- search:
|
||||
min_search_length: 2
|
||||
```
|
||||
|
||||
**default**: 3
|
||||
|
||||
##### **lang**
|
||||
|
||||
A list of languages to use when building the search index as identified by their
|
||||
|
||||
@@ -36,6 +36,7 @@ class SearchPlugin(BasePlugin):
|
||||
config_scheme = (
|
||||
('lang', LangOption(default=['en'])),
|
||||
('separator', config_options.Type(str, default=r'[\s\-]+')),
|
||||
('min_search_length', config_options.Type(int, default=3)),
|
||||
('prebuild_index', config_options.Choice((False, True, 'node', 'python'), default=False)),
|
||||
)
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ function displayResults (results) {
|
||||
|
||||
function doSearch () {
|
||||
var query = document.getElementById('mkdocs-search-query').value;
|
||||
if (query.length > 2) {
|
||||
if (query.length > min_search_length) {
|
||||
if (!window.Worker) {
|
||||
displayResults(search(query));
|
||||
} else {
|
||||
@@ -73,6 +73,8 @@ function onWorkerMessage (e) {
|
||||
} else if (e.data.results) {
|
||||
var results = e.data.results;
|
||||
displayResults(results);
|
||||
} else if (e.data.config) {
|
||||
min_search_length = e.data.config.min_search_length-1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ function onScriptsLoaded () {
|
||||
if (data.config && data.config.separator && data.config.separator.length) {
|
||||
lunr.tokenizer.separator = new RegExp(data.config.separator);
|
||||
}
|
||||
|
||||
if (data.index) {
|
||||
index = lunr.Index.load(data.index);
|
||||
data.docs.forEach(function (doc) {
|
||||
@@ -84,6 +85,7 @@ function onScriptsLoaded () {
|
||||
console.log('Lunr index built, search ready');
|
||||
}
|
||||
allowSearch = true;
|
||||
postMessage({config: data.config});
|
||||
postMessage({allowSearch: allowSearch});
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ class SearchPluginTests(unittest.TestCase):
|
||||
expected = {
|
||||
'lang': ['en'],
|
||||
'separator': r'[\s\-]+',
|
||||
'min_search_length': 3,
|
||||
'prebuild_index': False
|
||||
}
|
||||
plugin = search.SearchPlugin()
|
||||
@@ -70,6 +71,7 @@ class SearchPluginTests(unittest.TestCase):
|
||||
expected = {
|
||||
'lang': ['es'],
|
||||
'separator': r'[\s\-]+',
|
||||
'min_search_length': 3,
|
||||
'prebuild_index': False
|
||||
}
|
||||
plugin = search.SearchPlugin()
|
||||
@@ -82,6 +84,7 @@ class SearchPluginTests(unittest.TestCase):
|
||||
expected = {
|
||||
'lang': ['en'],
|
||||
'separator': r'[\s\-\.]+',
|
||||
'min_search_length': 3,
|
||||
'prebuild_index': False
|
||||
}
|
||||
plugin = search.SearchPlugin()
|
||||
@@ -90,10 +93,24 @@ class SearchPluginTests(unittest.TestCase):
|
||||
self.assertEqual(errors, [])
|
||||
self.assertEqual(warnings, [])
|
||||
|
||||
def test_plugin_config_min_search_length(self):
|
||||
expected = {
|
||||
'lang': ['en'],
|
||||
'separator': r'[\s\-]+',
|
||||
'min_search_length': 2,
|
||||
'prebuild_index': False
|
||||
}
|
||||
plugin = search.SearchPlugin()
|
||||
errors, warnings = plugin.load_config({'min_search_length': 2})
|
||||
self.assertEqual(plugin.config, expected)
|
||||
self.assertEqual(errors, [])
|
||||
self.assertEqual(warnings, [])
|
||||
|
||||
def test_plugin_config_prebuild_index(self):
|
||||
expected = {
|
||||
'lang': ['en'],
|
||||
'separator': r'[\s\-]+',
|
||||
'min_search_length': 3,
|
||||
'prebuild_index': True
|
||||
}
|
||||
plugin = search.SearchPlugin()
|
||||
|
||||
Reference in New Issue
Block a user