Skip to content
鼓励作者:欢迎打赏犒劳

08.VitePress配置sitemap

执行build命令,dist文件夹下会生成sitemap.xml文件

依赖

shell
yarn add sitemap

配置

docs/.vitepress/config.js

js
import { createWriteStream } from 'node:fs'
import { resolve } from 'node:path'
import { SitemapStream } from 'sitemap'

const links = []

export default {
    title: '敲代码的卡卡罗特',
    description: '灵魂和身体都要在路上',
    base:'/',
    lang: 'zh-CN',
    lastUpdated: true,
    themeConfig: {
        lastUpdatedText: "最后更新",
    },
    /* 站点地图 */
    transformHtml: (_, id, { pageData }) => {
        if (!/[\\/]404\.html$/.test(id))
            links.push({
                url: pageData.relativePath.replace(/(^|\/)?\.md$/, '$1.html'),
                lastmod: pageData.lastUpdated
        })
        console.log(links)
    },
    buildEnd: async (siteConfig) => {
        const sitemap = new SitemapStream({ hostname: 'https://blog.share888.top' })
        const writeStream = createWriteStream(resolve(siteConfig.outDir, 'sitemap.xml'))
        sitemap.pipe(writeStream)
        links.forEach((link) => sitemap.write(link))
        sitemap.end()
        await new Promise((r) => writeStream.on('finish', r))

    }
}

如有转载或 CV 的请标注本站原文地址