mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Merge pull request #1778 from squidfunk/feature/search-with-quotes
Support for quotation marks in search
This commit is contained in:
commit
004c21e747
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
2
material/assets/javascripts/bundle.fc9c3121.min.js
vendored
Normal file
2
material/assets/javascripts/bundle.fc9c3121.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
material/assets/javascripts/bundle.fc9c3121.min.js.map
Normal file
1
material/assets/javascripts/bundle.fc9c3121.min.js.map
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,10 +1,10 @@
|
||||
{
|
||||
"assets/javascripts/bundle.js": "assets/javascripts/bundle.38f5c9b5.min.js",
|
||||
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.38f5c9b5.min.js.map",
|
||||
"assets/javascripts/bundle.js": "assets/javascripts/bundle.fc9c3121.min.js",
|
||||
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.fc9c3121.min.js.map",
|
||||
"assets/javascripts/vendor.js": "assets/javascripts/vendor.de50e36d.min.js",
|
||||
"assets/javascripts/vendor.js.map": "assets/javascripts/vendor.de50e36d.min.js.map",
|
||||
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.9b3611bd.min.js",
|
||||
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.9b3611bd.min.js.map",
|
||||
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.a68abb33.min.js",
|
||||
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.a68abb33.min.js.map",
|
||||
"assets/stylesheets/main.css": "assets/stylesheets/main.545621a7.min.css",
|
||||
"assets/stylesheets/main.css.map": "assets/stylesheets/main.545621a7.min.css.map",
|
||||
"assets/stylesheets/palette.css": "assets/stylesheets/palette.36d1b78f.min.css",
|
||||
|
@ -183,7 +183,7 @@
|
||||
</div>
|
||||
{% block scripts %}
|
||||
<script src="{{ 'assets/javascripts/vendor.de50e36d.min.js' | url }}"></script>
|
||||
<script src="{{ 'assets/javascripts/bundle.38f5c9b5.min.js' | url }}"></script>
|
||||
<script src="{{ 'assets/javascripts/bundle.fc9c3121.min.js' | url }}"></script>
|
||||
{%- set translations = {} -%}
|
||||
{%- for key in [
|
||||
"clipboard.copy",
|
||||
@ -207,7 +207,7 @@
|
||||
base: "{{ base_url }}",
|
||||
features: {{ config.theme.features | tojson }},
|
||||
search: Object.assign({
|
||||
worker: "{{ 'assets/javascripts/worker/search.9b3611bd.min.js' | url }}"
|
||||
worker: "{{ 'assets/javascripts/worker/search.a68abb33.min.js' | url }}"
|
||||
}, typeof search !== "undefined" && search)
|
||||
})
|
||||
</script>
|
||||
|
@ -40,8 +40,24 @@ export type SearchTransformFn = (value: string) => string
|
||||
/**
|
||||
* Default transformation function
|
||||
*
|
||||
* Rogue control characters are filtered before handing the query to the
|
||||
* search index, as `lunr` will throw otherwise.
|
||||
* 1. Search for terms in quotation marks and prepend a `+` modifier to denote
|
||||
* that the resulting document must contain all terms, converting the query
|
||||
* to an `AND` query (as opposed to the default `OR` behavior). While users
|
||||
* may expect terms enclosed in quotation marks to map to span queries, i.e.
|
||||
* for which order is important, `lunr` doesn't support them, so the best
|
||||
* we can do is to convert the terms to an `AND` query.
|
||||
*
|
||||
* 2. Replace control characters which are not located at the beginning of the
|
||||
* query or preceded by white space, or are not followed by a non-whitespace
|
||||
* character or are at the end of the query string. Furthermore, filter
|
||||
* unmatched quotation marks.
|
||||
*
|
||||
* 3. Trim excess whitespace from left and right.
|
||||
*
|
||||
* 4. Append a wildcard to the end of every word to make every word a prefix
|
||||
* query in order to provide a good type-ahead experience, by adding an
|
||||
* asterisk (wildcard) in between terms, which can be denoted by whitespace,
|
||||
* any non-control character, or a word boundary.
|
||||
*
|
||||
* @param value - Query value
|
||||
*
|
||||
@ -49,7 +65,13 @@ export type SearchTransformFn = (value: string) => string
|
||||
*/
|
||||
export function defaultTransform(value: string): string {
|
||||
return value
|
||||
.replace(/(?:^|\s+)[*+\-:^~]+(?=\s+|$)/g, "")
|
||||
.trim()
|
||||
.replace(/\s+|(?![^\x00-\x7F]|^)$|\b$/g, "* ")
|
||||
.split(/"([^"]+)"/g) /* => 1 */
|
||||
.map((terms, i) => i & 1
|
||||
? terms.replace(/^\b|^(?![^\x00-\x7F]|$)|\s+/g, " +")
|
||||
: terms
|
||||
)
|
||||
.join("")
|
||||
.replace(/"|(?:^|\s+)[*+\-:^~]+(?=\s+|$)/g, "") /* => 2 */
|
||||
.trim() /* => 3 */
|
||||
.replace(/\s+|(?![^\x00-\x7F]|^)$|\b$/g, "* ") /* => 4 */
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user