@tryghost/mg-substack 0.3.7 → 0.3.8

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.
Files changed (2) hide show
  1. package/lib/process.js +55 -11
  2. package/package.json +6 -5
package/lib/process.js CHANGED
@@ -4,10 +4,13 @@ import url from 'node:url';
4
4
  import $ from 'cheerio';
5
5
  import errors from '@tryghost/errors';
6
6
  import SimpleDom from 'simple-dom';
7
+ import imageCard from '@tryghost/kg-default-cards/lib/cards/image.js';
7
8
  import audioCard from '@tryghost/kg-default-cards/lib/cards/audio.js';
8
9
  import {decode} from 'html-entities';
10
+ import {parseSrcset} from 'srcset';
9
11
  import {_base as debugFactory} from '@tryghost/debug';
10
12
 
13
+ const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
11
14
  const debug = debugFactory('migrate:substack:process');
12
15
 
13
16
  const getFiles = async (filePath) => {
@@ -44,6 +47,26 @@ const getUnsizedImageName = (str) => {
44
47
  }
45
48
  };
46
49
 
50
+ const largestSrc = ($imageElem) => {
51
+ const src = $imageElem.attr('src');
52
+ const srcset = $imageElem.attr('srcset') || false;
53
+
54
+ let srcToUse = src;
55
+
56
+ if (srcset) {
57
+ const parsedSrcset = parseSrcset(srcset);
58
+
59
+ if (parsedSrcset && parsedSrcset.length > 0) {
60
+ srcToUse = parsedSrcset[0].url;
61
+ }
62
+ }
63
+
64
+ // Remove the width from the image URL
65
+ srcToUse = srcToUse.replace(/w_[0-9]{2,4},c_limit,/, '');
66
+
67
+ return srcToUse;
68
+ };
69
+
47
70
  const processContent = (post, siteUrl, options) => {
48
71
  const {substackPodcastURL, metaData} = post;
49
72
  const responseData = metaData?.responseData || {};
@@ -136,23 +159,44 @@ const processContent = (post, siteUrl, options) => {
136
159
  });
137
160
 
138
161
  $html('.captioned-image-container').each((i, div) => {
139
- const hasCaption = $(div).find('figcaption').length;
162
+ const imgAlt = $(div).find('img[alt]').attr('alt') || '';
163
+ const linkHref = $(div).find('a.image-link').attr('href') || false;
164
+ const imgCaption = $(div).find('figcaption').html() || false;
165
+ const imageSrc = largestSrc($(div).find('img'));
140
166
 
141
- $(div).find('a').removeAttr('class');
167
+ let cardOpts = {
168
+ env: {dom: new SimpleDom.Document()},
169
+ payload: {
170
+ src: imageSrc,
171
+ alt: imgAlt,
172
+ caption: imgCaption,
173
+ href: linkHref
174
+ }
175
+ };
176
+
177
+ $(div).replaceWith(serializer.serialize(imageCard.render(cardOpts)));
178
+ });
142
179
 
143
- $(div).find('img').removeAttr('data-attrs');
144
- $(div).find('img').removeAttr('srcset');
145
- $(div).find('img').removeAttr('width');
146
- $(div).find('img').removeAttr('height');
147
- $(div).find('img').addClass('kg-image');
180
+ $html('.image-link').each((i, anchor) => {
181
+ const imgAlt = $(anchor).find('img[alt]').attr('alt') || '';
182
+ const linkHref = $(anchor).attr('href');
183
+ const imageSrc = largestSrc($(anchor).find('img'));
148
184
 
149
- $(div).find('figure').addClass('kg-card kg-image-card');
185
+ let cardOpts = {
186
+ env: {dom: new SimpleDom.Document()},
187
+ payload: {
188
+ src: imageSrc,
189
+ alt: imgAlt
190
+ }
191
+ };
150
192
 
151
- if (hasCaption) {
152
- $(div).find('figure').addClass('kg-card-hascaption');
193
+ // If the anchor links to the image itself
194
+ const splitRegexp = /public\/images\/|public%2Fimages%2F/;
195
+ if (imageSrc.split(splitRegexp)[1] !== linkHref.split(splitRegexp)[1]) {
196
+ cardOpts.payload.href = linkHref;
153
197
  }
154
198
 
155
- $(div).replaceWith($(div).find('figure'));
199
+ $(anchor).replaceWith(serializer.serialize(imageCard.render(cardOpts)));
156
200
  });
157
201
 
158
202
  $html('a > style').each((i, style) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-substack",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-substack",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "access": "public"
21
21
  },
22
22
  "devDependencies": {
23
- "eslint": "8.35.0",
23
+ "eslint": "8.36.0",
24
24
  "jest": "29.5.0",
25
25
  "jest-extended": "3.2.4"
26
26
  },
@@ -28,12 +28,13 @@
28
28
  "@tryghost/debug": "0.1.21",
29
29
  "@tryghost/errors": "1.2.20",
30
30
  "@tryghost/kg-default-cards": "6.0.4",
31
- "@tryghost/mg-fs-utils": "^0.12.5",
31
+ "@tryghost/mg-fs-utils": "^0.12.6",
32
32
  "@tryghost/string": "0.2.2",
33
33
  "date-fns": "2.29.3",
34
34
  "html-entities": "2.3.3",
35
35
  "lodash": "4.17.21",
36
- "simple-dom": "1.4.0"
36
+ "simple-dom": "1.4.0",
37
+ "srcset": "5.0.0"
37
38
  },
38
- "gitHead": "52a63bfa94530ec88efba202ad9581bcd4a9790c"
39
+ "gitHead": "a5514de6f98983d97e392f4c9ef89d8715b5273a"
39
40
  }