forked from oedotme/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.ts
39 lines (30 loc) · 1.29 KB
/
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
import fs from 'fs/promises'
import prettier from 'prettier'
import { constants } from './src/config'
const getFiles = (path: string) => fs.readdir(process.cwd() + path)
const generateSitemap = async () => {
const tsx = /^[a-z].+\.tsx$/
const pages = (await getFiles('/src/pages')).map((page) => (tsx.test(page) ? `/${page.replace('.tsx', '')}` : ''))
const posts = (await getFiles('/content/posts')).map((post) => `/blog/${post.replace('.mdx', '')}`)
const urls = [...pages, ...posts].filter(Boolean)
const sitemap = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls
.map(
(url) => `
<url>
<loc>${constants.profile.url + (url === '/index' ? '' : url)}</loc>
</url>
`
)
.join('')}
</urlset>
`
const config = await prettier.resolveConfig('./prettier.config.js')
const formatted = prettier.format(sitemap, { ...config, parser: 'html' })
await fs.writeFile(process.cwd() + '/public/sitemap.xml', formatted).catch(() => process.exit(1))
const { cyan, green, reset } = { cyan: '\x1b[36m', green: '\x1b[32m', reset: '\x1b[0m' }
console.log(`${cyan}info${reset} - Updated ${green}public/sitemap.xml${reset}\n`)
}
generateSitemap()