-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (51 loc) · 1.97 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
const name = new URLSearchParams(location.search).get('doc');
let list = [];
if(name){
//문서 내용 가지고 오기
fetch(`https://raw.githubusercontent.com/jedenzero/alcze/main/docs/${name}.md`)
.then(response => response.text())
.then(data => {
document.querySelector('#content').innerHTML = `<h1>${name}</h1>`;
document.querySelector('#content').innerHTML += parse(data);
})
.catch(() => {
document.querySelector('#content').innerHTML = `<div class="danger">존재하지 않는 문서입니다.</div>`;
});
}
//문서 목록 가지고 오기
fetch(`https://api.github.com/repos/jedenzero/alcze/contents/docs`)
.then(response => response.json())
.then(data => {
list = data.map(obj => obj['name'].replace('.md', ''));
});
//문서 검색
function search(){
document.querySelector('#result').innerHTML = '';
const keyword = document.querySelector('input').value;
let result = list.filter(doc => doc.includes(keyword));
result.sort((a,b) => a.length-b.length || a.localeCompare(b));
result = result.slice(0,5);
result.forEach(doc => {
document.querySelector('#result').innerHTML += `<a href="./?doc=${doc}"><div>${doc}</div></a>`;
});
}
function parse(doc){
if(doc.match(/^<<[^\n>]+>>\n/)){
let theme = '';
doc = doc.replace(/^<<([^\n>]+)>>\n/,(match, captured_theme)=>{
theme = captured_theme;
return '';
});
const theme_link = document.createElement('link');
theme_link.rel = 'stylesheet';
theme_link.href = `./themes/${theme}.css`;
document.head.appendChild(theme_link);
document.querySelector('#theme_default').disabled = true;
}
doc = marked.parse(doc);
doc = doc.replace(/<a href="([^"]+)">/g, '<a href="./?doc=$1">');
doc = doc.replace(/(?<=<a href="[^"]*)_(?=[^">]*">)/g, ' ');
doc = doc.replace(/\[\[([^\[\]\n]+)\]\]/g, '<a href="./?doc=$1">$1</a>');
doc = doc.replace(/<img src="([^"]+)"/g, '<img src="./imgs/$1"');
return doc;
}