Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOC (Table of contents) helper #408

Closed
tommy351 opened this issue Dec 28, 2013 · 19 comments
Closed

TOC (Table of contents) helper #408

tommy351 opened this issue Dec 28, 2013 · 19 comments
Milestone

Comments

@tommy351
Copy link
Member

Usage

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

commit: 3efb980

@wzpan
Copy link
Member

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.

@simonexmachina
Copy link
Contributor

Can someone provide an example of how to use this?

@simonexmachina
Copy link
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
Copy link
Member Author

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
Copy link

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
Copy link
Contributor

@fwz See this pull request.

@fwz
Copy link

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
Copy link
Contributor

@fwz Sorry, this one:

#780

@fwz
Copy link

fwz commented Aug 31, 2014

@NicholasTD07

Thanks!

@yantze
Copy link

yantze commented Nov 16, 2014

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

@kuanyui
Copy link

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
Copy link
Member

wzpan commented Mar 9, 2015

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

@kuanyui
Copy link

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
Copy link
Member

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
Copy link

"<%- 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
Copy link

bubkoo commented Jun 2, 2015

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

@BrunoHu
Copy link

BrunoHu commented Aug 14, 2015

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

@bennycode
Copy link

bennycode commented Apr 10, 2022

I saw that the tocHelper is registered in the Hexo.js code with helper.register('toc', require('./toc'));. Is there any clean way to make use of this if you want to apply the tocHelper in your own code?

@kuanyui: Currently I am importing the tocHelper in the following way:

const tocHelper = require('hexo/lib/plugins/helper/toc');
const toc = tocHelper(html, { max_depth: 2 });

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

No branches or pull requests