Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

202410 en rss #1235

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-jp</language>",
phenylshima marked this conversation as resolved.
Show resolved Hide resolved
`<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" });
}