-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakelinks.js
81 lines (64 loc) · 2.22 KB
/
makelinks.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
const fs = require('fs');
const filePath = '/srv/www/htdocs/blog/index.html';
const blogs = require("/srv/www/htdocs/blog/blogs.js")
function escapeQuotes(value) {
return value.replace(/["'&<>]/g, function (char) {
switch (char) {
case '"':
return """;
case "'":
return "'";
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
default:
return char;
}
});
}
console.log(blogs)
let linksText = "<a href='blogs/all.html'>ALL POSTS</a>"
let dateObject
let month
let year
let oldMonth = "blibidy blob"
let oldYear = "bloopy aw"
for (let page = blogs.length - 1; page >= 0; page--) {
dateObject = new Date(blogs[page][2])
month = dateObject.toLocaleString('en-US', {month: 'long'});
year = dateObject.getFullYear()
const dayOfWeek = dateObject.toLocaleString('en-US', { weekday: 'long' }); // 'long' returns the full day name
const dayOfMonth = dateObject.getDate();
console.log(month, year, dayOfMonth, dayOfWeek)
if (month != oldMonth) {
linksText += (`<h4>${month} ${year}</h4>`)
}
linksText += (`<li class="indent">• <a href='blogs/${page}.html'>${escapeQuotes(blogs[page][0])}</a> -- ${dayOfWeek} ${dayOfMonth}</li>`)
oldYear = year
oldMonth = month
}
// Step 2: Read the HTML file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Step 3: Identify the target div
const targetDivId = 'links';
// Step 4: Create the new content (e.g., a paragraph element)
const newContent = linksText
// Step 5: Replace the existing content in the target div
const regex = new RegExp(`<div id="${targetDivId}">([\\s\\S]*?)<\/div>`);
const updatedData = data.replace(regex, `<div id="${targetDivId}">${newContent}</div>`);
// Step 6: Save the updated content back to the file
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
console.log('Content replaced successfully in ', filePath);
});
});