From d890f76f87922ee8089afaa7f3c229728fef2108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javis=20V=2E=20P=C3=A9rez?= Date: Wed, 8 May 2024 08:33:09 -0400 Subject: [PATCH] Generate a dynamic sitemap.xml on build --- docs/.vitepress/config.mts | 25 ++++++++++++++++++++++++- docs/package.json | 1 + docs/pnpm-lock.yaml | 29 ++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 7b149f0b..2110ef91 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -1,8 +1,12 @@ import { URL, fileURLToPath } from 'node:url' +import { createWriteStream } from 'node:fs' +import { resolve } from 'path' import { defineConfig } from 'vitepress' import { getSidebarItemsFromMdFiles } from './utils.mts' -import { resolve } from 'path' +import { SitemapStream } from 'sitemap' + +const links = [] // https://vitepress.dev/reference/site-config export default defineConfig({ @@ -123,6 +127,25 @@ export default defineConfig({ ]) }, + // Generate the sitemap.xml + transformHtml: (_, id, { pageData }) => { + if (!/[\\/]404\.html$/.test(id)) { + links.push({ + url: pageData.relativePath.replace(/\/index\.md$/, '/').replace(/\.md$/, '.html'), + lastmod: pageData.lastUpdated, + }) + } + }, + + buildEnd: async ({ outDir }) => { + const sitemap = new SitemapStream({ hostname: 'https://kitops.ml/' }) + const writeStream = createWriteStream(resolve(outDir, 'sitemap.xml')) + sitemap.pipe(writeStream) + links.forEach((link) => sitemap.write(link)) + sitemap.end() + await new Promise((r) => writeStream.on('finish', r)) + }, + vite: { resolve: { alias: [ diff --git a/docs/package.json b/docs/package.json index 9f928043..de22aa96 100644 --- a/docs/package.json +++ b/docs/package.json @@ -17,6 +17,7 @@ "dependencies": { "@vueuse/core": "^10.7.2", "cheerio": "1.0.0-rc.12", + "sitemap": "^7.1.1", "vue3-marquee": "^4.2.0" } } \ No newline at end of file diff --git a/docs/pnpm-lock.yaml b/docs/pnpm-lock.yaml index 1e1ab86d..143a0b54 100644 --- a/docs/pnpm-lock.yaml +++ b/docs/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: cheerio: specifier: 1.0.0-rc.12 version: 1.0.0-rc.12 + sitemap: + specifier: ^7.1.1 + version: 7.1.1 vue3-marquee: specifier: ^4.2.0 version: 4.2.0(vue@3.4.18) @@ -664,6 +667,16 @@ packages: resolution: {integrity: sha512-6L6VymKTzYSrEf4Nev4Xa1LCHKrlTlYCBMTlQKFuddo1CvQcE52I0mwfOJayueUC7MJuXOeHTcIU683lzd0cUA==} dev: true + /@types/node@17.0.45: + resolution: {integrity: sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==} + dev: false + + /@types/sax@1.2.7: + resolution: {integrity: sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==} + dependencies: + '@types/node': 17.0.45 + dev: false + /@types/web-bluetooth@0.0.20: resolution: {integrity: sha512-g9gZnnXVq7gM7v3tJCWV/qw7w+KeOlSHAhgF9RytFyifW6AF61hdT2ucrYhPq9hLs5JIryeupHV3qGk95dH9ow==} @@ -896,7 +909,6 @@ packages: /arg@5.0.2: resolution: {integrity: sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==} - dev: true /autoprefixer@10.4.17(postcss@8.4.35): resolution: {integrity: sha512-/cpVNRLSfhOtcGflT13P2794gVSgmPgTR+erw5ifnMLZb0UnSlkK4tquLmkd3BhA+nLo5tX8Cu0upUsGKvKbmg==} @@ -1600,6 +1612,10 @@ packages: queue-microtask: 1.2.3 dev: true + /sax@1.3.0: + resolution: {integrity: sha512-0s+oAmw9zLl1V1cS9BtZN7JAd0cW5e0QH4W3LWEK6a4LaLEA2OTpGYWDY+6XasBLtz6wkm3u1xRw95mRuJ59WA==} + dev: false + /search-insights@2.13.0: resolution: {integrity: sha512-Orrsjf9trHHxFRuo9/rzm0KIWmgzE8RMlZMzuhZOJ01Rnz3D0YBAe+V6473t6/H6c7irs6Lt48brULAiRWb3Vw==} dev: true @@ -1627,6 +1643,17 @@ packages: engines: {node: '>=14'} dev: true + /sitemap@7.1.1: + resolution: {integrity: sha512-mK3aFtjz4VdJN0igpIJrinf3EO8U8mxOPsTBzSsy06UtjZQJ3YY3o3Xa7zSc5nMqcMrRwlChHZ18Kxg0caiPBg==} + engines: {node: '>=12.0.0', npm: '>=5.6.0'} + hasBin: true + dependencies: + '@types/node': 17.0.45 + '@types/sax': 1.2.7 + arg: 5.0.2 + sax: 1.3.0 + dev: false + /source-map-js@1.0.2: resolution: {integrity: sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==} engines: {node: '>=0.10.0'}