在 jekyll 博客中支持标签的简单方法
我正在使用标准 jekyll 安装来维护博客,一切都很顺利。但我真的很想标记我的帖子。
我可以使用 YAML 标题来标记帖子,但是如何为每个标签生成可以列出该标签的所有帖子的页面?
I am using the standard jekyll installation to maintain a blog, everything is going fine. Except I would really like to tag my posts.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个在单个页面上按字母顺序排序标签的解决方案。
它仅使用 Liquid,这意味着它可以在 GitHub Pages 上运行:
您可以此处查看它的实际情况。
编辑:
还有一种方法可以为每个标签生成一个单独的页面,无需插件(可在 GitHub Pages 上运行)。
我的博客上有更详细的解释:
每个标签/类别有单独的页面使用Jekyll(不带插件)
首先,你需要一个新的布局文件:
/_layouts/tagpage.html
:有了这个布局文件,你可以通过添加新文件来添加新的标签页只需两行 YAML 前面的内容。
以下是
jekyll
标签的示例:/tags/jekyll/index.html
:这种方法的唯一缺点:每次您第一次使用新标签时,您必须记住为其创建一个新的两行文件。
要生成根索引文件(即链接到
/tags/jekyll/index.html
等的标签列表),您可以使用类似的解决方案在这个答案的顶部,我生成一个包含按字母顺序排序的标签的单个页面:这将生成如下链接列表:
请注意,此解决方案使用空白来分割标签,因此当您的标签包含时它不起作用空白和 Yevgeniy Brikman 的评论也适用于此处。
Here is a solution with alphabetically sorted tags on a single page.
It uses Liquid only, which means that it works on GitHub Pages:
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).
I have a more detailed explanation on my blog:
Separate pages per tag/category with Jekyll (without plugins)
First, you need a new layout file:
/_layouts/tagpage.html
:With this layout file, you can add a new tag page by adding a new file with just two lines of YAML front-matter.
Here's an example for the
jekyll
tag:/tags/jekyll/index.html
: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:This will generate a list of links like this:
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.
此要点将为您生成每个类别的页面: https://gist.github.com/524748
使用 Jekyll Generator 插件以及 Page 子类。
This gist will generate a page per category for you: https://gist.github.com/524748
It uses a Jekyll Generator plugin, plus a Page subclass.
查看使用 jekyll 的网站。有一些自定义分叉已经实现了标记功能,希望也能按照您想要的方式:-)
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 :-)
我有同样的问题,并偶然发现了这个:http://gist.github.com/143571。
这是一个生成标签列表的 rake 任务。我稍微修改了一下,我的版本是:
http://github.com/mattfoster/mattfoster.github.com/blob /master/Rakefile。
虽然这不会为您提供每个标签的页面,但您可以使用锚点,这已经完成了一半!
I had the same question, and stumbled upon this: 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.
Whilst this doesn't give you a page per tag, you can use anchors, which is half way there!
我使用很棒的 Jekyll Tagging 插件,它可以自动生成标签云和标签页面。易于安装和使用。
这是我的博客上的“照片”标签的页面(法语),以及您可以在底部看到标签云。
I use the great Jekyll Tagging plugin that automatically generates a tags cloud and tag pages. Easy to install and use.
Here is a page for the "photo" tag on my blog (in french), and you can see the tags cloud in the bottom.
根据上面克里斯蒂安的回答,我制作了一个 bash 脚本来执行他所描述的操作。
https://github.com/ObjectiveTruth/objectivetruth.github.io /blob/master/rebuild_tags.sh
确保附带 14 行 vim 脚本 在/non_website_resources/
目录AND制作
/_layouts/tagpage .html
如上面 Christian 的回答所示,但将其重命名为/_layouts/tag_pages.html
文件结构应如下所示:
从根目录运行
./rebuild_tags.sh
如果您收到权限被拒绝错误,请务必运行
chmod 777rebuild_tags.sh
如果您查看脚本注释,它相当简单:
使用
sed
查找所有_post
目录中每个.md
文件中的标签使用
sed
将数据调整为正确的格式获取所有唯一标签并为每个标签创建一个目录和一个
index.html
这样,如果您有任何新标签,只需运行脚本来重建页面,然后再推送到 github
一个很好的简单非插件方式来处理标签
编辑
删除了对其他文件的依赖。只需要一个脚本!
Based on Christian's answer above I made a bash script that does what he described.
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/
directoryANDMake the
/_layouts/tagpage.html
shown in Christian's answer above but rename it to/_layouts/tag_pages.html
File structure should be like this:
Run from the root directory
./rebuild_tags.sh
If you get permission denied error be sure to run
chmod 777 rebuild_tags.sh
If you look at scripts comments its fairly simple:
Uses
sed
to find all the tags in every.md
file in_post
directoryUses
sed
to massage the data to proper formatTakes all the unique tags and makes a directory and a
index.html
for eachThis way, if you have any new tags, just run the script to rebuild the pages before pushing to github
A nice simple non-plugin way to do tags
EDIT
Removed dependency on other files. Just need the one script!
我用 CSS 来做这些。首先列出一个元素并使用标签名称作为其 id。
然后列出所有帖子并使用其标签作为“标签”自定义属性的值。
然后在 CSS 中,默认隐藏所有帖子,并且仅显示带有与 url id/哈希匹配的标签的帖子
我写了一篇关于此的文章 此处。
I do these with CSS. First lists an element and use the tag name as its id.
And then lists all the post and use its tags as a value for the "tags" custom attribute.
And then in CSS, hide all the posts by default, and only show posts with tags matches the url id/ hash
I made an article about this here.
我写了一个
pre-push
挂钩来检测丢失的标签页。它将使用 bash 脚本gentag
创建缺失的标签页面。如果推动吃水,钩子也会发出警告。为简单起见,它假设标签作为 yaml 数组输入到前面的内容中,并使用
yq
收集。还有一个
pre-receive
挂钩可以将网站部署在个人网络服务器上。I've written a
pre-push
hook to detect missing tag pages. It will offer to create the missing tag pages with a bash script,gentag
. The hook will also warn if pushing a draft.For simplicity, it assumes tags are entered as a yaml array in front matter which are collected with
yq
.There's also a
pre-receive
hook to deploy the site on a personal webserver.