-
Notifications
You must be signed in to change notification settings - Fork 4
/
sitemap.js
30 lines (25 loc) · 934 Bytes
/
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
import fs from 'fs'
const host = 'https://igcsekit.vercel.app/'
const XML = '<?xml version="1.0" encoding="UTF-8"?>'
function indexFolder(dir) {
const urls = []
const folders = fs.readdirSync(dir).filter(file => !file.includes('.'))
for (const folder of folders) {
const path = dir + '/' + folder
const items = indexFolder(path)
urls.push(...items)
}
return[dir, ...urls]
}
function createMap(dir) {
const urls = indexFolder(dir)
let urlset = ''
for (const url of urls) {
const relative = url.replace(dir + '/', '')
const date = new Date()
urlset += `\n\t<url>\n\t\t<loc>${host + relative}</loc>\n\t\t<lastmod>${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}</lastmod>\n\t</url>`
}
const sitemap = XML + `\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urlset}\n</urlset>`
return sitemap
}
fs.writeFileSync('static/sitemap.xml', createMap('src/routes'))