-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (64 loc) · 2.24 KB
/
index.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
(function(){
let header = document.querySelector('header');
let addMenuItem = item => {
let node = document.createElement('span');
if(typeof item == "string"){
node.innerText = item;
} else {
node.innerHTML = `<a href="${item.url || config.baseURL || '#'}">${item.text}</a>`;
if(item.style) node.style = item.style;
}
header.append(node);
};
fetch('config.json')
.then(res => res.json())
.then(config => {
self.config = config;
document.querySelector('title').innerText = config.title;
config.menu.forEach(addMenuItem);
header.classList.remove('hidden');
});
let main = document.querySelector('main');
let article = document.querySelector('article');
let converter = new showdown.Converter({
'tables': true,
'simplifiedAutoLink': true,
'extensions': ['tufte']
});
let parser = new DOMParser();
const loadScript = async function(url){
let script = document.createElement("script");
script.type = "text/javascript";
await fetch(url).then(r => r.text().then(s => script.innerHTML = s));
document.body.appendChild(script);
};
let renderPage = page => {
main.classList.add('hidden');
fetch(page)
.then(res => res.text())
.then(md => {
let html = converter.makeHtml(md);
article.innerHTML = html;
document.querySelectorAll('pre code').forEach(hljs.highlightBlock);
renderMathInElement(main, {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "$", right: "$", display: false},
{left: "\\(", right: "\\)", display: false},
{left: "\\[", right: "\\]", display: true}
]
});
let content = parser.parseFromString(html, 'text/html');
let promises = [];
let scripts = [];
content.querySelectorAll('script').forEach(script => {
if(script.src) promises.push(loadScript(script.src));
else scripts.push(script.innerText);
});
Promise.all(promises).then(() => scripts.forEach(eval));
main.classList.remove('hidden');
});
}
let params = new URLSearchParams(document.location.search.substring(1));
renderPage(params.get('q') || 'README.md');
})();