gpt4 book ai didi

tags - 无论大小写,Jekyll 标签都按字母顺序排列

转载 作者:行者123 更新时间:2023-12-05 05:12:21 24 4
gpt4 key购买 nike

我在博客中使用 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>

最佳答案

你真的很接近!有一些错误,但其他一切都是正确的。

在创建标签数组的第一部分中,连接到一个数组比创建一个字符串然后将其拆分为一个数组要容易一些。

// 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>

关于tags - 无论大小写,Jekyll 标签都按字母顺序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com