Skip to content

Commit

Permalink
substack: Skips post embed if JSON is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAdamDavis committed Jan 8, 2025
1 parent b95ef0f commit 3fcdbb5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/mg-substack/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,15 @@ const processContent = (post, siteUrl, options) => {

$html('.digest-post-embed').each((i, el) => {
const attrsRaw = $(el).attr('data-attrs');
const attrs = JSON.parse(attrsRaw);

let attrs;

// Return early if JSON is invalid
try {
attrs = JSON.parse(attrsRaw);
} catch (error) {
return;
}

const postUrl = attrs.canonical_url;
const postTitle = attrs.title;
Expand Down
17 changes: 17 additions & 0 deletions packages/mg-substack/test/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,23 @@ describe('Convert HTML from Substack to Ghost-compatible HTML', function () {
expect(processed.data.html).not.toInclude('<div class="digest-post-embed"');
expect(processed.data.html).toInclude('<figure class="kg-card kg-bookmark-card"><a class="kg-bookmark-container" href="https://www.exampe.com/p/the-link"><div class="kg-bookmark-content"><div class="kg-bookmark-title">The Title</div><div class="kg-bookmark-description">The caption</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-9772-40c8-bc77-0ae3e6ec4774_205x205.png" alt><span class="kg-bookmark-author">Pub Name</span><span class="kg-bookmark-publisher">Author Name</span></div></div><div class="kg-bookmark-thumbnail"><img src="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-1a48-4dd3-ac53-06900dc9a92d_1200x800.jpeg" alt></div></a></figure>');
});

test('Skips post embed if JSON is invalid', async function () {
const post = {
data: {
html: `<p>Lorem ipsum.</p><div class="digest-post-embed" data-attrs="{&quot;no"></div><p>Dolore magna.</p>`,
title: 'My embed post'
}
};
const url = 'https://example.com';
const options = {
useFirstImage: false
};

const processed = await processContent(post, url, options);

expect(processed.data.html).toEqual('<p>Lorem ipsum.</p><div class="digest-post-embed" data-attrs="{&quot;no"></div><p>Dolore magna.</p>');
});
});

describe('Image handling', function () {
Expand Down

0 comments on commit 3fcdbb5

Please sign in to comment.