From f61c7370e545dc8ccb9577a8a64146103e94a167 Mon Sep 17 00:00:00 2001 From: lewisnyman Date: Tue, 31 Jul 2018 13:13:38 +0100 Subject: [PATCH] Add configuration options to remove default pipeline functions --- README.md | 9 ++++++++- lib/middleman-search/extension.rb | 1 + lib/middleman-search/search-index-resource.rb | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78a19e4..498abc8 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ activate :search do |search| search.resources = ['blog/', 'index.html', 'contactus/index.html'] search.index_path = 'search/lunr-index.json' # defaults to `search.json` - + search.lunr_dirs = ['source/vendor/lunr-custom/'] # optional alternate paths where to look for lunr js files search.language = 'es' # defaults to 'en' @@ -123,6 +123,13 @@ Note that if you add a function to the pipeline, it will also be loaded when de- <%= search_lunr_js_pipeline %> ``` +You can also remove pipeline functions that Lunr.js enables by default: trimmer, stemmer, and stopWordFilter. + +```ruby +search.pipeline_remove = [ + 'stopWordFilter' +] +``` ## Index file diff --git a/lib/middleman-search/extension.rb b/lib/middleman-search/extension.rb index e05b32f..51bf0e9 100644 --- a/lib/middleman-search/extension.rb +++ b/lib/middleman-search/extension.rb @@ -8,6 +8,7 @@ class SearchExtension < Middleman::Extension option :before_index, nil, 'Callback to execute before indexing a document' option :index_path, 'search.json', 'Index file path' option :pipeline, {}, 'Javascript pipeline functions to use in lunr index' + option :pipeline_remove, {}, 'Default pipeline functions to remove' option :cache, false, 'Avoid the cache to be rebuilt on every request in development mode' option :language, 'en', 'Language code ("es", "fr") to use when indexing site\'s content' option :lunr_dirs, [], 'Directories in which to look for custom lunr.js files' diff --git a/lib/middleman-search/search-index-resource.rb b/lib/middleman-search/search-index-resource.rb index daae8d5..01296ac 100644 --- a/lib/middleman-search/search-index-resource.rb +++ b/lib/middleman-search/search-index-resource.rb @@ -8,6 +8,7 @@ def initialize(store, path, options) @fields = options[:fields] @callback = options[:before_index] @pipeline = options[:pipeline] + @pipeline_remove = options[:pipeline_remove] @cache_index = options[:cache] @language = options[:language] @lunr_dirs = options[:lunr_dirs] + [File.expand_path("../../../vendor/assets/javascripts/", __FILE__)] @@ -52,6 +53,11 @@ def build_index # Use autogenerated id field as reference source << "this.ref('id');" + # Remove default pipeline filters + @pipeline_remove.each do |name| + source << "this.pipeline.remove(lunr.#{name});" + end + # Add functions to pipeline (just registering them isn't enough) @pipeline.each do |name, function| source << "this.pipeline.add(lunr.Pipeline.registeredFunctions.#{name});"