TOC (Table of contents) helper #408

Closed
tommy351 opened this Issue Dec 28, 2013 · 18 comments

Projects

None yet

10 participants

@tommy351
Member

Usage

<%- toc(str, [options]) %>
  • str - Input string
  • options - Options
    • class - Class name
    • list_number - Display list number
@tommy351
Member

commit: 3efb980

@tommy351 tommy351 closed this Dec 29, 2013
@wzpan
wzpan commented Dec 31, 2013

Cool. Maybe add one more option:

  • options - Options
    • class - Class name
    • list_number - Display list number
    • depth - the deepest heading level to be included.
@aexmachina
Contributor

Can someone provide an example of how to use this?

@aexmachina
Contributor

I'd like to be able to insert [toc] into my Markdown to display a TOC, which I can do using a filter, but how can I access this helper from my script?

@tommy351
Member
tommy351 commented Jan 8, 2014

Put the following in the theme:

<%- toc(page.content) %>

And this helper will scan all headers in the content and generate a table of contents.

@fwz
fwz commented Jul 8, 2014

Hi, I am using Hexo 2.6.1 and landscape theme, and try to add a toc into my post.

Adding

<%- toc(page.content) %>

into themes/landscape/layout/_partial/article.ejs doesn't work. but adding

<%- toc(post.content) %>

shows the TOC.

However, there are extra numbers before each line of TOC.
I attach the HTML here: http://jsfiddle.net/de8a8/
Could anyone please take a look? how could I remove that number?

Thanks!

@NicholasTD07
Contributor

@fwz See this pull request.

@fwz
fwz commented Aug 30, 2014

@NicholasTD07
Thanks. Are you referring to 3efb980 ?

If so, is it possible to set the list_number as false by configuration?

@NicholasTD07
Contributor

@fwz Sorry, this one:

#780

@fwz
fwz commented Aug 31, 2014

@NicholasTD07

Thanks!

@yantze
yantze commented Nov 16, 2014

<%- toc(page.content, {list_number: false}) %>

@kuanyui
kuanyui commented Mar 8, 2015

Any way to use this like <!-- toc --> in article ? Because I tend to place the TOC at exactly where I want in different article.

I've tried to reuse tocHelper(), but cannot find a way to call the function... (Sorry, I'm not familiar with Node.js...):

hexo.extend.filter.register('post', function(data) {
    data.content = data.content.replace(/<!-{2,} *toc *-{2,}>/, hexo.plugins.helper.toc.tocHelper(data.content));
    return data;
});
@wzpan
wzpan commented Mar 9, 2015

@Kuanyui ,you can only call this helper in your theme template files(.ejs).

@kuanyui
kuanyui commented Mar 9, 2015

But I don't want to add the TOC at a fix position.
For example, sometimes I insert the TOC after preamble, sometimes before.

@wzpan
wzpan commented Mar 10, 2015

If so, then don't use this helper. There are many other jQuery plugins that are more flexible and suitable for your demand e.g. http://fuelyourcoding.com/scripts/toc/

@JiantaoFu JiantaoFu referenced this issue in LouisBarranqueiro/hexo-theme-tranquilpeak Jun 1, 2015
Closed

How to enable ToC #21

@JiantaoFu

"<%- toc(page.content, {list_number: true}) %>" adding extra list number, and i can't remove it by {list_number: false}
image

{list_number: true}
image

@bubkoo
bubkoo commented Jun 2, 2015

@JiantaoFu set list-style:none in your css.

@Arnold-Hu

Could toc function provide the depth option? I think this option could make the page more simple and eligant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment