vsoch / docsy-jekyll

A Jekyll version of the beautiful Docsy!
https://vsoch.github.io/docsy-jekyll/
Apache License 2.0
199 stars 142 forks source link

Need some help with Jekyll; no idea how to reach you #63

Closed mig281 closed 2 years ago

mig281 commented 2 years ago

Dr. Sochat, I wanted to ask you a question about Jekyll, but I can't locate an email address for you. You seem to have solved the problem of the trailing JSON comma quite elegantly, but I'm having trouble applying your solution to my problem.

Would you be willing to tell me what I'm doing wrong here? I would be much obliged, because I don't want to insert any empty JSON elements either...

Here is the source of my woe, search.json:

[
{% for page in site.html_pages %}
{% unless page.search == "exclude" %}
{
"title": "{{ page.title | escape }}",
"tags": "{{ page.tags }}",
"keywords": "{{page.keywords}}",
"url": "{{ page.url | remove: "/"}}",
"summary": "{{page.summary | strip }}"
}
{% unless forloop.last and site.posts.size < 1 %},{% endunless %}
{% endunless %}
{% endfor %}

{% for post in site.posts %}

{
"title": "{{ post.title | escape }}",
"tags": "{{ post.tags }}",
"keywords": "{{post.keywords}}",
"url": "{{ post.url | remove: "/" }}",
"summary": "{{post.summary | strip }}"
}
{% unless forloop.last %},{% endunless %}
{% endfor %}

]

Thank you in advance for any help!

vsoch commented 2 years ago

You can certainly reach me on a GitHub issue without needing my email!

For trailing commas in json lists I usually just do:

{% if forloop.last %}{% else %},{% endif %}

and you shouldn't need to check if the posts < 1, if that's the case it would == 0 and wouldn't even enter the loop.

mig281 commented 2 years ago

@vsoch (My apologies. I wasn't sure if it was appropriate to open an issue not directly related to a project.)

Alas, what you propose doesn't seem to be working for me (although your suggestion about checking the number of posts makes perfect sense—I don't even have any posts; it's all pages). Unfortunately, the following still doesn't work and I get the trailing comma...

Do you think I need to resort to the complex hack you describe in your post?

[
{% for page in site.html_pages %}
{% unless page.search == "exclude" %}
{
"title": "{{ page.title | escape }}",
"tags": "{{ page.tags }}",
"keywords": "{{page.keywords}}",
"url": "{{ page.url | remove: "/"}}",
"summary": "{{page.summary | strip }}"
}
{% if forloop.last  %}{% else %},{% endif %}
{% endunless %}
{% endfor %}

]
vsoch commented 2 years ago

Can you throw your code (and empty posts) somewhere on GitHub so I can take a look and reproduce?

mig281 commented 2 years ago

You can fork the whole repo here: https://github.com/Qumulo/docs/ The unmodified code is here, in situ: https://github.com/Qumulo/docs/blob/gh-pages/search.json

vsoch commented 2 years ago

Yeah this is the problem I ran into where the unless is biting you because the forloop counter doesn't matter - you can't be sure that you're at the last one (so the last one doesn't render and you get a comma from the previous).

I would do a derivation of what I describe here - https://vsoch.github.io/2019/jekyll-lists/ you bascailly need to assemble all your things to render first, and then have one clean loop where you can be sure every item is followed by a comma (and then not at the end).