christian-fei / Simple-Jekyll-Search

A JavaScript library to add search functionality to any Jekyll blog.
https://christian-fei.github.io/Simple-Jekyll-Search/
MIT License
1.34k stars 203 forks source link

Can we add custom fields to search.json? #171

Closed digitalformula closed 3 years ago

digitalformula commented 3 years ago

Hi - I'm not sure where else to post this, so hoping an issue is OK.

Can we add custom fields to search.json? For example, all my pages have "is_book: yes" or "is_book: no" and I'd like to pass that variable through to search.json so that it can be evaluated when the JS is embedded. I'm also trying to pass a variable called subtitle.

For subtitle - using {subtitle} in the script just returns literally {subtitle} even if the variable is set. For is_book I get a Javascript error that says "Uncaught TypeError: str.trim is not a function" on line 86 of simple-jekyll-search.json, but that line is only triggered if str is not false. That suggests the string is being passed through.

Here's the relevant part of my search.json:

{
    "title": {{page.title | jsonify}},
    "subtitle": {{page.subtitle | jsonify}},
    "is_book": {{page.is_book | jsonify}},
    "content": {{page.content | strip_html | jsonify}},
    "link": "{{ page.url }}",
    "date": {{ page.date | jsonify }},
    "excerpt": {{ page.description | jsonify }}
}

Interestingly, if I make an intentional type and say {{page.isbook}} (incorrect variable name), the JS error is not thrown. That suggests it is something to with the parsing/evaluation of that specific variable. Having {{page.subtitle}} there, while it doesn't work, doesn't throw an error.

Then, in the markdown file that embeds the script, I'd like to use something like this:

<script>
    SimpleJekyllSearch({
        ...
        searchResultTemplate: "{is_book}" == "no" ? '<li><a href="{link}" title="{excerpt}">(BOOK) {title} - {subtitle}</a></li>' : '<li><a href="{link}" title="{excerpt}">{title}</a></li>',
        ...
    })
</script>

Can that be done? If so, how?

digitalformula commented 3 years ago

Anyone?

christian-fei commented 3 years ago

hey, this is currently not supported, and I don't plan to add this feature, since this is not about custom fields, but about conditional rendering of the template.

perhaps you could try to achieve the same with CSS?