Skip to content

Commit

Permalink
Implemented support for AND search with quotation marks
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Jun 28, 2020
1 parent e2d8a0e commit 83bba6b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
2 changes: 0 additions & 2 deletions material/assets/javascripts/bundle.38f5c9b5.min.js

This file was deleted.

1 change: 0 additions & 1 deletion material/assets/javascripts/bundle.38f5c9b5.min.js.map

This file was deleted.

2 changes: 2 additions & 0 deletions material/assets/javascripts/bundle.8db80c2a.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions material/assets/javascripts/bundle.8db80c2a.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions material/assets/manifest.json
@@ -1,6 +1,6 @@
{
"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.8db80c2a.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.8db80c2a.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",
Expand Down
2 changes: 1 addition & 1 deletion material/base.html
Expand Up @@ -183,7 +183,7 @@ <h1>{{ page.title | default(config.site_name, true)}}</h1>
</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.8db80c2a.min.js' | url }}"></script>
{%- set translations = {} -%}
{%- for key in [
"clipboard.copy",
Expand Down
32 changes: 27 additions & 5 deletions src/assets/javascripts/integrations/search/transform/index.ts
Expand Up @@ -40,16 +40,38 @@ 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 and `AND` query (as opposed to the default `OR` behavior). While users
* may expected 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
* asterisik (wildcard) in between terms, which can be denoted by whitespace,
* any non-control character, or a word boundary.
*
* @param value - Query value
*
* @return Transformed query value
*/
export function defaultTransform(value: string): string {
return value
.replace(/(?:^|\s+)[*+\-:^~]+(?=\s+|$)/g, "")
.trim()
.replace(/\s+|(?![^\x00-\x7F]|^)$|\b$/g, "* ")
.split(/"([^"]+)"/) /* => 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 */
}

0 comments on commit 83bba6b

Please sign in to comment.