From 39cf9274a6edd43ee4a7f9022409cb8e03f381f4 Mon Sep 17 00:00:00 2001 From: James Tsang Date: Wed, 13 Dec 2023 23:47:07 +0800 Subject: [PATCH] fix(route): fix Yuque book route (#14022) * fix: fix Yuque book route * fix: sort switch conditions * fix: add cookieJar --------- --- lib/v2/yuque/book.js | 7 ++++++- lib/v2/yuque/utils.js | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/v2/yuque/book.js b/lib/v2/yuque/book.js index aa6ba0fd8..f66380cf7 100644 --- a/lib/v2/yuque/book.js +++ b/lib/v2/yuque/book.js @@ -2,15 +2,19 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); const { card2Html } = require('./utils'); +const { CookieJar } = require('tough-cookie'); const APP_DATA_REGEX = /window\.appData = JSON\.parse\(decodeURIComponent\("(.+?)"\)\);/; const baseUrl = 'https://www.yuque.com'; module.exports = async (ctx) => { + const cookieJar = new CookieJar(); const { name, book } = ctx.params; const bookUrl = `${baseUrl}/${name}/${book}`; - const { data: bookHtml } = await got(bookUrl); + const { data: bookHtml } = await got(bookUrl, { + cookieJar, + }); const $ = cheerio.load(bookHtml); const appData = JSON.parse(decodeURIComponent($('script').text().match(APP_DATA_REGEX)[1])); @@ -22,6 +26,7 @@ module.exports = async (ctx) => { searchParams: { book_id: bookId, }, + cookieJar, }); const list = docs.map((item) => ({ diff --git a/lib/v2/yuque/utils.js b/lib/v2/yuque/utils.js index af5565dd6..0e65363e9 100644 --- a/lib/v2/yuque/utils.js +++ b/lib/v2/yuque/utils.js @@ -14,6 +14,7 @@ const card2Html = (elem, link) => { break; case 'bookmarkInline': case 'bookmarklink': + case 'yuqueinline': html = `${value.detail.title}`; break; case 'checkbox': @@ -32,6 +33,9 @@ const card2Html = (elem, link) => { case 'hr': html = '
'; break; + case 'label': + html = `${value.label}`; + break; case 'mention': html = `${value.name}`; break; @@ -46,14 +50,26 @@ const card2Html = (elem, link) => { html = ``; } else if (elem.attr('alias') === 'bilibili' || elem.attr('alias') === undefined) { html = ``; + } else if (value.type === 'codepen') { + html = ``; } else { throw Error(`Unhandled thirdparty on ${link}: ${elem.attr('alias')}`); } break; + case 'yuque': + if (value.mode === 'card') { + html = `${value.detail.title}`; + } else if (value.mode === 'embed') { + html = ``; + } else { + throw Error(`Unhandled mode on ${link}: ${value.mode}`); + } + break; case 'video': // fake video src html = ``; break; + default: throw Error(`Unhandled card on ${link}: ${name}`); }