/***************************************************************** * * $.toc() * by rebecca murphey * rmurphey gmail com * * This function is called on its own and takes as an argument * a list of selectors with which it will build a table of * contents. * * The first selector will make up the top level of the TOC; * the second selector will make up the second level of the TOC; * etc. * * This function returns a div containing nested unordered lists; * each list item is linked to an anchor tag added before the item * on the page. * * usage: $.toc('h1,h2,h3').prependTo('body'); * ************************************************************************/ (function($) { $.toc = function(tocList) { $(tocList).addClass('jquery-toc'); var tocListArray = tocList.split(','); $.each(tocListArray, function(i,v) { tocListArray[i] = $.trim(v); }); var $elements = $('.jquery-toc'); $('body').append('
'); var $toc = $('body div:last'); var lastLevel = 1; $toc.append('
    '); $elements.each(function() { var $e = $(this); var text = $e.text(); var anchor = text.replace(/ /g,'-'); $e.before(''); var level; $.each(tocListArray, function(i,v) { if (v.match(' ')) { var vArray = v.split(' '); var e = vArray[vArray.length - 1]; } else { e = v; } if ($e.is(e)) { level = i+1; } }); var className = 'jquery-toc-' + level; var li = '
  1. ' + text + '
  2. '; if (level == lastLevel) { $('ol.' + className + ':last',$toc).append(li); } else if (level > lastLevel) { var parentLevel = level - 1; var parentClassName = 'jquery-toc-' + parentLevel; $('ol.' + parentClassName + ':last',$toc). append('
      '); $('ol.' + className + ':last',$toc).append(li); } else if (level < lastLevel) { $('ol.' + className + ':last',$toc).append(li); } lastLevel = level; }); var $toc_ol = $('ol.jquery-toc-1',$toc); $toc.remove(); return($toc_ol); } })(jQuery);