Files
RSSHub/routes/bbc/utils.js
T
Henry Wang 88829f8776 增加 BBC 中文网 (#832)
做自己的有缘人 close #740
2018-10-07 16:03:31 +08:00

75 lines
2.2 KiB
JavaScript

const ProcessImage = ($, e, c) => {
const img = $(e).find(c);
if (img.length > 0) {
let message;
// handle cover image and lazy-loading images
if (img[0].attribs.src) {
message = `<figure><img 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']}' 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 copyright
const copyright = $(e).find('.story-image-copyright');
if (copyright.length > 0) {
message += ` ©${$(copyright[0])
.text()
.trim()}`;
}
message += '</figcaption></figure>';
$(message).insertAfter(e);
}
};
const ProcessFeed = ($, link) => {
// by default treat it as a hybrid news with video and story-body__inner
let content = $('div.story-body__inner');
if (content.length === 0) {
// it's a video news with video and story-body
content = $('div.story-body');
}
if ($('#comp-media-player').length > 0) {
// there is a cover video
let message = `<br><a href='${link}'>使用浏览器播视频/view video in browser</a>`;
const cover = $('#comp-media-player').find('img.player-with-placeholder__image');
if (cover.length > 0) {
// there is a cover image
message = `<br><img src='${cover[0].attribs.src}'>` + message;
}
$(message).insertAfter(content[0].lastChild);
}
// resize all images
content.find('figure').each((i, e) => {
ProcessImage($, e, 'img');
// handle lazy-loading images
ProcessImage($, e, '.js-delayed-image-load');
$(e).remove();
});
return content;
};
module.exports = {
ProcessFeed,
};