diff --git a/lib/routes/tailwindcss/blog.ts b/lib/routes/tailwindcss/blog.ts new file mode 100644 index 000000000..afcb8b411 --- /dev/null +++ b/lib/routes/tailwindcss/blog.ts @@ -0,0 +1,26 @@ +import { Route } from '@/types'; +import { Context } from 'hono'; +import { fetchFeed } from './utils'; + +export const handler = (ctx: Context) => { + const limit = Number.parseInt(ctx.req.query('limit') || '10'); + + return fetchFeed(limit); +}; + +export const route: Route = { + path: '/blog', + categories: ['programming'], + example: '/tailwindcss/blog', + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + name: 'Blog', + maintainers: ['goestav'], + handler, +}; diff --git a/lib/routes/tailwindcss/constants.ts b/lib/routes/tailwindcss/constants.ts new file mode 100644 index 000000000..9a916a1c1 --- /dev/null +++ b/lib/routes/tailwindcss/constants.ts @@ -0,0 +1,2 @@ +/** The base URL of this route. */ +export const BASE_URL = new URL('https://tailwindcss.com/'); diff --git a/lib/routes/tailwindcss/namespace.ts b/lib/routes/tailwindcss/namespace.ts new file mode 100644 index 000000000..7deefd33b --- /dev/null +++ b/lib/routes/tailwindcss/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'TailwindCSS', + url: 'tailwindcss.com', + lang: 'en', +}; diff --git a/lib/routes/tailwindcss/utils.ts b/lib/routes/tailwindcss/utils.ts new file mode 100644 index 000000000..b0e1ec0df --- /dev/null +++ b/lib/routes/tailwindcss/utils.ts @@ -0,0 +1,68 @@ +import ofetch from '@/utils/ofetch'; +import { Data, DataItem } from '@/types'; +import { BASE_URL } from './constants'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; + +/** Fetch the content of a given article. */ +export const fetchArticleContent = async (url: string): Promise => { + const data = await ofetch(url, { responseType: 'text' }); + const $ = load(data); + + return $('article.prose').html() ?? ''; +}; + +/** Fetch the feed object. */ +export const fetchFeed = async (limit: number): Promise => { + const url = new URL('/feeds/atom.xml', BASE_URL).href; + + const data = await ofetch(url, { responseType: 'text' }); + + const $ = load(data, { xml: true }); + + const items = await Promise.all( + $('entry') + .toArray() + .slice(0, limit) + .map((entry) => { + const title = $(entry).find('title').text(); + const id = $(entry).find('id').text(); + const url = $(entry).find('link[href]').attr('href'); + const imageUrl = $(entry).find('link[rel="enclosure"]').attr('href'); + + if (url === undefined) { + throw new Error(`No article URL found for article with id ${id}`); + } + + return cache.tryGet( + `tailwindcss:${id}`, + async () => + ({ + title, + link: url, + image: imageUrl, + description: await fetchArticleContent(url), + author: $(entry) + .find('author') + .toArray() + .map((el) => ({ + name: $(el).find('name').text(), + url: $(el).find('url').text(), + })), + pubDate: $(entry).find('updated').text(), + guid: $(entry).find('id').text(), + }) as DataItem + ); + }) + ); + + return { + title: $('feed > title').text(), + item: items, + author: $('feed > author > name').text(), + logo: $('feed > logo').text(), + icon: $('feed > icon').text(), + description: $('feed > subtitle').text(), + link: $('feed > link[rel="alternate"]').attr('href'), + } as Data; +};