From 8280a6015af1a5f253b49184c495cd32994694bc Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sun, 20 Dec 2020 13:30:44 -0500 Subject: [PATCH] Fix issue where images sometimes not set as cover image --- src/parser.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/parser.js b/src/parser.js index 56504fb..cff6ae9 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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); } - } + }); }); }