Greasy Fork is available in English.

GithHub Toc

Generate TOC(Table of Content) for Github readme and Github Wiki

2014-10-06 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name GithHub Toc
  3. // @description Generate TOC(Table of Content) for Github readme and Github Wiki
  4. // @author wenLiangcan
  5. // @version 0.1
  6. // @namespace https://github.com/wenLiangcan
  7. // @homepage https://github.com/summerblue/github-toc
  8. // @license MIT (http://mit-license.org/)
  9. // @copyright Copyright © 2014 wenLiangcan
  10. // @updateURL
  11. // @downloadURL
  12. // @include https://github.com/*
  13. // @include http://github.com/*
  14. // @run-at document-end
  15. // @require http://code.jquery.com/jquery-2.1.1.min.js
  16. // @require https://greatest.deepsurf.us/scripts/5542-toc/code/toc.js?version=20193
  17. // @grant GM_addStyle
  18. // ==/UserScript==
  19.  
  20.  
  21. GM_addStyle("#toc{margin-bottom:25px}#toc ul{margin:0;padding:0;list-style:disc;margin-left:15px}#toc li a{padding:5px 10px}#toc a{text-decoration:none;display:block}#toc a:hover{background:#F3F4F8}#toc .toc-h1{padding-left:0}#toc .toc-h2{padding-left:0}#toc .toc-h3{padding-left:10px;list-style:none}article.markdown-body h2,#wiki-body .markdown-body h2{padding-bottom:.3em;font-size:1.5em;line-height:1.225;border-bottom:2px solid #eee}article.markdown-body h3,#wiki-body .markdown-body h3{padding-bottom:.3em;font-size:1.3em;line-height:1.225;border-bottom:1px solid #eee}");
  22.  
  23. (function($) {
  24. $('#wiki-body .markdown-body').prepend("<div id='toc'><div>");
  25. $('#wiki-body .markdown-body').addClass('toc-mian-body')
  26. $('article.markdown-body').prepend("<div id='toc'><div>");
  27. $('article.markdown-body').addClass('toc-mian-body')
  28. $('#toc').toc({
  29. 'selectors': 'h1,h2,h3', //elements to use as headings
  30. 'container': '.markdown-body.toc-mian-body', //element to find all selectors in
  31. 'smoothScrolling': true, //enable or disable smooth scrolling on click
  32. 'prefix': 'toc', //prefix for anchor tags and class names
  33. 'onHighlight': function(el) {}, //called when a new section is highlighted
  34. 'highlightOnScroll': true, //add class to heading that is currently in focus
  35. 'highlightOffset': 100, //offset to trigger the next headline
  36. 'anchorName': function(i, heading, prefix) { //custom function for anchor name
  37. return prefix + i;
  38. },
  39. 'headerText': function(i, heading, $heading) { //custom function building the header-item text
  40. var rank = $heading.prop("tagName").substr(1)
  41. if (rank > 2) {
  42. return '- ' + $heading.text();
  43. }
  44. return $heading.text();
  45. },
  46. });
  47. $('.markdown-body.toc-mian-body').prepend("<h2>Table of Contents</h2>");
  48. })(jQuery);