Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
const { generateRssFeed, generateAtomFeed } = require('feedsmith');
const { gravatar, full_url_for, encodeURL } = require('hexo-util');

function revertLazyload(html) {
if (typeof html !== 'string') return html;
return html.replace(/<img[^>]+>/ig, img => {
const match = img.match(/\s+data-src=(["'])(.*?)\1/i);
if (match) {
img = img.replace(/\s+src=(["']).*?\1/ig, '');
img = img.replace(/\s+data-src=(["'])(.*?)\1/ig, ' src=$1$2$1');
img = img.replace(/\s+data-srcset=(["'])(.*?)\1/ig, ' srcset=$1$2$1');
img = img.replace(/\s+data-sizes=(["'])(.*?)\1/ig, ' sizes=$1$2$1');
}
return img;
});
}

function composePosts(posts, feedConfig) {
const { limit, order_by } = feedConfig;

Expand Down Expand Up @@ -91,10 +105,10 @@ function composeItem(post, feedConfig, context) {
return {
title: post.title,
link: encodeURL(full_url_for.call(context, post.permalink)),
description: composeItemDescription(post, feedConfig),
description: revertLazyload(composeItemDescription(post, feedConfig)),
published: post.date.toDate(),
updated: post.updated ? post.updated.toDate() : post.date.toDate(),
content: composeItemContent(post, feedConfig),
content: revertLazyload(composeItemContent(post, feedConfig)),
enclosures: post.image && [{ url: full_url_for.call(context, post.image) }],
categories: composeItemCategories(post)
};
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ describe('Feed generator', () => {
title: 'No Delimiter Test',
content: 'This content has no delimiter and should be truncated at content_limit.',
date: 1e8
},
{
source: 'lazyload-test',
slug: 'lazyload-test',
title: 'LazyLoad Test',
content: '<img data-src="https://example.com/image.png" alt="Test" class="lazyload" src="data:image/gif;base64,...">',
date: 1e8
}
]);
locals = hexo.locals.toObject();
Expand Down Expand Up @@ -636,6 +643,26 @@ describe('Feed generator', () => {
// Should be truncated at content_limit since delimiter not found
description.should.eql('This content has no delimiter');
});

it('Reverts lazyloaded images in content', async () => {
hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
content: true
};
hexo.config = Object.assign(hexo.config, urlConfig);
const feedCfg = hexo.config.feed;
const updatedLocals = hexo.locals.toObject();
const result = generator(updatedLocals, feedCfg.type, feedCfg.path);

const { items } = await p(result.data);
const post = items.filter(({ title }) => title === 'LazyLoad Test');
post.length.should.eql(1);
const { description } = post[0];

description.should.include('src="https://example.com/image.png"');
description.should.not.include('data-src');
});
});

it('No posts', () => {
Expand Down