Skip to content

Commit 7a0ae82

Browse files
authored
chore: add sitemap.xml generating script (#197)
1 parent 4a264a7 commit 7a0ae82

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

packages/website/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"scripts": {
66
"clear:cache": "rimraf ../../parcel-cache",
77
"dev": "yarn clear:cache && yarn docs && parcel ./src/index.html -p 3000 --open",
8-
"build": "yarn docs && parcel build ./src/index.html --dist-dir out",
8+
"build": "yarn docs && parcel build ./src/index.html --dist-dir out && yarn sitemap",
99
"lint": "eslint src",
1010
"docs": "node ./scripts/docs",
11-
"deploy": "yarn build && firebase deploy"
11+
"deploy": "yarn build && firebase deploy",
12+
"sitemap": "node ./scripts/sitemap"
1213
},
1314
"dependencies": {
1415
"@html-eslint/eslint-plugin": "^0.25.0",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const path = require("path");
2+
const fs = require("fs");
3+
4+
/**
5+
* @param {string} current
6+
* @param {(relativePath: string) => void} callback
7+
*/
8+
function traverseHTMLs(current, callback) {
9+
if (fs.lstatSync(current).isDirectory()) {
10+
fs.readdirSync(current).forEach((next) => {
11+
traverseHTMLs(path.join(current, next), callback);
12+
});
13+
return;
14+
} else if (fs.lstatSync(current).isFile()) {
15+
const parsed = path.parse(current);
16+
if (parsed.ext === ".html") {
17+
callback(current);
18+
}
19+
}
20+
}
21+
22+
/**
23+
* @param {string} relativePath
24+
* @param {string} baseUrl
25+
* @returns {[string, priority]}
26+
*/
27+
function toURL(relativePath, baseUrl) {
28+
const parsed = path.parse(relativePath);
29+
const name = parsed.name === "index" ? "" : parsed.name;
30+
const url = baseUrl + path.join(parsed.dir, name);
31+
return [url, name === "" ? 1 : 0.7];
32+
}
33+
34+
/**
35+
* @param {string} src
36+
* @param {string} dest
37+
* @param {string} baseUrl
38+
*/
39+
function generateSitemap(src, dest, baseUrl) {
40+
/**
41+
* @type {string[]}
42+
*/
43+
const urls = [];
44+
45+
traverseHTMLs(src, (path) => {
46+
urls.push(toURL(path.replace(src, ""), baseUrl));
47+
});
48+
const date = new Date().toISOString();
49+
50+
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
51+
<urlset
52+
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
53+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54+
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
55+
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
56+
${urls
57+
.map(([url, priority]) => {
58+
return `<url>
59+
<loc>${url}</loc>
60+
<lastmod>${date}</lastmod>
61+
<priority>${priority}</priority>
62+
</url>`;
63+
})
64+
.join("\n")}
65+
</urlset>`;
66+
67+
fs.writeFileSync(dest, sitemap, "utf-8");
68+
}
69+
70+
module.exports = generateSitemap;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require("path");
2+
const generateSitemap = require("./generates/sitemap");
3+
4+
generateSitemap(
5+
path.join(__dirname, "../out"),
6+
path.join(__dirname, "../out/sitemap.xml"),
7+
"https://html-eslint.org"
8+
);

0 commit comments

Comments
 (0)