-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sitemap.js
51 lines (42 loc) · 1.39 KB
/
generate-sitemap.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
import { writeFileSync, readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname } from "path";
// Since __dirname is not available in ES Modules, we need to derive it
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Read and parse the JSON data
const rawData = readFileSync(`${__dirname}/src/blog-data.json`, "utf8");
const blogPostsJson = JSON.parse(rawData);
function generateSitemap() {
const baseUrl = "https://osamakawish.com";
let sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
// Add static routes
const staticRoutes = [
{ path: "/", freq: "monthly", priority: "1.0" },
{ path: "/blog", freq: "weekly", priority: "0.9" },
{ path: "/about", freq: "yearly", priority: "0.7" },
];
for (let route of staticRoutes) {
sitemap += `
<url>
<loc>${baseUrl}${route.path}</loc>
<changefreq>${route.freq}</changefreq>
<priority>${route.priority}</priority>
</url>`;
}
// Add blog posts
for (let postId in blogPostsJson) {
sitemap += `
<url>
<loc>${baseUrl}/blog/post/${postId}</loc>
<changefreq>yearly</changefreq>
<priority>0.8</priority>
</url>`;
}
sitemap += "\n</urlset>";
// Write to sitemap.xml
writeFileSync(`${__dirname}/public/sitemap.xml`, sitemap);
console.log("Sitemap generated!");
}
generateSitemap();