Last active
October 5, 2020 11:40
-
-
Save ongspxm/1bddabc57b9426a0daaa6997b240f375 to your computer and use it in GitHub Desktop.
Tag and Category plugin for Jekyll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
class CatIndex < Page | |
def initialize(site, base, dir, cat) | |
@site = site | |
@base = base | |
@dir = dir | |
@name = 'index.html' | |
self.process(@name) | |
self.read_yaml(File.join(base, '_layouts'), 'cat_index.html') | |
self.data['cat'] = cat | |
end | |
end | |
class CatGenerator < Generator | |
safe true | |
def generate(site) | |
if site.layouts.key? 'cat_index' | |
dir = 'blog/categories' | |
site.categories.keys.each do |cat| | |
write_cat_index(site, File.join(dir, cat), cat) | |
end | |
end | |
end | |
def write_cat_index(site, dir, cat) | |
index = CatIndex.new(site, site.source, dir, cat) | |
index.render(site.layouts, site.site_payload) | |
index.write(site.dest) | |
site.pages << index | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
class TagIndex < Page | |
def initialize(site, base, dir, tag) | |
@site = site | |
@base = base | |
@dir = dir | |
@name = 'index.html' | |
self.process(@name) | |
self.read_yaml(File.join(base, '_layouts'), 'tag_index.html') | |
self.data['tag'] = tag | |
end | |
end | |
class TagGenerator < Generator | |
safe true | |
def generate(site) | |
if site.layouts.key? 'tag_index' | |
dir = 'blog/tags' | |
site.tags.keys.each do |tag| | |
write_tag_index(site, File.join(dir, tag), tag) | |
end | |
end | |
end | |
def write_tag_index(site, dir, tag) | |
index = TagIndex.new(site, site.source, dir, tag) | |
index.render(site.layouts, site.site_payload) | |
index.write(site.dest) | |
site.pages << index | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
layout: default | |
--- | |
<style> | |
.tag .tag__title{ | |
margin-top:1rem; | |
} | |
.tag .tag__content a{ | |
display:inline-block; | |
line-height:1.25em; | |
} | |
</style> | |
<div class="tag"> | |
<a class="tag__back" href="/"><-- back</a> | |
<h1 class="tag__title">All {{ page.cat }}</h1> | |
<div class="tag__content"> | |
{% if site.categories[page.cat] %} | |
<ul> | |
{% for post in site.categories[page.cat] %} | |
<li> | |
<span class="date">{{ post.date | date: '%Y %b %d' }}</span> - <a href="{{ post.url }}">{{ post.title }}</a> | |
</li> | |
{% endfor %} | |
</ul> | |
{% else %} | |
<p>There is no posts for this category.</p> | |
{% endif %} | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if page.tags %} | |
<span> | </span> | |
{% for tag in page.tags %} | |
<span> | |
<a href='/blog/tags/{{tag}}'>{{ tag }}</a>{% if forloop.last==false %}, {% endif %} | |
</span> | |
{% endfor %} | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
layout: default | |
--- | |
<style> | |
.tag .tag__title{ | |
margin-top:1rem; | |
} | |
.tag .tag__content a{ | |
display:inline-block; | |
line-height:1.25em; | |
} | |
</style> | |
<div class="tag"> | |
<a class="tag__back" href="/"><-- back</a> | |
<h1 class="tag__title">Posts tagged "{{ page.tag }}"</h1> | |
<div class="tag__content"> | |
{% if site.tags[page.tag] %} | |
<ul> | |
{% for post in site.tags[page.tag] %} | |
<li> | |
<span class="date">{{ post.date | date: '%Y %b %d' }}</span> - <a href="{{ post.url }}">{{ post.title }}</a> | |
</li> | |
{% endfor %} | |
</ul> | |
{% else %} | |
<p>There is no posts for this tag.</p> | |
{% endif %} | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the related changes in the file "_config.yml"?
I only see the post https://ongspxm.gitlab.io/blog/2016/09/jekyll-tag-and-categories/ and code here. Tried, it does not work.