samnang
5/29/2015 - 2:49 AM

TOC of Github's README

TOC of Github's README

function tocLink(heading, title, href) {
  var ele = '<li class="tooltipped tooltipped-w" aria-label="'+title+'">'+
    '<a href="'+href+'" aria-label="'+title+'"'+
    'class="js-selected-navigation-item sunken-menu-item">'+
    '<span class="full-word">'+title+'</span></a></li>'
   return ele;
}

var header = "<li class='tooltipped tooltipped-w'><strong>&nbsp;Table of Contents</strong></li>"
var rows = [];

$('a[class=anchor').each(function(i, anchor) {
  $anchor = $(anchor);
  $heading = $anchor.parent();
  rows.push(
    tocLink(
      $heading.prop("tagName").toLowerCase(),
      $heading.text(),
      $anchor.attr('href')
    )
  );
})

var template = "<div class='repository-sidebar'><nav class='sunken-menu repo-nav' role='navigation'><div class='sunken-menu-separator'></div><ul class='sunken-menu-group'>"+
  header+rows.join('')+"</ul></nav></div>"

$($('.repository-with-sidebar.with-full-navigation .repository-sidebar')[0]).append(template);