-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateSitemapXml.js
54 lines (42 loc) · 1.59 KB
/
generateSitemapXml.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
const fs = require('fs')
const path = require('path')
const util = require('util')
const { SitemapStream, streamToPromise } = require('sitemap')
const languages = require('../utils/languages')
const readdir = util.promisify(fs.readdir)
const postsDirectory = path.resolve(__dirname, '../_posts')
const generateSitemap = async () => {
const postsLinks = await Promise.all(
languages.map((language) =>
readdir(path.join(postsDirectory, language)).then((fileNames) => {
return fileNames
.filter((name) => !(process.env.IS_PRODUCTION && name === 'example.md'))
.map((postName) => ({
url: `https://blog.csssr.com/${language}/article/${postName.replace('.md', '')}/`,
changefreq: 'weekly',
priority: 0.8,
}))
}),
),
)
const newsLinks = await readdir(path.join(postsDirectory, 'news512')).then((fileNames) => {
return fileNames.map((postName) => ({
url: `https://blog.csssr.com/ru/news512/episode/${postName.replace('.md', '')}/`,
changefreq: 'weekly',
priority: 0.8,
}))
})
const links = [
{ url: 'https://blog.csssr.com/en/', changefreq: 'weekly', priority: 1 },
{ url: 'https://blog.csssr.com/ru/', changefreq: 'weekly', priority: 1 },
...postsLinks.flat(),
...newsLinks.flat(),
]
const stream = new SitemapStream({ hostname: process.env.BLOG_HOST })
links.forEach((link) => stream.write(link))
stream.end()
void streamToPromise(stream).then((data) =>
fs.writeFileSync(path.resolve(__dirname, '../out/sitemap.xml'), data.toString()),
)
}
generateSitemap()