diff --git a/frontend/package.json b/frontend/package.json index 02d1c79..1945bd2 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -6,6 +6,7 @@ "scripts": { "dev": "vite", "build": "tsc -b && vite build", + "postbuild": "ts-node scripts/generate-sitemap.ts", "lint": "eslint .", "preview": "vite preview" }, diff --git a/frontend/scripts/generate-sitemap.ts b/frontend/scripts/generate-sitemap.ts new file mode 100644 index 0000000..8397f68 --- /dev/null +++ b/frontend/scripts/generate-sitemap.ts @@ -0,0 +1,32 @@ +import axios from 'axios' +import fs from 'fs' +import path from 'path' + +const DOMAIN = 'https://hub.nizika.monster' +const API_BASE_URL = 'https://hub.nizika.monster/api' + +const fetchPostIds = + async () => (await axios.get (`${ API_BASE_URL }/posts`)).data.posts.map (post => post.id) + +const fetchTagNames = + async () => (await axios.get (`${ API_BASE_URL }/tags`)).data.map (tag => tag.name) + +const fetchWikiTitles = + async () => (await axios.get (`${ API_BASE_URL }/wiki`)).data.map (page => page.title) + +const routes = [ + '/', + ...(await fetchTagNames ()).map (tags => `/posts?${ (new URLSearchParams ({ tags })).toString () }`), + ...(await fetchPostIds ()).map (id => `/posts/${ id }`), + '/tags', + '/tags/nico', + '/wiki', + '/wiki/changes', + ...(await fetchWikiTitles ()).map (title => `/wiki/${ encodeURIComponent (title) }`), + '/users', + '/users/settings'] + +const xml = ` + +${ routes.map (route => ` ${ DOMAIN }${ route }`).join ('\n') } +`