Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (138 loc) · 5.38 KB
/
Copy pathscript.js
File metadata and controls
164 lines (138 loc) · 5.38 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Function to load content dynamically
async function loadContent(path) {
const markdownPath = path + 'index.md';
try {
const res = await fetch(markdownPath);
const md = await res.text();
const html = marked.parse(md); // Convert Markdown to HTML
document.getElementById("dynamic-content").innerHTML = html;
// Apply syntax highlighting
document.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
// Generate Table of Contents
generateTOC();
window.scrollTo({ top: 0, behavior: 'smooth' });
} catch (err) {
document.getElementById("dynamic-content").innerHTML =
`<p style="color: red;">Updates are on the way for this course.</p>`;
}
}
// Static welcome content
const defaultStaticHTML = `
<div class="welcome-block animate-fade-in-up">
<img src="Java.svg" alt="Java Logo" class="java-logo animate-logo-pop" />
<h2>Let's Get Started with Java</h2>
<p>Explore the structured Java path starting from Core to Senior level topics:</p>
<br/>
<ul class="starter-list">
<li>🔰 Core Java: Variables, Control Flow, OOP Basics</li>
<li>⚙️ Advanced Java: Threads, Collections, File I/O</li>
<li>👨💻 Senior Level: JVM, Microservices, Performance</li>
</ul><br/>
<p class="start-note">Use the sidebar to begin your journey. Happy learning! ☕</p>
</div>
`;
// Handle sidebar toggles for mobile via Logo
document.getElementById('logo-menu-toggle').addEventListener('click', () => {
document.getElementById('left-sidebar').classList.toggle('active');
});
// Show category from Top Navbar
function showCategory(categoryId) {
// Hide all submenus
document.querySelectorAll('.submenu').forEach(menu => {
menu.style.display = 'none';
});
// Show selected submenu
const activeMenu = document.getElementById('cat-' + categoryId);
if (activeMenu) {
activeMenu.style.display = 'block';
}
// Update active state on top nav
document.querySelectorAll('.top-link').forEach(link => {
link.classList.remove('active-top');
if (link.getAttribute('onclick') && link.getAttribute('onclick').includes("showCategory('" + categoryId + "')")) {
link.classList.add('active-top');
}
});
// Open sidebar on mobile when a category is selected
if(window.innerWidth <= 900) {
document.getElementById('left-sidebar').classList.add('active');
}
}
// Generate dynamic Table of Contents
function generateTOC() {
const content = document.getElementById('dynamic-content');
const tocContainer = document.getElementById('toc-content');
tocContainer.innerHTML = ''; // Clear previous
const headings = content.querySelectorAll('h2, h3');
if (headings.length === 0) {
tocContainer.innerHTML = '<p style="color: gray; font-size: 13px;">No headings on this page.</p>';
return;
}
headings.forEach((heading, index) => {
// Ensure heading has an ID
if (!heading.id) {
heading.id = 'heading-' + index;
}
const link = document.createElement('a');
link.href = '#' + heading.id;
link.textContent = heading.textContent;
link.className = 'toc-link ' + (heading.tagName.toLowerCase() === 'h2' ? 'toc-h2' : 'toc-h3');
// Smooth scroll
link.addEventListener('click', (e) => {
e.preventDefault();
heading.scrollIntoView({ behavior: 'smooth', block: 'start' });
// Close mobile sidebar on click
if(window.innerWidth <= 900) {
document.getElementById('left-sidebar').classList.remove('active');
}
});
tocContainer.appendChild(link);
});
}
// Add close behavior for all nav links on mobile
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', () => {
if(window.innerWidth <= 900) {
document.getElementById('left-sidebar').classList.remove('active');
}
// Highlight active link
document.querySelectorAll('.nav-link').forEach(l => l.classList.remove('active'));
link.classList.add('active');
});
});
// Handle theme toggle
document.getElementById('theme-toggle').addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
const nextTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', nextTheme);
localStorage.setItem('theme', nextTheme);
});
// On page load: static first, then markdown + theme
window.addEventListener('DOMContentLoaded', () => {
// Load static HTML first
document.getElementById('dynamic-content').innerHTML = defaultStaticHTML;
// Generate static TOC for the welcome page
generateTOC();
// Apply saved theme
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
});
// Select all dropdown buttons
const dropdownBtns = document.querySelectorAll('.dropdown-btn');
// Add click event to toggle dropdown visibility
dropdownBtns.forEach(btn => {
btn.addEventListener('click', () => {
const dropdownContainer = btn.nextElementSibling;
// Toggle the 'open' class to show or hide the dropdown
dropdownContainer.classList.toggle('open');
});
});
const links = document.querySelectorAll('.dropdown-container a');
links.forEach(link => {
link.addEventListener('click', () => {
links.forEach(l => l.classList.remove('active')); // remove from all
link.classList.add('active'); // set to clicked one
});
});