Flexible plugin for nested table of contents

Posted

My partner saw the search results page for this blog and, always on the lookout for a way to improve things, suggested that it would benefit from a table of contents at the top. I could do this with Wordpress, of course, but it sounded like a good idea for a jQuery plugin, too. A little googling showed that there were a couple of existing solutions, but there were two things I wanted that they didn't seem to offer:

  • the ability to select via selectors exactly which elements would be included (i.e., #content h2 instead of just h2)
  • the ability to nest the lists of sections to reflect the hierarchy of the page
After puzzling a bit over how to get a list of selected elements in the order they appear in the document (see note below), I wrote my own plugin. Usage:
var $toc = $.toc('#content h1, h2, h3.foo');
So you can do:
$.toc('h1,h2,h3').prependTo('body').attr('id','toc');

Note

$('h1,h2') will return a jQuery object containing all the h1's in a document, followed by all the h2's in a document: h1 h1 h1 h2 h2 h2. I needed all the h1's and h2's in the document in the order they appeared; so, perhaps, h1 h1 h2 h2 h1 h2 h2. The obvious-in-hindsight answer:
$('h1,h2').addClass('selectedElements');
var selectedElements = $('.selectedElements');
Of course, the bad thing about this is that, on pages with lots of elements, it's going to be slow. Any better suggestions?

Posted

8 comments

Dec 26, 2007
rdmey | Revisiting rebeccamurphey.com said...
[...] Flexible plugin for nested table of contents [...]
Dec 29, 2007
Paul Irish said...
Nice plugin. Well done
Mar 02, 2008
Jeroen Coumans said...
Nice plugin! I made two changes though. I wrapped the child list in it's own element, which is a a little more semantically correct. And I adjusted the regex to this:

var anchor = text.replace(/([^A-Za-z])/g,'');

That way, only text characters remain.

May 16, 2008
Kev Mears said...
Nice plug-in. Almost exactly what I needed. I might even be able to understand some of the magic if I try hard enough!
Jun 09, 2008
milkshake said...
Thanks, works great :)
Jun 09, 2008
milkshake said...
... could you suggest how to only create the toc *if* it found some matching headings? Currently it outputs an empty ul.

Thanks again.

Oct 21, 2009
Yavuz Bogazci said...
Hi,

how can i make it, that i

1. set a number for each toc element?
2. set exactly this number to he h1 inner html?

So that the result is:

1. Head 1
2. Head 2

...
...

1. Head 1
djaskdjasödjaskdj aslökjd asd
dkajsdjaslödkjasdkljasldkjasd

2. Head 2
sldkasäfasdfkasdjflöasdf
asdfasdkfjasdfnasdm,fn sdjaf asöd

Thank you!

Nov 25, 2009
網站製作學習誌 » [Web] 連結分享 said...
[...] Flexible plugin for nested table of contents [...]

Leave a comment...