fix(route): fix Yuque book route (#14022)

* fix: fix Yuque book route

* fix: sort switch conditions

* fix: add cookieJar

---------
This commit is contained in:
James Tsang 2023-12-13 23:47:07 +08:00 committed by GitHub
parent 095aa9e8f1
commit 39cf9274a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -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) => ({

View File

@ -14,6 +14,7 @@ const card2Html = (elem, link) => {
break;
case 'bookmarkInline':
case 'bookmarklink':
case 'yuqueinline':
html = `<a href='${value.src}'>${value.detail.title}</a>`;
break;
case 'checkbox':
@ -32,6 +33,9 @@ const card2Html = (elem, link) => {
case 'hr':
html = '<hr>';
break;
case 'label':
html = `<b>${value.label}</b>`;
break;
case 'mention':
html = `<a href='https://www.yuque.com/${value.login}'>${value.name}</a>`;
break;
@ -46,14 +50,26 @@ const card2Html = (elem, link) => {
html = `<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" height=66 src="${value.src}"></iframe>`;
} else if (elem.attr('alias') === 'bilibili' || elem.attr('alias') === undefined) {
html = `<iframe src="${value.src}&high_quality=1&autoplay=0" width="650" height="477" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>`;
} else if (value.type === 'codepen') {
html = `<iframe height="265" style="width: 100%;" scrolling="no" title="codepen" src="${value.url}" frameborder="no" allowtransparency="true" allowfullscreen="true"></iframe>`;
} else {
throw Error(`Unhandled thirdparty on ${link}: ${elem.attr('alias')}`);
}
break;
case 'yuque':
if (value.mode === 'card') {
html = `<a href='${value.src}'>${value.detail.title}</a>`;
} else if (value.mode === 'embed') {
html = `<iframe src="${value.url}" width="100%" height="518"></iframe>`;
} else {
throw Error(`Unhandled mode on ${link}: ${value.mode}`);
}
break;
case 'video':
// fake video src
html = `<video src='${value.videoId}'></video>`;
break;
default:
throw Error(`Unhandled card on ${link}: ${name}`);
}