forked from ringcentral/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add showing/hiding submenus, fix sidebar styling, fix bug where includes wouldn't appear in ToC * Update ToC to highlight last header if page is scrolled to very bottom, fixes #280 * Set HTML title to current h1 section text, see #133 * Fix menu not opening on mobile * Add back increase toc item height on mobile * Fix padding bug * Add back in ToC sliding animation
- Loading branch information
Showing
13 changed files
with
202 additions
and
1,697 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'nokogiri' | ||
|
||
def toc_data(page_content) | ||
html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content) | ||
|
||
# get a flat list of headers | ||
headers = [] | ||
html_doc.css('h1, h2, h3').each do |header| | ||
headers.push({ | ||
id: header.attribute('id').to_s, | ||
content: header.content, | ||
level: header.name[1].to_i, | ||
children: [] | ||
}) | ||
end | ||
|
||
[3,2].each do |header_level| | ||
header_to_nest = nil | ||
headers = headers.reject do |header| | ||
if header[:level] == header_level | ||
header_to_nest[:children].push header if header_to_nest | ||
true | ||
else | ||
header_to_nest = header if header[:level] == (header_level - 1) | ||
false | ||
end | ||
end | ||
end | ||
headers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
//= require ./lib/_energize | ||
//= require ./app/_lang | ||
//= require ./all_nosearch | ||
//= require ./app/_search | ||
//= require ./app/_toc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
//= require ./lib/_energize | ||
//= require ./app/_lang | ||
//= require ./app/_toc | ||
//= require ./app/_lang | ||
|
||
$(function() { | ||
loadToc($('#toc'), '.toc-link', '.toc-list-h2', 10); | ||
setupLanguages($('body').data('languages')); | ||
$('.content').imagesLoaded( function() { | ||
window.recacheHeights(); | ||
window.refreshToc(); | ||
}); | ||
}); | ||
|
||
window.onpopstate = function() { | ||
activateLanguage(getLanguageFromQueryString()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,106 @@ | ||
//= require ../lib/_jquery | ||
//= require ../lib/_jquery_ui | ||
//= require ../lib/_jquery.tocify | ||
//= require ../lib/_imagesloaded.min | ||
(function (global) { | ||
;(function () { | ||
'use strict'; | ||
|
||
var debounce = function(func, waitTime) { | ||
var timeout = false; | ||
return function() { | ||
if (timeout === false) { | ||
setTimeout(function() { | ||
func(); | ||
timeout = false; | ||
}, waitTime); | ||
timeout = true; | ||
} | ||
}; | ||
}; | ||
|
||
var closeToc = function() { | ||
$(".tocify-wrapper").removeClass('open'); | ||
$("#nav-button").removeClass('open'); | ||
}; | ||
|
||
var makeToc = function() { | ||
global.toc = $("#toc").tocify({ | ||
selectors: 'h1, h2', | ||
extendPage: false, | ||
theme: 'none', | ||
smoothScroll: false, | ||
showEffectSpeed: 0, | ||
hideEffectSpeed: 180, | ||
ignoreSelector: '.toc-ignore', | ||
highlightOffset: 60, | ||
scrollTo: -1, | ||
scrollHistory: true, | ||
hashGenerator: function (text, element) { | ||
return element.prop('id'); | ||
function loadToc($toc, tocLinkSelector, tocListSelector, scrollOffset) { | ||
var headerHeights = {}; | ||
var pageHeight = 0; | ||
var windowHeight = 0; | ||
var originalTitle = document.title; | ||
|
||
var recacheHeights = function() { | ||
headerHeights = {}; | ||
pageHeight = $(document).height(); | ||
windowHeight = $(window).height(); | ||
|
||
$toc.find(tocLinkSelector).each(function() { | ||
var targetId = $(this).attr('href'); | ||
if (targetId[0] === "#") { | ||
headerHeights[targetId] = $(targetId).offset().top; | ||
} | ||
}); | ||
}; | ||
|
||
var refreshToc = function() { | ||
var currentTop = $(document).scrollTop() + scrollOffset; | ||
|
||
if (currentTop + windowHeight >= pageHeight) { | ||
// at bottom of page, so just select last header by making currentTop very large | ||
// this fixes the problem where the last header won't ever show as active if its content | ||
// is shorter than the window height | ||
currentTop = pageHeight + 1000; | ||
} | ||
|
||
var best = null; | ||
for (var name in headerHeights) { | ||
if ((headerHeights[name] < currentTop && headerHeights[name] > headerHeights[best]) || best === null) { | ||
best = name; | ||
} | ||
} | ||
}).data('toc-tocify'); | ||
|
||
$("#nav-button").click(function() { | ||
$(".tocify-wrapper").toggleClass('open'); | ||
$("#nav-button").toggleClass('open'); | ||
return false; | ||
}); | ||
var $best = $toc.find("[href='" + best + "']").first(); | ||
if (!$best.hasClass("active")) { | ||
$toc.find(".active").removeClass("active"); | ||
$best.addClass("active"); | ||
$best.parents(tocListSelector).addClass("active"); | ||
$best.siblings(tocListSelector).addClass("active"); | ||
$toc.find(tocListSelector).filter(":not(.active)").slideUp(150); | ||
$toc.find(tocListSelector).filter(".active").slideDown(150); | ||
if (window.history.pushState) { | ||
window.history.pushState(null, "", best); | ||
} | ||
// TODO remove classnames | ||
document.title = $best.data("title") + " – " + originalTitle; | ||
} | ||
}; | ||
|
||
$(".page-wrapper").click(closeToc); | ||
$(".tocify-item").click(closeToc); | ||
}; | ||
var makeToc = function() { | ||
recacheHeights(); | ||
refreshToc(); | ||
|
||
// Hack to make already open sections to start opened, | ||
// instead of displaying an ugly animation | ||
function animate() { | ||
setTimeout(function() { | ||
toc.setOption('showEffectSpeed', 180); | ||
}, 50); | ||
} | ||
$("#nav-button").click(function() { | ||
$(".toc-wrapper").toggleClass('open'); | ||
$("#nav-button").toggleClass('open'); | ||
return false; | ||
}); | ||
$(".page-wrapper").click(closeToc); | ||
$(".tocify-item").click(closeToc); | ||
|
||
// reload immediately after scrolling on toc click | ||
$toc.find(tocLinkSelector).click(function() { | ||
setTimeout(function() { | ||
refreshToc(); | ||
}, 0); | ||
}); | ||
|
||
$(window).scroll(debounce(refreshToc, 200)); | ||
$(window).resize(debounce(recacheHeights, 200)); | ||
}; | ||
|
||
$(function() { | ||
makeToc(); | ||
animate(); | ||
setupLanguages($('body').data('languages')); | ||
$('.content').imagesLoaded( function() { | ||
global.toc.calculateHeights(); | ||
}); | ||
}); | ||
})(window); | ||
|
||
window.recacheHeights = recacheHeights; | ||
window.refreshToc = refreshToc; | ||
} | ||
|
||
window.loadToc = loadToc; | ||
})(); |
Oops, something went wrong.