-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (65 loc) · 2.25 KB
/
index.js
File metadata and controls
68 lines (65 loc) · 2.25 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
activeElement = null;
function parse(md){
var lines = md.split("\n")
var ret = {};
var title = lines[0].replace("# ","").replace("\r","");
lines.splice(0,1);
var cur = "";
for (line of lines){
if (line.startsWith("<!")) {cur = line.replace("<!","").replace("\r","");ret[cur]=["# "+cur]; continue;}
if (line.startsWith("!>")) {cur = "";continue}
if (ret[cur]) ret[cur].push(line.replace("\r",""))
}
for (key of Object.keys(ret)){
ret[key] = ret[key].join("\n");
}
return {title:title,...ret};
}
function createUI(data){
console.log(data);
var details = document.createElement("details");
var summary = document.createElement("summary");
summary.innerText = data.title;
details.appendChild(summary);
for (key of Object.keys(data)){
if (key == "title") continue;
var div = document.createElement("div");
div.setAttribute("class","link-display");
div.md = data[key];
div.addEventListener("click",(e)=>{
// set all elements to be inactive first
var element = e.target;
if (e.target.tagName == "A") element = element.parentElement;
if (activeElement!= null) activeElement.setAttribute("class","link-display");
element.setAttribute("class","link-display-a");
activeElement = element;
renderMD(element.md);
})
var a = document.createElement("a");
a.text = key;
div.appendChild(a);
details.appendChild(div);
}
details.setAttribute("open","");
document.getElementById("s_content").appendChild(details);
}
async function createFromListing(){
var main = await fetch("md/main.md");
main_md = await main.text();
renderMD(main_md);
var _l = await fetch("md/listing");
var listing = (await _l.text()).split("\n");
for (item of listing){
var md = await fetch("md/"+item);
createUI(parse(await md.text()));
}
}
function renderMD(md){
var con = new showdown.Converter();
document.getElementById("md").innerHTML = con.makeHtml(md);
}
document.getElementById("home").addEventListener("click",()=>{
activeElement.setAttribute("class","link-display");
renderMD(main_md);
})
createFromListing();