Browse Source

#81 RSS 追加

#59
みてるぞ 2 weeks ago
parent
commit
2738d59d72
1 changed files with 43 additions and 12 deletions
  1. +43
    -12
      frontend/scripts/generate-sitemap.js

+ 43
- 12
frontend/scripts/generate-sitemap.js View File

@@ -10,32 +10,63 @@ const formatDate = (date = new Date) => {
return `${ year }-${ month }-${ day }`
}

const SITE_TITLE = 'ぼざクリ タグ広場'
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 fetchPosts = async () => (await axios.get (`${ API_BASE_URL }/posts`)).data.posts
const fetchPostIds = async () => (await fetchPosts ()).map (post => post.id)

const fetchTagNames =
async () => (await axios.get (`${ API_BASE_URL }/tags`)).data.map (tag => tag.name)
const fetchTags = async () => (await axios.get (`${ API_BASE_URL }/tags`)).data
const fetchTagNames = async () => (await fetchTags ()).map (tag => tag.name)

const fetchWikiTitles =
async () => (await axios.get (`${ API_BASE_URL }/wiki`)).data.map (page => page.title)
const fetchWikiPages = async () => (await axios.get (`${ API_BASE_URL }/wiki`)).data
const fetchWikiTitles = async () => (await fetchWikiPages ()).map (page => page.title)

const posts = (await fetchPosts ()).map (post => [
`/posts/${ post.id }`,
`${ post.title || post.url } | ${ SITE_TITLE }` ])

const tags = (await fetchTags ()).map (tag => [
`/posts?${ new URLSearchParams ({ tags: tag.name }) }`,
`${ tag.name } | ${ SITE_TITLE }`])

const wikiPages = (await fetchWikiPages ()).map (page => [
`/wiki/${ encodeURIComponent (page.title) }`,
`${ page.title } Wiki | ${ SITE_TITLE }`])

const routes = [
'/',
...(await fetchTagNames ()).map (tags => `/posts?${ (new URLSearchParams ({ tags })).toString () }`),
...(await fetchPostIds ()).map (id => `/posts/${ id }`),
'/wiki',
...(await fetchWikiTitles ()).map (title => `/wiki/${ encodeURIComponent (title) }`)]
['/', `${ SITE_TITLE } 〜 ぼざろクリーチャーシリーズ綜合リンク集サイト`],
...tags,
...posts,
['/wiki', `Wiki | ${ SITE_TITLE }`],
...wikiPages]

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${ routes.map (route => ` <url>
${ routes.map (([route]) => ` <url>
<loc>${ DOMAIN }${ route }</loc>
<lastmod>${ formatDate () }</lastmod>
<changefreq>daily</changefreq>
</url>`).join ('\n') }
</urlset>`

const rss = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>The Series of Bocchi the Rock! Creatures Hub Feed</title>
<link>${ DOMAIN }/</link>
<description>最新の投稿、タグ、Wiki ページの一覧</description>
<language>ja</language>
<pubDate>${ (new Date).toUTCString () }</pubDate>
${ routes.map (([route, title]) => ` <item>
<title>${ title }</title>
<link>${ DOMAIN }${ route }</link>
<guid>${ DOMAIN }${ route }</guid>
<pubDate>${ (new Date).toUTCString () }</pubDate>
</item>`).join ('\n') }
</channel>
</rss>`

fs.writeFileSync (path.resolve ('dist/sitemap.xml'), xml)
fs.writeFileSync (path.resolve ('dist/rss.xml'), rss)

Loading…
Cancel
Save