:
(Note: if you copy and paste this code into your HTML for some reason,
you’ll probably want to remove that example line above!)
It doesn’t depend on any other JS libraries, and I’ve tried to make
sure it doesn’t conflict with them either.
(Of course, you can host it yourself, too.)
This software is licensed under the "MIT License", which differs from
a dedication to the public domain only minimally; among other things,
it’s more enforceable in Germany, and you can’t remove the copyright
notice:
Copyright (c) 2010 Kragen Javier Sitaker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function() {
// Go ahead and change this if you’re hosting the software yourself.
var url = "http://canonical.org/~kragen/sw/addtoc.js";
// These have to be in caps because we’re looking up elem.tagName
var header_rank = {H2: 2, H3: 3, H4: 4, H5: 5, H6: 6};
if (find('h1').length> 1) header_rank.H1 = 1;
var headers = get_list_of_headers(header_rank);
if (!headers.length) return;
assign_ids_if_necessary_to(headers);
// insert ToC before the first h2 (or other header)
insert_before(make_toc(url, header_rank, headers), headers[0]);
// imitating current Wikipedia table of contents style
inject_stylesheet(
".addtoc_toc { " +
" background-color: #eee; " +
" color: black; " +
" border: 1px solid #aaa; " +
" padding: 0.5em; " +
"}\n" +
// The !important is to override #foo a selectors on some pages.
".addtoc_toc a { color: blue !important }\n" +
".addtoc_title { text-align: center; font-weight: bold; margin-bottom: 0.5em }\n" +
".addtoc_toc * { list-style-type: none; margin: 0; padding: 0 }\n" +
".addtoc_toc ol ol { margin-left: 2em }\n" +
".addtoc_controls { display: none; font-weight: normal }\n" +
".addtoc_controls p { margin: 1em 0; max-width: 30em; text-align: left }\n" +
""
);
return;
function get_list_of_headers(header_rank) {
var headers = [];
var stack = [document.body];
// XXX there’s gotta be a better way
while (stack.length) {
var elem = stack.pop();
if (header_rank[elem.tagName] || is_filthy_pig_header(elem)) {
headers.push(elem);
}
for (var ii = elem.childNodes.length; ii> 0; ii--) {
var subelem = elem.childNodes[ii-1];
if (subelem.nodeType == subelem.ELEMENT_NODE) stack.push(subelem);
}
}
return headers;
}
// Some dirty people indicate headers by writing a short paragraph
// entirely in boldface, rather than using an actual
or
// whatever. This is an attempt to accommodate their pages; this
// function returns true if its argument is a or that
// is the sole child of a , or the only child before a
, and
// is short.
function is_filthy_pig_header(node) {
if (node.tagName !== 'B' && node.tagName !== 'STRONG') return false;
if (node.innerHTML.length> 80) return false;
if (node.parentNode.tagName !== 'P') return false;
var siblings = node.parentNode.childNodes;
for (var ii = 0; ii < siblings.length; ii++) { var sib = siblings[ii]; if (sib === node) continue; if (sib.nodeType === sib.TEXT_NODE) { if (/^\s*$/.test(sib.textContent)) continue; return false; } if (sib.tagName === 'BR') return true; return false; } return true; } function assign_ids_if_necessary_to(headers) { // assign them IDs if they don’t have them var id_serial = 1; for (var ii = 0; ii < headers.length; ii++) { if (!headers[ii].id) { headers[ii].id = "addtoc_" + id_serial; id_serial++; } } } function make_toc(url, header_rank, headers) { var olStack = [{rank: 2, list: make('ol'), numbering: [0]}]; if (header_rank.H1) olStack[0].rank = 1; for (var ii = 0; ii < headers.length; ii++) { var ahref = make('a', headers[ii].innerHTML); ahref.href = "#" + headers[ii].id; remove_all_inner_links_from(ahref); remove_all_ids_from(ahref.childNodes); // Treat filthy pig headers as .
var rank = header_rank[headers[ii].tagName] || 7;
while (olStack[olStack.length-1].rank> rank) olStack.pop();
if (olStack[olStack.length-1].rank < rank) { olStack.push(new_ol(rank, olStack[olStack.length-1])); } var top = olStack[olStack.length-1]; var li = make('li'); li.appendChild(ahref); add_numbering(top, li); top.list.appendChild(li); } var div = make('div'); div.innerHTML = ('' +
'' +
'');
var td = find('td', div)[0]
find('div', td)[0].appendChild(make_controls(url));
td.appendChild(olStack[0].list);
return div;
}
function make_controls(url) {
var rv = make('span', ' [more]' +
'hi');
var controls = find('div', rv)[0];
var link = find('a', rv)[0]
link.onclick = function(ev) {
controls.style.display = 'block';
link.style.display = 'none';
return false;
};
// Note that this will be inside the "" of an
, so it’s
// not safe for it to contain "" itself.
var bookmarklet_code = (
"(function() {" +
" var s=document.createElement('script');" +
" s.src='"+url+"';" +
" document.body.appendChild(s);" +
"})()" +
""
);
controls.innerHTML = (
'[hide table of contents] ' +
'
[hide these controls] ' +
'
This table-of-contents code is free software. You can use it ' +
'on any properly-written web page you read. ' +
'Drag the following ' +
'bookmarklet ' +
'to your browser\'s bookmark bar, and click it when you want a ToC: ' +
'. ' +
'
You can also use it in your own HTML documents. For instructions, ' +
'please see the source code of this copy at ' +
''+url+'. Share and enjoy!' +
'
Properly-marked-up web pages include Wikipedia pages ' +
"(although there's no point), " +
'WordPress and Blogger blogs ' +
"(even WordPress posts sometimes, although that's up to the author), " +
"most documents written in Markdown (e.g. Raganwald's homoiconic), " +
"Ars Technica articles, Henry Baker's papers, " +
"and the Lua reference manual." +
''
);
find('a', controls)[0].onclick = function() {
inject_stylesheet(".addtoc_toc { display: none }");
return false;
};
find('a', controls)[1].onclick = function() {
controls.style.display = 'none';
link.style.display = 'inline';
return false;
};
return rv;
}
function remove_all_inner_links_from(domnode) {
var links = [];
var links_search = find('a', domnode);
for (var ii = 0; ii < links_search.length; ii++) { if (links_search[ii].href) links.push(links_search[ii]); } for (var ii = 0; ii < links.length; ii++) { while (links[ii].childNodes.length) { insert_before(remove(links[ii].firstChild), links[ii]); } remove(links[ii]); } } function remove_all_ids_from(domnodes) { for (var ii = 0; ii < domnodes.length; ii++) { var node = domnodes[ii]; if (node.id) node.id = undefined; if (node.childNodes) remove_all_ids_from(node.childNodes); } } function new_ol(rank, parent) { // If this is the top element on the stack, it might not have an //
child yet.
if (!parent.list.childNodes.length) {
parent.list.appendChild(make('li', '(unnamed)'));
}
var ol = make('ol');
parent.list.lastChild.appendChild(ol);
var numbering = parent.numbering.slice();
numbering.push(0);
return {rank: rank, list: ol, numbering: numbering};
}
function add_numbering(parent, domnode) {
parent.numbering[parent.numbering.length-1]++;
var number = parent.numbering.join(".") + ") ";
insert_before(text(number), domnode.firstChild);
}
function inject_stylesheet(stylesheet) {
var style = make('style', stylesheet);
style.type = 'text/css';
document.body.appendChild(style);
}
function make(tagname, html) {
var elem = document.createElement(tagname);
if (html) elem.innerHTML = html;
return elem;
}
function find(tagname, where) {
if (!where) where = document;
return where.getElementsByTagName(tagname);
}
function text(text) { return document.createTextNode(text); }
function insert_before(new_node, reference) {
reference.parentNode.insertBefore(new_node, reference);
}
function remove(node) {
node.parentNode.removeChild(node);
return node;
}
})();