forked from prygunov/indexator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (81 loc) · 2.7 KB
/
Copy pathindex.js
File metadata and controls
94 lines (81 loc) · 2.7 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
import { readdirSync, statSync, writeFileSync } from "fs";
import { join } from "path";
function generateFileIndex(basePath) {
const index = [];
function walk(dir, currentIndex) {
const files = readdirSync(dir);
files.forEach((file) => {
const fullPath = join(dir, file);
if (fullPath.startsWith(".git")) return; // Ignore .git directory
if (statSync(fullPath).isDirectory()) {
const subDir = { dir: file, files: [] };
currentIndex.push(subDir)
walk(fullPath, subDir.files);
} else {
currentIndex.push(file)
}
});
}
walk(basePath, index);
return index;
}
function generateHtml(index) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
ul { list-style-type: none; padding-left: 20px; }
li { margin: 5px 0; }
a { text-decoration: none; color: #0366d6; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>Index</h1>
<div id="file-index"></div>
<script>
const fileIndex = ${JSON.stringify(index, null, 2)};
function renderIndex(index, container, basePath = "") {
const ul = document.createElement('ul');
index.forEach((item) => {
const li = document.createElement('li');
if (typeof item === 'string') {
// File
const a = document.createElement('a');
a.textContent = item;
a.href = basePath + item; // Correctly prepend the basePath for nested files
li.appendChild(a);
} else {
// Directory
const folderName = document.createTextNode(item.dir + '/');
li.appendChild(folderName);
const subContainer = document.createElement('div');
li.appendChild(subContainer);
renderIndex(item.files, subContainer, basePath + item.dir + "/");
}
ul.appendChild(li);
});
container.appendChild(ul);
}
const container = document.getElementById('file-index');
renderIndex(fileIndex, container);
</script>
</body>
</html>`;
}
function main() {
const basePath = process.env.INPUT_PATH || ".";
const outputPath = process.env.OUTPUT_INDEX_PATH || "index.html";
const index = generateFileIndex(basePath);
// console.log(JSON.stringify(index, null, 2));
// Generate HTML file
const html = generateHtml(index);
writeFileSync(outputPath, html);
// console.log("Generated index.html");
}
main();