/
generate_site_map.js
59 lines (41 loc) · 1.31 KB
/
generate_site_map.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
/*
GOAL OF THIS FILE
- Build a site map for all the files on the blog.
*/
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
retreiveFileNamesFromFolder = function(targetFolder, callback){
const directoryPath = path.join(__dirname, targetFolder);
let arrayOfFiles = []
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
callback(err)
}
callback(null, files)
});
}
convertFileExtensionToHTML = function(fileName){
HMTLString = fileName.toString().replace(/\..*/gim, ".html")
return 'https://tymerry.com/' + HMTLString
}
saveSitemap = function(arrayOfHTMLFiles){
const directoryPath = path.join(__dirname, "./markdown/.vuepress/public/sitemap.txt");
fs.writeFile(directoryPath, arrayOfHTMLFiles.reduce((accumulator, currentValue)=>{
return accumulator + "\n" + currentValue
}), function(err) {
if(err) {
return console.log(err);
}
console.log("The sitemap was saved!");
});
/* if succsess return true, else return false */
}
retreiveFileNamesFromFolder("./markdown/", function (err, content) {
saveSitemap(content.filter((file)=>{
return file.charAt(0) != "."
}).map((file)=>{
return convertFileExtensionToHTML(file)
}))
})