-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(route): github topic description (#13566)
- Loading branch information
Showing
1 changed file
with
26 additions
and
10 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 |
---|---|---|
@@ -1,25 +1,41 @@ | ||
const cheerio = require('cheerio'); | ||
const got = require('@/utils/got'); | ||
const { parseDate } = require('@/utils/parse-date'); | ||
|
||
module.exports = async (ctx) => { | ||
const link = `https://github.com/topics/${ctx.params.name}?${ctx.params.qs}`; | ||
const { data } = await got(link); | ||
const link = `https://github.com/topics/${ctx.params.name}`; | ||
const { data, url } = await got(link, { | ||
searchParams: new URLSearchParams(ctx.params.qs), | ||
}); | ||
const $ = cheerio.load(data); | ||
|
||
ctx.state.data = { | ||
title: $('title').text(), | ||
link, | ||
description: $('.markdown-body').text().trim(), | ||
link: url, | ||
item: $('article.my-4') | ||
.map((_, item) => { | ||
.toArray() | ||
.map((item) => { | ||
item = $(item); | ||
|
||
const title = $(item.find('h3 a').get(1)).attr('href').slice(1); | ||
const title = item.find('h3').text().trim(); | ||
const author = title.split('/')[0]; | ||
const description = item.find('div.border-bottom > div > p + div').text(); | ||
const link = `https://github.com/${title}`; | ||
const description = (item.find('a img').prop('outerHTML') ?? '') + item.find('div > div > p').text(); | ||
const link = `https://github.com${item.find('h3 a').last().attr('href')}`; | ||
const category = item | ||
.find('.topic-tag') | ||
.toArray() | ||
.map((item) => $(item).text().trim()); | ||
const pubDate = parseDate(item.find('relative-time').attr('datetime')); | ||
|
||
return { title, author, description, link }; | ||
}) | ||
.get(), | ||
return { | ||
title, | ||
author, | ||
description, | ||
link, | ||
category, | ||
pubDate, | ||
}; | ||
}), | ||
}; | ||
}; |