-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-display.js
More file actions
43 lines (37 loc) · 1.36 KB
/
code-display.js
File metadata and controls
43 lines (37 loc) · 1.36 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
// Function to fetch file contents and display them on the page
async function loadFiles() {
try {
// Fetch the list of files
const response = await fetch('content.json');
const data = await response.json();
// Container for the code
const container = document.getElementById('codeContainer');
// Iterate over each file and fetch its content
for (const file of data.files) {
const fileResponse = await fetch(file);
const fileContent = await fileResponse.text();
// Create a new section for each file
const section = document.createElement('section');
section.innerHTML = `<h2>${file}</h2><pre><code>${escapeHTML(fileContent)}</code></pre>`;
// Append the section to the container
container.appendChild(section);
}
} catch (error) {
console.error('Error fetching file contents:', error);
const container = document.getElementById('codeContainer');
container.innerHTML = 'Unable to load content.';
}
}
// Escape HTML special characters
function escapeHTML(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, (m) => map[m]);
}
// Load files on page load
window.onload = loadFiles;