diff --git a/lib/routes/leetcode/articles.js b/lib/routes/leetcode/articles.js index b143c044de..45d59e0786 100644 --- a/lib/routes/leetcode/articles.js +++ b/lib/routes/leetcode/articles.js @@ -1,11 +1,12 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const url = require('url'); +const showdown = require('showdown'); const host = 'https://leetcode.com'; module.exports = async (ctx) => { - const link = 'https://leetcode.com/articles/'; + const link = url.resolve(host, '/articles'); const response = await got.get(link); const $ = cheerio.load(response.data); @@ -26,16 +27,33 @@ module.exports = async (ctx) => { const out = await Promise.all( list.map(async (info) => { const itemUrl = url.resolve(host, info.link); + const titelSlug = info.link.split('/')[2]; const cache = await ctx.cache.get(itemUrl); if (cache) { return Promise.resolve(JSON.parse(cache)); } - const response = await got.get(itemUrl); - const $ = cheerio.load(response.data); + const questionData = await got + .post(url.resolve(host, '/graphql'), { + json: { operationName: 'questionData', variables: { titleSlug: titelSlug }, query: 'query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n }\n}\n' }, + }) + .json(); - const description = $('#question-preview').html().trim() + $('.article-body').html().trim(); + const questionNote = await got + .post(url.resolve(host, '/graphql'), { + json: { + operationName: 'QuestionNote', + variables: { titleSlug: titelSlug }, + query: 'query QuestionNote($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n solution {\n content\n }\n }\n}\n', + }, + }) + .json(); + + const converter = new showdown.Converter(); + const solution = converter.makeHtml(questionNote.data.question.solution.content); + + const description = questionData.data.question.content.trim() + solution; const single = { title: info.title, diff --git a/package.json b/package.json index 5ac3cda50c..f2cec123b8 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ "redis": "3.0.2", "require-all": "3.0.0", "rss-parser": "3.9.0", + "showdown": "1.9.1", "socks-proxy-agent": "5.0.0", "string-similarity": "^4.0.3", "tiny-async-pool": "1.2.0", diff --git a/yarn.lock b/yarn.lock index fe31e8070d..e2e3649f36 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3530,6 +3530,15 @@ cliui@^4.0.0: strip-ansi "^4.0.0" wrap-ansi "^2.0.0" +cliui@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/cliui/-/cliui-5.0.0.tgz#deefcfdb2e800784aa34f46fa08e06851c7bbbc5" + integrity sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA== + dependencies: + string-width "^3.1.0" + strip-ansi "^5.2.0" + wrap-ansi "^5.1.0" + cliui@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1" @@ -11304,6 +11313,13 @@ shellwords@^0.1.1: resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== +showdown@1.9.1: + version "1.9.1" + resolved "https://registry.yarnpkg.com/showdown/-/showdown-1.9.1.tgz#134e148e75cd4623e09c21b0511977d79b5ad0ef" + integrity sha512-9cGuS382HcvExtf5AHk7Cb4pAeQQ+h0eTr33V1mu+crYWV4KvWAw6el92bDrqGEk5d46Ai/fhbEUwqJ/mTCNEA== + dependencies: + yargs "^14.2" + signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" @@ -11752,7 +11768,7 @@ string-width@^1.0.1: is-fullwidth-code-point "^2.0.0" strip-ansi "^4.0.0" -string-width@^3.0.0: +string-width@^3.0.0, string-width@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/string-width/-/string-width-3.1.0.tgz#22767be21b62af1081574306f69ac51b62203961" integrity sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w== @@ -13588,6 +13604,14 @@ yargs-parser@^13.0.0: camelcase "^5.0.0" decamelize "^1.2.0" +yargs-parser@^15.0.1: + version "15.0.1" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-15.0.1.tgz#54786af40b820dcb2fb8025b11b4d659d76323b3" + integrity sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw== + dependencies: + camelcase "^5.0.0" + decamelize "^1.2.0" + yargs-parser@^18.1.2: version "18.1.3" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0" @@ -13614,6 +13638,23 @@ yargs@12.0.5: y18n "^3.2.1 || ^4.0.0" yargs-parser "^11.1.1" +yargs@^14.2: + version "14.2.3" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-14.2.3.tgz#1a1c3edced1afb2a2fea33604bc6d1d8d688a414" + integrity sha512-ZbotRWhF+lkjijC/VhmOT9wSgyBQ7+zr13+YLkhfsSiTriYsMzkTUFP18pFhWwBeMa5gUc1MzbhrO6/VB7c9Xg== + dependencies: + cliui "^5.0.0" + decamelize "^1.2.0" + find-up "^3.0.0" + get-caller-file "^2.0.1" + require-directory "^2.1.1" + require-main-filename "^2.0.0" + set-blocking "^2.0.0" + string-width "^3.0.0" + which-module "^2.0.0" + y18n "^4.0.0" + yargs-parser "^15.0.1" + yargs@^15.4.1: version "15.4.1" resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"