fix: BBC 中文网图片 (#5837)

close #5780
This commit is contained in:
Henry Wang 2020-10-09 23:11:39 +01:00 committed by GitHub
parent c7934464f2
commit c83aa45ec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 15 deletions

View File

@ -1,26 +1,34 @@
const ProcessImage = ($, e, c) => {
const cheerio = require('cheerio');
const ProcessImage = ($, e, c, noscript = false) => {
const img = $(e).find(c);
if (img.length > 0) {
let message;
// special handling for img wrapped in <noscript>
if (noscript) {
// eslint-disable-next-line no-unused-vars
const i = cheerio.load(img[0].children[0].data)('img')[0];
// handle cover image and lazy-loading images
if (img[0].attribs.src) {
message = `<figure><img alt='${img[0].attribs.alt ? img[0].attribs.alt : ''}' src='${img[0].attribs.src.replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
message = `<figure><img alt='${i.attribs.alt}' src='${i.attribs.src}'><br><figcaption>`;
} else {
message = `<figure><img alt='${img[0].attribs['data-alt'] ? img[0].attribs['data-alt'] : ''}' src='${img[0].attribs['data-src'].replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
}
if (img[0].attribs.src) {
// handle cover image and lazy-loading images
message = `<figure><img alt='${img[0].attribs.alt ? img[0].attribs.alt : ''}' src='${img[0].attribs.src.replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
} else {
message = `<figure><img alt='${img[0].attribs['data-alt'] ? img[0].attribs['data-alt'] : ''}' src='${img[0].attribs['data-src'].replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
}
// add image caption
const figcaption = $(e).find('.media-caption__text');
if (figcaption.length > 0) {
message += `${$(figcaption[0]).text().trim()}`;
}
// add image caption
const figcaption = $(e).find('.media-caption__text');
if (figcaption.length > 0) {
message += `${$(figcaption[0]).text().trim()}`;
}
// add image copyright
const copyright = $(e).find('.story-image-copyright');
if (copyright.length > 0) {
message += ` ©${$(copyright[0]).text().trim()}`;
// add image copyright
const copyright = $(e).find('.story-image-copyright');
if (copyright.length > 0) {
message += ` ©${$(copyright[0]).text().trim()}`;
}
}
message += '</figcaption></figure>';
@ -108,6 +116,10 @@ const ProcessFeed = ($, link) => {
// handle lazy-loading images
ProcessImage($, e, '.js-delayed-image-load');
ProcessImage($, e, 'div > div > div > img');
ProcessImage($, e, 'div > div > div > noscript', true);
ProcessVideo($, e, 'figure.media-with-caption > div.player-with-placeholder');
$(e).remove();