大小写字母顺序的Jekyll标签

时间:2019-02-10 15:39:04

标签: tags jekyll

我在博客上使用Jekyll,并且有一些主题标签(例如:CSS,JavaScript,前端,可访问性等)。我希望这些字符按字母顺序显示,无论大小写:辅助功能,CSS,前端,JavaScript等。

我是Jekyll的新手,但是我还没有发现任何能证明如何实现此目标的东西。这就是我当前的代码。任何建议都会有所帮助。

[//]: # (Get the tag name for every tag on the site and set them to the site_tags variable.)
{% capture site_tags %}{% for tag in site.tags %}{{ tag | first }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}

[//]: # (tag_words is a sorted array of the tag names.)
{% assign tag_words = site_tags | split:',' | sort %}

[//]: # (Build the Page)

[//]: # (List of all tags)
<section>
  <ul>
    {% for item in (0..site.tags.size) %}{% unless forloop.last %}
      {% capture this_word %}{{ tag_words[item] }}{% endcapture %}
      <li>
        <a href="#{{ this_word | cgi_escape }}" class="tag">{{ this_word }}
          <span>({{ site.tags[this_word].size }})</span>
        </a>
      </li>
    {% endunless %}{% endfor %}
  </ul>
</section>

[//]: # (Posts by tags)
<section class="tags">
  {% for item in (0..site.tags.size) %}{% unless forloop.last %}
    {% capture this_word %}{{ tag_words[item] }}{% endcapture %}
    <h3 id="{{ this_word | cgi_escape }}">{{ this_word }}</h3>
    {% for post in site.tags[this_word] %}{% if post.title != null %}
      <div class="row">
        <span>
          <a href="{{ post.url }}">{{ post.title }}</a>
        </span>
        <span class="post-date archive-date">
          {{ post.date | date: "%b %-d, %Y" }}
        </span>
      </div>
    {% endif %}{% endfor %}
  {% endunless %}{% endfor %}
</section>

2 个答案:

答案 0 :(得分:0)

您实际上真的很亲密!有一些错误,但是您还拥有其他所有功能。

在制作标签数组的第一部分中,连接到数组而不是创建字符串然后将其拆分成数组要容易一些。

// create empty array
{% assign tags = '' | split: '' %}

// iterate through tags, get tag name and make into an array, concat arrays
{% for tag in site.tags %}
    {% assign tagName = tag | first | split: ' ' %}
    {% assign tags = tags | concat: tagName %}
{% endfor %}

// sort tags
{% assign tags = tags | sort %}

现在您有了一个标签数组,您可以使用for循环遍历它们。无需迭代item=0...n并使用tags[item]

// create list of tags and number of posts with that tag
<section>
    <ul>
        {% for tag in tags %}
            {% assign postCount = site.tags[tag] | size %}
            <li>
                <a href="#{{ tag | cgi_escape }}" class="tag">
                    {{ tag }}
                    <span>({{ postCount }})</span>
                </a>
            </li>
        {% endfor %}
    </ul>
</section>

// create list of posts per title (posts are newest to oldest)
<section class="tags">
    {% for tag in tags %}
        <h3 id="{{ tag | cgi_escape }}">{{ tag }}</h3>
        {% for post in site.tags[tag] %}
            {% if post.title != null %}
                <div class="row">
                    <span>
                        <a href="{{ post.url }}">{{ post.title }}</a>
                    </span>
                    <span class="post-date archive-date">
                        {{ post.date | date: "%b %-d, %Y" }}
                    </span>
                </div>
            {% endif %}
        {% endfor %}
    {% endfor %}
</section>

答案 1 :(得分:-1)