-
Notifications
You must be signed in to change notification settings - Fork 292
/
main.js
147 lines (125 loc) · 5.32 KB
/
main.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
$(function(){
setup_search_box();
setup_auto_title_anchors();
setup_debug_mode();
setup_tables();
$(window).resize(setup_search_box);
});
function setup_tables() {
$('.pod-table').each(function() {
if ($(this).find('thead').length
&& $(this).find('tr').length > 10
&& ! ( // don't sort operators precedence table
$(this).find('thead th:first-child' ).text() == 'A'
&& $(this).find('thead th:first-child + th').text() == 'Level'
)) $(this).tablesorter({sortList: [[0,0]]})
})
}
function setup_search_box() {
var sel = $('#search'), head = $('#logo').parent();
if ( ! sel.length ) { return; }
/* Setup handling of narrow screens */
if ( head.offset().top + head.innerHeight() <= sel.offset().top ) {
sel.addClass('two-row');
}
else {
sel.removeClass('two-row');
}
}
function setup_auto_title_anchors() {
$('#content').find('h1,h2,h3,h4,h5,h6').each(function(i, el){
if ( ! $(el).attr('id') ) { return; }
$(el).append(
'<a href="#' + $(el).attr('id') + '" class="title-anchor">§</a>'
);
});
}
document.addEventListener("keyup", function(evt){
if(evt.key == "Escape" && $('body').scrollTop() == 0) {
$('#query').focus();
}
});
function setup_debug_mode(){
$('footer').children(':first').append('<span id="debug"> [Debug: '+ (window.sessionStorage.getItem("debug")?"on":"off") +']</span>');
$('#debug').click(function(){
if ( $(this).text().includes('off') ) {
window.sessionStorage.setItem("debug", "on");
$(this).html('[Debug: on]');
}else{
window.sessionStorage.removeItem("debug");
$(this).html('[Debug: off]');
}
});
if ( window.sessionStorage.getItem("debug") ) {
console.info("checking for duplicated name and id attrs");
var seen_name_or_id = [];
$('#content').css('overflow', 'visible');
$('html').find('a').each( function(i, el){
if ( el.name ) {
if ( seen_name_or_id.includes(el.name) ) {
console.log("found duplicate name attr in", el);
}
seen_name_or_id.push(el.name);
$(el).after('<span><a href="#' + el.name + '" style="color: magenta;">«#'+el.name+'»</a> </span>');
}
if ( el.id ) {
if ( seen_name_or_id.includes(el.id) ) {
console.log("found duplicate id attr in", el);
}
seen_name_or_id.push(el.id);
}
});
console.log('setup viewport resolution display');
$('body').append('<span id="screen_res" style="color: magenta; position: absolute; bottom: 0; left: 0;"></span>');
window.setInterval(function screen_size_on_status_bar(){
$('#screen_res').text(window.innerWidth + 'x' + window.innerHeight);
}, 1000);
console.info("add debug CSS");
$('head').append($('<style/>', {
id: 'debug',
html: 'table#TOC td.toc-number { display: inherit; }'
}));
if(window.localStorage){
var sS = window.localStorage;
var commit = $('#footer-commit').text();
if ( sS.getItem('commit') != commit ) {
sS.clear();
sS.setItem('commit', commit);
console.info("wiping cache");
}
if ( ! sS.getItem(commit+window.location.pathname) ) {
sS.setItem(commit+window.location.pathname, "seen");
console.info("checking for dead links");
function report_broken_link(url) {
$('html').find('#search').after('<div style="text-align: center;">Broken link: ' + url + ' found. Please report at <a href="https://web.libera.chat/?channel=#raku">irc.libera.chat#raku</a></div>');
}
var seen_link = [];
var links = [];
$('html').find('a[href]').each(function(i,el){ links.push(el.href)});
$('svg').find('a').filter(function(i,e){return e.href.baseVal}).each(function(i,el){ links.push(el.href.baseVal) });
links.forEach( function(el) {
var url_without_anchor = el.split('#')[0];
if ( ! seen_link.includes(decodeURIComponent(url_without_anchor)) ) {
seen_link.push(decodeURIComponent(url_without_anchor));
}
});
seen_link.forEach( function(url) {
var request = new XMLHttpRequest();
request.onreadystatechange = function(){
if ( request.readyState === 4 ) {
if ( request.status >= 400 ) {
report_broken_link(request.status + " for " + url);
} else {
// console.log(request.status + " for " + url);
}
}
}
try {
request.open('HEAD', url);
request.send();
} catch (e) { /* this will catch errors due to browser security settings for external links */ }
});
}
}
}
}