Sha256: 8e155ce0c78b21ac7b5ef03a52437762fb5b44aa8aaa60852432873d98d049f9

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'middleman-core'
require 'middleman-search/search-index-resource'

module Middleman
  class SearchExtension < Middleman::Extension
    option :resources, [], 'Paths of resources to index'
    option :fields, {}, 'Fields to index, with their options'
    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 :cache, false, 'Avoid the cache to be rebuilt on every request in development mode'

    def manipulate_resource_list(resources)
      resources.push Middleman::Sitemap::SearchIndexResource.new(@app.sitemap, @options[:index_path], @options)
      resources
    end

    helpers do
      def search_lunr_js_pipeline
        # Thanks http://stackoverflow.com/a/20187415/12791
        extensions[:search].options[:pipeline].map do |name, function|
          "lunr.Pipeline.registerFunction(#{function}, '#{name}');"
        end.join("\n")
      end

      def search_index_path
        "/" + sitemap.find_resource_by_path(extensions[:search].options[:index_path]).destination_path
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
middleman-search-0.7.0 lib/middleman-search/extension.rb
middleman-search-0.6.0 lib/middleman-search/extension.rb
middleman-search-0.5.0 lib/middleman-search/extension.rb