|
| 1 | +import { load } from 'cheerio'; |
| 2 | +import type { Context } from 'hono'; |
| 3 | + |
| 4 | +import type { Data, Route } from '@/types'; |
| 5 | +import { ViewType } from '@/types'; |
| 6 | +import ofetch from '@/utils/ofetch'; |
| 7 | +import { parseDate } from '@/utils/parse-date'; |
| 8 | + |
| 9 | +export const handler = async (ctx: Context): Promise<Data> => { |
| 10 | + const { category } = ctx.req.param(); |
| 11 | + const baseUrl = 'https://lancedb.com'; |
| 12 | + const url = category ? `${baseUrl}/blog/category/${category}` : `${baseUrl}/blog`; |
| 13 | + const response = await ofetch(url); |
| 14 | + const $ = load(response); |
| 15 | + |
| 16 | + const items = $('article') |
| 17 | + .toArray() |
| 18 | + .map((el) => { |
| 19 | + const $el = $(el); |
| 20 | + const title = $el.find('h2 a').text().trim(); |
| 21 | + const link = $el.find('h2 a').attr('href'); |
| 22 | + |
| 23 | + const description = $el.find('p.post-description').text().trim(); |
| 24 | + const meta = $el.find('.post-meta').text().trim().replaceAll(/\s+/g, ' '); |
| 25 | + const [cat, author, dateStr] = meta.split(' / '); |
| 26 | + const pubDate = dateStr ? parseDate(dateStr) : undefined; |
| 27 | + |
| 28 | + return { |
| 29 | + title, |
| 30 | + link, |
| 31 | + description, |
| 32 | + pubDate, |
| 33 | + author, |
| 34 | + category: cat ? [cat] : [], |
| 35 | + }; |
| 36 | + }); |
| 37 | + |
| 38 | + return { |
| 39 | + title: `LanceDB Blog${category ? ` - ${category}` : ''}`, |
| 40 | + link: url, |
| 41 | + item: items, |
| 42 | + }; |
| 43 | +}; |
| 44 | + |
| 45 | +export const route: Route = { |
| 46 | + path: '/blog/:category?', |
| 47 | + name: 'Blog', |
| 48 | + url: 'lancedb.com/blog', |
| 49 | + maintainers: ['HUSTERGS'], |
| 50 | + example: '/lancedb/blog', |
| 51 | + parameters: { |
| 52 | + category: 'filter blog post by category, return all posts if not specified', |
| 53 | + }, |
| 54 | + description: undefined, |
| 55 | + categories: ['programming'], |
| 56 | + features: { |
| 57 | + requireConfig: false, |
| 58 | + requirePuppeteer: false, |
| 59 | + antiCrawler: false, |
| 60 | + supportBT: false, |
| 61 | + supportPodcast: false, |
| 62 | + supportScihub: false, |
| 63 | + }, |
| 64 | + radar: [ |
| 65 | + { |
| 66 | + source: ['lancedb.com/blog', 'lancedb.com/blog/category/:category'], |
| 67 | + target: '/blog/:category', |
| 68 | + }, |
| 69 | + ], |
| 70 | + view: ViewType.Articles, |
| 71 | + handler, |
| 72 | +}; |
0 commit comments