-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.ts
50 lines (46 loc) · 1.51 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
40
41
42
43
44
45
46
47
48
49
50
import { SitemapStream, streamToPromise } from 'sitemap'
import { Readable } from 'stream'
import fg from 'fast-glob'
import matter from 'gray-matter'
import path from 'path'
import fs from 'fs'
const categories = ['news', 'announcements']
const links = []
for (const category of categories) {
const pages = fg
.sync(`./content/${category}/*.md`)
.map((entry) => {
return { entry, frontmatter: matter.read(entry) }
})
.map((file) => {
const { entry, frontmatter } = file
if (frontmatter.data.hidden) return undefined
const filename = path.parse(entry).name
return {
url: `/${category}/${filename}/`,
lastmod: frontmatter.data.time,
changefreq: 'monthly',
priority: 0.5,
}
})
.filter((page) => page !== undefined)
.sort((a, b) => new Date(b.lastmod).getTime() - new Date(a.lastmod).getTime())
links.push(...pages)
links.push({
url: `/${category}/`,
changefreq: 'daily',
priority: 0.8,
lastmod: pages.length > 0 ? new Date(pages[0].lastmod).toISOString() : new Date().toISOString(),
})
}
links.push({ url: '/', changefreq: 'daily', priority: 0.8, lastmod: new Date().toISOString() })
links.push({
url: '/about/',
changefreq: 'monthly',
priority: 0.5,
lastmod: new Date().toISOString(),
})
const stream = new SitemapStream({ hostname: 'https://lcpu.dev' })
const buffer = await streamToPromise(Readable.from(links).pipe(stream))
const sitemap = buffer.toString()
fs.writeFileSync('./dist/static/sitemap.xml', sitemap)