jekyll-table-of-contents icon indicating copy to clipboard operation
jekyll-table-of-contents copied to clipboard

Restrict to headers in one post or div

Open ryan-williams opened this issue 8 years ago • 2 comments

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).

ryan-williams avatar Mar 01 '17 01:03 ryan-williams

rather then taking in another selector string for the container (and adding special code in the toc function to look for closest(container)) it seems like it would be more flexible to just change the existing $(settings.headers).filter(...) into $(settings.headers, settings.context).filter(...) -- where settings.context defualts to $(this).context

That way, by default, the code would still look for all headers in the same context as the selected TOC elements -- which in the common case of $("#toc").toc() will be the whole document -- but people with special needs could do things like...

// for each div.post, create a unique .toc of the headers in that div.post
$("div.post").each(function() { $(".toc", this).toc() });

..and the context would be a lot more powerful then a simple selector string.

hossman avatar May 18 '17 04:05 hossman

NB: I ended up using this built-in kramdown ToC instead of this

ryan-williams avatar May 18 '17 05:05 ryan-williams