From 3bf9dd28c1661ad3d2b2d316aa30f900bb722594 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 2 Jan 2024 17:37:59 +0000 Subject: [PATCH] feat(route): backlinko (#14164) * feat(route): backlinko * Refactor blog.js to destructure nested properties --- lib/v2/backlinko/blog.js | 44 ++++++++++++++++++++++++++++++++++ lib/v2/backlinko/maintainer.js | 3 +++ lib/v2/backlinko/radar.js | 13 ++++++++++ lib/v2/backlinko/router.js | 3 +++ website/docs/routes/blog.mdx | 6 +++++ 5 files changed, 69 insertions(+) create mode 100644 lib/v2/backlinko/blog.js create mode 100644 lib/v2/backlinko/maintainer.js create mode 100644 lib/v2/backlinko/radar.js create mode 100644 lib/v2/backlinko/router.js diff --git a/lib/v2/backlinko/blog.js b/lib/v2/backlinko/blog.js new file mode 100644 index 000000000..6ceba525b --- /dev/null +++ b/lib/v2/backlinko/blog.js @@ -0,0 +1,44 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const baseUrl = 'https://backlinko.com'; + const { data: response, url: link } = await got(`${baseUrl}/blog`); + + const $ = cheerio.load(response); + const nextData = JSON.parse($('#__NEXT_DATA__').text()); + const { + buildId, + props: { pageProps }, + } = nextData; + + const posts = pageProps.posts.nodes.concat(pageProps.backlinkoLockedPosts.nodes).map((post) => ({ + title: post.title, + link: `${baseUrl}/${post.slug}`, + pubDate: parseDate(post.modified), + author: post.author.node.name, + apiUrl: `${baseUrl}/_next/data/${buildId}/${post.slug}.json`, + })); + + const items = await Promise.all( + posts.map((item) => + ctx.cache.tryGet(item.link, async () => { + const { data } = await got(item.apiUrl); + const post = data.pageProps.post || data.pageProps.lockedPost; + + item.description = post.content; + + return item; + }) + ) + ); + + ctx.state.data = { + title: pageProps.page.seo.title, + description: pageProps.page.seo.metaDesc, + link, + language: 'en', + item: items, + }; +}; diff --git a/lib/v2/backlinko/maintainer.js b/lib/v2/backlinko/maintainer.js new file mode 100644 index 000000000..96ec7b2a6 --- /dev/null +++ b/lib/v2/backlinko/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/blog': ['TonyRL'], +}; diff --git a/lib/v2/backlinko/radar.js b/lib/v2/backlinko/radar.js new file mode 100644 index 000000000..173c86303 --- /dev/null +++ b/lib/v2/backlinko/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'backlinko.com': { + _name: 'Backlinko', + '.': [ + { + title: 'Blog', + docs: 'https://docs.rsshub.app/routes/blog#backlinko', + source: ['/blog', '/'], + target: '/backlinko/blog', + }, + ], + }, +}; diff --git a/lib/v2/backlinko/router.js b/lib/v2/backlinko/router.js new file mode 100644 index 000000000..eadc22005 --- /dev/null +++ b/lib/v2/backlinko/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/blog', require('./blog')); +}; diff --git a/website/docs/routes/blog.mdx b/website/docs/routes/blog.mdx index 7ba996bc8..51dae7bcd 100644 --- a/website/docs/routes/blog.mdx +++ b/website/docs/routes/blog.mdx @@ -22,6 +22,12 @@ +## Backlinko {#backlinko} + +### Blog {#backlinko-blog} + + + ## Benedict Evans {#benedict-evans} ### Essays {#benedict-evans-essays}