-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathsee-also.js
More file actions
48 lines (41 loc) · 1.26 KB
/
see-also.js
File metadata and controls
48 lines (41 loc) · 1.26 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
(function () {
'use strict';
var path = window.location.pathname;
if (path.indexOf('/wiki/') !== 0) return;
var target = document.querySelector('.page__content');
if (!target) return;
fetch('/assets/see-also.json', { credentials: 'same-origin' })
.then(function (r) {
if (!r.ok) throw new Error('HTTP ' + r.status);
return r.json();
})
.then(function (data) {
var recs = data[path];
if (!recs || recs.length === 0) return;
render(recs);
})
.catch(function (err) {
if (window.console && console.warn) console.warn('[see-also]', err);
});
function render(recs) {
var panel = document.createElement('section');
panel.className = 'sa-panel';
panel.setAttribute('aria-label', 'Related articles');
var h = document.createElement('h3');
h.className = 'sa-heading';
h.textContent = 'See also';
panel.appendChild(h);
var ul = document.createElement('ul');
ul.className = 'sa-list';
recs.forEach(function (r) {
var li = document.createElement('li');
var a = document.createElement('a');
a.href = r.url;
a.textContent = r.title;
li.appendChild(a);
ul.appendChild(li);
});
panel.appendChild(ul);
target.appendChild(panel);
}
})();