-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd.html
67 lines (64 loc) · 3.36 KB
/
md.html
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
<!DOCTYPE html><html lang=en> <head><meta charset=UTF-8><meta name=viewport content="width=device-width, initial-scale=1.0"><title>tcrouzet.com archives markdown</title><style>
ul {
list-style-type: none;
}
.nested {
display: none;
}
.active {
display: block;
}
.year, .month {
cursor: pointer;
font-weight: bold;
}
</style></head> <body> <h1>tcrouzet.com archives markdown</h1> <ul id=markdown-list></ul> <script>
document.addEventListener('DOMContentLoaded', function() {
const repoUrl = 'https://api.github.com/repos/tcrouzet/md/contents/';
function createListItem(text, isFolder = false) {
const listItem = document.createElement('li');
listItem.textContent = text;
if (isFolder) {
listItem.classList.add('folder');
}
return listItem;
}
function fetchContents(url, parentElement, isMonth = false) {
fetch(url)
.then(response => response.json())
.then(data => {
data.sort((a, b) => {
const nameA = a.name.length === 1 ? '0' + a.name : a.name;
const nameB = b.name.length === 1 ? '0' + b.name : b.name;
return nameA.localeCompare(nameB);
});
data.forEach(file => {
if (file.type === 'dir' && !file.name.startsWith('_') && !file.name.startsWith('.')) {
const listItem = createListItem(file.name, true);
listItem.classList.add(isMonth ? 'month' : 'year');
parentElement.appendChild(listItem);
const nestedList = document.createElement('ul');
nestedList.classList.add('nested');
parentElement.appendChild(nestedList);
listItem.addEventListener('click', function() {
nestedList.classList.toggle('active');
if (!nestedList.classList.contains('loaded')) {
fetchContents(file.url, nestedList, !isMonth);
nestedList.classList.add('loaded');
}
});
} else if (file.name.endsWith('.md') && parentElement !== document.getElementById('markdown-list')) {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.href = file.html_url;
link.textContent = file.name;
listItem.appendChild(link);
parentElement.appendChild(listItem);
}
});
})
.catch(error => console.error('Error:', error));
}
fetchContents(repoUrl, document.getElementById('markdown-list'));
});
</script> </body> </html>