-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1235 from utelecon/202410-en-rss
202410 en rss
- Loading branch information
Showing
4 changed files
with
73 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} |