forked from nodejs/node-v0.x-archive
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Generate Table of Contents at compile time.
- Loading branch information
Showing
3 changed files
with
99 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,13 @@ | ||
$(function(){ | ||
highlight(undefined, undefined, 'pre'); | ||
var $headings = $("h2, h3, h4, h5, h6"); | ||
|
||
if(! $("body").hasClass("index") && $headings.size() > 2){ | ||
var current_level | ||
, last_level = 0 | ||
, toc = [ | ||
'<div id="toc">', | ||
'<h2>Table Of Contents <a id="toggler" href="#toc">Hide</a></h2>' | ||
]; | ||
|
||
for(var i=0, hl=$headings.size()+1; i < hl; i++) { | ||
var heading = $headings[i] || false; | ||
if(heading) { | ||
current_level = heading.tagName.substr(1,1); | ||
|
||
console.log(current_level, last_level, $(heading).text()); | ||
|
||
if(last_level != 0 && current_level <= last_level) { | ||
toc.push("</li>"); | ||
} | ||
|
||
if(current_level > last_level) { | ||
toc.push("<ul>"); | ||
toc.push("<li>"); | ||
} else if(current_level < last_level) { | ||
console.log(last_level-current_level); | ||
for(var c=last_level-current_level; 0 < c ; c-- ){ | ||
toc.push("</ul>"); | ||
toc.push("</li>"); | ||
} | ||
} | ||
|
||
if(current_level == last_level || current_level < last_level) { | ||
toc.push("<li>"); | ||
} | ||
|
||
toc.push('<a href="#'+$(heading).attr("id")+'">'+$(heading).text()+'</a>'); | ||
last_level = current_level; | ||
} else { | ||
toc.push("</li>"); | ||
toc.push("</ul>"); | ||
} | ||
} | ||
|
||
toc.push("</li>"); | ||
toc.push("</ul>"); | ||
toc.push("<hr />"); | ||
toc.push("</div>"); | ||
|
||
$("#container header").after(toc.join("\n")); | ||
$("#toggler").toggle(function(e){ | ||
$("#toc ul").hide(); | ||
$(this).text("show"); | ||
e.preventDefault(); | ||
}, function(e){ | ||
$("#toc ul").show(); | ||
$(this).text("hide"); | ||
e.preventDefault(); | ||
}); | ||
} | ||
|
||
$('<a href="#toc">Hide</a>').appendTo($("#toc h2")).toggle(function(e){ | ||
$("#toc ul").hide(); | ||
$(this).text("Show"); | ||
e.preventDefault(); | ||
}, function(e){ | ||
$("#toc ul").show(); | ||
$(this).text("Hide"); | ||
e.preventDefault(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters