-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.ts
71 lines (63 loc) · 2.06 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { SitemapStream, streamToPromise } from 'sitemap'
import { Readable } from 'stream'
import fg from 'fast-glob'
import matter from 'gray-matter'
import { dirname } from 'path'
import fs from 'fs'
import { RouteTitleRecord } from './src/site.ts'
import chalk from 'chalk'
const links = []
const pages = fg
.sync(`./content/**/index.md`)
.map((entry) => {
return { entry, frontmatter: matter.read(entry) }
})
.map((file) => {
const { entry, frontmatter } = file
if (frontmatter.data.hidden) return undefined
const dir = dirname(entry).split('/')
return {
url: frontmatter.data.slug
? `/${dir.slice(2, -1).join('/')}/${frontmatter.data.slug}/`
: `/${dir.slice(2).join('/')}/`,
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())
const pagesVue = fg.sync(`./content/**/index.vue`).map((file) => {
const dir = dirname(file).split('/')
return {
url: `/${dir.slice(2).join('/')}/`,
lastmod: new Date(),
changefreq: 'monthly',
priority: 0.5,
}
})
links.push(...pages, ...pagesVue)
const categories: { [key: string]: Date } = {}
for (const category of Object.keys(RouteTitleRecord)) {
categories[category] = fg
.sync(`./content/${category}/**/index.md`)
.map((entry) => {
return matter.read(entry).data.time || new Date()
})
.filter((page) => page !== undefined)
.sort((a, b) => Date.parse(b) - Date.parse(a))[0]
}
for (const category of Object.keys(RouteTitleRecord)) {
links.push({
url: `/${category}/`,
changefreq: 'daily',
priority: 0.8,
lastmod: categories[category].toISOString(),
})
}
console.log(chalk.bgYellow.greenBright('Sitemap:'))
console.log(links.map((x) => x.url).join('\n'))
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)