Fix issue where images sometimes not set as cover image

This commit is contained in:
Will Boyd
2020-12-20 13:30:44 -05:00
parent e4c130026a
commit 8280a6015a
+13 -13
View File
@@ -122,25 +122,25 @@ function collectScrapedImages(data) {
}
function mergeImagesIntoPosts(images, posts) {
// create lookup table for quicker traversal
const postsLookup = posts.reduce((lookup, post) => {
lookup[post.meta.id] = post;
return lookup;
}, {});
images.forEach(image => {
const post = postsLookup[image.postId];
if (post) {
posts.forEach(post => {
let shouldAttach = false;
// this image was uploaded as an attachment to this post
if (image.postId === post.meta.id) {
shouldAttach = true;
}
// this image was set as the featured image for this post
if (image.id === post.meta.coverImageId) {
// save cover image filename to frontmatter
shouldAttach = true;
post.frontmatter.coverImage = shared.getFilenameFromUrl(image.url);
}
// save (unique) full image URLs for downloading later
if (!post.meta.imageUrls.includes(image.url)) {
if (shouldAttach && !post.meta.imageUrls.includes(image.url)) {
post.meta.imageUrls.push(image.url);
}
}
});
});
}