@tryghost/mg-substack 0.4.2 → 0.4.3

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.
package/lib/mapper.js CHANGED
@@ -3,6 +3,7 @@ import _ from 'lodash';
3
3
  import errors from '@tryghost/errors';
4
4
  import {slugify} from '@tryghost/string';
5
5
  import {_base as debugFactory} from '@tryghost/debug';
6
+ import he from 'he';
6
7
 
7
8
  const debug = debugFactory('migrate:substack:mapper');
8
9
 
@@ -28,7 +29,7 @@ const mapConfig = (data, options) => {
28
29
  updated_at: data.post_date || dateNow,
29
30
  created_at: data.post_date || dateNow,
30
31
  title: data.title || slug,
31
- custom_excerpt: data.subtitle,
32
+ custom_excerpt: (data.subtitle) ? he.decode(data.subtitle) : null,
32
33
  type: contentType,
33
34
  html: data.html || null,
34
35
  status: data.is_published.toLowerCase() === `true` ? 'published' : 'draft',
package/lib/process.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import {promises as fs} from 'node:fs';
2
- import {join} from 'node:path';
2
+ import {join, basename} from 'node:path';
3
3
  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
7
  import imageCard from '@tryghost/kg-default-cards/lib/cards/image.js';
8
8
  import audioCard from '@tryghost/kg-default-cards/lib/cards/audio.js';
9
+ import galleryCard from '@tryghost/kg-default-cards/lib/cards/gallery.js';
9
10
  import {decode} from 'html-entities';
10
11
  import {parseSrcset} from 'srcset';
11
12
  import {_base as debugFactory} from '@tryghost/debug';
@@ -51,6 +52,20 @@ const getUnsizedImageName = (str) => {
51
52
  }
52
53
  };
53
54
 
55
+ const getImageDimensions = (str) => {
56
+ const imageSizeRegexp = /_([0-9]{2,5})x([0-9]{2,5}).[a-zA-Z]{2,4}/;
57
+ const matches = str.match(imageSizeRegexp);
58
+
59
+ if (matches && matches[1] && matches[2]) {
60
+ return {
61
+ width: parseInt(matches[1]),
62
+ height: parseInt(matches[2])
63
+ };
64
+ } else {
65
+ return false;
66
+ }
67
+ };
68
+
54
69
  const largestSrc = ($imageElem) => {
55
70
  const src = $imageElem.attr('src') ?? false;
56
71
  const srcset = $imageElem.attr('srcset') ?? false;
@@ -119,6 +134,22 @@ const processContent = (post, siteUrl, options) => {
119
134
  }
120
135
  });
121
136
 
137
+ $html('.image3').each((i, el) => {
138
+ const attrs = $(el).attr('data-attrs');
139
+ const attrsObj = JSON.parse(attrs);
140
+
141
+ let cardOpts = {
142
+ env: {dom: new SimpleDom.Document()},
143
+ payload: {
144
+ src: attrsObj.src,
145
+ alt: attrsObj.title,
146
+ caption: attrsObj.title
147
+ }
148
+ };
149
+
150
+ $(el).replaceWith(serializer.serialize(imageCard.render(cardOpts)));
151
+ });
152
+
122
153
  // We use the `'og:image` as the feature image. If the first item in the content is an image and is the same as the `og:image`, remove it
