How to get sorted tag_list in jekyll

I am using jekyll-bootstrap to blog on GitHub.

I would like to have a sorted tag_list. First comes the tag with the most posts. Then I can have a display that shows the first tags with larger font sizes and the last tags with smaller font size. And I also need a splice function.

If in python / Jinja2, I would like some kind of code to look like this:

{% for tag in sorted_tags[:10] %} <li style="font-size:{{ tag.count }}px;">{{ tag.name }}</li> {% endfor %} 

What is the equivalent implementation in ruby ​​/ jekyll?

+4
source share
4 answers

I thought the tag array was sorted. Suppose you can do this:

 {% for tag in site.tags %} <li style="font-size: {{ tag[1].size }}px">{{ tag[0] }}</li> {% endfor %} 

It seems a bit hacky, but it should work. Unfortunately, Liquid currently does not allow you to sort arrays in your templates. If you want to sort by array, you probably have to write a plugin to do this - it should not be too complicated. In fact, there is an existing plugin for sorting accessories that can do this: https://github.com/krazykylep/Jekyll-Sort

0
source

I needed to do this only in one place, on the page where my list of tags was indicated, so I wrote it as a Jekyll filter:

tag_index.html

 <h2>Posts by Tag</h2> <ul class="tags-list"> {{ site.tags | render_tags_list }} </ul> 

_plugins / filters.rb

 module Jekyll module Filters def render_tags_list(tags) sorted_tags = tags.keys.sort_by! { |tag| tag.downcase } str = '' sorted_tags.each { |tag| str << '<li>' + tags[tag].size.to_s + ' - <a href="/tag/' + tag + '">' + tag + '</a></li>' } str end end end 

You could just let the filter return sorted_tags above if you want to maintain a better separation between the view logic and the programming logic, but my case was very simple. Trying to re-access the hash value using a special key using Liquid templating was not a very compressed process, or maybe I just did it wrong, but in Ruby it was much easier.

0
source

I host my blog on github and wanted to find a solution to sort the tag list that does not involve any jekyll plugins, since Github does not allow plugins to be configured (jekyll bootstrap also tries this). My post here does not answer the question, since I am sorting by tag name, NOT by size. You can adapt this method to output the tag size as well as a string, and then execute some fancier splitting function to get a different sort order (but it will be messy).

I was able to do this with the following code:

 {% capture tagString %}{% for tag in site.tags %}{{ tag[0] }}{% unless forloop.last %}|{% endunless %}{% endfor %}{% endcapture %} {% assign tags = tagString | split: '|' | sort: 'downcase' %} <div id="cloud"> {% for tag in tags %} {% assign number = site.tags[tag].size %} {% assign slug = tag | downcase | replace: ' ', '_' %} <span class="{% if number == 1 %}small{% elsif number <= 5 %}medium{% elsif number <= 10 %}large{% else %}huge{% endif %}"> <a href="#tag-{{ slug }}">{{ tag | downcase }}</a> </span> {% endfor %} </div> 

This is odd as I commit the tag string (using | as a separator) and then use it to create an array. After this point (in a loop) I can refer to the tag as a tag and a list of sites that use this tag as site.tags[tag] .

I use this on my blog: https://github.com/kelsin/kelsin.imtqy.com/blob/master/tags/index.html

The rest of the code is how I selected the tag cloud on my tag page. Hope this helps someone else looking for a solution (without plugins)!

0
source

This is how I sort by the number of posts in the tag (top-down), without any plugins (i.e. compatible with GitHub Pages).

It also works when your tag names contain spaces; only , and : are forbidden characters (but you can easily change them).

 {% capture counts_with_tags_string %}{% for tag in site.tags %}{{ tag[1] | size | prepend:"000000" | slice:-6,6 }}:{{ tag[0] }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %} {% assign counts_with_tags = counts_with_tags_string | split:"," | sort | reverse %} <ol> {% for count_with_tag in counts_with_tags %} {% assign tag = count_with_tag | split:":" | last %} {% assign count = site.tags[tag] | size %} <li><a href="/blog/tags/{{ tag | slugify }}">{{ tag }} ({{ count }})</a></li> {% endfor %} </ol> 

This is super rude. What does he do:

  • counts_with_tags_string set to a string of type 000005:first_tag,000010:second_tag,000002:third_tag . Zero stocks are generated using a filter chain | prepend:"000000" | slice:-6,6 | prepend:"000000" | slice:-6,6 | prepend:"000000" | slice:-6,6 .
  • This is comma separated and sorted lexicographically, which works due to zero padding. The result is assigned to counts_with_tags .
  • Finally, we iterate over the elements and split each into : to search for the original tag name. We could find the invoice in the same way, but since it has zero padding, it’s easier to view it using site.tags[tag] | size site.tags[tag] | size .
0
source

Source: https://habr.com/ru/post/1441470/


All Articles