Jekyll Pagination Path Doesn't Include Page Number - ruby

I am attempting to create pagination in Jekyll under the path '/notes/'.
In my _config.yml, I have:
paginate: 2
paginate_path: "/notes/:num/"
In my /notes/index.html, I have:
<div class="pagination">
{% if paginator.previous_page %}
Previous
{% else %}
<span class="previous section link">Previous</span>
{% endif %}
<span class="page_number section">{{ paginator.page }} / {{ paginator.total_pages }}</span>
{% if paginator.next_page %}
Next
{% else %}
<span class="next section link">Next</span>
{% endif %}
</div>
When I load http://localhost:4000/notes, I see that the 'Previous' link is not linking to /notes/2, but instead it simply links to /notes/, the page that I am already on. Additionally, when looking in the generated _site folder, I see that /notes/2 has not been created, despite having more than two posts created.
So, I suppose my question is, how can I fix this and get jekyll to paginate properly?

Related

How to filter Taxonomies using Rust-based Zola / Tera?

I have recently discovered Zola and Tera (Rust frameworks for statically-generated websites) and found them amazing.
I'm trying to filter specific category pages to display in a section on the same page. To illustrate, I wrote some code like this:
<div class="content">
{% block content %}
<div class="list-posts">
{% for page in section.pages %}
{% for key, taxonomy in page.taxonomies %}
{% if key == "categories" %}
{% set categories = taxonomy %}
{% for category in categories %}
{% if category == "rust" %}
<article>
<h3 class="post__title">{{ page.title }}</h3>
</article>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endblock content %}
</div>
There should be MULTIPLE sections of the code above for different categories, e.g. "rust", "java", etc.
I wrote the code to explain my question, but it isn't the way I want it (and it doesn't work when the sections are duplicated).
How do I do the filtering of the particular category when the sections/pages are loaded?
The front-matter metadata in the content file is:
title = "A web page title"
[taxonomies]
categories = ["rust"]
If you see my example code above, I have to access it first via a hash map, then an array, in order to filter all pages which is "rust".
The filter below doesn't work:
for page in section.pages | filter(attribute="taxonomies.categories", value="rust"
I managed to resolve it. First, I did tests like this:
HTML test print output
{% set categories = get_taxonomy(kind="categories") %}
{% set rustItems = categories.items | filter(attribute="name", value="rust") %}
{% set javaItems = categories.items | filter(attribute="name", value="java") %}
{{ rustItems[0].pages | length }}
<br>
{{ rustItems[0].pages[0].title }}
<br>
{{ rustItems[0].pages[1].title }}
<br>
I was able to pick up the title as set in the .md file.
So I moved on further and I did:
{% set categories = get_taxonomy(kind="categories") %}
{% set category = categories.items | filter(attribute="name", value="business") | first %}
{% for page in category.pages %}
{{ page.title }}
... etc.
The above code will filter the pages for category taxonomy.

Create Jekyll link from YAML array

I have a YAML array in a file called navigation.yml as follows:
docs:
- title: Home
url: index.md
id: index
- title: Support
url: support.html
id: support
- title: About
url: about.md
id: about
I am creating a navigation bar as follows:
<section id="navigation" class="clearfix">
{% for item in site.data.navigation.docs %}
<span>{{ item.title }}</span>
{% endfor %}
</section>
What should I put in place of index.md to get the item.url that I want from the YAML file.
I am totally new to GitHub Pages, YAML, and Jekyll.
At the moment, the link tag doesn't seem to support variables.
There's a pull request trying to change this, but it has not been merged into the main Jekyll repo yet.
So if you want to do this now, you need to use some tricks.
The solution suggested by flyx in his comment (replace {% link index.md %} by {{ item.url }}) basically works, but shows the original filename written in the data file.
⇒ If index.md is automatically renamed to index.html while rendering the site, your link won't work anymore.
(or if support.html becomes support/index.html)
That's probably why you wanted to use the link tag instead.
Without using the link tag, you need to loop your data file, loop through all pages to find the respective page, and show that page's actual URL in your link:
<section id="navigation" class="clearfix">
{% for item in site.data.navigation.docs %}
{% for page in site.pages %}
{% if page.path == item.url %}
<span>{{ item.title }}</span>
{% endif %}
{% endfor %}
{% endfor %}
</section>
This even takes stuff like explicitly set permalinks (permalink: /whatever/ in the page's front matter) into account.

How to use Jekyll's pagination tool to sort through a subdirectory within _posts

Having trouble getting pagination to work properly in Jekyll using liquid markdown.
Used the paginator function and the posts do paginate but I'm trying to figure out a way to filter out what paginates.
Within my _posts directory I have a blog sub directory and a news subdirectory. I'm trying to only paginate the articles in blog and not news.
{% assign posts = site.posts | where: 'category','Blog' %}
<ul class="list-unstyled blog-list">
<!-- This loops through the paginated posts -->
{% for post in paginator.posts %}
{% assign posts = site.posts | where: 'category','Blog' %}
{% if post.category == 'Blog' %}
Do Stuff
{% endif %}
{% endfor %}
<!-- Pagination links -->
<div class="pagination">
{% if paginator.previous_page == 1 %}
Previous
{% endif %}
{% if paginator.previous_page > 1%}
Previous
{% endif %}
<span class="page_number ">Page: {{ paginator.page }} of {{ paginator.total_pages }}</span>
{% if paginator.next_page %}
Next
{% endif %}
</div>
</ul>
I was trying to retrieve posts that have the category 'Blog' but when I run it, it paginates to the 7 articles per page I wanted, but some pages have less then 7 articles or no articles show up at all.
I think somethings wrong with my loop logic but I'm not sure what.
Maybe it's grabbing all the articles in _posts regardless but not showing the news articles on the page?
Thanks!
Jekyll Paginate does not have filtering features therefore it's not possible to filter paginated posts by category. I'd recommend You to look into Octopress Paginate plug-in which provides the feature.

How do I add a link to an external site the main navbar in CKAN

I know that I can customize the set of navigation links at the top of the page in the standard template by overriding the header_site_navigation_tabs block e.g. as in the [datahub.io customization](https://github.com/okfn/ckanext-datahub/blob/3d64748fc1f3c4499780b199e971a5929ba69315/ckanext/datahub/templates/header.html#L9
)
{% block header_site_navigation_tabs %}
{{ h.build_nav_main(
('search', _('Datasets')),
('organizations_index', _('Organizations')),
('about', _('About'))
) }}
{% endblock %}
However, I want to add a link to this list to an external website? Can I just do (see extra entry at the end):
{% block header_site_navigation_tabs %}
{{ h.build_nav_main(
('search', _('Datasets')),
('organizations_index', _('Organizations')),
('about', _('About'))
('http://blog.datahub.io/', 'Blog')
) }}
{% endblock %}
No, you can't do that. The helper method looks for routes declared internally (config['routes.named_routes']). You can, however, simply add a li element, so the whole block would look like this
{% block header_site_navigation %}
<nav class="section navigation">
<ul class="nav nav-pills">
{% block header_site_navigation_tabs %}
{{ h.build_nav_main(
('search', _('Datasets')),
('organizations_index', _('Organizations')),
('about', _('About'))
) }}
<li>Blog</li>
{% endblock %}
</ul>
</nav>
{% endblock %}

Filter site.related_posts in Jekyll

I am very new to Jekyll and Ruby (yet, very excited).
Without using a plugin, I am trying to find a way to filter the site.related_posts.
For example, I am reading the post with title Foo and categories A, B.
The site contains in total 3 posts:
Foo (Categories: A, B)
Bar (Categories: A, C, D)
Zoo (Categories: B, F)
By the default, in Jekyll we do this:
{% for post in site.related_posts limit:5 %}
{% endfor %}
However, the above code returns all the (3) posts.
A post contains many categories, so categories should be an array.
How can I modify the code and return only those whose categories intersect with the current post's categories?
(In this example, I would like the code to return only Foo and Zoo.)
I don't have the ability to test this right now, but something like this will work given Liquid's limited syntax:
{% for post in site.related_posts limit:5 %}
{% assign match = false %}
{% for category in post.categories %}
{% if page.categories contains category %}
{% assign match = true %}
{% endif %}
{% endfor %}
{% if match %}
<li>{{ post.title }}</li>
{% endif %}
{% endfor %}
Make sure each post has a category in the YAML front matter, then add this to where you would like to show the post relating CATEGORY_NAME:
{% for post in site.categories.CATEGORY_NAME %}
<li>
<a href="{{ post.url }}">
<img src="{{ post.thumbnail }}">
<p>{{ post.excerpt }}</p>
</a>
</li>
{% endfor %}

Resources