I am using the standard jekyll installation to maintain a blog, everything is going fine. Except I would really like to tag my posts.
我正在使用标准的jekyll安装来维护一个博客,一切正常。除了我真的想标记我的帖子。
I can tag a post using the YAML front matter, but how do I generate pages for each tag that can will list all posts for a tag?
我可以使用YAML前面的问题来标记一个帖子,但是我如何为每个可以列出标签的帖子的标签生成页面呢?
6 个解决方案
#1
14
This gist will generate a page per category for you: https://gist.github.com/524748
这个要点将为您生成每个类别的页面:https://gist.github.com/524748
It uses a Jekyll Generator plugin, plus a Page subclass.
它使用Jekyll生成器插件和页面子类。
#2
73
Here is a solution with alphabetically sorted tags on a single page.
It uses Liquid only, which means that it works on GitHub Pages:
这是一个在单页上按字母顺序排序的解决方案。它只使用液体,这意味着它在GitHub页面上工作:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
<h3 id="{{ tag }}">{{ tag }}</h3>
<ul>
{% for post in site.tags[tag] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
You can see it in action here.
你可以在这里看到它的作用。
EDIT:
编辑:
There's also a way to generate a separate page for each tag without plugins (which will work on GitHub Pages).
还有一种方法可以为每个标记生成一个单独的页面,而不需要插件(这将用于GitHub页面)。
I have a more detailed explanation on my blog:
Separate pages per tag/category with Jekyll (without plugins)
我在我的博客上有一个更详细的解释:每个标签/类别都有Jekyll(没有插件)
First, you need a new layout file:
首先,您需要一个新的布局文件:
/_layouts/tagpage.html
:
---
layout: default
---
<h1>{{ page.tag }}</h1>
<ul>
{% for post in site.tags[page.tag] %}
<li>
{{ post.date | date: "%B %d, %Y" }}: <a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
With this layout file, you can add a new tag page by adding a new file with just two lines of YAML front-matter.
通过这个布局文件,您可以添加一个新的标签页,添加一个新的文件,只有两行YAML前置事项。
Here's an example for the jekyll
tag:
这是杰基尔标签的一个例子:
/tags/jekyll/index.html
:
---
layout: tagpage
tag: jekyll
---
The only disadvantage of this approach: each time you use a new tag for the first time, you have to remember to create a new two-line file for it.
这种方法的唯一缺点是:每次第一次使用新标记时,都必须记住为它创建一个新的两行文件。
To generate the root index file (i.e. the list of tags that links to /tags/jekyll/index.html
etc.), you can use a similar solution like the one on top of this answer where I generate a single page with alphebetically sorted tags:
生成根索引文件(即链接到/标记/jekyll/索引的标记列表)。),你可以使用类似的解决方案,比如上面的那个,我用alphebe排序标签生成一个页面:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
<a href="/tags/{{ tag }}/">{{ tag }}</a><br>
{% endfor %}
This will generate a list of links like this:
这将生成如下链接列表:
<ul>
<li><a href="/tags/.net/">.net</a></li>
<li><a href="/tags/authentication/">authentication</a></li>
<li><a href="/tags/backup/">backup</a></li>
</ul>
Note that this solution uses a blank to split tags, so it doesn't work when your tags contain blanks and Yevgeniy Brikman's comment applies here as well.
注意,这个解决方案使用空白来分割标记,所以当您的标记包含空格时,它就不起作用了,Yevgeniy Brikman的注释也适用于这里。
#3
8
Have a look at sites using jekyll. There are a few custom forks which have implemented tagging functionality, hopefully also in the way you want :-)
使用jekyll查看站点。有一些定制的福克斯实现了标签功能,希望也以你想要的方式:-)
#4
5
I had the same question, and stumbled upon this: http://gist.github.com/143571.
我也有同样的问题,偶然发现:http://gist.github.com/143571。
It's a rake task which generates a tag list. I modified it slightly, and my version is at: http://github.com/mattfoster/mattfoster.github.com/blob/master/Rakefile.
它是一个rake任务,生成一个标记列表。我稍微修改了一下,我的版本是:http://github.com/mattfoster/mattfoster.github.com/blob/master/Rakefile。
Whilst this doesn't give you a page per tag, you can use anchors, which is half way there!
虽然这并没有给你每个标签一个页面,但你可以使用锚点,这是一半!
#5
1
I use the great Jekyll Tagging plugin that automatically generates a tags cloud and tag pages. Easy to install and use.
我使用Jekyll标签插件自动生成标签云和标签页。易于安装和使用。
Here is a page for the "photo" tag on my blog (in french), and you can see the tags cloud in the bottom.
这是我博客上“照片”标签的一个页面(法语),你可以在底部看到标签云。
#6
1
Based on Christian's answer above I made a bash script that does what he described.
基于克里斯蒂安的回答,我制作了一个bash脚本,按照他的描述。
https://github.com/ObjectiveTruth/objectivetruth.github.io/blob/master/rebuild_tags.sh
https://github.com/ObjectiveTruth/objectivetruth.github.io/blob/master/rebuild_tags.sh
Be sure to have the accompanying 14 line vim script in the /non_website_resources/
directory
确保在/non_website_resources/目录中有附带的14行vim脚本
AND
和
Make the /_layouts/tagpage.html
shown in Christian's answer above but rename it to /_layouts/tag_pages.html
使/ _layouts / tagpage。上面克里斯蒂安的答案中显示的html,但是将它重命名为/_layouts/tag_pages.html
File structure should be like this:
文件结构应如下:
.jekyll_website_root
├── _posts
├── _layout
│ ├── tag_pages.html
├── rebuild_tags.sh
Run from the root directory ./rebuild_tags.sh
从根目录运行
If you get permission denied error be sure to run chmod 777 rebuild_tags.sh
如果您获得了拒绝权限错误,请确保运行chmod 777 rebuild_tag .sh
If you look at scripts comments its fairly simple:
如果你看一下剧本评论,它相当简单:
-
Uses
sed
to find all the tags in every.md
file in_post
directory使用sed在_post目录中查找每个.md文件中的所有标记。
-
Uses
sed
to massage the data to proper format使用sed将数据按适当格式进行按摩。
-
Takes all the unique tags and makes a directory and a
index.html
for each获取所有唯一的标记并创建一个目录和索引。html为每个
This way, if you have any new tags, just run the script to rebuild the pages before pushing to github
通过这种方式,如果您有任何新的标记,请在向github推送之前运行脚本以重新构建页面
A nice simple non-plugin way to do tags
一个很好的简单的非插件方式来做标签
EDIT
编辑
Removed dependency on other files. Just need the one script!
删除对其他文件的依赖。只需要一个脚本!
#1
14
This gist will generate a page per category for you: https://gist.github.com/524748
这个要点将为您生成每个类别的页面:https://gist.github.com/524748
It uses a Jekyll Generator plugin, plus a Page subclass.
它使用Jekyll生成器插件和页面子类。
#2
73
Here is a solution with alphabetically sorted tags on a single page.
It uses Liquid only, which means that it works on GitHub Pages:
这是一个在单页上按字母顺序排序的解决方案。它只使用液体,这意味着它在GitHub页面上工作:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
<h3 id="{{ tag }}">{{ tag }}</h3>
<ul>
{% for post in site.tags[tag] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
You can see it in action here.
你可以在这里看到它的作用。
EDIT:
编辑:
There's also a way to generate a separate page for each tag without plugins (which will work on GitHub Pages).
还有一种方法可以为每个标记生成一个单独的页面,而不需要插件(这将用于GitHub页面)。
I have a more detailed explanation on my blog:
Separate pages per tag/category with Jekyll (without plugins)
我在我的博客上有一个更详细的解释:每个标签/类别都有Jekyll(没有插件)
First, you need a new layout file:
首先,您需要一个新的布局文件:
/_layouts/tagpage.html
:
---
layout: default
---
<h1>{{ page.tag }}</h1>
<ul>
{% for post in site.tags[page.tag] %}
<li>
{{ post.date | date: "%B %d, %Y" }}: <a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
With this layout file, you can add a new tag page by adding a new file with just two lines of YAML front-matter.
通过这个布局文件,您可以添加一个新的标签页,添加一个新的文件,只有两行YAML前置事项。
Here's an example for the jekyll
tag:
这是杰基尔标签的一个例子:
/tags/jekyll/index.html
:
---
layout: tagpage
tag: jekyll
---
The only disadvantage of this approach: each time you use a new tag for the first time, you have to remember to create a new two-line file for it.
这种方法的唯一缺点是:每次第一次使用新标记时,都必须记住为它创建一个新的两行文件。
To generate the root index file (i.e. the list of tags that links to /tags/jekyll/index.html
etc.), you can use a similar solution like the one on top of this answer where I generate a single page with alphebetically sorted tags:
生成根索引文件(即链接到/标记/jekyll/索引的标记列表)。),你可以使用类似的解决方案,比如上面的那个,我用alphebe排序标签生成一个页面:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
<a href="/tags/{{ tag }}/">{{ tag }}</a><br>
{% endfor %}
This will generate a list of links like this:
这将生成如下链接列表:
<ul>
<li><a href="/tags/.net/">.net</a></li>
<li><a href="/tags/authentication/">authentication</a></li>
<li><a href="/tags/backup/">backup</a></li>
</ul>
Note that this solution uses a blank to split tags, so it doesn't work when your tags contain blanks and Yevgeniy Brikman's comment applies here as well.
注意,这个解决方案使用空白来分割标记,所以当您的标记包含空格时,它就不起作用了,Yevgeniy Brikman的注释也适用于这里。
#3
8
Have a look at sites using jekyll. There are a few custom forks which have implemented tagging functionality, hopefully also in the way you want :-)
使用jekyll查看站点。有一些定制的福克斯实现了标签功能,希望也以你想要的方式:-)
#4
5
I had the same question, and stumbled upon this: http://gist.github.com/143571.
我也有同样的问题,偶然发现:http://gist.github.com/143571。
It's a rake task which generates a tag list. I modified it slightly, and my version is at: http://github.com/mattfoster/mattfoster.github.com/blob/master/Rakefile.
它是一个rake任务,生成一个标记列表。我稍微修改了一下,我的版本是:http://github.com/mattfoster/mattfoster.github.com/blob/master/Rakefile。
Whilst this doesn't give you a page per tag, you can use anchors, which is half way there!
虽然这并没有给你每个标签一个页面,但你可以使用锚点,这是一半!
#5
1
I use the great Jekyll Tagging plugin that automatically generates a tags cloud and tag pages. Easy to install and use.
我使用Jekyll标签插件自动生成标签云和标签页。易于安装和使用。
Here is a page for the "photo" tag on my blog (in french), and you can see the tags cloud in the bottom.
这是我博客上“照片”标签的一个页面(法语),你可以在底部看到标签云。
#6
1
Based on Christian's answer above I made a bash script that does what he described.
基于克里斯蒂安的回答,我制作了一个bash脚本,按照他的描述。
https://github.com/ObjectiveTruth/objectivetruth.github.io/blob/master/rebuild_tags.sh
https://github.com/ObjectiveTruth/objectivetruth.github.io/blob/master/rebuild_tags.sh
Be sure to have the accompanying 14 line vim script in the /non_website_resources/
directory
确保在/non_website_resources/目录中有附带的14行vim脚本
AND
和
Make the /_layouts/tagpage.html
shown in Christian's answer above but rename it to /_layouts/tag_pages.html
使/ _layouts / tagpage。上面克里斯蒂安的答案中显示的html,但是将它重命名为/_layouts/tag_pages.html
File structure should be like this:
文件结构应如下:
.jekyll_website_root
├── _posts
├── _layout
│ ├── tag_pages.html
├── rebuild_tags.sh
Run from the root directory ./rebuild_tags.sh
从根目录运行
If you get permission denied error be sure to run chmod 777 rebuild_tags.sh
如果您获得了拒绝权限错误,请确保运行chmod 777 rebuild_tag .sh
If you look at scripts comments its fairly simple:
如果你看一下剧本评论,它相当简单:
-
Uses
sed
to find all the tags in every.md
file in_post
directory使用sed在_post目录中查找每个.md文件中的所有标记。
-
Uses
sed
to massage the data to proper format使用sed将数据按适当格式进行按摩。
-
Takes all the unique tags and makes a directory and a
index.html
for each获取所有唯一的标记并创建一个目录和索引。html为每个
This way, if you have any new tags, just run the script to rebuild the pages before pushing to github
通过这种方式,如果您有任何新的标记,请在向github推送之前运行脚本以重新构建页面
A nice simple non-plugin way to do tags
一个很好的简单的非插件方式来做标签
EDIT
编辑
Removed dependency on other files. Just need the one script!
删除对其他文件的依赖。只需要一个脚本!