Chad(wik)’s Musings…











Following up on my question to the wiki-dev list, I received a response with some unattributed code for generating a table of contents for a wiki page:

// set aside the old prepare method

if (window.prepare) window.beforeTOCPrepare = window.prepare;

// now add our own, which calls the old one

window.prepare = function(inAlwaysRun) {

if (window.beforeTOCPrepare) beforeTOCPrepare(inAlwaysRun);

if ($(‘editable_content’)) gTOCGenerator = new TOCGenerator();

// also fix up the WLTEditor class so that it redraws the TOC when saving

if (window.gEditor) {

gEditor.beforeTOCEditorCleanElementForEditing = gEditor.cleanElementForEditing;

gEditor.cleanElementForEditing = function(inElement) {

gEditor.beforeTOCEditorCleanElementForEditing(inElement);

gTOCGenerator.remove();

}

gEditor.beforeTOCCleanElementAfterEditing = gEditor.cleanElementAfterEditing;

gEditor.cleanElementAfterEditing = function(inElement, inResponseDict) {

gEditor.beforeTOCCleanElementAfterEditing(inElement, inResponseDict);

gTOCGenerator.draw();

}

}

}

Simply copy and paste this code into a .js (JavaScript) file and save it to your theme’s folder structure: /Library/Application Support/Apple/WikiServer/Themes/[your_theme_name]/

I sincerely apologize for the formatting but I’m sick of fighting with WordPress to get it right!  I’m no JavaScript expert so I do not know how the code works, but this can server as a starting point for future ToC development.  For now, when the title of an article with multiple headings is clicked, a menu of the headings appears:

Apple Wiki Table of Contents using JavaScript

Apple Wiki Table of Contents using JavaScript

With more time and a little Headfirst: JavaScript, I might just have a solution that makes a dynamic, permanent Table of Contents at the top of each article.

The code has not been attributed to any one person but I will gladly cite the author if someone claims to be he or she.



et cetera