Browse Source

Build the links to the headings

- add a running auto-ID for headings without one, but keep existing IDs
- add the links to the TOC entries
- various JSlint fixes
FGM 14 years ago
parent
commit
f333bc38a0
1 changed files with 24 additions and 7 deletions
  1. 24 7
      ah_toc.js

+ 24 - 7
ah_toc.js

@@ -20,9 +20,16 @@
  */
 function ahTocBuildToc() {
 
-  var context = $('div.advanced-help-topic');
-  var elements = $('*', context).filter(function (index) {
-    return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) != -1;
+  var context,  // the context of the help page 
+    elements,   // the list of headings
+    element,    // one of the headings
+    list,       // the string form of the toc list
+    index,      // a running index for missing heading IDs
+    id;         // the current id when iterating on the headings
+  
+  context = $('div.advanced-help-topic');
+  elements = $('*', context).filter(function (index) {
+    return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) !== -1;
   });
 
   /**
@@ -33,11 +40,21 @@ function ahTocBuildToc() {
   }
 
   // Build the headings list
-  var list = "<ul>\n";
+  list = "<ul>\n";
+  index = 0;
+  
   elements.each(function() {
-    list += '<li class="' + this.nodeName.toLowerCase() + '">'
-        + $(this).text() + "</li>\n";
-  })
+    element = $(this);
+    id = element.attr('id');
+    if (id === '') {
+      index += 1; // Avoid JSline warning on ++ use
+      id = 'toc' + index;
+      element.attr('id', id);
+    }
+    // @TODO use some form of l() instead of string concatenation
+    list += '<li class="' + this.nodeName.toLowerCase() + '">' +
+      '<a href="#' + id + '">' + element.text() + "</a></li>\n";
+  });
   list += "</ul>\n";
 
   // Avoid creating multiple TOCs if this function is called several times