diff --git a/docs/program-update.md b/docs/program-update.md index d69fe1cbe..06e7302a3 100644 --- a/docs/program-update.md +++ b/docs/program-update.md @@ -160,6 +160,10 @@ pageClass: routes +## Mathpix + + + ## Microsoft Edge ### 外接程序更新 diff --git a/lib/router.js b/lib/router.js index 5f2ca3ae2..39e41c2c6 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3512,6 +3512,9 @@ router.get('/uisdc/news', require('./routes/uisdc/news')); router.get('/uisdc/zt/:title?', require('./routes/uisdc/zt')); router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic')); +// Mathpix +router.get('/mathpix/blog', require('./routes/mathpix/blog')); + // OneNote Gem Add-Ins router.get('/onenotegem/release', require('./routes/onenotegem/release')); diff --git a/lib/routes/mathpix/blog.js b/lib/routes/mathpix/blog.js new file mode 100644 index 000000000..55921c606 --- /dev/null +++ b/lib/routes/mathpix/blog.js @@ -0,0 +1,53 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'https://mathpix.com'; + const currentUrl = `${rootUrl}/blog`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.articles__text-content a') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: new Date(item.parent().find('.articles__date').text()).toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + const titleImage = content('.article__image-area img'); + titleImage.attr('src', `${rootUrl}${titleImage.attr('srcset')}`); + titleImage.removeAttr('srcset'); + + item.author = content('.article__titles-wrapper summary').text().replace('By ', ''); + item.description = content('.article__image-area').html() + content('.article__content').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: 'Mathpix Blog', + link: currentUrl, + item: items, + }; +};