From c1367c7b577a8a45cc4cd732bde5ca5f68e0d9e3 Mon Sep 17 00:00:00 2001 From: Futa IWATA Date: Mon, 21 Oct 2024 07:32:29 +0900 Subject: [PATCH 1/3] refactor out rss generation --- src/components/pages/rss.ts | 56 +++++++++++++++++++++++++++++++++++++ src/pages/notice/rss.xml.ts | 49 +++----------------------------- 2 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 src/components/pages/rss.ts diff --git a/src/components/pages/rss.ts b/src/components/pages/rss.ts new file mode 100644 index 0000000000..56ee9fefdd --- /dev/null +++ b/src/components/pages/rss.ts @@ -0,0 +1,56 @@ +import type { RSSFeedItem } from "@astrojs/rss"; +import getRssResponse from "@astrojs/rss"; +import remarkParse from "remark-parse"; +import { unified } from "unified"; + +import { noticesWithIdReversed } from "@data/utils/notices"; +import { toHast } from "mdast-util-to-hast"; +import { toText } from "hast-util-to-text"; +import { select } from "hast-util-select"; +import type { Lang } from "@components/types"; + +const parser = unified().use(remarkParse); + +interface RssParams { + title: string; + description: string; + url: URL; + lang: Lang; +} + +export async function rss({ title, description, url }: RssParams) { + const itemsMap = new Map(); + + for (const notice of noticesWithIdReversed) { + if (!notice.content.ja) continue; + + const mdast = parser.parse(notice.content.ja); + const hast = toHast(mdast); + const title = toText(hast); + const a = select("p > a:only-child[href]", hast); + const link = + a && toText(a) === title + ? (a.properties.href as string) + : `/notice/#${notice.id}`; + itemsMap.set(link, { + title, + link, + pubDate: new Date(notice.date), + }); + } + + return getRssResponse({ + title, + description, + site: url.origin, + items: Array.from(itemsMap.values()).reverse(), + trailingSlash: false, + xmlns: { + atom: "http://www.w3.org/2005/Atom", + }, + customData: [ + "ja-jp", + ``, + ].join(""), + }); +} diff --git a/src/pages/notice/rss.xml.ts b/src/pages/notice/rss.xml.ts index a8910b4ef3..0c489a3818 100644 --- a/src/pages/notice/rss.xml.ts +++ b/src/pages/notice/rss.xml.ts @@ -1,51 +1,10 @@ -import rss, { type RSSFeedItem } from "@astrojs/rss"; -import type { Notice } from "@data/schemas/notice"; -import { unified } from "unified"; -import remarkParse from "remark-parse"; -import { toHast } from "mdast-util-to-hast"; -import { toText } from "hast-util-to-text"; -import { select } from "hast-util-select"; import type { APIContext } from "astro"; // @ts-ignore import { frontmatter } from "../index.mdx"; -import { noticesWithIdReversed } from "@data/utils/notices"; +import { rss } from "@components/pages/rss"; -const parser = unified().use(remarkParse); +const { title, description } = frontmatter; -export async function GET(context: APIContext) { - const itemsMap = new Map(); - - for (const notice of noticesWithIdReversed) { - if (!notice.content.ja) continue; - - const mdast = parser.parse(notice.content.ja); - const hast = toHast(mdast); - const title = toText(hast); - const a = select("p > a:only-child[href]", hast); - const link = - a && toText(a) === title - ? (a.properties.href as string) - : `/notice/#${notice.id}`; - itemsMap.set(link, { - title, - link, - pubDate: new Date(notice.date), - }); - } - - const { title, description } = frontmatter; - return rss({ - title, - description, - site: context.url.origin, - items: Array.from(itemsMap.values()).reverse(), - trailingSlash: false, - xmlns: { - atom: "http://www.w3.org/2005/Atom", - }, - customData: [ - "ja-jp", - ``, - ].join(""), - }); +export async function GET({ url }: APIContext) { + return rss({ title, description, url, lang: "ja" }); } From 9f5e96bd673313d352f70d2d50c37962d87e5beb Mon Sep 17 00:00:00 2001 From: Futa IWATA Date: Mon, 21 Oct 2024 07:38:00 +0900 Subject: [PATCH 2/3] /en/notice/rss.xml --- src/components/pages/rss.ts | 7 ++++--- src/pages/en/index.mdx | 2 ++ src/pages/en/notice/rss.xml.ts | 10 ++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/pages/en/notice/rss.xml.ts diff --git a/src/components/pages/rss.ts b/src/components/pages/rss.ts index 56ee9fefdd..8c53a03804 100644 --- a/src/components/pages/rss.ts +++ b/src/components/pages/rss.ts @@ -18,13 +18,14 @@ interface RssParams { lang: Lang; } -export async function rss({ title, description, url }: RssParams) { +export async function rss({ title, description, url, lang }: RssParams) { const itemsMap = new Map(); for (const notice of noticesWithIdReversed) { - if (!notice.content.ja) continue; + const content = notice.content[lang] ?? notice.content.ja; + if (!content) continue; - const mdast = parser.parse(notice.content.ja); + const mdast = parser.parse(content); const hast = toHast(mdast); const title = toText(hast); const a = select("p > a:only-child[href]", hast); diff --git a/src/pages/en/index.mdx b/src/pages/en/index.mdx index f3372c86a8..73fc4799f9 100644 --- a/src/pages/en/index.mdx +++ b/src/pages/en/index.mdx @@ -1,6 +1,8 @@ --- title: The Portal Site of Information Systems @ UTokyo top: true +# temporary +description: This website aims to provide one-stop information service about information systems in the University of Tokyo. toc: false sitemap: false --- diff --git a/src/pages/en/notice/rss.xml.ts b/src/pages/en/notice/rss.xml.ts new file mode 100644 index 0000000000..915aa6a21b --- /dev/null +++ b/src/pages/en/notice/rss.xml.ts @@ -0,0 +1,10 @@ +import type { APIContext } from "astro"; +// @ts-ignore +import { frontmatter } from "../index.mdx"; +import { rss } from "@components/pages/rss"; + +const { title, description } = frontmatter; + +export async function GET({ url }: APIContext) { + return rss({ title, description, url, lang: "en" }); +} From bd4da450f939ff143d0733e3a7436e534aab8c36 Mon Sep 17 00:00:00 2001 From: Futa IWATA Date: Fri, 1 Nov 2024 14:10:43 +0900 Subject: [PATCH 3/3] fix lang --- src/components/pages/rss.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/pages/rss.ts b/src/components/pages/rss.ts index 8c53a03804..f862629925 100644 --- a/src/components/pages/rss.ts +++ b/src/components/pages/rss.ts @@ -50,7 +50,7 @@ export async function rss({ title, description, url, lang }: RssParams) { atom: "http://www.w3.org/2005/Atom", }, customData: [ - "ja-jp", + `${{ ja: "ja-jp", en: "en-us" }[lang]}`, ``, ].join(""), });