|
| 1 | +import { parseDate } from '@/utils/parse-date'; |
| 2 | +import ofetch from '@/utils/ofetch'; // 统一使用的请求库 |
| 3 | +import { load } from 'cheerio'; // 类似 jQuery 的 API HTML 解析器 |
| 4 | +import { Route } from '@/types'; |
| 5 | +import cache from '@/utils/cache'; |
| 6 | +// import { getCurrentPath } from '@/utils/helpers'; |
| 7 | +// import { art } from '@/utils/render'; |
| 8 | +// import path from 'node:path'; |
| 9 | +// const __dirname = getCurrentPath(import.meta.url); |
| 10 | +export const route: Route = { |
| 11 | + path: '/:type', |
| 12 | + categories: ['blog'], |
| 13 | + example: '/dev', |
| 14 | + parameters: { type: 'dev | product-management | ux-design' }, |
| 15 | + radar: [ |
| 16 | + { |
| 17 | + source: ['blog.logrocket.com'], |
| 18 | + }, |
| 19 | + ], |
| 20 | + name: 'blog.logrocket', |
| 21 | + maintainers: ['findwei'], |
| 22 | + handler, |
| 23 | + url: 'blog.logrocket.com/', |
| 24 | +}; |
| 25 | +async function handler(ctx) { |
| 26 | + const type = ctx.req.param('type'); |
| 27 | + const link = 'https://blog.logrocket.com/'; |
| 28 | + let title = 'Dev'; |
| 29 | + if (type === 'product-management') { |
| 30 | + title = 'Product Management'; |
| 31 | + } else if (type === 'ux-design') { |
| 32 | + title = 'UX Design'; |
| 33 | + } |
| 34 | + const response = await ofetch(`${link}${type}`); |
| 35 | + const $ = load(response); |
| 36 | + const list = $('div.post-list .post-card') |
| 37 | + .toArray() |
| 38 | + .map((item) => { |
| 39 | + item = $(item); |
| 40 | + const a = item.find('a').first(); |
| 41 | + const title = item.find('.post-card-title').first(); |
| 42 | + return { |
| 43 | + title: title.text(), |
| 44 | + link: a.attr('href'), |
| 45 | + pubDate: parseDate(item.find('.post-card-author-name').next().text().split(' ⋅ ')[0], 'MMM D, YYYY'), |
| 46 | + author: item.find('.post-card-author-name').text(), |
| 47 | + }; |
| 48 | + }); |
| 49 | + const items = await Promise.all( |
| 50 | + list.map((item) => |
| 51 | + cache.tryGet(item.link, async () => { |
| 52 | + const response = await ofetch(item.link); |
| 53 | + const $ = load(response); |
| 54 | + // |
| 55 | + $('div.content-max-width .sidebar-container div.code-block').remove(); |
| 56 | + item.description = $('div.content-max-width .sidebar-container').html(); |
| 57 | + // item.description = art(path.join(__dirname, 'templates/description.art'), { |
| 58 | + // // header: $('#post-header').html(), |
| 59 | + // description: $('div.content-max-width .the-content-container').remove('.lr-content div.code-block.code-block-77').remove('.lr-content .code-block.code-block-57').html(), |
| 60 | + // }); |
| 61 | + return item; |
| 62 | + }) |
| 63 | + ) |
| 64 | + ); |
| 65 | + return { |
| 66 | + title: `logrocket-${title}`, |
| 67 | + link, |
| 68 | + description: `logrocket-${title}`, |
| 69 | + item: items, |
| 70 | + }; |
| 71 | +} |
0 commit comments