feat(route/tailwindcss): add tailwindcss blog route (#19148)

* feat(route/tailwindcss): add tailwindcss blog route

* refactor(route/tailwindcss): remove blog category

* refactor(route/tailwindcss): remove `.then` promise chain

* refactor(route/tailwindcss): simplify article content css selector
This commit is contained in:
Goestav
2025-05-21 04:39:10 +08:00
committed by GitHub
parent 43685a9c75
commit 1915b37e68
4 changed files with 103 additions and 0 deletions
+26
View File
@@ -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,
};
+2
View File
@@ -0,0 +1,2 @@
/** The base URL of this route. */
export const BASE_URL = new URL('https://tailwindcss.com/');
+7
View File
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'TailwindCSS',
url: 'tailwindcss.com',
lang: 'en',
};
+68
View File
@@ -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<string> => {
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<Data> => {
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;
};