• Stars
    star
    302
  • Rank 135,247 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Jekyll plugin which generates a table of contents.

jekyll-toc

CI Gem Version Code Climate Test Coverage

Table of Contents

Installation

Add jekyll-toc plugin in your site's Gemfile, and run bundle install.

gem 'jekyll-toc'

Add jekyll-toc to the gems: section in your site's _config.yml.

plugins:
  - jekyll-toc

Set toc: true in posts for which you want the TOC to appear.

---
layout: post
title: "Welcome to Jekyll!"
toc: true
---

Usage

There are three Liquid filters, which can be applied to HTML content, e.g. the Liquid variable content available in Jekyll's templates.

Basic Usage

toc filter

Add the toc filter to your site's {{ content }} (e.g. _layouts/post.html).

{{ content | toc }}

This filter places the TOC directly above the content.

Advanced Usage

If you'd like separated TOC and content, you can use {% toc %} tag (or toc_only filter) and inject_anchors filter.

{% toc %} tag / toc_only filter

Generates the TOC itself as described below. Mostly useful in cases where the TOC should not be placed immediately above the content but at some other place of the page, i.e. an aside.

<div>
  <div id="table-of-contents">
    {% toc %}
  </div>
  <div id="markdown-content">
    {{ content }}
  </div>
</div>

⚠️ {% toc %} Tag Limitation

{% toc %} works only for Jekyll Posts and Jekyll Collections. If you'd like to use {% toc %} except posts or collections, please use toc_only filter as described below.

<div>
  <div id="table-of-contents">
    {{ content | toc_only }}
  </div>
  <div id="markdown-content">
    {{ content | inject_anchors }}
  </div>
</div>

inject_anchors filter

Injects HTML anchors into the content without actually outputting the TOC itself. They are of the form:

<a class="anchor" href="#heading1-1" aria-hidden="true">
  <span class="octicon octicon-link"></span>
</a>

This is only useful when the TOC itself should be placed at some other location with the toc_only filter.

Generated HTML

jekyll-toc generates an unordered list by default. The HTML output is as follows.

<ul id="toc" class="section-nav">
  <li class="toc-entry toc-h1"><a href="#heading1">Heading.1</a>
    <ul>
      <li class="toc-entry toc-h2"><a href="#heading1-1">Heading.1-1</a></li>
      <li class="toc-entry toc-h2"><a href="#heading1-2">Heading.1-2</a></li>
    </ul>
  </li>
  <li class="toc-entry toc-h1"><a href="#heading2">Heading.2</a>
    <ul>
      <li class="toc-entry toc-h2"><a href="#heading2-1">Heading.2-1</a>
        <ul>
          <li class="toc-entry toc-h3"><a href="#heading2-1-1">Heading.2-1-1</a></li>
          <li class="toc-entry toc-h3"><a href="#heading2-1-2">Heading.2-1-2</a></li>
        </ul>
      </li>
      <li class="toc-entry toc-h2"><a href="#heading2-2">Heading.2-2</a></li>
    </ul>
  </li>
</ul>

screenshot

Customization

jekyll-toc is customizable on _config.yml.

Default Configuration

# _config.yml
toc:
  min_level: 1
  max_level: 6
  ordered_list: false
  no_toc_section_class: no_toc_section
  list_id: toc
  list_class: section-nav
  sublist_class: ''
  item_class: toc-entry
  item_prefix: toc-

TOC levels

# _config.yml
toc:
  min_level: 2 # default: 1
  max_level: 5 # default: 6

The default heading range is from <h1> to <h6>.

Enable TOC by default

You can enable TOC by default with Front Matter Defaults:

# _config.yml
defaults:
  - scope:
      path: ""
    values:
      toc: true

Skip TOC

The heading is ignored in the toc by adding no_toc class.

<h1>h1</h1>
<h1 class="no_toc">This heading is ignored in the TOC</h1>
<h2>h2</h2>

Skip TOC Sectionally

The headings are ignored inside the element which has no_toc_section class.

<h1>h1</h1>
<div class="no_toc_section">
  <h2>This heading is ignored in the TOC</h2>
  <h3>This heading is ignored in the TOC</h3>
</div>
<h4>h4</h4>

Which would result in only the <h1> & <h4> within the example being included in the TOC.

The class can be configured on _config.yml:

# _config.yml
toc:
  no_toc_section_class: exclude # default: no_toc_section

Configuring multiple classes are allowed:

# _config.yml
toc:
  no_toc_section_class:
    - no_toc_section
    - exclude
    - your_custom_skip_class_name

CSS Styling

The toc can be modified with CSS. The sample CSS is the following.

.section-nav {
  background-color: #fff;
  margin: 5px 0;
  padding: 10px 30px;
  border: 1px solid #e8e8e8;
  border-radius: 3px;
}

screenshot

Each TOC li entry has two CSS classes for further styling. The general toc-entry is applied to all li elements in the ul.section-nav.

