Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of default search query pre-processing function #1518

Closed
3 tasks done
wilhelmer opened this issue Mar 19, 2020 · 3 comments
Closed
3 tasks done

Allow configuration of default search query pre-processing function #1518

wilhelmer opened this issue Mar 19, 2020 · 3 comments
Labels
change request Issue requests a new feature or improvement

Comments

@wilhelmer
Copy link
Contributor

I checked that...

  • ... the documentation does not mention anything about my idea
  • ... to my best knowledge, my idea wouldn't break something for other users
  • ... there are no open or closed issues that are related to my idea

Description

Just a reminder that you mentioned here that you want to make the following function overridable in Material 5.0 or 5.1:

function defaultTransform(value: string): string {
return value
.replace(/(?:^|\s+)[*+-:^~]+(?=\s+|$)/g, "")
.trim()
.replace(/\s+|\b$/g, "* ")

Use Cases

Overriding defaultTransform() is useful to modify the search behavior, e.g., to disable the addition of a wildcard (*) to each search query.

@squidfunk squidfunk added the change request Issue requests a new feature or improvement label Mar 19, 2020
@squidfunk
Copy link
Owner

Thanks for reporting! This is the only thing I'll try to change before the final release which may impact the configuration structure. Other than that the latest RC should be pretty stable.

@squidfunk squidfunk changed the title Make defaultTransform() overridable Allow configuration of default search query pre-processing function Mar 19, 2020
@squidfunk
Copy link
Owner

squidfunk commented Mar 27, 2020

64caf62 adds support for custom search transforms via theme extension, e.g. a (non-sensical) query transform that lowercases all characters before sending it to the index:

{% block config %}
  <script>
    var search = {
      transform: function(query) { 
        return query.toLowerCase()
      }
    }
  </script>
{% endblock %}

If no custom transform is given, the default transform is applied:

export function defaultTransform(value: string): string {
return value
.replace(/(?:^|\s+)[*+-:^~]+(?=\s+|$)/g, "")
.trim()
.replace(/\s+|\b$/g, "* ")
}

The default transform now allows for quantifiers (+, - etc.) and automatically adds a wildcard at the end, which is the default behavior of Material for MkDocs since version 1:

foo bar       => foo* bar*
foo -bar      => foo* -bar*
foo +bar      => foo* +bar*

@squidfunk
Copy link
Owner

The fix was just released as part of RC 4. See #1498 for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change request Issue requests a new feature or improvement
Projects
None yet
Development

No branches or pull requests

2 participants