-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathwds-utils.js
More file actions
81 lines (68 loc) · 1.9 KB
/
wds-utils.js
File metadata and controls
81 lines (68 loc) · 1.9 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
import fs from 'node:fs';
export function appendStyles(html) {
const preventFouc = `
<style>
body:not(.resolved) {
opacity: 0;
}
body {
transition: opacity 0.2s;
}
</style>
<script type="module">
// It's important to use type module for the script so the timing is correct
document.body.classList.add('resolved');
</script>
`;
return html.replace(/<\/body>/u, `${preventFouc}\n</body>`);
}
export function isIndexPage(html) {
return html.includes('<!-- LISTING -->');
}
function isSubPage(dir, file) {
const filePath = `${dir}/${file}`;
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
const indexPath = `${filePath}/index.html`;
return fs.existsSync(indexPath);
}
return false;
}
export function generateListing(html, dir) {
if (isIndexPage(html)) {
const files = fs.readdirSync(dir || '.').filter((file) => file !== 'index.html' && file !== 'dist');
// Separate sub pages (folders with index.html) from HTML files
const subPages = files.filter((file) => isSubPage(dir, file));
const htmlFiles = files.filter((file) => file.endsWith('.html'));
let listing = '';
// Add sub pages section if there are any
if (subPages.length > 0) {
listing += `
<h2>Sub pages</h2>
<ul>
${subPages
.map(
(file) => `<li>
<a href="${file}/">${file}/</a>
</li>`,
)
.join('')}
</ul>`;
}
// Add HTML files section if there are any
if (htmlFiles.length > 0) {
listing += `
<h2>Pages</h2>
<ul>
${htmlFiles
.map(
(file) => `<li>
<a href="${file}">${file}</a>
</li>`,
)
.join('')}
</ul>`;
}
return html.replace(/<!-- LISTING -->/u, listing);
}
return html;
}