Depending on the heading level each specific entry refers to, it has a second CSS class toc-XX, where XX is the HTML heading tag name. For example, the TOC entry linking to a heading <h1>...</h1> (a single # in Markdown) will get the CSS class toc-h1.

Custom CSS Class and ID

You can apply custom CSS classes to the generated <ul> and <li> tags.

# _config.yml
toc:
  list_id: my-toc-id # Default: "toc"
  list_class: my-list-class # Default: "section-nav"
  sublist_class: my-sublist-class # Default: no class for sublists
  item_class: my-item-class # Default: "toc-entry"
  item_prefix: item- # Default: "toc-":

Using Unordered/Ordered lists

By default the table of contents will be generated as an unordered list via <ul></ul> tags. This can be configured to use ordered lists instead <ol></ol>. This can be configured in _config.yml:

# _config.yml
toc:
  ordered_list: true # default is false

In order to change the list type, use the list-style-type css property. Add a class to the sublist_class configuration to append it to the ol tags so that you can add the list-style-type property.

Example

# _config.yml
toc:
  ordered_list: true
  list_class: my-list-class
  sublist_class: my-sublist-class
.my-list-class {
  list-style-type: upper-alpha;
}

.my-sublist-class: {
  list-style-type: lower-alpha;
}

This will produce:

screenshot

Alternative Tools

More Repositories

1

nyan

Colored `cat` command.
Go
209
star
2

rubocop-rails_config

RuboCop configuration which has the same code style checking as official Ruby on Rails.
Ruby
152
star
3

RailsTwitterClone

Simple Twitter clone using Ruby on Rails 6.
Ruby
118
star
4

auto-author-assign

GitHub Actions: Automatically assign pull request authors.
JavaScript
87
star
5

rubocop-rails

RuboCop configuration which has the same code style checking as official Ruby on Rails
Ruby
63
star
6

jekyll-tagging-related_posts

Jekyll `related_posts` function based on tags.
Ruby
54
star
7

hybrid-next-plus

VSCode Hybrid Next Plus Theme.
17
star
8

rack-simple_user_agent

Rack::SimpleUserAgent is stupidly simple UA detector
Ruby
13
star
9

hubot-hanakin

花金だーワッショーイ!テンションAGEAGEマック
CoffeeScript
11
star
10

hubot-docomochatter

A hubot plugin which allows you to chat with hubot via Docomo Zatsudan-Taiwa(雑談対話) API. Japanese is recommended.
CoffeeScript
5
star
11

dotfiles

My dotfiles. Configure it!
Shell
5
star
12

terraform-digitalocean-rails

A terraform simple sample on DigitalOcean for Ruby on Rails.
HCL
5
star
13

backlog-pr-link-action

Link GitHub Pull Request to Backlog issue.
TypeScript
5
star
14

ec2-price

Retrieve the latest EC2 prices via command line.
JavaScript
5
star
15

ruby-short_url

Ruby implementation of python-short_url.
Ruby
4
star
16

blog.toshima.ru

My blog in English.
SCSS
3
star
17

factory-bot-the-right-way

Source code for Kaigi on Rails presentation. https://kaigionrails.org
Ruby
3
star
18

rdm-rails5.1

Ruby
3
star
19

jekyll-theme-classless-simple

Jekyll theme with simple.css, classless CSS framework.
SCSS
3
star
20

blog.toshimaru.net

My blog powered by Jekyll.
HTML
3
star
21

jekyll-include_sass

Jekyll include_sass tag which includes and converts SASS/SCSS file. Useful for Google AMP HTML!
Ruby
2
star
22

docomochatter

Docomo Zatsudan API Client
CoffeeScript
2
star
23

attribute_validation

Attributes Validation Extension for ActiveRecord/ActiveModel
Ruby
2
star
24

til.toshimaru.net

My TIL.
HTML
1
star
25

serverkit-vscode

Serverkit plug-in for VSCode.
Ruby
1
star
26

vagrant-digitalocean-puppet

Create a droplet on Digital Ocean with puppet.
Puppet
1
star
27

opsworks-rails

Rails 4.2.0 sample application on OpsWorks.
Ruby
1
star
28

rails_backbone_demo_app

Demo application for RoR and Backbone.js
Ruby
1
star
29

LeetCode

LeetCode Solutions.
Ruby
1
star
30

memcacher

Better memcache integration for Rails.
Ruby
1
star
31

toshimaru

My idea storage. Work it harder. Make it better. Do it faster. Makes me stronger.
1
star
32

go-network

Network programming in Go
Go
1
star
33

mitamae-ruby-setup

Setup Ruby environment via Mitamae.
Ruby
1
star
34

ratiocalculator

Ratio Calculator.
HTML
1
star
35

compilerbook

C
1
star
36

bundled_gems

Install gem specified in Gemfile.lock without `bundle install`.
Ruby
1
star
37

redirect_follow_get

redirect_follow_get is simple http get method following redirect.
Ruby
1
star
38

SimpleRequestDispatcher

PHP Simple Request Dispatcher dispatches URL to Controllers. No Models, No Views.
PHP
1
star
39

packer-digitalocean-sample

Create a snapshot on Digital Ocean with packer.
Shell
1
star
40

kramdown-amp

Kramdown Converter for AMP. It works as a Jekyll-plugin.
Ruby
1
star
41

mitamae-plugin-recipe-ssh_users

SSH user setup for mitamae.
Ruby
1
star
42

circleci-orb-bundle-install

[DEPRECATED]CircleCI Orb which runs Ruby `bundle install` command with cache handling.
1
star
43

DesignPattern

Learn design pattern in various languages.
C#
1
star
44

capybara-screenshot_config

Provides better options for Capybara screenshot.
Ruby
1
star
45

hubot-best-summer

最高の夏にしようぜ!!!
CoffeeScript
1
star
46

torch

`torch` = `mkdir` + `touch`. Written in Rust.
Rust
1
star