-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.js
160 lines (155 loc) · 4.71 KB
/
template.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
const config = require("./config");
function generateFileList(files, relativeDir) {
return files
.map((file) => {
let bgcolor = "transparent";
let txtcolor = config.theme.colors.accent.default;
const extension = Object.keys(config.theme.colors.files).find((ext) =>
file.name.endsWith(ext)
);
if (extension) {
({ bgcolor, txtcolor } = config.theme.colors.files[extension]);
}
return `
<li class="file-item">
<a href="${relativeDir ? `/${relativeDir}` : ""}/${file.name}${
file.isDirectory ? "/" : ""
}"
data-name="${file.name}"
data-is-directory="${file.isDirectory}"
style="padding: 0 20px; border-radius: ${
config.theme.element_styles.file.border_radius
};
${
file.isDirectory
? `background: ${config.theme.colors.elements.file.is_folder.background};
color: ${config.theme.colors.elements.file.is_folder.text_color};`
: `background:${bgcolor};color:${txtcolor};`
}"
>${file.name}</a>
</li>`;
})
.join("");
}
function generateHtml({
relativeDir,
parentDir,
dirInfo,
files,
metaTags,
seoTags,
styles,
}) {
return `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
${metaTags}
${seoTags}
<title>${relativeDir || "/"}</title>
<style>${styles}</style>
</head>
<body>
<a class="go-home" href="${
parentDir ? `/${parentDir}` : "/"
}" rel="noopener noreferrer" target="_self"
style="font-size:${
config.theme.element_styles.parent_dir.font_size
}">
..<span style="font-size:${
config.theme.element_styles.parent_dir.slash.font_size
};
font-weight:${
config.theme.element_styles.parent_dir.slash
.font_weight
};">
/
</span>
</a>
<h1 class="title">${relativeDir || "/"}</h1>
<input class="search" id="search" onkeyup="filterFiles()" placeholder="Search files...">
<div class="file-info">
<p>Total size: ${dirInfo.totalSizeFormatted}</p>
<p>Number of files: ${dirInfo.fileCount}</p>
</div>
<div class="files">
<ul id="fileList">
${generateFileList(files, relativeDir)}
</ul>
</div>
<a href="/all-files.html" class="all-files-link">View All Files</a>
<script>
function filterFiles() {
const searchInput = document.getElementById('search').value.toLowerCase();
const fileItems = document.querySelectorAll('.file-item');
fileItems.forEach(item => {
const fileName = item.textContent.toLowerCase();
if (fileName.includes(searchInput)) {
item.style.display = '';
} else {
item.style.display = 'none';
}
});
}
</script>
</body>
</html>
`;
}
function generateAllFilesHtml({ allFiles, metaTags, seoTags, styles }) {
const fileList = allFiles
.map(
(file) => `
<tr>
<td><a href="/${file.path}">${file.name}</a></td>
<td>${file.size}</td>
<td>${file.lastModified}</td>
</tr>
`
)
.join("");
return `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
${metaTags}
${seoTags}
<title>All Files</title>
<style>
${styles}
</style>
</head>
<body>
<h1 class="title">All Files</h1>
<input class="search" id="search" onkeyup="filterFiles()" placeholder="Search files...">
<table>
<thead>
<tr>
<th>Name</th>
<th>Size</th>
<th>Last Modified</th>
</tr>
</thead>
<tbody id="fileList">
${fileList}
</tbody>
</table>
<script>
function filterFiles() {
const searchInput = document.getElementById('search').value.toLowerCase();
const rows = document.querySelectorAll('#fileList tr');
rows.forEach(row => {
const text = row.textContent.toLowerCase();
row.style.display = text.includes(searchInput) ? '' : 'none';
});
}
</script>
</body>
</html>
`;
}
module.exports = { generateHtml, generateAllFilesHtml };