Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

toc.js 3.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  1. // https://github.com/ghiculescu/jekyll-table-of-contents
  2. // this library modified by fastai to:
  3. // - update the location.href with the correct anchor when a toc item is clicked on
  4. (function($){
  5. $.fn.toc = function(options) {
  6. var defaults = {
  7. noBackToTopLinks: false,
  8. title: '',
  9. minimumHeaders: 3,
  10. headers: 'h1, h2, h3, h4',
  11. listType: 'ol', // values: [ol|ul]
  12. showEffect: 'show', // values: [show|slideDown|fadeIn|none]
  13. showSpeed: 'slow' // set to 0 to deactivate effect
  14. },
  15. settings = $.extend(defaults, options);
  16. var headers = $(settings.headers).filter(function() {
  17. // get all headers with an ID
  18. var previousSiblingName = $(this).prev().attr( "name" );
  19. if (!this.id && previousSiblingName) {
  20. this.id = $(this).attr( "id", previousSiblingName.replace(/\./g, "-") );
  21. }
  22. return this.id;
  23. }), output = $(this);
  24. if (!headers.length || headers.length < settings.minimumHeaders || !output.length) {
  25. return;
  26. }
  27. if (0 === settings.showSpeed) {
  28. settings.showEffect = 'none';
  29. }
  30. var render = {
  31. show: function() { output.hide().html(html).show(settings.showSpeed); },
  32. slideDown: function() { output.hide().html(html).slideDown(settings.showSpeed); },
  33. fadeIn: function() { output.hide().html(html).fadeIn(settings.showSpeed); },
  34. none: function() { output.html(html); }
  35. };
  36. var get_level = function(ele) { return parseInt(ele.nodeName.replace("H", ""), 10); }
  37. var highest_level = headers.map(function(_, ele) { return get_level(ele); }).get().sort()[0];
  38. //var return_to_top = '<i class="glyphicon glyphicon-upload back-to-top"></i>';
  39. // other nice icons that can be used instead: glyphicon-upload glyphicon-hand-up glyphicon-chevron-up glyphicon-menu-up glyphicon-triangle-top
  40. var level = get_level(headers[0]),
  41. this_level,
  42. html = settings.title + " <"+settings.listType+">";
  43. headers.on('click', function() {
  44. if (!settings.noBackToTopLinks) {
  45. window.location.hash = this.id;
  46. }
  47. })
  48. .addClass('clickable-header')
  49. .each(function(_, header) {
  50. base_url = window.location.href;
  51. base_url = base_url.replace(/#.*$/, "");
  52. this_level = get_level(header);
  53. //if (!settings.noBackToTopLinks && this_level > 1) {
  54. // $(header).addClass('top-level-header').before(return_to_top);
  55. //}
  56. txt = header.textContent.split('¶')[0].split('(')[0].split('[')[0];
  57. if (!txt) {return;}
  58. if (this_level === level) // same level as before; same indenting
  59. html += "<li><a href='" + base_url + "#" + header.id + "'>" + txt + "</a>";
  60. else if (this_level <= level){ // higher level than before; end parent ol
  61. for(i = this_level; i < level; i++) {
  62. html += "</li></"+settings.listType+">"
  63. }
  64. html += "<li><a href='" + base_url + "#" + header.id + "'>" + txt + "</a>";
  65. }
  66. else if (this_level > level) { // lower level than before; expand the previous to contain a ol
  67. for(i = this_level; i > level; i--) {
  68. html += "<"+settings.listType+">"+((i-level == 2) ? "<li class=\"hide_content\">" : "<li>")
  69. }
  70. html += "<a href='" + base_url + "#" + header.id + "'>" + txt + "</a>";
  71. }
  72. level = this_level; // update for the next one
  73. });
  74. html += "</"+settings.listType+">";
  75. if (!settings.noBackToTopLinks) {
  76. $(document).on('click', '.back-to-top', function() {
  77. $(window).scrollTop(0);
  78. window.location.hash = '';
  79. });
  80. }
  81. render[settings.showEffect]();
  82. };
  83. })(jQuery);
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...