Skip to content

Commit

Permalink
Merge pull request #1235 from utelecon/202410-en-rss
Browse files Browse the repository at this point in the history
202410 en rss
  • Loading branch information
jtamatsukuri authored Nov 1, 2024
2 parents 189e18c + bd4da45 commit f6a80fc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 45 deletions.
57 changes: 57 additions & 0 deletions src/components/pages/rss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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, lang }: RssParams) {
const itemsMap = new Map<string, RSSFeedItem>();

for (const notice of noticesWithIdReversed) {
const content = notice.content[lang] ?? notice.content.ja;
if (!content) continue;

const mdast = parser.parse(content);
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: [
`<language>${{ ja: "ja-jp", en: "en-us" }[lang]}</language>`,
`<atom:link href="${url}" rel="self" type="application/rss+xml" />`,
].join(""),
});
}
2 changes: 2 additions & 0 deletions src/pages/en/index.mdx
Original file line number Diff line number Diff line change
@@ -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
---
Expand Down
10 changes: 10 additions & 0 deletions src/pages/en/notice/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -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" });
}
49 changes: 4 additions & 45 deletions src/pages/notice/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -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<string, RSSFeedItem>();

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: [
"<language>ja-jp</language>",
`<atom:link href="${context.url}" rel="self" type="application/rss+xml" />`,
].join(""),
});
export async function GET({ url }: APIContext) {
return rss({ title, description, url, lang: "ja" });
}

0 comments on commit f6a80fc

Please sign in to comment.