manastech / middleman-search

LunrJS-based search for Middleman
MIT License
58 stars 31 forks source link

Indexing dynamic pages outside frontmatter #35

Closed marwann closed 6 years ago

marwann commented 6 years ago

Hello,

I'm creating dynamic pages based on a yaml file :

Example application.yml

- url: 'page-slug'
  title: 'Page Title'
  description: 'Page Description'

Example config.rb

data.application.each do |f|
  proxy "/categories/#{f['url']}/index.html", "landing_page.html", locals: {
    data: {
      url: f['url'],
      title: f['title'],
      description: f['description']
    }
  }, ignore: true
end

activate :search do |search|
  search.resources = ['categories/', 'resources/']
  search.index_path = 'search.json'
  search.fields = 
    {
      title:   {boost: 100, store: true},
      description:  {boost: 100, store: true},
      url: {boost: 100, store: true},
   }
end

As I understand, the data indexed for search are parsed in the dynamic pages Frontmatter, however, it is impossible to add variables like the following piece of code. So the result is that I either get a generic title/description for all my dynamic pages, or have them replaced by null inside the json file generated by Middleman-search.

landing_page.html.erb

---
title: <%= data[:description %>
description: #{data[:description]}
---

How should I proceed to ensure the data is still parsed by the extension?

matiasgarciaisaia commented 6 years ago

Hi, @marwann!

May it be that your issue is related to #28?

marwann commented 6 years ago

Hello @matiasgarciaisaia,

I missed that issue while searching, but that's indeed related. Good new is that after a good night's sleep, I found a solution after re-reading the doc.

All fields values are retrieved from the resource data (i.e. its frontmatter), or from the options in the resource.metadata (i.e. any options specified in a proxy page)

So the trick was just to add options in the proxy and then to add them to the index.

Here's some sample code for the proxy :

data.application.each do |f|
  proxy "/categories/#{f['url']}/index.html", "landing_page.html", locals: {
    data: {
      url: f['url'],
      title: f['title'],
      description: f['description']
    }
  }, ignore: true, "title" => f['title'], "description" => f['description']
end

And sample code for the extension in config.rb

activate :search do |search|
  search.resources = ['categories/', 'index.html']
  search.index_path = 'search.json'
  search.fields = 
    {
      "title" => {boost: 100, store: true},
      "description" => {boost: 100, store: true},
  }
end
matiasgarciaisaia commented 6 years ago

Awesome! 👌

So, I'm closing this - feel free to reply anyway if there's anything else.

macandcheese commented 6 years ago

This was super helpful - the above plus just a few syntax tweaks helped me to get this working. Much appreciated.