-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgenerate-sitemap.ts
93 lines (84 loc) · 2.86 KB
/
generate-sitemap.ts
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
import { SitemapStream, streamToPromise } from "sitemap";
import { Readable } from "stream";
import * as fs from "fs";
import { PAGES } from "./src/components/AppRouter/pages";
import { EXAMPLES_PAGES } from "./src/components/AppRouter/examplePages";
import { EPageFramework } from "./src/helpers/shared/Helpers/frameworkParametrization";
enum EChangeFreq {
Always = "always",
Hourly = "hourly",
Daily = "daily",
Weekly = "weekly",
Monthly = "monthly",
Yearly = "yearly",
Never = "never",
}
type TLink = {
url: string;
changefreq: EChangeFreq;
priority: number;
lastmod: string;
img?: { url: string }[];
};
const basePath = "https://demo.scichart.com";
console.log("Generating sitemap...");
(async () => {
// An array with your links
// const links = [{ url: "/page-1/", changefreq: "daily", priority: 0.3 }];
const links: TLink[] = [];
const dateNow = new Date().toISOString();
const lastmod = dateNow.substring(0, 10);
// Add pages links
Object.values(PAGES).forEach((el) => {
links.push({
url: el.path,
changefreq: EChangeFreq.Weekly,
priority: 1,
lastmod,
});
});
// Add examples links
for (const framework of ["react", "javascript", "angular"]) {
Object.values(EXAMPLES_PAGES).forEach((el) => {
if (el.thumbnailImage) {
links.push({
url: framework + "/" + el.path,
changefreq: EChangeFreq.Weekly,
priority: 0.5,
lastmod,
img: [{ url: `/images/${el.thumbnailImage}` }],
});
} else {
links.push({
url: framework + "/" + el.path,
changefreq: EChangeFreq.Weekly,
priority: 0.5,
lastmod,
});
}
});
}
// Create a stream to write to
const stream = new SitemapStream({
hostname: basePath,
lastmodDateOnly: true,
xmlns: {
// trim the xml namespace
news: false, // flip to false to omit the xml namespace for news
xhtml: false,
image: true,
video: false,
custom: [
'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"',
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
],
},
});
// Return a promise that resolves with your XML string
const data = await streamToPromise(Readable.from(links).pipe(stream));
const xmlStringResult = data.toString();
fs.writeFile("sitemap.xml", xmlStringResult, (err) => {
if (err) console.log(err);
console.log("sitemap.xml is successfully written to file.");
});
})();