You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vitepress/docs/zh/guide/sitemap-generation.md

1.6 KiB

Sitemap 生成器

VitePress 提供开箱即用的为你的网站生成 sitemap.xml 文件。要启用它,请将以下内容添加到 .vitepress/config.js 中:

import { defineConfig } from 'vitepress'

export default defineConfig({
  sitemap: {
    hostname: 'https://example.com'
  }
})

要在 sitemap.xml 中有 <lastmod> 标签,你可以启用 lastUpdated 选项。

选项

站点地图由 sitemap 模块提供支持。你可以将其支持的任何选项传递给配置文件中的 sitemap 选项。这些将直接传递给 SitemapStream 构造函数。有关更多详细信息,请参阅 sitemap 文档。例如:

import { defineConfig } from 'vitepress'

export default defineConfig({
  sitemap: {
    hostname: 'https://example.com',
    lastmodDateOnly: false
  }
})

transformItems Hook

在将站点地图项写入 sitemap.xml 文件之前,你可以使用 sitemap.transformItems 钩子来修改站点地图项。使用站点地图项数组调用此挂钩,并期望返回站点地图项数组。例子:

import { defineConfig } from 'vitepress'

export default defineConfig({
  sitemap: {
    hostname: 'https://example.com',
    transformItems: (items) => {
      // 添加新项目或修改/过滤现有项目
      items.push({
        url: '/extra-page',
        changefreq: 'monthly',
        priority: 0.8
      })
      return items
    }
  }
})