summaryrefslogtreecommitdiff
path: root/spec/public/scripts
diff options
context:
space:
mode:
authorAntoine Gourlay <antoine@gourlay.fr>2014-09-22 14:29:32 +0200
committerAntoine Gourlay <antoine@gourlay.fr>2014-09-22 16:50:34 +0200
commit3a72a9d1109e995b9c4de70f260ac3b5ac8f73b9 (patch)
treec97e3192030f7896d3eb56ea42166fe07a4b5f4b /spec/public/scripts
parent79564697351c439c8c79ecd50c802defda5bfa4e (diff)
downloadscala-3a72a9d1109e995b9c4de70f260ac3b5ac8f73b9.tar.gz
scala-3a72a9d1109e995b9c4de70f260ac3b5ac8f73b9.tar.bz2
scala-3a72a9d1109e995b9c4de70f260ac3b5ac8f73b9.zip
spec: generated TOC with linkable headers
This adds a table of contents to the spec, with a way to get the link of a header on hover (like on GitHub) and a "return to top" small icon at the end of each section. Jekyll generates the H1-level table of contents, and then the current one is expended using jQuery all the way down to H5 titles. Examples and H6 headers are ignored. GitHub's (MIT licensed) octicons are used for nice "link" and "up" icons.
Diffstat (limited to 'spec/public/scripts')
-rw-r--r--spec/public/scripts/navigation.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/spec/public/scripts/navigation.js b/spec/public/scripts/navigation.js
new file mode 100644
index 0000000000..c046bf4d54
--- /dev/null
+++ b/spec/public/scripts/navigation.js
@@ -0,0 +1,70 @@
+(function($){ $.fn.navigation = function() {
+
+ // the TOC already contains H1 so we start at H2
+ var headers = $('h2, h3, h4, h5').filter(function() {
+ // exclude examples
+ if (this.id.substr(0, 7) == 'example') {
+ return false;
+ }
+
+ // get all headers with an id
+ return this.id;
+ });
+
+ var output = $(this);
+
+ var get_level = function(n) { return parseInt(n.nodeName.replace('H', ''), 10); }
+
+ var back_to_top = '<span title="Return to top" class="to_top octicon octicon-chevron-up"></span>';
+
+ if (headers.length && output.length) {
+ var level = get_level(headers[0]);
+ var current_level;
+ var html = '<ol>';
+
+ headers.each(function(_, header) {
+ current_level = get_level(header);
+
+ if (current_level === level) {
+ // same level as before
+ html += '<li><a href="#' + header.id + '">' + header.innerHTML + '</a>';
+ } else if (current_level <= level) {
+ // higher level, we go back up and chose intermediary lists
+ for(i = current_level; i < level; i++) {
+ html += '</li></ol>';
+ }
+ html += '<li><a href="#' + header.id + '">' + header.innerHTML + '</a>';
+ } else if (current_level > level) {
+ // lower level, we open new nested lists
+ for(i = current_level; i > level; i--) {
+ html += '<ol><li>';
+ }
+ html += '<a href="#' + header.id + '">' + header.innerHTML + '</a>';
+ }
+
+ var header_link = '<a class="anchor" href="#' + header.id + '"><span class="octicon octicon-link"></span></a>';
+ $(header).prepend(header_link);
+
+ if (!$(header).prev().is('h1')) {
+ $(header).after(back_to_top);
+ }
+
+ level = current_level;
+ });
+
+ html += '</ol>';
+
+ output.html(html);
+ }
+
+ // back to top links
+ $(document).on('click', '.to_top', function() {
+ $(window).scrollTop(0);
+ window.location.hash = '';
+ });
+
+ // we add one more at the end of the document
+ $('#content-container').append(back_to_top);
+
+};})(jQuery);
+