Skip to content

Commit

Permalink
[U] Sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
cvyl committed May 21, 2024
1 parent 461fead commit 56b8980
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from 'vitepress';
import { createContentLoader, defineConfig } from 'vitepress';
import { createWriteStream } from 'node:fs'
import { resolve } from 'node:path'
import { SitemapStream } from 'sitemap'
Expand Down Expand Up @@ -139,20 +139,19 @@ export default defineConfig({
lang: 'en'
},
},
transformHtml: (_, id, { pageData }) => {
if (!/[\\/]404\.html$/.test(id)) {
const links: { url: string; lastmod: number | undefined }[] = [];
links.push({ url: pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2'), lastmod: pageData.lastUpdated });
}
},
buildEnd: async ({ outDir }) => {
const sitemap = new SitemapStream({
hostname: 'https://hrt.info/'
})
const sitemap = new SitemapStream({ hostname: 'https://hrt.info/' })
const pages = await createContentLoader('*.md').load()
const writeStream = createWriteStream(resolve(outDir, 'sitemap.xml'))

sitemap.pipe(writeStream)
links.forEach((link) => sitemap.write(link))
pages.forEach((page) => sitemap.write(
page.url
.replace(/index.html$/g, '')
.replace(/^\/docs/, '')
))
sitemap.end()

await new Promise((r) => writeStream.on('finish', r))
},
}
});

0 comments on commit 56b8980

Please sign in to comment.