@tryghost/mg-substack 0.4.2 → 0.5.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.
- package/lib/mapper.js +2 -1
- package/lib/process.js +147 -3
- package/package.json +4 -3
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,40 @@ 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>') ?? false;
|
|
235
|
+
let tweetAuthorName = $(el)?.find('.tweet-author-name')?.html()?.trim() ?? false;
|
|
236
|
+
let tweetAuthorHandle = $(el)?.find('.tweet-author-handle')?.html()?.trim() ?? false;
|
|
237
|
+
let tweetDateTime = $(el)?.find('.tweet-date')?.text()?.trim() ?? false;
|
|
238
|
+
let tweetURL = $(el)?.find('.tweet-link-top')?.attr('href') ?? false;
|
|
239
|
+
|
|
240
|
+
let theHtml = [];
|
|
241
|
+
|
|
242
|
+
if (tweetText) {
|
|
243
|
+
theHtml.push(`<p lang="en" dir="ltr">${tweetText}</p>`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
theHtml.push(`—`);
|
|
247
|
+
|
|
248
|
+
if (tweetAuthorName) {
|
|
249
|
+
theHtml.push(`${tweetAuthorName}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (tweetAuthorHandle) {
|
|
253
|
+
theHtml.push(`(${tweetAuthorHandle})`);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (tweetURL) {
|
|
257
|
+
theHtml.push(`<a href="${tweetURL}">`);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (tweetDateTime) {
|
|
261
|
+
theHtml.push(`${tweetDateTime}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
theHtml.push(`</a>`);
|
|
265
|
+
|
|
201
266
|
let $figure = $('<figure class="kg-card kg-embed-card"></figure>');
|
|
202
|
-
let $blockquote = $(
|
|
267
|
+
let $blockquote = $(`<blockquote class="twitter-tweet">${theHtml.join(' ')}</blockquote>`);
|
|
203
268
|
let $anchor = $(`<a href="${src}"></a>`);
|
|
204
269
|
let $script = $('<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>');
|
|
205
270
|
|
|
@@ -211,6 +276,82 @@ const processContent = (post, siteUrl, options) => {
|
|
|
211
276
|
$(el).replaceWith($figure);
|
|
212
277
|
});
|
|
213
278
|
|
|
279
|
+
$html('.image-gallery-embed').each((i, el) => {
|
|
280
|
+
const attrs = $(el).attr('data-attrs');
|
|
281
|
+
const attrsObj = JSON.parse(attrs);
|
|
282
|
+
|
|
283
|
+
let items = [];
|
|
284
|
+
|
|
285
|
+
attrsObj.gallery.images.forEach((item) => {
|
|
286
|
+
const dimensions = getImageDimensions(item.src);
|
|
287
|
+
|
|
288
|
+
items.push({
|
|
289
|
+
fileName: basename(item.src),
|
|
290
|
+
src: item.src,
|
|
291
|
+
width: (dimensions) ? dimensions.width : '100',
|
|
292
|
+
height: (dimensions) ? dimensions.height : '100'
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
items = items.map((item, index) => {
|
|
297
|
+
return {
|
|
298
|
+
...item,
|
|
299
|
+
row: Math.floor(index / 3)
|
|
300
|
+
};
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
let cardOpts = {
|
|
304
|
+
env: {dom: new SimpleDom.Document()},
|
|
305
|
+
payload: {
|
|
306
|
+
images: items,
|
|
307
|
+
caption: attrsObj.gallery.caption
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
$(el).replaceWith(serializer.serialize(galleryCard.render(cardOpts)));
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
$html('[class*="ImageGallery-module__imageGallery"]').each((i, el) => {
|
|
315
|
+
const $row = $(el).find('[class*="ImageGallery-module__imageRow"]');
|
|
316
|
+
const caption = $(el).find('figcaption').html();
|
|
317
|
+
|
|
318
|
+
let items = [];
|
|
319
|
+
|
|
320
|
+
$row.each((ii, row) => {
|
|
321
|
+
const $pictures = $(row).find('picture');
|
|
322
|
+
|
|
323
|
+
$pictures.each((iii, picture) => {
|
|
324
|
+
const $img = $(picture).find('img');
|
|
325
|
+
const src = largestSrc($img);
|
|
326
|
+
const dimensions = getImageDimensions(src);
|
|
327
|
+
|
|
328
|
+
items.push({
|
|
329
|
+
fileName: basename(src),
|
|
330
|
+
src: src,
|
|
331
|
+
width: (dimensions) ? dimensions.width : '100',
|
|
332
|
+
height: (dimensions) ? dimensions.height : '100'
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
items = items.map((item, index) => {
|
|
338
|
+
return {
|
|
339
|
+
...item,
|
|
340
|
+
row: Math.floor(index / 3)
|
|
341
|
+
};
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
let cardOpts = {
|
|
345
|
+
env: {dom: new SimpleDom.Document()},
|
|
346
|
+
payload: {
|
|
347
|
+
images: items,
|
|
348
|
+
caption: caption
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
$(el).replaceWith(serializer.serialize(galleryCard.render(cardOpts)));
|
|
353
|
+
});
|
|
354
|
+
|
|
214
355
|
$html('.captioned-image-container').each((i, div) => {
|
|
215
356
|
const imgAlt = $(div).find('img[alt]').attr('alt') || '';
|
|
216
357
|
const linkHref = $(div).find('a.image-link').attr('href') || false;
|
|
@@ -493,4 +634,7 @@ export default async (input, ctx) => {
|
|
|
493
634
|
return output;
|
|
494
635
|
};
|
|
495
636
|
|
|
496
|
-
export {
|
|
637
|
+
export {
|
|
638
|
+
processContent,
|
|
639
|
+
getImageDimensions
|
|
640
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-substack",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
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": "
|
|
30
|
+
"@tryghost/kg-default-cards": "10.0.2",
|
|
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": "
|
|
41
|
+
"gitHead": "d0c6f970f484101ab9b4dcf8ba95fa1398da4b79"
|
|
41
42
|
}
|