-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sitemap.cjs
93 lines (79 loc) · 2.68 KB
/
generate-sitemap.cjs
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
const fs = require("fs");
const frontMatter = require("front-matter");
const BASE_URL = "https://slavbasharov.com/";
const ROUTES = "./src/routes";
const POSTS = "./src/posts";
const pages = [""];
const getRoutes = async (root) => {
const productsJs = await import("./src/routes/shop/_products.js");
const products = productsJs.default;
// console.log("products", products);
fs.readdirSync(root).forEach((file) => {
const excluded = ["index", "rss", "sitemap", "images", "[slug]"];
const fileStart = file.split(".")[0];
if (fileStart.charAt(0) !== "_" && fileStart.charAt(0) !== "+" && !excluded.includes(fileStart)) {
const directory = `${root}/${file}`;
// Add shop products
if (
fs.lstatSync(directory).isDirectory() &&
directory === ROUTES + "/" + "shop"
) {
pages.push(directory.replace(ROUTES + "/", ""));
products.forEach((product) => {
pages.push(product.url);
});
} else if (
fs.lstatSync(directory).isDirectory() &&
directory === ROUTES + "/" + "blog"
) {
pages.push(directory.replace(ROUTES + "/", ""));
products.forEach((product) => {
pages.push(product.url);
});
} else if (fs.lstatSync(directory).isDirectory()) {
pages.push(directory.replace(ROUTES + "/", ""));
getRoutes(directory);
} else {
pages.push(fileStart);
}
// Add blog posts
}
});
};
const getPosts = () => {
return Promise.all(
fs.readdirSync(POSTS).map(
(file) =>
new Promise((resolve, reject) => {
const path = `blog/${file}`;
const fileText = fs.readFileSync(POSTS + "/" + file, "utf-8");
const post = frontMatter(fileText);
if (post.attributes.published) {
pages.push(path.replace(".md", ""));
}
resolve();
})
)
);
};
async function generate() {
await getRoutes(ROUTES);
await getPosts();
const sitemap = render(pages);
createFile("./static/sitemap.xml", sitemap);
}
const render = (pages) => `<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
${pages.map((page) => `<url><loc>${BASE_URL}${page}</loc></url>`).join("\n")}
</urlset>`;
function createFile(filename, sitemap) {
fs.open(filename, "r", function (err, fd) {
fs.writeFile(filename, sitemap, function (err) {
if (err) {
console.log(err);
}
console.log("The file was saved!");
});
});
}
generate();