Git Product home page Git Product logo

jekyll-table-of-contents's Introduction

jekyll-table-of-contents

A simple JavaScript table of contents generator. Works well with jekyll static sites.

Usage

Basic Usage

The script requires jQuery. First, reference toc.js in templates where you would like to add the table of content. Then, create an HTML element wherever you want your table of contents to appear:

<div id="toc"></div>

Finally, call the .toc() function when the DOM is ready:

<script type="text/javascript">
$(document).ready(function() {
    $('#toc').toc();
});
</script>

If you use redcarpet, you need to have the option with_toc_data in order to add HTML anchors to each header:

markdown: redcarpet
redcarpet:
    extensions: [with_toc_data]

If you use rdiscount, enable the following option in order to generate the TOC:

markdown: rdiscount
rdiscount:
    extensions:
      - generate_toc

How It Works

The script works by looking for headers (h1, h2, h3, h4, h5, h6) which have an id. An id is added automatically if you're using Jekyll and Markdown.

The table of contents automatically handles nesting of headers. For example, this Markdown post:

## Title
## Page 1
### Note on Paragraph 3
## Page 2
### Note on Paragraph 2
### Note on Paragraph 4

Will render this table of contents:

1. Title
2. Page 1
  a. Note on Paragraph 3
3. Page 2
  a. Note on Paragraph 2
  b. Note on Paragraph 4

Configuration

List Type

By default the table of contents is rendered as an <ol>, so you can change the number formatting using CSS. However you can use the <ul> tag, using the listType option:

$('#toc').toc({ listType: 'ul' });

Header Styling

The script also adds an <i> tag next to each header. This uses the class icon-arrow-up, which if you're using Bootstrap, will be an arrow pointing to the top of the page. Clicking that arrow will scroll you to the top, while clicking on a header will get a permanent link to that particular header (using window.location.hash).

If you don't want this feature, add this setting:

$('#toc').toc({ noBackToTopLinks: true });

Otherwise, you can use the stylesheet below to have the icon and the header aligned nicely:

.clickable-header {
  cursor:pointer;
}
.clickable-header:hover {
  text-decoration:underline;
}
.top-level-header {
  display:inline;
}
.back-to-top {
  margin-left:5px;
  cursor:pointer;
}

Headers Used

By default the table of content is displayed when at least 3 headers are found. You can customize the minimum number of headers required with this setting:

$('#toc').toc({ minimumHeaders: 2 });

And you can also select which headers you want to link to. By default h1, h2, h3, h4, h5, h6 are displayed, but changing the headers setting lets you tweak it:

$('#toc').toc({ headers: 'h3, h4, h5, h6' });
$('#toc').toc({ headers: '.content h1, .content h2, .content h3, .content h4, .content h5, .content h6' });

Effects

Finally, you can also change the way the toc is displayed, choosing a slideShow or a fadeIn effect instead of show:

$('#toc').toc({ showEffect: 'slideDown' });

Otherwise, to deactivate the effect, set it up like this:

$('#toc').toc({ showSpeed: 0 });

List And Item Styling

You can specify additional CSS classes for both lists (<ul>, <ol>) and items (<li>):

$('#toc').toc({ classes: { list: 'lorem ipsum',
                           item: 'dolor sit amet'
                         }
              });

Copyright

See LICENSE.txt for further details. But basically, do what you like with this.

jekyll-table-of-contents's People

Contributors

anthodev avatar antoine-richard avatar drewcrawford avatar eksperimental avatar fokkezb avatar ghiculescu avatar heristop avatar neway6655 avatar nscyclone avatar weiweitop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jekyll-table-of-contents's Issues

Compatible with pandoc?

Trying to enable this on my Jekyll site (self-hosted, not github pages).
I am not entirely sure I understood the instructions, so I tried to put the <div> and <script> tags in a few different places (I know, total noob) but it seemed to have no effect.

So I wonder, does the fact that I use pandoc markdown affect anything? Should I add something to extensions in my _config.yml?

This is the relevant section of my current _config.yml:

