feat: Add sitemap for the docs website
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
"serve": "vitepress serve pages"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sitemap": "^7.1.1",
|
||||
"vitepress": "^1.0.0-alpha.21",
|
||||
"vue": "^3.2.37"
|
||||
},
|
||||
|
@@ -1,7 +1,13 @@
|
||||
import { defineConfig } from 'vitepress';
|
||||
import { createWriteStream } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import { SitemapStream } from 'sitemap';
|
||||
|
||||
const BASE = process.env.BASE_URL || '/';
|
||||
|
||||
const links = [];
|
||||
const PROD_BASE_URL = 'https://automatisch.io/docs';
|
||||
|
||||
export default defineConfig({
|
||||
base: BASE,
|
||||
lang: 'en-US',
|
||||
@@ -102,7 +108,7 @@ export default defineConfig({
|
||||
if (ctx.pageData.relativePath === '') return; // Skip 404 page.
|
||||
|
||||
const isHomepage = ctx.pageData.relativePath === 'index.md';
|
||||
let canonicalUrl = 'https://automatisch.io/docs';
|
||||
let canonicalUrl = PROD_BASE_URL;
|
||||
|
||||
if (!isHomepage) {
|
||||
canonicalUrl =
|
||||
@@ -125,4 +131,32 @@ export default defineConfig({
|
||||
],
|
||||
];
|
||||
},
|
||||
|
||||
async transformHtml(_, id, { pageData }) {
|
||||
if (!/[\\/]404\.html$/.test(id)) {
|
||||
let url = pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2');
|
||||
|
||||
const isHomepage = url === '';
|
||||
|
||||
if (isHomepage) {
|
||||
url = '/docs';
|
||||
}
|
||||
|
||||
links.push({
|
||||
url,
|
||||
lastmod: pageData.lastUpdated,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async buildEnd({ outDir }) {
|
||||
const sitemap = new SitemapStream({
|
||||
hostname: `${PROD_BASE_URL}/`,
|
||||
});
|
||||
|
||||
const writeStream = createWriteStream(resolve(outDir, 'sitemap.xml'));
|
||||
sitemap.pipe(writeStream);
|
||||
links.forEach((link) => sitemap.write(link));
|
||||
sitemap.end();
|
||||
},
|
||||
});
|
||||
|
Reference in New Issue
Block a user