feat: Add sitemap for the docs website

This commit is contained in:
Faruk AYDIN
2022-10-18 01:42:11 +02:00
committed by Ali BARIN
parent 72599c6683
commit bc402883b3
3 changed files with 64 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
"serve": "vitepress serve pages"
},
"devDependencies": {
"sitemap": "^7.1.1",
"vitepress": "^1.0.0-alpha.21",
"vue": "^3.2.37"
},

View File

@@ -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();
},
});