Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Allow searching of pages & posts #18

Closed
daviddarnes opened this issue Nov 3, 2014 · 2 comments
Closed

Allow searching of pages & posts #18

daviddarnes opened this issue Nov 3, 2014 · 2 comments

Comments

@daviddarnes
Copy link
Contributor

I've been using this JS plugin with a documentation site, which meant I needed to search through pages rather than posts. This is the code I used:


---
title: nil

---
{% assign items = site.pages %}
[
    {% for item in items %}{% if item.url != '/search.json' %}{% if item.url != '/css/screen.css' %}
        {
            "title"    : "{{ item.title }}",
            "content"  : {{ item.content | jsonify }},
            "url"      : {{ item.url | jsonify }}
        }{% unless forloop.last %},{% endunless %}{% endif %}{% endif %}
    {% endfor %}
]

I've swapped the posts for pages, but using an assigned variable so I'm not repeating myself. I've also used the jsonify filter to clean up the content and url incase it has anything that will clash with json syntax.

The only problem is that I had to exclude the search.json and the outputted CSS using two conditional statements, I'm not sure why it thinks they are pages. Thoughts?

@absolutejam
Copy link

I don't know if this is what you're after, but have you tried using for {% page in site.html_pages %}?

I'm trying that approach and it seems to also include my .md pages (I'm guessing it's because they become .html once served). I don't know if this is standard behaviour, or if it's because I have supplied the 'permalink' front-matter on them (Eg. permalink: /about/ in about.md).

I have however had issues with using the above approach as it seems to ignore the limit argument, so I always have 10 suggested items (Which is too many for my liking). If I use just site.html_pages or use it along side site.posts, I get this issue.

My code is:

---

---
[
  {% for post in site.posts %}
    {
      "title"    : "{{ post.title | escape }}",
      "category" : "{{ post.category }}",
      "tags"     : "{{ post.tags | array_to_sentence_string }}",
      "url"      : "{{ site.baseurl }}{{ post.url }}",
      "date"     : "{{ post.date }}",
      "formatted-date" : "{{ post.date | date: '%b %-d, %Y' }}",
      "type"     : "Post"
    },
  {% endfor %}
  {% for page in site.html_pages %}
  {
    "title"    : "{{ page.title | escape }}",
    "url"      : "{{ site.baseurl }}{{ page.url }}",
    "type"     : "Page"
    }{% unless forloop.last %},{% endunless %}
  {% endfor %}
]

@daviddarnes daviddarnes changed the title Allow searching of pages Allow searching of pages & posts Nov 24, 2014
@daviddarnes
Copy link
Contributor Author

This looks pretty good! Thanks for your reply. Due to the project I am working on I have had to make a slight modification to how the json file outputs, I've had to figure out a way to exclude pages from the search. Here's what I have currently:

---
searchable: false
---
{% assign items = site.pages %}
[
    {% for item in items %}{% if item.searchable == false %}
        {
            "title"     : " ",
            "content"   : " ",
            "url"       : " ",
            "searchable": "{{ item.searchable }}"
        }{% else %}
        {
            "title"     : {{ item.title | jsonify }},
            "content"   : {{ item.content | jsonify }},
            "url"       : {{ item.url | jsonify }},
            "searchable": "{{ item.searchable }}"
        }{% endif %}{% unless forloop.last %},{% endunless %}
    {% endfor %}
]

Due to how the loop works I couldn't exclude 'non-searchable' pages from the json file as the forloop.last would then be incorrect and the trailing comma would sometimes fail to appear.

I believe I also had the 10 items limit issue, my new sample code avoids that but in not really in a clean way.

I'm sorry to go into so much detail with my case use, but I feel that explaining my real world usage of the plugin it will help with its development.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants