@tryghost/mg-substack 1.14.0-0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,882 @@
1
+ import assert from 'node:assert/strict';
2
+ import { describe, it } from 'node:test';
3
+ import { processContent, getImageDimensions, largeImageUrl } from '../index.js';
4
+ describe('Convert HTML from Substack to Ghost-compatible HTML', function () {
5
+ it('Returns post object if no HTML present', async function () {
6
+ const post = {
7
+ data: {}
8
+ };
9
+ const url = 'https://example.com';
10
+ const options = {};
11
+ const processed = await processContent(post, url, options);
12
+ assert.equal(processed.data.html, '');
13
+ });
14
+ it('Can transform paywalls', async function () {
15
+ const post = {
16
+ data: {
17
+ html: `<p>Public text</p><div class="paywall-jump" data-component-name="PaywallToDOM"></div><p>Premium text</p>`
18
+ }
19
+ };
20
+ const url = 'https://example.com';
21
+ const options = {};
22
+ const processed = await processContent(post, url, options);
23
+ assert.equal(processed.data.html, `<p>Public text</p><!--members-only--><p>Premium text</p>`);
24
+ });
25
+ it('Can transform subscribe links with custom defined URL', async function () {
26
+ const post = {
27
+ data: {
28
+ html: `<p class="button-wrapper" data-attrs='{"url":"https://example.com/subscribe?","text":"Sign up now","class":null}'><a class="button primary" href="https://example.com/subscribe?"><span>Sign up now</span></a></p><p><a href="https://example.com/subscribe">Subscribe</a></p>`
29
+ }
30
+ };
31
+ const url = 'https://example.com';
32
+ const options = {
33
+ subscribeLink: '#/portal/signup'
34
+ };
35
+ const processed = await processContent(post, url, options);
36
+ assert.equal(processed.data.html, `<div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent">Sign up now</a></div><p><a href="#/portal/signup">Subscribe</a></p>`);
37
+ });
38
+ it('Removes subscribe buttons and links when noSubscribeButtons is true', async function () {
39
+ const post = {
40
+ data: {
41
+ html: `<p class="button-wrapper" data-attrs='{"url":"https://example.com/subscribe?","text":"Sign up now","class":null}'><a class="button primary" href="https://example.com/subscribe?"><span>Sign up now</span></a></p><p><a href="https://example.com/subscribe">Subscribe</a></p><p>Keep this paragraph.</p>`
42
+ }
43
+ };
44
+ const url = 'https://example.com';
45
+ const options = {
46
+ noSubscribeButtons: true
47
+ };
48
+ const processed = await processContent(post, url, options);
49
+ assert.ok(!processed.data.html.includes('subscribe'));
50
+ assert.ok(!processed.data.html.includes('Sign up now'));
51
+ assert.ok(processed.data.html.includes('Keep this paragraph.'));
52
+ });
53
+ it('Removes subscription widget when noSubscribeButtons is true', async function () {
54
+ const post = {
55
+ data: {
56
+ html: `<p>Before</p><div class="subscription-widget-wrap" data-attrs="{&quot;url&quot;:&quot;https://example.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;}"><div class="subscription-widget show-subscribe"><form class="subscription-widget-subscribe"><input type="submit" class="button primary" value="Subscribe"></form></div></div><p>After</p>`
57
+ }
58
+ };
59
+ const url = 'https://example.com';
60
+ const options = {
61
+ noSubscribeButtons: true
62
+ };
63
+ const processed = await processContent(post, url, options);
64
+ assert.ok(processed.data.html.includes('<p>Before</p>'));
65
+ assert.ok(processed.data.html.includes('<p>After</p>'));
66
+ assert.ok(!processed.data.html.includes('subscription-widget-wrap'));
67
+ });
68
+ it('Can transform comment buttons with custom defined URL', async function () {
69
+ const post = {
70
+ data: {
71
+ html: `<p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://example.com/p/my-post/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}"><a class="button primary" href="https://example.com/p/my-post/comments"><span>Leave a comment</span></a></p>`
72
+ }
73
+ };
74
+ const url = 'https://example.com';
75
+ const options = {
76
+ comments: true,
77
+ commentLink: '#post-comments'
78
+ };
79
+ const processed = await processContent(post, url, options);
80
+ assert.equal(processed.data.html, `<div class="kg-card kg-button-card kg-align-center"><a href="#post-comments" class="kg-btn kg-btn-accent">Leave a comment</a></div>`);
81
+ });
82
+ it('Can remove transform comment buttons', async function () {
83
+ const post = {
84
+ data: {
85
+ html: `<p>Hello</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://example.com/p/my-post/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}"><a class="button primary" href="https://example.com/p/my-post/comments"><span>Leave a comment</span></a></p><p>World</p>`
86
+ }
87
+ };
88
+ const url = 'https://example.com';
89
+ const options = {
90
+ comments: false
91
+ };
92
+ const processed = await processContent(post, url, options);
93
+ assert.equal(processed.data.html, `<p>Hello</p><p>World</p>`);
94
+ });
95
+ it('Will not change button element hrefs that are not subscribe buttons', async function () {
96
+ const post = {
97
+ data: {
98
+ html: `<p class="button-wrapper" data-attrs='{"url":"https://example.com/subscribe?","text":"Sign up now","class":null}'><a class="button primary" href="https://example.com/subscribe?"><span>Sign up now</span></a></p><p>Lorem ipsum dolor sit.</p><p class="button-wrapper" data-attrs='{"url":"https://ghost.org","text":"Try Ghost","class":null}'><a class="button primary" href="https://ghost.org"><span>Try Ghost</span></a></p>`
99
+ }
100
+ };
101
+ const url = 'https://example.com';
102
+ const options = {
103
+ subscribeLink: '#/portal/signup'
104
+ };
105
+ const processed = await processContent(post, url, options);
106
+ assert.equal(processed.data.html, `<div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent">Sign up now</a></div><p>Lorem ipsum dolor sit.</p><div class="kg-card kg-button-card kg-align-center"><a href="https://ghost.org/" class="kg-btn kg-btn-accent">Try Ghost</a></div>`);
107
+ });
108
+ it('Will remove share buttons', async function () {
109
+ const post = {
110
+ data: {
111
+ html: `<p class="button-wrapper" data-attrs='{"url":"https://example.com/subscribe?","text":"Sign up now","class":null}'><a class="button primary" href="https://example.com/subscribe?"><span>Sign up now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://example.com/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share PodSnacks&quot;,&quot;action&quot;:null,&quot;class&quot;:null}"><a class="button primary" href="https://example.com/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share PodSnacks</span></a></p>`
112
+ }
113
+ };
114
+ const url = 'https://example.com';
115
+ const options = {
116
+ subscribeLink: '#/portal/signup'
117
+ };
118
+ const processed = await processContent(post, url, options);
119
+ assert.equal(processed.data.html, `<div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent">Sign up now</a></div>`);
120
+ });
121
+ it('Can convert signup forms to signup buttons', async function () {
122
+ const post = {
123
+ data: {
124
+ html: `<p>Lorem ipsum</p><div class="subscription-widget-wrap" data-attrs="{&quot;url&quot;:&quot;https://example.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;}"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">You should sign up!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email…" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>`
125
+ }
126
+ };
127
+ const url = 'https://example.com';
128
+ const options = {
129
+ subscribeLink: '#/portal/signup'
130
+ };
131
+ const processed = await processContent(post, url, options);
132
+ assert.equal(processed.data.html, `<p>Lorem ipsum</p><div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent">Subscribe</a></div>`);
133
+ });
134
+ it('Removes bold tags from headings', async function () {
135
+ const post = {
136
+ data: {
137
+ html: `<h1><strong>Heading 1</strong></h1>
138
+ <h2>Heading <b>2</b></h2>
139
+ <h3><strong>Heading <em>3</em></strong></h3>
140
+ <h4><b>Heading 4</b></h4>
141
+ <h5><strong>Heading 5</strong></h5>
142
+ <h6><b>Heading 6</b></h6>
143
+ <h2><a href="https://example.com">Linked Heading</a></h2>
144
+ <h3>Plain Heading</h3>
145
+ <h4><a href="https://example.com"><b>Bold Linked Heading</b></a></h4>
146
+ <h5><strong>Linked <a href="https://example.com">Bold</a> Heading</strong></h5>
147
+ <p><strong>Paragraph stays bold</strong></p>`
148
+ }
149
+ };
150
+ const url = 'https://example.com';
151
+ const options = {};
152
+ const processed = await processContent(post, url, options);
153
+ assert.equal(processed.data.html, '<h1>Heading 1</h1>\n' +
154
+ ' <h2>Heading 2</h2>\n' +
155
+ ' <h3>Heading <em>3</em></h3>\n' +
156
+ ' <h4>Heading 4</h4>\n' +
157
+ ' <h5>Heading 5</h5>\n' +
158
+ ' <h6>Heading 6</h6>\n' +
159
+ ' <h2><a href="https://example.com">Linked Heading</a></h2>\n' +
160
+ ' <h3>Plain Heading</h3>\n' +
161
+ ' <h4><a href="https://example.com">Bold Linked Heading</a></h4>\n' +
162
+ ' <h5>Linked <a href="https://example.com">Bold</a> Heading</h5>\n' +
163
+ ' <p><strong>Paragraph stays bold</strong></p>');
164
+ });
165
+ it('Can convert an image wrapped with a link', async function () {
166
+ const post = {
167
+ data: {
168
+ html: `<div class="captioned-image-container">
169
+ <figure>
170
+ <a class="image-link image2 image2-800-1200" target="_blank" href="https://example.com">
171
+ <img src="https://example.com/photo_1200x800.jpeg" data-attrs="{&quot;src&quot;:&quot;https://example.com/photo_1200x800.jpeg&quot;,&quot;height&quot;:800,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:185654,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:&quot;https://apple.com&quot;}" alt="A nice photo">
172
+ <style>
173
+ a.image2.image-link.image2-800-1200 {
174
+ padding-bottom: 66.66666666666666%;
175
+ padding-bottom: min(66.66666666666666%, 800px);
176
+ width: 100%;
177
+ height: 0;
178
+ }
179
+ a.image2.image-link.image2-800-1200 img {
180
+ max-width: 1200px;
181
+ max-height: 800px;
182
+ }
183
+ </style>
184
+ </a>
185
+ <figcaption class="image-caption">This is a <a href="https://example.com/page">really</a> nice photo</figcaption>
186
+ </figure>
187
+ </div>
188
+ <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>`
189
+ }
190
+ };
191
+ const url = 'https://example.com';
192
+ const options = {};
193
+ const processed = await processContent(post, url, options);
194
+ assert.equal(processed.data.html, '<figure class="kg-card kg-image-card kg-card-hascaption"><a href="https://example.com"><img src="https://example.com/photo_1200x800.jpeg" class="kg-image" alt="A nice photo" loading="lazy"></a><figcaption>This is a <a href="https://example.com/page">really</a> nice photo</figcaption></figure>\n' +
195
+ ' <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>');
196
+ });
197
+ it('Can wrap lists with images in HTML comments', async function () {
198
+ const post = {
199
+ data: {
200
+ html: `<ul>
201
+ <li>Proin nunc purus, sollicitudin vitae dui id, condimentum efficitur mauris</li>
202
+ <li><img src="https://example.com/photo.jpg" alt="A nice photo"></li>
203
+ <li>Vivamus <a href="https://example.com">congue</a> nisl in gravida blandit</li>
204
+ </ul>
205
+ <ul>
206
+ <li>Proin nunc purus, sollicitudin vitae dui id, condimentum efficitur mauris</li>
207
+ <li>Vivamus <a href="https://example.com">congue</a> nisl in gravida blandit</li>
208
+ </ul>`
209
+ }
210
+ };
211
+ const url = 'https://example.com';
212
+ const options = {};
213
+ const processed = await processContent(post, url, options);
214
+ assert.equal(processed.data.html, '<!--kg-card-begin: html--><ul>\n' +
215
+ ' <li>Proin nunc purus, sollicitudin vitae dui id, condimentum efficitur mauris</li>\n' +
216
+ ' <li><img src="https://example.com/photo.jpg" alt="A nice photo"></li>\n' +
217
+ ' <li>Vivamus <a href="https://example.com">congue</a> nisl in gravida blandit</li>\n' +
218
+ ' </ul><!--kg-card-end: html-->\n' +
219
+ ' <ul>\n' +
220
+ ' <li>Proin nunc purus, sollicitudin vitae dui id, condimentum efficitur mauris</li>\n' +
221
+ ' <li>Vivamus <a href="https://example.com">congue</a> nisl in gravida blandit</li>\n' +
222
+ ' </ul>');
223
+ });
224
+ it('Can process footnotes in text content in old style', async function () {
225
+ const post = {
226
+ data: {
227
+ html: `<p>Lorem ipsum</p>
228
+ <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<a class="footnote-anchor" id="footnote-anchor-1" href="#footnote-1">1</a></p>
229
+ <ol>
230
+ <li>Lorem</li>
231
+ <li>Phasellus scelerisque metus id elit elementum venenatis.<a class="footnote-anchor" id="footnote-anchor-2" href="#footnote-2">2</a></li>
232
+ </ol>
233
+ <ul>
234
+ <li>Ipsum</li>
235
+ <li>Quisque consectetur laoreet felis, sit amet rutrum mi blandit eu.<a class="footnote-anchor" id="footnote-anchor-3" href="#footnote-3">3</a></li>
236
+ </ul>
237
+ <div class="footnote" id="footnote-1">
238
+ <a href="#footnote-anchor-1" class="footnote-number" contenteditable="false">1</a>
239
+ <div class="footnote-content">
240
+ <p>Lorem ipsum</p>
241
+ <p>Dolor simet</p>
242
+ </div>
243
+ </div>
244
+ <div class="footnote" id="footnote-2">
245
+ <a href="#footnote-anchor-2" class="footnote-number" contenteditable="false">2</a>
246
+ <div class="footnote-content">
247
+ <p>Consectetur adipiscing</p>
248
+ </div>
249
+ </div>
250
+ <div class="footnote" id="footnote-3">
251
+ <a href="#footnote-anchor-3" class="footnote-number" contenteditable="false">3</a>
252
+ <div class="footnote-content">
253
+ <p>Elit elementum venenatis</p>
254
+ </div>
255
+ </div>`
256
+ }
257
+ };
258
+ const url = 'https://example.com';
259
+ const options = {};
260
+ const processed = await processContent(post, url, options);
261
+ assert.equal(processed.data.html, '<p>Lorem ipsum</p>\n' +
262
+ ' <!--kg-card-begin: html--><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<a class="footnote-anchor" id="footnote-anchor-1" href="#footnote-1">1</a></p><!--kg-card-end: html-->\n' +
263
+ ' <!--kg-card-begin: html--><ol>\n' +
264
+ ' <li>Lorem</li>\n' +
265
+ ' <li>Phasellus scelerisque metus id elit elementum venenatis.<a class="footnote-anchor" id="footnote-anchor-2" href="#footnote-2">2</a></li>\n' +
266
+ ' </ol><!--kg-card-end: html-->\n' +
267
+ ' <!--kg-card-begin: html--><ul>\n' +
268
+ ' <li>Ipsum</li>\n' +
269
+ ' <li>Quisque consectetur laoreet felis, sit amet rutrum mi blandit eu.<a class="footnote-anchor" id="footnote-anchor-3" href="#footnote-3">3</a></li>\n' +
270
+ ' </ul><!--kg-card-end: html-->\n' +
271
+ ' \n' +
272
+ ' \n' +
273
+ ' <!--kg-card-begin: html--><div class="footnotes"><hr><ol><li id="footnote-1">\n' +
274
+ ' <p>Lorem ipsum</p>\n' +
275
+ ' <p>Dolor simet <a href="#footnote-anchor-1" title="Jump back to footnote 1 in the text.">↩</a></p>\n' +
276
+ ' </li><li id="footnote-2">\n' +
277
+ ' <p>Consectetur adipiscing <a href="#footnote-anchor-2" title="Jump back to footnote 2 in the text.">↩</a></p>\n' +
278
+ ' </li><li id="footnote-3">\n' +
279
+ ' <p>Elit elementum venenatis <a href="#footnote-anchor-3" title="Jump back to footnote 3 in the text.">↩</a></p>\n' +
280
+ ' </li></ol></div><!--kg-card-end: html-->');
281
+ });
282
+ it('Can process footnotes in text content in new style', async function () {
283
+ const post = {
284
+ data: {
285
+ html: `<p>Lorem ipsum</p>
286
+ <p>Lorem ipsum<a class="footnote-anchor" id="footnote-anchor-1" href="#footnote-1">1</a>.</p>
287
+ <p>Dolor simet</p>
288
+ <p>Dolor simet <a class="footnote-anchor" id="footnote-anchor-2" href="#footnote-2">2</a>.</p>
289
+ <p>Hello world</p>
290
+ <p>Hello world. This<a class="footnote-anchor" id="footnote-anchor-3" href="#footnote-3">3</a> is new</p>
291
+ <div class="footnote"><a id="footnote-1" href="#footnote-anchor-1" class="footnote-number" contenteditable="false">2</a>
292
+ <div class="footnote-content">
293
+ <p>Note content</p>
294
+ <p>Two lines</p>
295
+ </div>
296
+ </div>
297
+ <div class="footnote"><a id="footnote-2" href="#footnote-anchor-2" class="footnote-number" contenteditable="false">3</a>
298
+ <div class="footnote-content">
299
+ <p>More notes</p>
300
+ </div>
301
+ </div>
302
+ <div class="footnote"><a id="footnote-3" href="#footnote-anchor-3" class="footnote-number" contenteditable="false">4</a>
303
+ <div class="footnote-content">
304
+ <p><a href="https://ghost.org">Link</a> in this one</p>
305
+ </div>
306
+ </div>`
307
+ }
308
+ };
309
+ const url = 'https://example.com';
310
+ const options = {};
311
+ const processed = await processContent(post, url, options);
312
+ assert.equal(processed.data.html, '<p>Lorem ipsum</p>\n' +
313
+ ' <!--kg-card-begin: html--><p>Lorem ipsum<a class="footnote-anchor" id="footnote-anchor-1" href="#footnote-1">1</a>.</p><!--kg-card-end: html-->\n' +
314
+ ' <p>Dolor simet</p>\n' +
315
+ ' <!--kg-card-begin: html--><p>Dolor simet <a class="footnote-anchor" id="footnote-anchor-2" href="#footnote-2">2</a>.</p><!--kg-card-end: html-->\n' +
316
+ ' <p>Hello world</p>\n' +
317
+ ' <!--kg-card-begin: html--><p>Hello world. This<a class="footnote-anchor" id="footnote-anchor-3" href="#footnote-3">3</a> is new</p><!--kg-card-end: html-->\n' +
318
+ ' \n' +
319
+ ' \n' +
320
+ ' <!--kg-card-begin: html--><div class="footnotes"><hr><ol><li id="footnote-1">\n' +
321
+ ' <p>Note content</p>\n' +
322
+ ' <p>Two lines <a href="#footnote-anchor-1" title="Jump back to footnote 1 in the text.">↩</a></p>\n' +
323
+ ' </li><li id="footnote-2">\n' +
324
+ ' <p>More notes <a href="#footnote-anchor-2" title="Jump back to footnote 2 in the text.">↩</a></p>\n' +
325
+ ' </li><li id="footnote-3">\n' +
326
+ ' <p><a href="https://ghost.org">Link</a> in this one <a href="#footnote-anchor-3" title="Jump back to footnote 3 in the text.">↩</a></p>\n' +
327
+ ' </li></ol></div><!--kg-card-end: html-->');
328
+ });
329
+ it('Can process embedded posts', async function () {
330
+ const post = {
331
+ data: {
332
+ html: `<p>Lorem ipsum</p>
333
+
334
+ <div class="embedded-post-wrap"
335
+ data-attrs="{&quot;id&quot;: 123456,&quot;url&quot;: &quot;https://example.substack.com/p/example-post&quot;,&quot;publication_id&quot;: 1234,&quot;publication_name&quot;: &quot;Example Site&quot;,&quot;publication_logo_url&quot;: &quot;https://bucketeer-1234.s3.amazonaws.com/public/images/5678_680x680.png&quot;,&quot;title&quot;: &quot;Lorem ipsum, This is the Title I’m Showing You&quot;,&quot;truncated_body_text&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad it’s veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat …&quot;,&quot;date&quot;: &quot;2020-01-24T18:45:11.711Z&quot;,&quot;like_count&quot;: 8,&quot;comment_count&quot;: 3,&quot;utm_campaign&quot;: null}">
336
+ <a class="embedded-post" native="true"
337
+ href="https://example.substack.com/p/example-post?utm_source=substack&amp;utm_campaign=post_embed&amp;utm_medium=web">
338
+ <div class="embedded-post-header">
339
+ <img class="embedded-post-publication-logo"
340
+ src="https://bucketeer-1234.s3.amazonaws.com/public/images/5678_680x680.png">
341
+ <span class="embedded-post-publication-name">Example Site</span>
342
+ </div>
343
+ <div class="embedded-post-title">Lorem ipsum, This is the Title I’m Showing You</div>
344
+ <div class="embedded-post-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad it’s veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat …</div><span
345
+ class="embedded-post-read-more">Read more</span>
346
+ <div class="embedded-post-meta">2 years ago · 8 likes · 3 comments · Example User</div>
347
+ </a>
348
+ </div>
349
+
350
+ <p>Dolor Simet</p>`
351
+ }
352
+ };
353
+ const url = 'https://example.com';
354
+ const options = {};
355
+ const processed = await processContent(post, url, options);
356
+ assert.equal(processed.data.html, '<p>Lorem ipsum</p>\n' +
357
+ '\n' +
358
+ ' <!--kg-card-begin: html--><figure class="kg-card kg-bookmark-card"><a class="kg-bookmark-container" href="https://example.substack.com/p/example-post"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Lorem ipsum, This is the Title I’m Showing You</div><div class="kg-bookmark-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad it’s veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat …</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="https://substack-post-media.s3.amazonaws.com/public/images/5678_680x680.png"><span class="kg-bookmark-author">Example Site</span></div></div></a></figure><!--kg-card-end: html-->\n' +
359
+ '\n' +
360
+ ' <p>Dolor Simet</p>');
361
+ });
362
+ it('Can add an audio card for podcast posts', async function () {
363
+ const post = {
364
+ substackPodcastURL: 'https://example.com/files/audio.mp3',
365
+ data: {
366
+ html: `<p>Hello</p>`,
367
+ title: 'My Podcast Episode #1'
368
+ }
369
+ };
370
+ const url = 'https://example.com';
371
+ const options = {};
372
+ const processed = await processContent(post, url, options);
373
+ assert.ok(processed.data.html.startsWith('<div class="kg-card kg-audio-card">'));
374
+ assert.ok(processed.data.html.includes('<div class="kg-audio-player-container">'));
375
+ assert.ok(processed.data.html.includes('<audio src="https://example.com/files/audio.mp3" preload="metadata"></audio>'));
376
+ assert.ok(processed.data.html.includes('<div class="kg-audio-title">My Podcast Episode #1</div>'));
377
+ assert.ok(processed.data.html.includes('<p>Hello</p>'));
378
+ });
379
+ it('Scraped audio`', async function () {
380
+ const post = {
381
+ data: {
382
+ html: `<p>My content</p>`,
383
+ title: 'My Podcast Post',
384
+ podcast_audio_src: 'https://api.substack.com/api/v1/audio/upload/1234abcd-1234-abcd-5678-123456abcdef/src'
385
+ }
386
+ };
387
+ const url = 'https://example.com';
388
+ const options = {};
389
+ const processed = await processContent(post, url, options);
390
+ assert.ok(processed.data.html.startsWith('<div class="kg-card kg-audio-card">'));
391
+ assert.ok(processed.data.html.includes('<audio src="https://api.substack.com/api/v1/audio/upload/1234abcd-1234-abcd-5678-123456abcdef/src"'));
392
+ assert.ok(processed.data.html.includes('<p>My content</p>'));
393
+ });
394
+ it('Can remove the first image if it is the same as `og:image`', async function () {
395
+ const post = {
396
+ data: {
397
+ html: `<p></p><img src="https://example.com/content/file_1200x800.jpg" /><p>My content</p>`,
398
+ title: 'My Image Post',
399
+ og_image: 'https://example.com/content/file_1024x768.jpg'
400
+ }
401
+ };
402
+ const url = 'https://example.com';
403
+ const options = {};
404
+ const processed = await processContent(post, url, options);
405
+ assert.equal(processed.data.html, '<p>My content</p>');
406
+ });
407
+ it('Will take useMetaImage as priority over useFirstImage', async function () {
408
+ const post = {
409
+ data: {
410
+ html: `<p></p><figure><img src="https://example.com/content/first-image.jpg" /><figcaption>My image</figcaption></figure><p>My content</p>`,
411
+ title: 'My Image Post',
412
+ og_image: 'https://example.com/content/file_1024x768.jpg'
413
+ }
414
+ };
415
+ const url = 'https://example.com';
416
+ const options = {
417
+ useMetaImage: true,
418
+ useFirstImage: true
419
+ };
420
+ const processed = await processContent(post, url, options);
421
+ assert.equal(processed.data.html, '<figure><img src="https://example.com/content/first-image.jpg"><figcaption>My image</figcaption></figure><p>My content</p>');
422
+ assert.equal(processed.data.feature_image, 'https://example.com/content/file_1024x768.jpg');
423
+ });
424
+ it('Can useFirstImage', async function () {
425
+ const post = {
426
+ data: {
427
+ html: `<p></p><figure><img src="https://example.com/content/first-image.jpg" /><figcaption>My image</figcaption></figure><p>My content</p>`,
428
+ title: 'My Image Post'
429
+ }
430
+ };
431
+ const url = 'https://example.com';
432
+ const options = {
433
+ useFirstImage: true
434
+ };
435
+ const processed = await processContent(post, url, options);
436
+ assert.equal(processed.data.html, '<p>My content</p>');
437
+ assert.equal(processed.data.feature_image, 'https://example.com/content/first-image.jpg');
438
+ assert.equal(processed.data.feature_image_caption, 'My image');
439
+ });
440
+ it('Converts bucketeer image paths', async function () {
441
+ const post = {
442
+ data: {
443
+ html: '<div class="captioned-image-container"><figure><a class="image-link is-viewable-img image2" target="_blank" href="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 1456w" sizes="100vw"><img src="https://bucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/efgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg" width="1200" height="800.390625" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/efgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:683,&quot;width&quot;:1024,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:188889,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fefgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture></div></a><figcaption class="image-caption">My image</figcaption></figure></div><p>Hello</p>',
444
+ title: 'My Image Post'
445
+ }
446
+ };
447
+ const url = 'https://example.com';
448
+ const options = {
449
+ useFirstImage: false
450
+ };
451
+ const processed = await processContent(post, url, options);
452
+ assert.equal(processed.data.html, '<figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://substack-post-media.s3.amazonaws.com/public/images/efgh5678-fad6-49df-b659-b16976e1ce59_1024x683.jpeg" class="kg-image" alt loading="lazy"><figcaption>My image</figcaption></figure><p>Hello</p>');
453
+ });
454
+ it('Converts galleries', async function () {
455
+ const post = {
456
+ data: {
457
+ html: `<div dir="auto" class="body markup">
458
+ <p>Hello</p>
459
+ <figure class="frontend-components-ImageGallery-module__imageGallery--shoTe">
460
+ <div class="pencraft pc-display-flex pc-flexDirection-column pc-gap-8 pc-reset">
461
+ <div
462
+ class="pencraft pc-display-flex pc-gap-8 pc-reset frontend-components-ImageGallery-module__imageRow--RFMqP">
463
+ <picture>
464
+ <source type="image/webp"
465
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg 474w"
466
+ sizes="100vw"><img
467
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg"
468
+ sizes="100vw" alt="My alt text"
469
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg 474w"
470
+ width="474"
471
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
472
+ </picture>
473
+ <picture>
474
+ <source type="image/webp"
475
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg 474w"
476
+ sizes="100vw"><img
477
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg"
478
+ sizes="100vw" alt="My alt text"
479
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg 474w"
480
+ width="474"
481
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
482
+ </picture>
483
+ <picture>
484
+ <source type="image/webp"
485
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg 474w"
486
+ sizes="100vw"><img
487
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg"
488
+ sizes="100vw" alt="My alt text"
489
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg 474w"
490
+ width="474"
491
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
492
+ </picture>
493
+ </div>
494
+ <div
495
+ class="pencraft pc-display-flex pc-gap-8 pc-reset frontend-components-ImageGallery-module__imageRow--RFMqP">
496
+ <picture>
497
+ <source type="image/webp"
498
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg 474w"
499
+ sizes="100vw"><img
500
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg"
501
+ sizes="100vw" alt="My alt text"
502
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg 474w"
503
+ width="474"
504
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
505
+ </picture>
506
+ <picture>
507
+ <source type="image/webp"
508
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg 474w"
509
+ sizes="100vw"><img
510
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg"
511
+ sizes="100vw" alt="My alt text"
512
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg 474w"
513
+ width="474"
514
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
515
+ </picture>
516
+ <picture>
517
+ <source type="image/webp"
518
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg 474w"
519
+ sizes="100vw"><img
520
+ src="https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg"
521
+ sizes="100vw" alt="My alt text"
522
+ srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg 424w, https://substackcdn.com/image/fetch/w_474,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg 474w"
523
+ width="474"
524
+ class="frontend-components-responsive_img-module__img--Pgjj2 frontend-components-ImageGallery-module__image--g2yvp frontend-components-ImageGallery-module__small--Muz63 frontend-components-ImageGallery-module__zoom--eQKQQ pencraft pc-reset">
525
+ </picture>
526
+ </div>
527
+ <figcaption class="frontend-components-ImageGallery-module__imageCaption--JESLj">My caption</figcaption>
528
+ </div>
529
+ </figure>
530
+ <p>World</p>
531
+ </div>`,
532
+ title: 'My gallery post'
533
+ }
534
+ };
535
+ const url = 'https://example.com';
536
+ const options = {
537
+ useFirstImage: false
538
+ };
539
+ const processed = await processContent(post, url, options);
540
+ assert.ok(!processed.data.html.includes('<figure class="frontend-components-ImageGallery-module__imageGallery--shoTe">'));
541
+ assert.ok(processed.data.html.includes('<figure class="kg-card kg-gallery-card kg-width-wide kg-card-hascaption"><div class="kg-gallery-container"><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg" width="7008" height="4672" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg" width="4000" height="6000" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg" width="3600" height="2025" loading="lazy" alt></div></div><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg" width="8048" height="5368" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg" width="4032" height="3024" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg" width="9504" height="6336" loading="lazy" alt></div></div></div><figcaption>My caption</figcaption></figure>'));
542
+ });
543
+ it('Converts embeded galleries', async function () {
544
+ const post = {
545
+ data: {
546
+ html: `<p>Before</p><div class="image-gallery-embed" data-attrs="{&quot;gallery&quot;:{&quot;images&quot;:[{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg&quot;},{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg&quot;},{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg&quot;},{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg&quot;},{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg&quot;},{&quot;type&quot;:&quot;image/jpeg&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg&quot;}],&quot;caption&quot;:&quot;My caption&quot;,&quot;alt&quot;:&quot;My alt text&quot;,&quot;staticGalleryImage&quot;:{&quot;type&quot;:&quot;image/png&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abcd1240-6dbe-4463-8494-4171b6c05b3a_1456x964.png&quot;}},&quot;isEditorNode&quot;:true}"></div><p>After</p>`,
547
+ title: 'My gallery embed post'
548
+ }
549
+ };
550
+ const url = 'https://example.com';
551
+ const options = {
552
+ useFirstImage: false
553
+ };
554
+ const processed = await processContent(post, url, options);
555
+ assert.equal(processed.data.html, '<p>Before</p><figure class="kg-card kg-gallery-card kg-width-wide kg-card-hascaption"><div class="kg-gallery-container"><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1234-2021-4a0e-b815-90b9ae74d5a0_7008x4672.jpeg" width="7008" height="4672" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abc1235-6563-4aa8-bde7-e5b3ddcd2729_4000x6000.jpeg" width="4000" height="6000" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1236-c233-4e0d-87c0-6a0d128bc07d_3600x2025.jpeg" width="3600" height="2025" loading="lazy" alt></div></div><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1237-2469-4db8-874b-36ad8733bd97_8048x5368.jpeg" width="8048" height="5368" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1238-2236-442c-9d9a-b0387c946a18_4032x3024.jpeg" width="4032" height="3024" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://substack-post-media.s3.amazonaws.com/public/images/abcd1239-e31d-401c-b94f-ceae393d95f6_9504x6336.jpeg" width="9504" height="6336" loading="lazy" alt></div></div></div><figcaption>My caption</figcaption></figure><p>After</p>');
556
+ });
557
+ it('Includes supplied content in tweet blockquote', async function () {
558
+ const post = {
559
+ data: {
560
+ html: `<p>Hello</p><div class="tweet">
561
+ <a class="tweet-link-top" href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890" target="_blank">
562
+ <div class="tweet-header">
563
+ <img class="tweet-header-avatar" src="https://example.com/image/twitter_name/w_96/example.jpg" alt="Twitter avatar for @example" loading="lazy">
564
+ <div class="tweet-header-text">
565
+ <span class="tweet-author-name">example </span><span class="tweet-author-handle">@example</span>
566
+ </div>
567
+ </div>
568
+ <div class="tweet-text">This is the tweet text</div>
569
+ </a>
570
+ <a class="tweet-link-bottom" href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890" target="_blank">
571
+ <div class="tweet-footer">
572
+ <span class="tweet-date">1:26 AM ∙ Apr 28, 2022</span>
573
+ <hr>
574
+ <div class="tweet-ufi">
575
+ <span href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890/likes" class="likes">
576
+ <span class="like-count">50</span>
577
+ Likes
578
+ </span>
579
+ <span href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890/retweets"
580
+ class="retweets">
581
+ <span class="rt-count">35</span>
582
+ Retweets
583
+ </span>
584
+ </div>
585
+ </div>
586
+ </a>
587
+ </div><p>World</p>`,
588
+ title: 'My tweet post'
589
+ }
590
+ };
591
+ const url = 'https://example.com';
592
+ const options = {
593
+ useFirstImage: false
594
+ };
595
+ const processed = await processContent(post, url, options);
596
+ assert.equal(processed.data.html, '<p>Hello</p><figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet"><p lang="en" dir="ltr">This is the tweet text</p> — example (@example) <a href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890"> 1:26 AM ∙ Apr 28, 2022 </a><a href="https://twitter.com/example/status/123456"></a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></figure><p>World</p>');
597
+ });
598
+ it('Includes supplied content in tweet blockquote derp', async function () {
599
+ const post = {
600
+ data: {
601
+ html: `<p>Hello</p><div class="tweet">
602
+ <a class="tweet-link-top" href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890" target="_blank">
603
+ <div class="tweet-header">
604
+ <img class="tweet-header-avatar" src="https://example.com/image/twitter_name/w_96/example.jpg" alt="Twitter avatar for @example" loading="lazy">
605
+ <div class="tweet-header-text">
606
+ <span class="tweet-author-name">example </span>
607
+ </div>
608
+ </div>
609
+ </a>
610
+ <a class="tweet-link-bottom" href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890" target="_blank">
611
+ <div class="tweet-footer">
612
+ <span class="tweet-date">1:26 AM ∙ Apr 28, 2022</span>
613
+ <hr>
614
+ <div class="tweet-ufi">
615
+ <span href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890/likes" class="likes">
616
+ <span class="like-count">50</span>
617
+ Likes
618
+ </span>
619
+ <span href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890/retweets"
620
+ class="retweets">
621
+ <span class="rt-count">35</span>
622
+ Retweets
623
+ </span>
624
+ </div>
625
+ </div>
626
+ </a>
627
+ </div><p>World</p>`,
628
+ title: 'My tweet post'
629
+ }
630
+ };
631
+ const url = 'https://example.com';
632
+ const options = {
633
+ useFirstImage: false
634
+ };
635
+ const processed = await processContent(post, url, options);
636
+ assert.equal(processed.data.html, '<p>Hello</p><figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet">— example <a href="https://twitter.com/example/status/123456?s=21&amp;t=abcd7890"> 1:26 AM ∙ Apr 28, 2022 </a><a href="https://twitter.com/example/status/123456"></a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></figure><p>World</p>');
637
+ });
638
+ it('Handle post embeds', async function () {
639
+ const post = {
640
+ data: {
641
+ html: `<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.</p>
642
+ <div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1234abcd-99c9-4df9-965a-0dd00e91708a&quot;,&quot;caption&quot;:&quot;The caption&quot;,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;The Title&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:8097277,&quot;name&quot;:&quot;Author Name&quot;,&quot;bio&quot;:&quot;Author bio&quot;,&quot;photo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcd1234-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F27491f91-70a2-4e95-a648-1fd9d4795106_400x400.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-10-07T01:06:09.384Z&quot;,&quot;cover_image&quot;:&quot;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&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.exampe.com/p/the-link&quot;,&quot;section_name&quot;:null,&quot;id&quot;:149897757,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_name&quot;:&quot;Pub Name&quot;,&quot;publication_logo_url&quot;:&quot;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&quot;,&quot;belowTheFold&quot;:true}"></div>
643
+ <p>Dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
644
+ `,
645
+ title: 'My embed post'
646
+ }
647
+ };
648
+ const url = 'https://example.com';
649
+ const options = {
650
+ useFirstImage: false
651
+ };
652
+ const processed = await processContent(post, url, options);
653
+ assert.ok(!processed.data.html.includes('<div class="digest-post-embed"'));
654
+ assert.ok(processed.data.html.includes('<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://substack-post-media.s3.amazonaws.com/public/images/abcd1234-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://substack-post-media.s3.amazonaws.com/public/images/abcd1234-1a48-4dd3-ac53-06900dc9a92d_1200x800.jpeg" alt></div></a></figure>'));
655
+ });
656
+ it('Skips post embed if JSON is invalid', async function () {
657
+ const post = {
658
+ data: {
659
+ html: `<p>Lorem ipsum.</p><div class="digest-post-embed" data-attrs="{&quot;no"></div><p>Dolore magna.</p>`,
660
+ title: 'My embed post'
661
+ }
662
+ };
663
+ const url = 'https://example.com';
664
+ const options = {
665
+ useFirstImage: false
666
+ };
667
+ const processed = await processContent(post, url, options);
668
+ assert.equal(processed.data.html, '<p>Lorem ipsum.</p><div class="digest-post-embed" data-attrs="{&quot;no"></div><p>Dolore magna.</p>');
669
+ });
670
+ it('Handle files cards', async function () {
671
+ const post = {
672
+ data: {
673
+ html: `<p>Lorem ipsum.</p><div class="file-embed-wrapper" data-component-name="FileToDOM">
674
+ <div class="file-embed-container-reader">
675
+ <div class="file-embed-container-top">
676
+ <image class="file-embed-thumbnail-default" src="https://substack.com/img/attachment_icon.svg"></image>
677
+ <div class="file-embed-details">
678
+ <div class="file-embed-details-h1">Information Document</div>
679
+ <div class="file-embed-details-h2">31.6KB ∙ PDF file</div>
680
+ </div><a class="file-embed-button wide" href="https://example.com/api/v1/file/1234-abcd.pdf"><span class="file-embed-button-text">Download</span></a>
681
+ </div><a class="file-embed-button narrow" href="https://example.com/api/v1/file/1234-abcd.pdf"><span class="file-embed-button-text">Download</span></a>
682
+ </div>
683
+ </div><p>Dolore magna.</p>`,
684
+ title: 'My embed post'
685
+ }
686
+ };
687
+ const url = 'https://example.com';
688
+ const options = {
689
+ useFirstImage: false
690
+ };
691
+ const processed = await processContent(post, url, options);
692
+ assert.ok(processed.data.html.includes('<a class="kg-file-card-container" href="https://example.com/api/v1/file/1234-abcd.pdf" title="Download" download>'));
693
+ assert.ok(!processed.data.html.includes('<a class="file-embed-button wide" href="https://example.com/api/v1/file/1234-abcd.pdf"><span class="file-embed-button-text">Download</span></a>'));
694
+ });
695
+ it('Handle latex cards', async function () {
696
+ const post = {
697
+ data: {
698
+ html: `<p>Lorem ipsum.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;ABC_{\\text{Terminal}}ABCTerminal&quot;,&quot;id&quot;:&quot;ACASDWEARE&quot;}" data-component-name="LatexBlockToDOM"></div><p>Dolore magna.</p>`,
699
+ title: 'My embed post'
700
+ }
701
+ };
702
+ const url = 'https://example.com';
703
+ const options = {
704
+ useFirstImage: false
705
+ };
706
+ const processed = await processContent(post, url, options);
707
+ assert.equal(processed.data.html, '<p>Lorem ipsum.</p><!--kg-card-begin: html--><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;ABC_{\\text{Terminal}}ABCTerminal&quot;,&quot;id&quot;:&quot;ACASDWEARE&quot;}" data-component-name="LatexBlockToDOM"></div><!--kg-card-end: html--><p>Dolore magna.</p>');
708
+ });
709
+ it('Escapes mention names', async function () {
710
+ const post = {
711
+ data: {
712
+ html: '<p><span class="mention-wrap" data-attrs="{&quot;name&quot;:&quot;&lt;b&gt;Bold&lt;/b&gt;&quot;,&quot;id&quot;:1}"></span></p>'
713
+ }
714
+ };
715
+ const processed = await processContent(post, 'https://example.com', { useFirstImage: false });
716
+ assert.equal(processed.data.html, '<p><a href="https://open.substack.com/users/1">&lt;b&gt;Bold&lt;/b&gt;</a></p>');
717
+ });
718
+ it('Can convert mention spans to links', async function () {
719
+ const post = {
720
+ data: {
721
+ html: '<p>Thanks <span class="mention-wrap" data-attrs="{&quot;name&quot;:&quot;Test User&quot;,&quot;id&quot;:12345678,&quot;type&quot;:&quot;user&quot;,&quot;url&quot;:null,&quot;photo_url&quot;:&quot;https://example.com/photo.png&quot;,&quot;uuid&quot;:&quot;00000000-0000-0000-0000-000000000000&quot;}" data-component-name="MentionToDOM"></span> for the tip!</p>'
722
+ }
723
+ };
724
+ const url = 'https://example.com';
725
+ const options = {
726
+ useFirstImage: false
727
+ };
728
+ const processed = await processContent(post, url, options);
729
+ assert.equal(processed.data.html, '<p>Thanks <a href="https://open.substack.com/users/12345678">Test User</a> for the tip!</p>');
730
+ });
731
+ });
732
+ describe('Image handling', function () {
733
+ it('Handle images that link to themselves', async function () {
734
+ let thehtml = `<a class="image-link is-viewable-img image2" target="_blank" href="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png"><div class="image2-inset"><picture> <source type="image/webp" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1456w" sizes="100vw"><img src="https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" width="968" height="813" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png&quot;,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:813,&quot;width&quot;:968,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;internalRedirect&quot;:null}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1456w" sizes="100vw"></picture></div></a>`;
735
+ const post = {
736
+ data: {
737
+ html: thehtml,
738
+ title: 'My Image Post'
739
+ }
740
+ };
741
+ const processed = await processContent(post, 'https://example.com', {});
742
+ assert.equal(processed.data.html, '<figure class="kg-card kg-image-card"><img src="https://substack-post-media.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" class="kg-image" alt loading="lazy"></figure>');
743
+ });
744
+ it('Handle images that link to themselves with no srcset', async function () {
745
+ let thehtml = `<a class="image-link is-viewable-img image2" target="_blank" href="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png"><div class="image2-inset"><picture> <source type="image/webp" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1456w" sizes="100vw"><img src="https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" width="968" height="813" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png&quot;,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:813,&quot;width&quot;:968,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;internalRedirect&quot;:null}" class="sizing-normal" alt="" sizes="100vw"></picture></div></a>`;
746
+ const post = {
747
+ data: {
748
+ html: thehtml,
749
+ title: 'My Image Post'
750
+ }
751
+ };
752
+ const processed = await processContent(post, 'https://example.com', {});
753
+ assert.equal(processed.data.html, '<figure class="kg-card kg-image-card"><img src="https://substack-post-media.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" class="kg-image" alt loading="lazy"></figure>');
754
+ });
755
+ it('Handle images with external links', async function () {
756
+ let thehtml = `<a class="image-link is-viewable-img image2" target="_blank" href="https://ghost.org"><div class="image2-inset"><picture> <source type="image/webp" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1456w" sizes="100vw"><img src="https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" width="968" height="813" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png&quot;,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:813,&quot;width&quot;:968,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;internalRedirect&quot;:null}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 424w, https://substackcdn.com/image/fetch/w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 848w, https://substackcdn.com/image/fetch/w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1272w, https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png 1456w" sizes="100vw"></picture></div></a>`;
757
+ const post = {
758
+ data: {
759
+ html: thehtml,
760
+ title: 'My Image Post'
761
+ }
762
+ };
763
+ const processed = await processContent(post, 'https://example.com', {});
764
+ assert.equal(processed.data.html, '<figure class="kg-card kg-image-card"><a href="https://ghost.org"><img src="https://substack-post-media.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png" class="kg-image" alt loading="lazy"></a></figure>');
765
+ });
766
+ it('Handles images with no src attribute', async function () {
767
+ let thehtml = `<p>Hello</p><a class="image-link is-viewable-img image2" target="_blank" href="https://ghost.org"><div class="image2-inset"><picture> <source type="image/webp"><img class="sizing-normal" alt=""></picture></div></a>`;
768
+ const post = {
769
+ data: {
770
+ html: thehtml,
771
+ title: 'My Image Post'
772
+ }
773
+ };
774
+ const processed = await processContent(post, 'https://example.com', {});
775
+ assert.equal(processed.data.html, '<p>Hello</p>');
776
+ });
777
+ it('Can process Instagram embeds', async () => {
778
+ let theHtml = `<div class="instagram" data-attrs="{&quot;instagram_id&quot;:&quot;QWERTYNgZO8&quot;,&quot;title&quot;:&quot;A post shared by Example User (@exampleuser)&quot;,&quot;author_name&quot;:&quot;exampleuser&quot;,&quot;thumbnail_url&quot;:&quot;https://scontent.cdninstagram.com/v/t01.2345-67/e01/p480x480/125269447_123456789439748_5866997504257834626_n.jpg?_nc_ht=scontent.cdninstagram.com&amp;_nc_cat=100&amp;_nc_ohc=ABCDKvvBbVMAX-g_pZ8&amp;tp=1&amp;oh=c4e4faca2360d3c909134133edddccd6&amp;oe=60138323&quot;,&quot;timestamp&quot;:null,&quot;belowTheFold&quot;:false}"><div class="instagram-top-bar"><a class="instagram-author-name" href="https://instagram.com/exampleuser" target="_blank">exampleuser</a></div><a class="instagram-image" href="https://instagram.com/p/QWERTYNgZO8" target="_blank"><img src="https://scontent.cdninstagram.com/v/t01.2345-67/e01/p480x480/125269447_123456789439748_5866997504257834626_n.jpg?_nc_ht=scontent.cdninstagram.com&amp;_nc_cat=100&amp;_nc_ohc=ABCDKvvBbVMAX-g_pZ8&amp;tp=1&amp;oh=c4e4faca2360d3c909134133edddccd6&amp;oe=60138323"></a><div class="instagram-bottom-bar"><div class="instagram-title">A post shared by Example User (<a href="https://instagram.com/exampleuser" target="_blank">@exampleuser</a>)</div></div></div>`;
779
+ const post = {
780
+ data: {
781
+ html: theHtml,
782
+ title: 'My Instagram Post'
783
+ }
784
+ };
785
+ const processed = await processContent(post, 'https://example.com', {});
786
+ assert.equal(processed.data.html, '<figure class="instagram"><iframe class="instagram-media instagram-media-rendered" id="instagram-embed-0" allowtransparency="true" allowfullscreen="true" frameborder="0" height="968" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="background: white; max-width: 658px; width: calc(100% - 2px); border-radius: 3px; border: 1px solid rgb(219, 219, 219); box-shadow: none; display: block; margin: 0px 0px 12px; min-width: 326px; padding: 0px;" src="https://instagram.com/p/QWERTYNgZO8/embed/captioned/"></iframe><script async src="//www.instagram.com/embed.js"></script></figure>');
787
+ });
788
+ it('Can get dimensions from images', () => {
789
+ const url1 = 'https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png';
790
+ const url2 = 'https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png';
791
+ const url3 = 'https://bucketeer-abcdabcd-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/12345678-2415-4bdd-8878-bb841f9ca9d4_968x813.png?lorem=ipsum&dolor-simet12345.fake';
792
+ const d1 = getImageDimensions(url1);
793
+ assert.deepEqual(d1, { width: 968, height: 813 });
794
+ const d2 = getImageDimensions(url2);
795
+ assert.deepEqual(d2, { width: 968, height: 813 });
796
+ const d3 = getImageDimensions(url3);
797
+ assert.deepEqual(d3, { width: 968, height: 813 });
798
+ });
799
+ it('Can upscale images and remove crop', async () => {
800
+ let originalSrc = 'https://substackcdn.com/image/fetch/w_1200,h_600,c_fill,f_jpg,q_auto:good,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4b3aa9-abcd-efgh-1234-1234567891234_4368x2151.png';
801
+ let larger = largeImageUrl(originalSrc);
802
+ assert.equal(larger, 'https://substack-post-media.s3.amazonaws.com/public/images/3c4b3aa9-abcd-efgh-1234-1234567891234_4368x2151.png');
803
+ });
804
+ it("Skips upscale images and remove crop if image doesn't need it", async () => {
805
+ let originalSrc = 'https://substack-post-media.s3.amazonaws.com/public/images/db9ef48f-933f-48f0-a6c4-1234567891234_4368x2151.png';
806
+ let larger = largeImageUrl(originalSrc);
807
+ assert.equal(larger, 'https://substack-post-media.s3.amazonaws.com/public/images/db9ef48f-933f-48f0-a6c4-1234567891234_4368x2151.png');
808
+ });
809
+ });
810
+ describe('Converting less common Substack HTML', function () {
811
+ const convert = (html, options = {}) => processContent({ data: { html, title: 'T', slug: 's' } }, 'https://example.com', options).data;
812
+ it('uses an image that is the first element as the feature image', function () {
813
+ assert.deepEqual(convert('<img src="https://example.com/a.jpg"><p>Text</p>', { useFirstImage: true }), {
814
+ html: '<p>Text</p>',
815
+ title: 'T',
816
+ slug: 's',
817
+ feature_image: 'https://example.com/a.jpg'
818
+ });
819
+ });
820
+ it('keeps a first image without a usable source in the content', function () {
821
+ for (const src of ['', 'https://substackcdn.com/image/fetch/']) {
822
+ const data = convert(`<img src="${src}"><p>Text</p>`, { useFirstImage: true });
823
+ assert.equal(data.html, '<img src><p>Text</p>');
824
+ assert.equal(data.feature_image, undefined);
825
+ }
826
+ });
827
+ it('removes video and poll embeds, comments and styles inside links', function () {
828
+ assert.equal(convert('<p>A</p><div class="native-video-embed">v</div><div class="poll-embed">p</div><div class="comment">c</div><a href="https://x.com"><style>.a{}</style>Link</a>').html, '<p>A</p><a href="https://x.com">Link</a>');
829
+ });
830
+ it('converts image3 blocks to image cards', function () {
831
+ assert.equal(convert(`<div class="image3" data-attrs='{"src":"https://substackcdn.com/image/fetch/w_1456/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa.png","title":"Title"}'></div>`).html, '<figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://substack-post-media.s3.amazonaws.com/public/images/a.png" class="kg-image" alt="Title" loading="lazy"><figcaption>Title</figcaption></figure>');
832
+ });
833
+ it('converts a tweet with only its text', function () {
834
+ assert.equal(convert('<div class="tweet"><a href="https://twitter.com/x/status/1?s=20"></a><div class="tweet-text">Hello</div></div>').html, '<figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet"><p lang="en" dir="ltr">Hello</p> — <a href="https://twitter.com/x/status/1"></a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></figure>');
835
+ });
836
+ it('sizes gallery images without dimensions in their names as 100×100', function () {
837
+ const gallery = convert(`<div class="image-gallery-embed" data-attrs='{"gallery":{"images":[{"src":"https://example.com/a.jpg"},{"src":"https://example.com/b_200x100.jpg"}],"caption":"Cap"}}'></div>`).html;
838
+ assert.ok(gallery.includes('<img src="https://example.com/a.jpg" width="100" height="100"'));
839
+ assert.ok(gallery.includes('<img src="https://example.com/b_200x100.jpg" width="200" height="100"'));
840
+ assert.ok(gallery.includes('<figcaption>Cap</figcaption>'));
841
+ const moduleGallery = convert('<div class="ImageGallery-module__imageGallery"><div class="ImageGallery-module__imageRow"><picture><img src="https://example.com/a.jpg"></picture></div></div>').html;
842
+ assert.ok(moduleGallery.includes('<img src="https://example.com/a.jpg" width="100" height="100"'));
843
+ assert.ok(!moduleGallery.includes('figcaption'));
844
+ });
845
+ it('converts images without alt text, links or captions', function () {
846
+ const expected = '<figure class="kg-card kg-image-card"><img src="https://example.com/a.jpg" class="kg-image" alt loading="lazy"></figure>';
847
+ assert.equal(convert('<div class="captioned-image-container"><img src="https://example.com/a.jpg"></div>').html, expected);
848
+ assert.equal(convert('<a class="image-link" href="https://ghost.org"><img src="https://example.com/a.jpg"></a>').html, expected);
849
+ });
850
+ it('converts a file embed without a title or size', function () {
851
+ const html = convert('<div class="file-embed-wrapper"><a class="file-embed-button" href="https://example.com/f.pdf">Download</a></div>').html;
852
+ assert.ok(html.includes('href="https://example.com/f.pdf"'));
853
+ assert.ok(html.includes('<div class="kg-file-card-filesize">0 Byte</div>'));
854
+ });
855
+ it('leaves mentions with invalid data and embedded posts without data unchanged', function () {
856
+ const mention = '<p><span class="mention-wrap" data-attrs="not json">@x</span></p>';
857
+ assert.equal(convert(mention).html, mention);
858
+ assert.equal(convert('<div class="embedded-post-wrap">x</div>').html, '<div class="embedded-post-wrap">x</div>');
859
+ });
860
+ it('converts a digest post embed without bylines', function () {
861
+ assert.equal(convert(`<div class="digest-post-embed" data-attrs='{"canonical_url":"https://a.com/p/x","title":"T","caption":"C","cover_image":"https://a.com/c.jpg","publication_logo_url":"https://a.com/l.png","publishedBylines":[],"publication_name":"Pub"}'></div>`).html, '<figure class="kg-card kg-bookmark-card"><a class="kg-bookmark-container" href="https://a.com/p/x"><div class="kg-bookmark-content"><div class="kg-bookmark-title">T</div><div class="kg-bookmark-description">C</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="https://a.com/l.png" alt><span class="kg-bookmark-author">Pub</span></div></div><div class="kg-bookmark-thumbnail"><img src="https://a.com/c.jpg" alt></div></a></figure>');
862
+ });
863
+ it('points subscribe buttons at Portal by default', function () {
864
+ assert.equal(convert('<p class="button-wrapper"><a class="button primary" href="https://example.com/subscribe?"><span>Subscribe</span></a></p>').html, '<div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent">Subscribe</a></div>');
865
+ });
866
+ it('wraps nested lists in an HTML card', function () {
867
+ assert.equal(convert('<ul><li>One<ul><li>Two</li></ul></li></ul>').html, '<!--kg-card-begin: html--><ul><li>One<ul><li>Two</li></ul></li></ul><!--kg-card-end: html-->');
868
+ });
869
+ it('replaces subscribe links and forms, skipping anchors without a link', function () {
870
+ const options = { subscribeLink: '#/portal/signup' };
871
+ assert.equal(convert('<p><a name="x">Anchor</a> <a href="/subscribe">Sub</a></p>', options).html, '<p><a name="x">Anchor</a> <a href="#/portal/signup">Sub</a></p>');
872
+ assert.equal(convert('<div class="subscription-widget-wrap"><form><input type="email"></form></div>', options).html, '<div class="kg-card kg-button-card kg-align-center"><a href="#/portal/signup" class="kg-btn kg-btn-accent"></a></div>');
873
+ });
874
+ it('embeds Instagram links without a trailing slash, and leaves embeds without a link', function () {
875
+ assert.ok(convert('<div class="instagram"><a class="instagram-image" href="https://instagram.com/p/ABC/?utm=1"></a></div>').html.includes('src="https://instagram.com/p/ABC/embed/captioned/"'));
876
+ assert.equal(convert('<div class="instagram"><p>x</p></div>').html, '<div class="instagram"><p>x</p></div>');
877
+ });
878
+ it('returns no dimensions for an image name without a size', function () {
879
+ assert.equal(getImageDimensions('https://example.com/a.jpg'), false);
880
+ });
881
+ });
882
+ //# sourceMappingURL=process.test.js.map