123
154
  if (post.data?.og_image) {
124
155
  if (useMetaImage) {
@@ -137,6 +168,8 @@ const processContent = (post, siteUrl, options) => {
137
168
  let ogImgSrc = post.data.og_image;
138
169
  let unsizedOgSrc = getUnsizedImageName(ogImgSrc);
139
170
 
171
+ // console.log({unsizedFirstSrc, unsizedOgSrc});
172
+
140
173
  if (unsizedFirstSrc === unsizedOgSrc) {
141
174
  if ($(firstElement).find('figcaption').length) {
142
175
  post.data.feature_image_caption = $(firstElement).find('figcaption').html();
@@ -198,8 +231,16 @@ const processContent = (post, siteUrl, options) => {
198
231
  }
199
232
  src = url.format(parsed, {search: false});
200
233
 
234
+ let tweetText = $(el).find('.tweet-text').html().replace(/(?:\r\n|\r|\n)/g, '<br>');
235
+ let tweetAuthorName = $(el).find('.tweet-author-name').html().trim();
236
+ let tweetAuthorHandle = $(el).find('.tweet-author-handle').html().trim();
237
+ let tweetDateTime = $(el).find('.tweet-date').text().trim();
238
+ let tweetURL = $(el).find('.tweet-link-top').attr('href');
239
+
240
+ let theHtml = `<p lang="en" dir="ltr">${tweetText}</p>&mdash; ${tweetAuthorName} (${tweetAuthorHandle}) <a href="${tweetURL}">${tweetDateTime}</a>`;
241
+
201
242
  let $figure = $('<figure class="kg-card kg-embed-card"></figure>');
202
- let $blockquote = $('<blockquote class="twitter-tweet"></blockquote>');
243
+ let $blockquote = $(`<blockquote class="twitter-tweet">${theHtml}</blockquote>`);
203
244
  let $anchor = $(`<a href="${src}"></a>`);
204
245
  let $script = $('<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>');
205
246
 
@@ -211,6 +252,82 @@ const processContent = (post, siteUrl, options) => {
211
252
  $(el).replaceWith($figure);
212
253
  });
213
254
 
255
+ $html('.image-gallery-embed').each((i, el) => {
256
+ const attrs = $(el).attr('data-attrs');
257
+ const attrsObj = JSON.parse(attrs);
258
+
259
+ let items = [];
260
+
261
+ attrsObj.gallery.images.forEach((item) => {
262
+ const dimensions = getImageDimensions(item.src);
263
+
264
+ items.push({
265
+ fileName: basename(item.src),
266
+ src: item.src,
267
+ width: (dimensions) ? dimensions.width : '100',
268
+ height: (dimensions) ? dimensions.height : '100'
269
+ });
270
+ });
271
+
272
+ items = items.map((item, index) => {
273
+ return {
274
+ ...item,
275
+ row: Math.floor(index / 3)
276
+ };
277
+ });
278
+
279
+ let cardOpts = {
280
+ env: {dom: new SimpleDom.Document()},
281
+ payload: {
282
+ images: items,
283
+ caption: attrsObj.gallery.caption
284
+ }
285
+ };
286
+
287
+ $(el).replaceWith(serializer.serialize(galleryCard.render(cardOpts)));
288
+ });
289
+
290
+ $html('[class*="ImageGallery-module__imageGallery"]').each((i, el) => {
291
+ const $row = $(el).find('[class*="ImageGallery-module__imageRow"]');
292
+ const caption = $(el).find('figcaption').html();
293
+
294
+ let items = [];
295
+
296
+ $row.each((ii, row) => {
297
+ const $pictures = $(row).find('picture');
298
+
299
+ $pictures.each((iii, picture) => {
300
+ const $img = $(picture).find('img');
301
+ const src = largestSrc($img);
302
+ const dimensions = getImageDimensions(src);
303
+
304
+ items.push({
305
+ fileName: basename(src),
306
+ src: src,
307
+ width: (dimensions) ? dimensions.width : '100',
308
+ height: (dimensions) ? dimensions.height : '100'
309
+ });
310
+ });
311
+ });
312
+
313
+ items = items.map((item, index) => {
314
+ return {
315
+ ...item,
316
+ row: Math.floor(index / 3)
317
+ };
318
+ });
319
+
320
+ let cardOpts = {
321
+ env: {dom: new SimpleDom.Document()},
322
+ payload: {
323
+ images: items,
324
+ caption: caption
325
+ }
326
+ };
327
+
328
+ $(el).replaceWith(serializer.serialize(galleryCard.render(cardOpts)));
329
+ });
330
+
214
331
  $html('.captioned-image-container').each((i, div) => {
215
332
  const imgAlt = $(div).find('img[alt]').attr('alt') || '';
216
333
  const linkHref = $(div).find('a.image-link').attr('href') || false;
@@ -493,4 +610,7 @@ export default async (input, ctx) => {
493
610
  return output;
494
611
  };
495
612
 
496
- export {processContent};
613
+ export {
614
+ processContent,
615
+ getImageDimensions
616
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-substack",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-substack",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -27,15 +27,16 @@
27
27
  "dependencies": {
28
28
  "@tryghost/debug": "0.1.27",
29
29
  "@tryghost/errors": "1.3.0",
30
- "@tryghost/kg-default-cards": "9.1.9",
30
+ "@tryghost/kg-default-cards": "10.0.1",
31
31
  "@tryghost/mg-fs-utils": "^0.13.0",
32
32
  "@tryghost/string": "0.2.11",
33
33
  "cheerio": "1.0.0-rc.12",
34
34
  "date-fns": "2.30.0",
35
+ "he": "1.2.0",
35
36
  "html-entities": "2.4.0",
36
37
  "lodash": "4.17.21",
37
38
  "simple-dom": "1.4.0",
38
39
  "srcset": "5.0.0"
39
40
  },
40
- "gitHead": "1e250c8bacc23b674d5e9c42e2529bf07ff0aba0"
41
+ "gitHead": "73234756d0188f35dac8de168ed4a27148c7d971"
41
42
  }