markdown: pandoc
pandoc:
   extensions:
   - smart
   - mathjax

In _layouts/default.html, I have (only showing the header here):

<head><script type="text/javascript" src="/js/toc.js"></script></head>

and in _layouts/post.html, this is the code inside the <article> tag:

<article class="post-content">
<div id="toc">
<script type="text/javascript">
   $(document).ready(function() {
      $('#toc').toc();
   });
</script>
</div>
{{ content }}
</article>

Have I put the TOC code in the right places? Not even close?

Follow the tutorial, but it is unsuccessful

//\_layouts\post.html

<!-- toc -->
<div id="toc"></div>
<script src="assets\scripts\toc.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
      $('#toc').toc();
  });
  </script>
<!-- toc -->

I added this code to the \_layouts\post.html but didn't generate the corresponding content

Thanks!

Didn't have time to make a legit pull request at the moment, but just wanted to say Thanks!...it's great when someone else on the Internet has anticipated exactly what I need (a way to do TOC when kramdown won't do and plugins are disabled) and has made it just work.

gem "rdiscount"

At first, I executed this code

gem install rdiscount
jekyll build

An error has occurred

Dependency Error: Yikes! It looks like you don't have rdiscount or one of its dependencies installed. In order to use Jekyll as currently configured, you'll need to install this gem. The full error message from Ruby is: 'cannot load such file -- rdiscount' If you run into trouble, you can find helpful resources at https://jekyllrb.com/help/!
  Conversion error: Jekyll::Converters::Markdown encountered an error while converting '_posts/2017-06-15-1cheng.md':
                    rdiscount
             ERROR: YOUR SITE COULD NOT BE BUILT:
                    ------------------------------------
                    rdiscount

And then I used the following code

$ bundle clean --force
Removing rdiscount (2.2.0.1)

I thought that was not adding dependencies, so I added the following code to the file Gemfile

gem "rdiscount", "2.2.0.1"
$ bundle clean --force
Could not find gem 'rdiscount (= 2.2.0.1) x64-mingw32' in any of the gem sources
listed in your Gemfile.
$ bundle install
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using public_suffix 3.0.1
Using colorator 1.1.0
Using rb-fsevent 0.10.2
Using ffi 1.9.18 (x64-mingw32)
Using ruby_dep 1.5.0
Using kramdown 1.16.2
Using liquid 3.0.6
Using mercenary 0.3.6
Using forwardable-extended 2.6.0
Using rouge 1.11.1
Using safe_yaml 1.0.4
Fetching rdiscount 2.2.0.1
Installing rdiscount 2.2.0.1 with native extensions
Using thread_safe 0.3.6
Using bundler 1.15.1
Using addressable 2.5.2
Using rb-inotify 0.9.10
Using pathutil 0.16.1
Using tzinfo 1.2.4
Using sass-listen 4.0.0
Using listen 3.1.5
Using tzinfo-data 1.2017.3
Using sass 3.5.5
Using jekyll-watch 1.5.1
Using jekyll-sass-converter 1.5.1
Using jekyll 3.4.3
Using minima 2.1.1
Using jekyll-feed 0.9.2
Bundle complete! 5 Gemfile dependencies, 27 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
$ jekyll build
Configuration file: E:/ProjectOwn/Github/WowProgramming/_config.yml
            Source: E:/ProjectOwn/Github/WowProgramming
       Destination: E:/ProjectOwn/Github/WowProgramming/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
                    done in 0.823 seconds.
 Auto-regeneration: disabled. Use --watch to enable.

There was a warning, but it worked.
Should I add such a statement?

gem "rdiscount", "2.2.0.1"

Add jQuery as a dependency in the documentation

Can you add that jQuery is a dependency for this script to work?

Somewhere in the Jekyll project, a user should reference jQuery -- e.g.,

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Restrict to headers in one post or div

This script looks great, but my blog home page renders multiple posts in an "infinite scroll" kind of interface, and the TOC on the first post includes all headers on all rendered posts.

It would be nice if the scanning could be restricted to descendants of a given DOM node (e.g. a given post's container).

Add header IDs

The readme says

An id is added automatically if you're using Jekyll and Markdown.

This wasn't actually true in my case (Jekyll 1.5.1, OSX). I needed to add these options to _config.yml:

redcarpet:
    extensions: [with_toc_data]

account for offset?

I love the plugin. One issue. I have a fixed floating header. As a result, the links in the toc jump to the wrong spot. I need to offset it by about 20 px. Any idea how I do that?

How can we show the back-to-top links on second-highest headers as well?

I see the following lines let the back-to-top icon/text/... be shown on the top-level headings only:

    var get_level = function(ele) { return parseInt(ele.nodeName.replace("H", ""), 10); };
    var highest_level = headers.map(function(_, ele) { return get_level(ele); }).get().sort()[0];

And later this returns:

&& this_level === highest_level

However, how can we expand this such that this applies to the 2 highest levels?

top problem

what if my site has a fixed title bar? I can't jump to the top of site, any solution measures?

rdiscount generate TOC with the same id for all headers

I use rdiscount to generate toc, with configuration:

markdown: rdiscount
rdiscount:
    extensions:
      - generate_toc

I just found that it generated the same id for all header tags(h4, h5, ...). for example:
screen shot 2015-07-27 at 5 30 30 pm

which makes all the toc navigate links to the same place. Anyone encounter the same issue?

Issue when jumping from H2s to H3s

Here's my MD file, which I'm using with Jekyll:

---
title: Testing TOC
summary: Testing TOCs.
permalink: testTOC.html
---

Lorem ipsum.

### Here's an H3
Lorem ipsum.

## Here's an H2
Lorem ipsum.

### Here's an H3
Lorem ipsum.

## Here's an H2
Lorem ipsum.

The TOC output by toc.js isn't right:

<div id="toc">
  <ul>
    <li>
      <a href="#heres-an-h3">Here’s an H3</a><a href="#heres-an-h3"></a>
    </li>
  </ul>
  <li>
    <a href="#heres-an-h2">Here’s an H2</a><a href="#heres-an-h2"></a>
    <ul>
      <li>
        <a href="#heres-an-h3-1">Here’s an H3</a><a href="#heres-an-h3-1"></a>
      </li>
    </ul>
  </li>
  <li>
    <a href="#heres-an-h2-1">Here’s an H2</a><a href="#heres-an-h2-1"></a>
  </li>
</div>

You can see that the main <ul> is closed too early, causing the results to look wrong.

Yet another letter of thanks

Hello!

I would thank you for this simple but excellent javascript component

I've used it to improve features of git-wiki project

http://www.drassil.org/git-wiki/

It allows you to build a totally customizable wiki using github pages instead of limited integrated wiki.

I'll add it in git-wiki description

P.S.
Could you help me to change the style resambling the mediawiki toc?

Changing the settings have no effect

      noBackToTopLinks: false,
      title: '',
      minimumHeaders: 1,
      headers: 'h1, h2', // original h1, h2, h3, h4
      listType: 'ol', // values: [ol|ul]
      showEffect: 'show', // values: [show|slideDown|fadeIn|none]
      showSpeed: 'slow' // set to 0 to deactivate effect

I have set this in my toc.js source but it is having no effect when I recompile my project. I've tried switching minimumHeaders and headers but none of it has an effect. Any ideas?

Req: Option to use slideDown() rather than show()

As I mentioned in #1, when the table of contents has a background, its animation can interact very poorly with the contents when using show().

The simple fix is to offer the option of using slideDown() instead.

Add option to add classes to list elements

I would like to add bootstrap classes to style the

    ,
      , and
    1. elements, and it would be nice if there was some sort of option so you could call the toc() function with a classes parameter for
        or
          elements, accordint to listType parameter, something like:

          $('#toc').toc({
              listType: 'ul', // values: [ol|ul]
              classes: 'nav, nav-stacked',
          });

          So the

            element has the appearence of nav-stacked elements according to bootstrap

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.