-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
401 lines (347 loc) · 15.9 KB
/
script.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Fetch structure.json
async function fetchStructure() {
try {
const response = await fetch('structure.json');
if (!response.ok) {
throw new Error(`Failed to fetch structure.json: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error(error.message);
document.getElementById('content').innerHTML = `<p class="text-danger">Error loading structure: ${error.message}</p>`;
}
}
// Create dropdowns and links for the navbar
function createDropdown(folder) {
const dropdownItem = document.createElement('li');
dropdownItem.className = 'nav-item dropdown';
const button = document.createElement('a');
button.className = 'nav-link dropdown-toggle';
button.href = '#';
button.id = `dropdown-${folder.name}`;
button.setAttribute('role', 'button');
button.setAttribute('data-bs-toggle', 'dropdown');
button.setAttribute('aria-expanded', 'false');
button.textContent = folder.name;
const menu = document.createElement('ul');
menu.className = 'dropdown-menu';
menu.setAttribute('aria-labelledby', `dropdown-${folder.name}`);
folder.children.forEach((item) => {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.className = 'dropdown-item';
link.textContent = item.name;
if (!item.isDirectory) {
link.href = '#';
link.addEventListener('click', (e) => {
e.preventDefault();
loadFile(item.path);
});
}
listItem.appendChild(link);
menu.appendChild(listItem);
});
dropdownItem.appendChild(button);
dropdownItem.appendChild(menu);
return dropdownItem;
}
function renderNavbar(structure) {
const navItems = document.getElementById('nav-items');
navItems.innerHTML = ''; // Clear existing items if any
structure.children.forEach((item) => {
if (!item.isDirectory) {
// Add individual file as a navbar item
const navItem = document.createElement('li');
navItem.className = 'nav-item';
const link = document.createElement('a');
link.className = 'nav-link';
link.textContent = item.name;
link.href = '#';
link.addEventListener('click', (e) => {
e.preventDefault();
loadFile(item.path);
});
navItem.appendChild(link);
navItems.appendChild(navItem);
} else {
// Create dropdown for folders
const dropdown = createDropdown(item);
navItems.appendChild(dropdown);
}
});
}
// Load and display markdown content
async function loadFile(filePath) {
try {
const response = await fetch(filePath);
if (!response.ok) throw new Error(`Failed to load file: ${filePath}`);
let markdown = await response.text();
// Correct image paths in the markdown content
const basePath = filePath.substring(0, filePath.lastIndexOf('/') + 1);
markdown = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (match, alt, src) => {
if (!src.startsWith('http') && !src.startsWith('/')) {
src = basePath + src;
}
return `![${alt}](${src})`;
});
const htmlContent = marked.parse(markdown);
document.getElementById('content').innerHTML = `
<div class="markdown-content">${htmlContent}</div>
`;
addCopyAndCollapseActions(); // Add copy and collapsibility actions to code blocks
hljs.highlightAll(); // Apply syntax highlighting
// Store the full markdown content and file name for global actions
window.currentMarkdownContent = markdown;
window.currentFileName = filePath.split('/').pop();
// Show the content area and hide the cards container
document.getElementById('content').style.display = 'block';
document.getElementById('cards-container').style.display = 'none';
// Show global buttons
document.querySelector('.global-buttons').style.display = 'flex';
// Hide search bar, search results, and tree view container
document.querySelector('.search-bar').style.display = 'none';
document.getElementById('search-results').style.display = 'none';
document.getElementById('tree-view-container').style.display = 'none';
} catch (error) {
document.getElementById('content').innerHTML = `<p class="text-danger">Error: ${error.message}</p>`;
}
}
// Add copy-to-clipboard, download, view raw content, and collapsibility to code blocks
function addCopyAndCollapseActions() {
const preBlocks = document.querySelectorAll('pre');
preBlocks.forEach((pre, index) => {
// Create a wrapper for the code block
const wrapper = document.createElement('div');
wrapper.className = 'code-block-wrapper border rounded position-relative mb-4';
wrapper.style.overflow = 'hidden';
// Create header with toggle and copy buttons
const header = document.createElement('div');
header.className = 'code-block-header d-flex justify-content-between align-items-center px-3 py-2 bg-light border-bottom';
// Title for the code block (optional, e.g., "Code Block X")
const title = document.createElement('span');
title.textContent = `Code Block ${index + 1}`;
title.className = 'code-block-title fw-bold';
// Create actions container
const actionsDiv = document.createElement('div');
actionsDiv.className = 'code-actions d-flex gap-2';
// Add Copy Button
const copyButton = document.createElement('button');
copyButton.className = 'btn btn-sm btn-outline-primary d-flex align-items-center gap-1';
copyButton.innerHTML = `<i class="fas fa-copy"></i><span>Copy</span>`;
copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(pre.textContent).then(() => {
copyButton.innerHTML = `<i class="fas fa-check"></i><span>Copied</span>`;
setTimeout(() => (copyButton.innerHTML = `<i class="fas fa-copy"></i><span>Copy</span>`), 2000);
}).catch((err) => console.error('Failed to copy text:', err));
});
// Add Download Button
const downloadButton = document.createElement('button');
downloadButton.className = 'btn btn-sm btn-outline-success d-flex align-items-center gap-1';
downloadButton.innerHTML = `<i class="fas fa-download"></i><span>Download</span>`;
downloadButton.addEventListener('click', () => {
const blob = new Blob([pre.textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `code-block-${index + 1}.txt`;
a.click();
URL.revokeObjectURL(url);
});
// Add View Raw Content Button
const viewRawButton = document.createElement('button');
viewRawButton.className = 'btn btn-sm btn-outline-info d-flex align-items-center gap-1';
viewRawButton.innerHTML = `<i class="fas fa-eye"></i><span>View Raw</span>`;
viewRawButton.addEventListener('click', () => {
const rawWindow = window.open('', '_blank');
rawWindow.document.write(`<pre>${pre.textContent}</pre>`);
rawWindow.document.close();
});
// Add Collapse/Expand Button
const collapseButton = document.createElement('button');
collapseButton.className = 'btn btn-sm btn-outline-secondary d-flex align-items-center gap-1';
collapseButton.innerHTML = `<i class="fas fa-chevron-up"></i><span>Collapse</span>`;
collapseButton.addEventListener('click', () => {
pre.classList.toggle('collapsed');
collapseButton.innerHTML = pre.classList.contains('collapsed')
? `<i class="fas fa-chevron-down"></i><span>Expand</span>`
: `<i class="fas fa-chevron-up"></i><span>Collapse</span>`;
pre.style.display = pre.classList.contains('collapsed') ? 'none' : 'block';
});
// Append buttons to the actions container
actionsDiv.appendChild(copyButton);
actionsDiv.appendChild(downloadButton);
actionsDiv.appendChild(viewRawButton);
actionsDiv.appendChild(collapseButton);
// Append title and actions to the header
header.appendChild(title);
header.appendChild(actionsDiv);
// Wrap the pre block
wrapper.appendChild(header);
pre.parentNode.insertBefore(wrapper, pre);
wrapper.appendChild(pre);
// Add initial styles for collapsed state
pre.style.margin = '0';
pre.style.padding = '1rem';
pre.style.backgroundColor = '#f8f9fa';
});
}
// Add global toggle button for all code blocks
function addGlobalToggleButton() {
const globalToggleButton = document.createElement('button');
globalToggleButton.className = 'btn btn-outline-secondary';
globalToggleButton.innerHTML = '<i class="fas fa-code"></i>'; // Use icon instead of text
globalToggleButton.addEventListener('click', () => {
const preBlocks = document.querySelectorAll('pre');
preBlocks.forEach(pre => {
pre.classList.toggle('collapsed');
pre.style.display = pre.classList.contains('collapsed') ? 'none' : 'block';
});
});
document.querySelector('.global-buttons').appendChild(globalToggleButton);
}
// Add global buttons for view raw and download all code blocks
function addGlobalButtons() {
const container = document.querySelector('.global-buttons');
// Global View Raw Button
const globalViewRawButton = document.createElement('button');
globalViewRawButton.className = 'btn btn-outline-info';
globalViewRawButton.innerHTML = '<i class="fas fa-eye"></i>'; // Use icon instead of text
globalViewRawButton.addEventListener('click', () => {
const rawWindow = window.open('', '_blank');
rawWindow.document.write(`<pre>${window.currentMarkdownContent}</pre>`);
rawWindow.document.close();
});
// Global Download Button
const globalDownloadButton = document.createElement('button');
globalDownloadButton.className = 'btn btn-outline-success';
globalDownloadButton.innerHTML = '<i class="fas fa-download"></i>'; // Use icon instead of text
globalDownloadButton.addEventListener('click', () => {
const blob = new Blob([window.currentMarkdownContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = window.currentFileName || 'full-markdown-file.md';
a.click();
URL.revokeObjectURL(url);
});
// Insert buttons into the global-buttons container
container.appendChild(globalViewRawButton);
container.appendChild(globalDownloadButton);
}
function createTreeView(structure, container) {
const ul = document.createElement('ul');
ul.className = 'tree-view';
structure.children.forEach(item => {
const li = document.createElement('li');
if (item.isDirectory) {
li.className = 'folder';
li.textContent = item.name;
li.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent click event from propagating to parent elements
li.classList.toggle('expanded');
// Toggle visibility of child elements
const childUl = li.querySelector('ul');
if (childUl) {
childUl.style.display = childUl.style.display === 'none' ? 'block' : 'none';
}
});
if (item.children) {
const childUl = document.createElement('ul');
childUl.style.display = 'none';
createTreeView(item, childUl);
li.appendChild(childUl);
}
} else {
li.className = 'file';
li.textContent = item.name;
li.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent click event from propagating to parent elements
loadFile(item.path);
});
}
ul.appendChild(li);
});
container.appendChild(ul);
}
function searchFiles(query, structure, results = []) {
structure.children.forEach(item => {
if (item.isDirectory) {
searchFiles(query, item, results);
} else if (item.name.toLowerCase().includes(query.toLowerCase())) {
results.push(item);
}
});
return results;
}
function renderSearchResults(results) {
const searchResultsContainer = document.getElementById('search-results');
searchResultsContainer.innerHTML = '';
if (results.length === 0) {
searchResultsContainer.innerHTML = '<p>No results found.</p>';
return;
}
const ul = document.createElement('ul');
ul.className = 'list-group';
results.forEach(item => {
const li = document.createElement('li');
li.className = 'list-group-item file';
li.textContent = item.name;
li.addEventListener('click', () => loadFile(item.path));
ul.appendChild(li);
});
searchResultsContainer.appendChild(ul);
}
// Hide Some blocs by default
function HideBlocs()
{
// Hide global buttons by default
document.querySelector('.global-buttons').style.display = 'none';
// document.querySelector('.cards-container').style.display = 'none';
}
// Show search bar and cards container on home page
function showHomePage() {
document.querySelector('.search-bar').style.display = 'block';
document.getElementById('search-results').style.display = 'block';
document.getElementById('tree-view-container').style.display = 'block';
document.getElementById('content').style.display = 'none';
document.querySelector('.global-buttons').style.display = 'none';
}
// Method to clear search results when clicking outside
function clearSearchResultsOnClickOutside() {
document.addEventListener('click', function(event) {
var searchResults = document.getElementById('search-results');
if (!searchResults.contains(event.target)) {
searchResults.innerHTML = '';
}
});
}
// Initialize the app
(async () => {
const structure = await fetchStructure();
if (structure) {
renderNavbar(structure);
hljs.highlightAll(); // Apply syntax highlighting
addGlobalToggleButton(); // Add global toggle button
addGlobalButtons(); // Add global view raw and download buttons
HideBlocs(); // Hide some blocs by default
// Create tree view
const treeViewContainer = document.getElementById('tree-view-container');
createTreeView(structure, treeViewContainer);
// Add search functionality
const searchInput = document.getElementById('search-input');
searchInput.addEventListener('input', () => {
const query = searchInput.value;
const results = searchFiles(query, structure);
renderSearchResults(results);
});
// Add event listener to the blog link to display cards
document.querySelector('.navbar-brand').addEventListener('click', (e) => {
e.preventDefault();
showHomePage();
});
// Add event listener to clear search results on click outside
clearSearchResultsOnClickOutside();
// Show home page by default
showHomePage();
}
})();