为 Octopress 侧边栏添加分类列表

/

Octopress 默认侧边栏没有分类列表,添加步骤大致如下:

增加 category_list 插件

保存以下代码到 plugins/category_list.rb

require 'stringex'
module Jekyll
  class CategoryListTag < Liquid::Tag
    def render(context)
      category_dir = context.registers[:site].config['category_dir']
      categories = context.registers[:site].categories.keys
      categories.sort.reduce("") do |html, category|
        posts_in_category = context.registers[:site].categories[category].size
        att = Jekyll.get_category_attributes(category)
        html << "<li class='category'><a href='/#{category_dir}/#{att[0]}/'>#{att[1]} (#{posts_in_category})</a></li>\n"
      end
    end
  end
end

Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)

get_category_attributes 函数见另一篇文章

增加 aside

保存以下代码到 source/_includes/custom/asides/category_list.html

<section>
    <h1>Categories</h1>
    <ul id="categories">
        {% category_list %}
    </ul>
</section>

_config.yml 中修改 default_asides 项:

default_asides: [asides/recent_posts.html, custom/asides/category_list.html]

Comments