Jekyll is not reading site.posts


Hi, Jekyll is not reading site.posts. As you can see, my search.html is like this:


<script src="assets/js/simple-jekyll-search.min.js" type="text/javascript"></script>

<!-- Configuration -->
<script>
var sjs = SimpleJekyllSearch({
  searchInput: document.getElementById('search-input'),
  resultsContainer: document.getElementById('results-container'),
  json: '/search.json',
  success: function(){}
})
</script>

And my search.json is:

---
---
[
  {% for post in site.posts %}
    {
      "title"    : "{{ post.title | escape }}",
      "category" : "{{ post.category }}",
      "tags"     : "{{ post.tags | join: ', ' }}",
      "url"      : "{{ site.baseurl }}{{ post.url }}",
      "date"     : "{{ post.date }}"
    } {% unless forloop.last %},{% endunless %}
  {% endfor %}
]

So, I can’t see the search results.

My repo: https://github.com/fsuaterdogan/help-tourhunter-com

I don’t see a _posts folder - looks like you are using collections instead which are not part of site.posts.

Try site.documents instead of site.posts.

Indeed. Create _posts folder and contents based on a Jekyll posts tutorial (pay attention to naming of posts as 2020-04-04-my-post.md for example using date and the start and no underscores .

Or reference site.data.my_data

Thanks, @rdyar, but unfortunately it is not working, it writes:

Uncaught TypeError: Cannot read property ‘search’ of undefined

Could some of my posts be in Russian language? I think can’t read because it’s in Russian

but did you get the search.json file populated? one problem at a time… that is a JS error.

Maybe populate the json file manually with one or two bits of info and see if you can get it to work - that would eliminate any language issues.

@rydar Yes this is. I populated the file. I did it on search.html

<div class="form-group mx-auto  form-search"><i class="fas fa-search"></i>
    <input type="text" class="form-control" id="search-input" placeholder="{{ site.data.i18n[page.lang].main.welcomeSection.searchbar }}">
    <br/>
    <ul id="results-container"></ul>
</div>

<script src="{{ site.baseurl }}/assets/js/simple-jekyll-search.min.js"></script>

<!-- Configuration -->
<script>
window.sjs = SimpleJekyllSearch({
  searchInput: document.getElementById('search-input'),
  resultsContainer: document.getElementById('results-container'),
  json: '/search.json'
})
</script>

If there is something I am looking for, it does not show a result. If there is nothing I am looking for, it says No match found.

This is my JSON:

[
  
    {
      "title"    : "A",
      "category" : "",
      "tags"     : "",
      "url"      : "/a/",
      "date"     : "2020-04-11 18:08:51 +0000"
    } ,
  
    {
      "title"    : "B",
      "category" : "",
      "tags"     : "",
      "url"      : "/b",
      "date"     : "2020-04-11 18:08:51 +0000"
    } ,
  
    {
      "title"    : "C",
      "category" : "c",
      "tags"     : "",
      "url"      : "/c",
      "date"     : "2020-04-11 18:08:51 +0000"
    }

]

are you pushing changes back to GH? the search.json file there still has site.postsz and has not changed in 6 months.

is the live site using your latest code? hard to test if you are just working locally and not pushing changes out.

it sounds like you are not getting an error now, so maybe there is something else going on.

Hi @rdyar, I found it. I didnt initialize SimpleJekyllSearch plugin after the DOM loaded. Thanks.