-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspa_readme.js
More file actions
88 lines (78 loc) · 2.56 KB
/
spa_readme.js
File metadata and controls
88 lines (78 loc) · 2.56 KB
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
function getSectionId(text) {
var plainId = text
.trim() // remove spaces from start and the end
.toLowerCase() // optional
.replace(/\s/g, '-') // convert all spaces to underscores
.replace(/[^\w-]/g, ''); // remove all special characters
var id = plainId;
while ( document.getElementById(id)!=null ) {
var index = plainId==id ? 0 : parseInt(id.substring(id.lastIndexOf("-") + 1));
id = plainId+'-'+(index+1);
}
return id;
}
function getHeadingLevel(el) {
return parseInt(el.nodeName.substring(1),10);
}
function getSelfAndHigherHeadingSelector(level) {
var selfAndHigherArray = new Array;
for ( var i = 1 ; i <= level ;i++ ) {
selfAndHigherArray.push('h'+i);
}
return selfAndHigherArray.join(',');
}
function addContentDiv() {
$('h1').first().nextAll().wrapAll('<div id="content"></div>');
$('div#content').wrap('<div id="contentArea"></div>');
}
function addSections() {
$('h2, h3').each(function(i, el){
var title = $(this).text();
var level = getHeadingLevel(el);
var id = getSectionId(title);
var section = '<section id="'+id+'" title="'+title+'" aria-level="'+level+'"></section>';
$(this).nextUntil(getSelfAndHigherHeadingSelector(level)).addBack().wrapAll(section)
});
}
function generateToC() {
$('body').prepend('<div id="tocArea"><ul id="toc"></ul></div>');
$('section').each(function(i, el) {
var level = $(this).attr('aria-level');
var indent = (level-2)+'em';
var href = '#'+$(this).attr('id');
var title = $(this).attr('title');
$('ul#toc').append('<li style="padding-left: '+indent+';"><a href="'+href+'">'+title+'</a></li>');
});
}
function getShowSectionHref(id) {
return 'javascript:showSection(\''+id+'\')';
}
function showSection(id) {
var $target = $('#'+id);
if ( $target.length ) {
var $content = $('#content');
$content.children('section').addClass('hidden');
$content.children('section#'+id).removeClass('hidden');
$content.children('section').has('#'+id).removeClass('hidden');
$content.scrollTop(0);
$content.animate({
scrollTop: $target.offset().top - $content.offset().top + $content.scrollTop()
});
document.getElementById(id).scrollIntoView(); // In case the above doesn't work
}
}
function showHashSection() {
var hash = window.location.hash
var id = ( typeof hash === 'undefined' || hash === null || document.getElementById(hash.substring(1))==null )
? $('section').first().attr('id')
: hash.substring(1);
showSection(id);
}
$(function() {
addSections();
addContentDiv();
generateToC();
showHashSection();
window.onhashchange = showHashSection;
hljs.initHighlighting();
});