-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (66 loc) · 2.42 KB
/
app.js
File metadata and controls
77 lines (66 loc) · 2.42 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
69
70
71
72
73
74
75
76
77
const USERNAME = 'redbackspider77';
const MAIN_REPO_NAME = 'redbackspider77.github.io';
async function fetchGitHubRepos() {
try {
// Fetch public repositories for the user
const response = await fetch(`https://api.github.com/users/${USERNAME}/repos?per_page=100`);
if (!response.ok) throw new Error('Failed to fetch repositories');
const repos = await response.json();
// Filter for repos that have GitHub Pages enabled and are not the main site itself
return repos
.filter(repo => repo.has_pages && repo.name !== MAIN_REPO_NAME)
.map(repo => ({
name: repo.name,
// Use the repository name as the path (standard for GitHub Pages)
url: `/${repo.name}/`
}));
} catch (error) {
console.error('Error fetching GitHub repos:', error);
return [];
}
}
async function fetchLocalDirectories() {
try {
const response = await fetch('subdirectories.json');
if (!response.ok) throw new Error('Failed to fetch local directories');
const dirs = await response.json();
return dirs.map(dir => ({
name: dir,
url: `${dir}/`
}));
} catch (error) {
console.error('Error fetching local directories:', error);
return [];
}
}
async function init() {
const list = document.getElementById("subdirectoryList");
// Fetch both sources in parallel
const [repos, localDirs] = await Promise.all([
fetchGitHubRepos(),
fetchLocalDirectories()
]);
// Combine both lists
const allLinks = [...localDirs, ...repos];
// Deduplicate by name (in case a repo is also listed in subdirectories.json)
const uniqueLinksMap = new Map();
allLinks.forEach(link => uniqueLinksMap.set(link.name, link));
const uniqueLinks = Array.from(uniqueLinksMap.values());
// Sort alphabetically
uniqueLinks.sort((a, b) => a.name.localeCompare(b.name));
// Render the list
uniqueLinks.forEach(link => {
const li = document.createElement("li");
const a = document.createElement("a");
a.href = link.url;
a.textContent = link.name;
li.appendChild(a);
list.appendChild(li);
});
}
// Ensure DOM is ready before running
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}