@tryghost/mg-substack 1.7.0 → 1.9.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/README.md +2 -2
- package/index.js +2 -4
- package/lib/mapper.js +32 -19
- package/lib/process.js +108 -74
- package/lib/read-zip.js +4 -6
- package/package.json +5 -9
package/README.md
CHANGED
|
@@ -147,8 +147,8 @@ pnpm dev substack <pathToZip>
|
|
|
147
147
|
|
|
148
148
|
## Test
|
|
149
149
|
|
|
150
|
-
- `pnpm
|
|
151
|
-
-
|
|
150
|
+
- `pnpm test` runs the package tests
|
|
151
|
+
- Linting and formatting are run from the repo root (`pnpm lint`, `pnpm format`)
|
|
152
152
|
|
|
153
153
|
|
|
154
154
|
# Copyright & License
|
package/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import process from './lib/process.js';
|
|
|
5
5
|
|
|
6
6
|
const parseString = fsUtils.csv.parseString;
|
|
7
7
|
|
|
8
|
-
const ingest = async
|
|
8
|
+
const ingest = async ctx => {
|
|
9
9
|
// Extract the ZIP file
|
|
10
10
|
let zipContent = readZip(ctx.options.pathToZip);
|
|
11
11
|
|
|
@@ -26,6 +26,4 @@ export default {
|
|
|
26
26
|
process
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
export {
|
|
30
|
-
contentStats
|
|
31
|
-
};
|
|
29
|
+
export {contentStats};
|
package/lib/mapper.js
CHANGED
|
@@ -13,7 +13,7 @@ const mapConfig = (data, options) => {
|
|
|
13
13
|
|
|
14
14
|
const typeSlug = slugify(data.type);
|
|
15
15
|
|
|
16
|
-
const contentType =
|
|
16
|
+
const contentType = typeSlug === 'page' ? 'page' : 'post';
|
|
17
17
|
|
|
18
18
|
const mappedData = {
|
|
19
19
|
url: `${url}/p/${slug}`,
|
|
@@ -27,7 +27,7 @@ const mapConfig = (data, options) => {
|
|
|
27
27
|
updated_at: data.post_date || dateNow,
|
|
28
28
|
created_at: data.post_date || dateNow,
|
|
29
29
|
title: data.title || slug,
|
|
30
|
-
custom_excerpt:
|
|
30
|
+
custom_excerpt: data.subtitle ? he.decode(data.subtitle) : null,
|
|
31
31
|
type: contentType,
|
|
32
32
|
html: data.html || null,
|
|
33
33
|
status: data.is_published.toLowerCase() === `true` ? 'published' : 'draft',
|
|
@@ -44,9 +44,7 @@ const mapConfig = (data, options) => {
|
|
|
44
44
|
data: {
|
|
45
45
|
email: email,
|
|
46
46
|
slug: authorSlug,
|
|
47
|
-
roles: [
|
|
48
|
-
'Contributor'
|
|
49
|
-
]
|
|
47
|
+
roles: ['Contributor']
|
|
50
48
|
}
|
|
51
49
|
};
|
|
52
50
|
}
|
|
@@ -74,14 +72,29 @@ export default async (input, options) => {
|
|
|
74
72
|
|
|
75
73
|
// Drive from HTML files we have, pulling in CSV metadata for each
|
|
76
74
|
const csvMeta = input.meta;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
meta
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
const htmlPosts = input.posts;
|
|
76
|
+
input = htmlPosts
|
|
77
|
+
.map(htmlPost => {
|
|
78
|
+
const postId = htmlPost.name.replace(/\.html$/, '');
|
|
79
|
+
const meta = csvMeta.find(item => item.post_id === postId);
|
|
80
|
+
if (meta) {
|
|
81
|
+
meta.html = htmlPost.html;
|
|
82
|
+
}
|
|
83
|
+
return meta;
|
|
84
|
+
})
|
|
85
|
+
.filter(Boolean);
|
|
86
|
+
|
|
87
|
+
// The migration is driven from the HTML files in the export, so any post listed
|
|
88
|
+
// in the CSV without a matching HTML file is skipped - often the most recent
|
|
89
|
+
// posts, whose HTML can be missing from a Substack export. Warn (without stopping
|
|
90
|
+
// the migration) so these dropped posts aren't a silent surprise.
|
|
91
|
+
const skippedPostCount = csvMeta.length - input.length;
|
|
92
|
+
if (skippedPostCount > 0) {
|
|
93
|
+
// eslint-disable-next-line no-console
|
|
94
|
+
console.warn(
|
|
95
|
+
`Warning: ${skippedPostCount} of ${csvMeta.length} posts in the CSV have no matching HTML file in the export and will be skipped (${htmlPosts.length} HTML files found).`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
85
98
|
|
|
86
99
|
// Preserve consistent ordering (chronological, with numeric ID as tiebreaker)
|
|
87
100
|
input.sort((a, b) => {
|
|
@@ -122,8 +135,8 @@ export default async (input, options) => {
|
|
|
122
135
|
const endDate = new Date(new Date(options.postsBefore).setDate(new Date(options.postsBefore).getDate() + 1));
|
|
123
136
|
debug(`Getting posts between ${startDate} and ${endDate}`);
|
|
124
137
|
|
|
125
|
-
await input.forEach(
|
|
126
|
-
if (
|
|
138
|
+
await input.forEach(data => {
|
|
139
|
+
if (new Date(data.post_date) > startDate && new Date(data.post_date) < endDate) {
|
|
127
140
|
output.posts.push(mapConfig(data, options));
|
|
128
141
|
}
|
|
129
142
|
});
|
|
@@ -131,8 +144,8 @@ export default async (input, options) => {
|
|
|
131
144
|
const startDate = new Date(options.postsAfter);
|
|
132
145
|
debug(`Getting posts after ${startDate}`);
|
|
133
146
|
|
|
134
|
-
await input.forEach(
|
|
135
|
-
if (
|
|
147
|
+
await input.forEach(data => {
|
|
148
|
+
if (new Date(data.post_date) > startDate) {
|
|
136
149
|
output.posts.push(mapConfig(data, options));
|
|
137
150
|
}
|
|
138
151
|
});
|
|
@@ -140,14 +153,14 @@ export default async (input, options) => {
|
|
|
140
153
|
const endDate = new Date(new Date(options.postsBefore).setDate(new Date(options.postsBefore).getDate() + 1));
|
|
141
154
|
debug(`Getting posts until ${endDate}`);
|
|
142
155
|
|
|
143
|
-
await input.forEach(
|
|
156
|
+
await input.forEach(data => {
|
|
144
157
|
if (new Date(data.post_date) < endDate) {
|
|
145
158
|
output.posts.push(mapConfig(data, options));
|
|
146
159
|
}
|
|
147
160
|
});
|
|
148
161
|
} else {
|
|
149
162
|
debug(`Getting all posts`);
|
|
150
|
-
await input.forEach(
|
|
163
|
+
await input.forEach(data => {
|
|
151
164
|
output.posts.push(mapConfig(data, options));
|
|
152
165
|
});
|
|
153
166
|
}
|
package/lib/process.js
CHANGED
|
@@ -22,13 +22,13 @@ const galleryCard = cardUtils.getCard('gallery');
|
|
|
22
22
|
const bookmarkCard = cardUtils.getCard('bookmark');
|
|
23
23
|
const fileCard = cardUtils.getCard('file');
|
|
24
24
|
|
|
25
|
-
const getFiles = async
|
|
25
|
+
const getFiles = async filePath => {
|
|
26
26
|
let filenames = await fs.readdir(filePath);
|
|
27
27
|
|
|
28
28
|
return filenames.filter(filename => filename.match(/\.html/));
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
const readContent = async
|
|
31
|
+
const readContent = async filePath => {
|
|
32
32
|
return fs.readFile(filePath, 'utf-8');
|
|
33
33
|
};
|
|
34
34
|
|
|
@@ -43,7 +43,7 @@ const readFiles = async (files, postsDir) => {
|
|
|
43
43
|
return postContent;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
const largeImageUrl =
|
|
46
|
+
const largeImageUrl = path => {
|
|
47
47
|
const substackCdnUrl = URL.parse(path);
|
|
48
48
|
if (substackCdnUrl?.hostname === 'substackcdn.com' && substackCdnUrl.pathname.startsWith('/image/fetch/')) {
|
|
49
49
|
path = decodeURIComponent(path.split('/').pop());
|
|
@@ -51,26 +51,33 @@ const largeImageUrl = (path) => {
|
|
|
51
51
|
|
|
52
52
|
const bucketeerUrl = URL.parse(path);
|
|
53
53
|
if (bucketeerUrl?.hostname.startsWith('bucketeer-')) {
|
|
54
|
-
path = path.replace(/https
|
|
54
|
+
path = path.replace(/https:\/\/[^/]*\.s3\.amazonaws\.com/gim, 'https://substack-post-media.s3.amazonaws.com');
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
return path;
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
const getUnsizedImageName =
|
|
61
|
-
|
|
60
|
+
const getUnsizedImageName = str => {
|
|
61
|
+
// Match the size suffix (e.g. `_1200x800.jpg`) directly rather than with a
|
|
62
|
+
// leading `(.*)` capture, which is vulnerable to polynomial backtracking.
|
|
63
|
+
const sizeSuffixRegex = /_[0-9]{1,4}x[0-9]{1,4}.[a-z]{2,4}/gi;
|
|
62
64
|
let srcParts = str.split(/\/|%2F/);
|
|
63
65
|
let last = srcParts.slice(-1)[0];
|
|
64
|
-
let matches = noSizeRegex.exec(last);
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
// Greedy `(.*)` settled on the last occurrence, so mirror that here
|
|
68
|
+
let lastMatch = null;
|
|
69
|
+
for (const match of last.matchAll(sizeSuffixRegex)) {
|
|
70
|
+
lastMatch = match;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (lastMatch) {
|
|
74
|
+
return last.slice(0, lastMatch.index);
|
|
68
75
|
} else {
|
|
69
76
|
return str;
|
|
70
77
|
}
|
|
71
78
|
};
|
|
72
79
|
|
|
73
|
-
const getImageDimensions =
|
|
80
|
+
const getImageDimensions = str => {
|
|
74
81
|
const imageSizeRegexp = /_([0-9]{2,5})x([0-9]{2,5}).[a-zA-Z]{2,4}/;
|
|
75
82
|
const matches = str.match(imageSizeRegexp);
|
|
76
83
|
|
|
@@ -84,7 +91,7 @@ const getImageDimensions = (str) => {
|
|
|
84
91
|
}
|
|
85
92
|
};
|
|
86
93
|
|
|
87
|
-
const largestSrc =
|
|
94
|
+
const largestSrc = imageElem => {
|
|
88
95
|
const src = attr(imageElem, 'src');
|
|
89
96
|
const srcset = attr(imageElem, 'srcset');
|
|
90
97
|
|
|
@@ -125,12 +132,12 @@ const processContent = (post, siteUrl, options) => {
|
|
|
125
132
|
const parsed = parseFragment(`<div class="migrate-substack-wrapper">${html}</div>`);
|
|
126
133
|
|
|
127
134
|
// Change paywall card to comment
|
|
128
|
-
parsed.$('.paywall-jump').forEach(
|
|
135
|
+
parsed.$('.paywall-jump').forEach(el => {
|
|
129
136
|
replaceWith(el, '<!--members-only-->');
|
|
130
137
|
});
|
|
131
138
|
|
|
132
139
|
// Empty text elements are commonplace and are not needed
|
|
133
|
-
parsed.$('p').forEach(
|
|
140
|
+
parsed.$('p').forEach(el => {
|
|
134
141
|
let content = serializeChildren(el).trim();
|
|
135
142
|
|
|
136
143
|
if (content.length === 0) {
|
|
@@ -138,22 +145,28 @@ const processContent = (post, siteUrl, options) => {
|
|
|
138
145
|
}
|
|
139
146
|
});
|
|
140
147
|
|
|
148
|
+
parsed
|
|
149
|
+
.$('h1 strong, h2 strong, h3 strong, h4 strong, h5 strong, h6 strong, h1 b, h2 b, h3 b, h4 b, h5 b, h6 b')
|
|
150
|
+
.forEach(el => {
|
|
151
|
+
replaceWith(el, serializeChildren(el));
|
|
152
|
+
});
|
|
153
|
+
|
|
141
154
|
// Wrap these in a HTML card so they can be handled by publishers as needed
|
|
142
|
-
parsed.$('div.latex-rendered').forEach(
|
|
155
|
+
parsed.$('div.latex-rendered').forEach(el => {
|
|
143
156
|
insertBefore(el, '<!--kg-card-begin: html-->');
|
|
144
157
|
insertAfter(el, '<!--kg-card-end: html-->');
|
|
145
158
|
});
|
|
146
159
|
|
|
147
160
|
// We don't currently handle these, so remove them to clean up the document
|
|
148
|
-
parsed.$('div.native-video-embed').forEach(
|
|
161
|
+
parsed.$('div.native-video-embed').forEach(el => {
|
|
149
162
|
el.remove();
|
|
150
163
|
});
|
|
151
164
|
|
|
152
|
-
parsed.$('div.poll-embed').forEach(
|
|
165
|
+
parsed.$('div.poll-embed').forEach(el => {
|
|
153
166
|
el.remove();
|
|
154
167
|
});
|
|
155
168
|
|
|
156
|
-
parsed.$('.image3').forEach(
|
|
169
|
+
parsed.$('.image3').forEach(el => {
|
|
157
170
|
const attrs = attr(el, 'data-attrs');
|
|
158
171
|
const attrsObj = JSON.parse(attrs);
|
|
159
172
|
|
|
@@ -193,7 +206,9 @@ const processContent = (post, siteUrl, options) => {
|
|
|
193
206
|
|
|
194
207
|
if (unsizedFirstSrc === unsizedOgSrc) {
|
|
195
208
|
if (parsed.$('figcaption', firstElement).length) {
|
|
196
|
-
post.data.feature_image_caption = serializeChildren(
|
|
209
|
+
post.data.feature_image_caption = serializeChildren(
|
|
210
|
+
parsed.$('figcaption', firstElement)[0]
|
|
211
|
+
);
|
|
197
212
|
}
|
|
198
213
|
|
|
199
214
|
firstElement.remove();
|
|
@@ -221,7 +236,9 @@ const processContent = (post, siteUrl, options) => {
|
|
|
221
236
|
post.data.feature_image = unsizedFirstSrc;
|
|
222
237
|
|
|
223
238
|
if (parsed.$('figcaption', firstElement).length) {
|
|
224
|
-
post.data.feature_image_caption = serializeChildren(
|
|
239
|
+
post.data.feature_image_caption = serializeChildren(
|
|
240
|
+
parsed.$('figcaption', firstElement)[0]
|
|
241
|
+
);
|
|
225
242
|
}
|
|
226
243
|
|
|
227
244
|
firstElement.remove();
|
|
@@ -261,7 +278,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
261
278
|
}
|
|
262
279
|
}
|
|
263
280
|
|
|
264
|
-
parsed.$('div.tweet').forEach(
|
|
281
|
+
parsed.$('div.tweet').forEach(el => {
|
|
265
282
|
let childAnchors = parsed.$(':scope > a', el);
|
|
266
283
|
let src = attr(childAnchors[0], 'href');
|
|
267
284
|
let parsedUrl = url.parse(src);
|
|
@@ -313,20 +330,20 @@ const processContent = (post, siteUrl, options) => {
|
|
|
313
330
|
replaceWith(el, tweetHtml);
|
|
314
331
|
});
|
|
315
332
|
|
|
316
|
-
parsed.$('.image-gallery-embed').forEach(
|
|
333
|
+
parsed.$('.image-gallery-embed').forEach(el => {
|
|
317
334
|
const attrs = attr(el, 'data-attrs');
|
|
318
335
|
const attrsObj = JSON.parse(attrs);
|
|
319
336
|
|
|
320
337
|
let items = [];
|
|
321
338
|
|
|
322
|
-
attrsObj.gallery.images.forEach(
|
|
339
|
+
attrsObj.gallery.images.forEach(item => {
|
|
323
340
|
const dimensions = getImageDimensions(item.src);
|
|
324
341
|
|
|
325
342
|
items.push({
|
|
326
343
|
fileName: basename(item.src),
|
|
327
344
|
src: largeImageUrl(item.src),
|
|
328
|
-
width:
|
|
329
|
-
height:
|
|
345
|
+
width: dimensions ? dimensions.width : '100',
|
|
346
|
+
height: dimensions ? dimensions.height : '100'
|
|
330
347
|
});
|
|
331
348
|
});
|
|
332
349
|
|
|
@@ -348,17 +365,17 @@ const processContent = (post, siteUrl, options) => {
|
|
|
348
365
|
replaceWith(el, serializer.serialize(galleryCard.render(cardOpts)));
|
|
349
366
|
});
|
|
350
367
|
|
|
351
|
-
parsed.$('[class*="ImageGallery-module__imageGallery"]').forEach(
|
|
368
|
+
parsed.$('[class*="ImageGallery-module__imageGallery"]').forEach(el => {
|
|
352
369
|
const rows = parsed.$('[class*="ImageGallery-module__imageRow"]', el);
|
|
353
370
|
const figcaptionEl = parsed.$('figcaption', el)[0];
|
|
354
371
|
const caption = figcaptionEl ? serializeChildren(figcaptionEl) : null;
|
|
355
372
|
|
|
356
373
|
let items = [];
|
|
357
374
|
|
|
358
|
-
rows.forEach(
|
|
375
|
+
rows.forEach(row => {
|
|
359
376
|
const pictures = parsed.$('picture', row);
|
|
360
377
|
|
|
361
|
-
pictures.forEach(
|
|
378
|
+
pictures.forEach(picture => {
|
|
362
379
|
const img = parsed.$('img', picture)[0];
|
|
363
380
|
const src = largestSrc(img);
|
|
364
381
|
const dimensions = getImageDimensions(src);
|
|
@@ -366,8 +383,8 @@ const processContent = (post, siteUrl, options) => {
|
|
|
366
383
|
items.push({
|
|
367
384
|
fileName: basename(src),
|
|
368
385
|
src: src,
|
|
369
|
-
width:
|
|
370
|
-
height:
|
|
386
|
+
width: dimensions ? dimensions.width : '100',
|
|
387
|
+
height: dimensions ? dimensions.height : '100'
|
|
371
388
|
});
|
|
372
389
|
});
|
|
373
390
|
});
|
|
@@ -390,7 +407,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
390
407
|
replaceWith(el, serializer.serialize(galleryCard.render(cardOpts)));
|
|
391
408
|
});
|
|
392
409
|
|
|
393
|
-
parsed.$('.captioned-image-container').forEach(
|
|
410
|
+
parsed.$('.captioned-image-container').forEach(div => {
|
|
394
411
|
const imgAltEl = parsed.$('img[alt]', div)[0];
|
|
395
412
|
const imgAlt = imgAltEl ? attr(imgAltEl, 'alt') : '';
|
|
396
413
|
const linkEl = parsed.$('a.image-link', div)[0];
|
|
@@ -416,7 +433,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
416
433
|
replaceWith(div, serializer.serialize(imageCard.render(cardOpts)));
|
|
417
434
|
});
|
|
418
435
|
|
|
419
|
-
parsed.$('.image-link').forEach(
|
|
436
|
+
parsed.$('.image-link').forEach(anchor => {
|
|
420
437
|
const imgAltEl = parsed.$('img[alt]', anchor)[0];
|
|
421
438
|
const imgAlt = imgAltEl ? attr(imgAltEl, 'alt') : '';
|
|
422
439
|
const linkHref = attr(anchor, 'href');
|
|
@@ -440,7 +457,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
440
457
|
replaceWith(anchor, serializer.serialize(imageCard.render(cardOpts)));
|
|
441
458
|
});
|
|
442
459
|
|
|
443
|
-
parsed.$('.file-embed-wrapper').forEach(
|
|
460
|
+
parsed.$('.file-embed-wrapper').forEach(el => {
|
|
444
461
|
const fileSrcEl = parsed.$('.file-embed-button', el)[0];
|
|
445
462
|
const fileSrc = attr(fileSrcEl, 'href');
|
|
446
463
|
const fileTitleEl = parsed.$('.file-embed-details-h1', el)[0];
|
|
@@ -456,20 +473,20 @@ const processContent = (post, siteUrl, options) => {
|
|
|
456
473
|
const unit = fileSizeMatch[2].toUpperCase();
|
|
457
474
|
|
|
458
475
|
switch (unit) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
476
|
+
case 'KB':
|
|
477
|
+
fileSizeBytes = size * 1024;
|
|
478
|
+
break;
|
|
479
|
+
case 'MB':
|
|
480
|
+
fileSizeBytes = size * 1024 * 1024;
|
|
481
|
+
break;
|
|
482
|
+
case 'GB':
|
|
483
|
+
fileSizeBytes = size * 1024 * 1024 * 1024;
|
|
484
|
+
break;
|
|
485
|
+
case 'TB':
|
|
486
|
+
fileSizeBytes = size * 1024 * 1024 * 1024 * 1024;
|
|
487
|
+
break;
|
|
488
|
+
default:
|
|
489
|
+
fileSizeBytes = size;
|
|
473
490
|
}
|
|
474
491
|
}
|
|
475
492
|
|
|
@@ -486,11 +503,11 @@ const processContent = (post, siteUrl, options) => {
|
|
|
486
503
|
replaceWith(el, serializer.serialize(fileCard.render(cardOpts)));
|
|
487
504
|
});
|
|
488
505
|
|
|
489
|
-
parsed.$('.comment').forEach(
|
|
506
|
+
parsed.$('.comment').forEach(el => {
|
|
490
507
|
el.remove();
|
|
491
508
|
});
|
|
492
509
|
|
|
493
|
-
parsed.$('.mention-wrap').forEach(
|
|
510
|
+
parsed.$('.mention-wrap').forEach(el => {
|
|
494
511
|
const attrsRaw = attr(el, 'data-attrs');
|
|
495
512
|
|
|
496
513
|
let attrs;
|
|
@@ -509,7 +526,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
509
526
|
}
|
|
510
527
|
});
|
|
511
528
|
|
|
512
|
-
parsed.$('.digest-post-embed').forEach(
|
|
529
|
+
parsed.$('.digest-post-embed').forEach(el => {
|
|
513
530
|
const attrsRaw = attr(el, 'data-attrs');
|
|
514
531
|
|
|
515
532
|
let attrs;
|
|
@@ -551,11 +568,11 @@ const processContent = (post, siteUrl, options) => {
|
|
|
551
568
|
replaceWith(el, bookmarkHtml);
|
|
552
569
|
});
|
|
553
570
|
|
|
554
|
-
parsed.$('a > style').forEach(
|
|
571
|
+
parsed.$('a > style').forEach(style => {
|
|
555
572
|
style.remove();
|
|
556
573
|
});
|
|
557
574
|
|
|
558
|
-
parsed.$('ul, ol').forEach(
|
|
575
|
+
parsed.$('ul, ol').forEach(list => {
|
|
559
576
|
if (parsed.$('img, div, figure, blockquote, .button-wrapper', list).length) {
|
|
560
577
|
insertBefore(list, '<!--kg-card-begin: html-->');
|
|
561
578
|
insertAfter(list, '<!--kg-card-end: html-->');
|
|
@@ -563,7 +580,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
563
580
|
});
|
|
564
581
|
|
|
565
582
|
// Remove Substack share buttons
|
|
566
|
-
parsed.$('p.button-wrapper').forEach(
|
|
583
|
+
parsed.$('p.button-wrapper').forEach(button => {
|
|
567
584
|
let shareLinks = parsed.$(':scope > a.button', button);
|
|
568
585
|
if (shareLinks.length === 1 && siteUrl) {
|
|
569
586
|
let shareLink = shareLinks[0];
|
|
@@ -585,10 +602,13 @@ const processContent = (post, siteUrl, options) => {
|
|
|
585
602
|
});
|
|
586
603
|
|
|
587
604
|
// Update button elements
|
|
588
|
-
parsed.$('p.button-wrapper').forEach(
|
|
605
|
+
parsed.$('p.button-wrapper').forEach(button => {
|
|
589
606
|
let buttons = parsed.$(':scope > a.button', button);
|
|
590
607
|
if (buttons.length === 1 && siteUrl) {
|
|
591
|
-
let siteRegex = new RegExp(
|
|
608
|
+
let siteRegex = new RegExp(
|
|
609
|
+
`^(?:${escapeStringRegexp(siteUrl)}(?:\\/?)(?:p\\/)?)([a-zA-Z-_\\d]*)(?:\\/?)`,
|
|
610
|
+
'gi'
|
|
611
|
+
);
|
|
592
612
|
let buttonLink = buttons[0];
|
|
593
613
|
let buttonHref = attr(buttonLink, 'href');
|
|
594
614
|
let buttonText = buttonLink.textContent;
|
|
@@ -620,13 +640,20 @@ const processContent = (post, siteUrl, options) => {
|
|
|
620
640
|
}
|
|
621
641
|
}
|
|
622
642
|
|
|
623
|
-
|
|
643
|
+
const card = parsed.document.createElement('div');
|
|
644
|
+
card.className = 'kg-card kg-button-card kg-align-center';
|
|
645
|
+
const link = parsed.document.createElement('a');
|
|
646
|
+
link.className = 'kg-btn kg-btn-accent';
|
|
647
|
+
link.setAttribute('href', buttonHref);
|
|
648
|
+
link.textContent = buttonText;
|
|
649
|
+
card.appendChild(link);
|
|
650
|
+
replaceWith(button, card);
|
|
624
651
|
}
|
|
625
652
|
});
|
|
626
653
|
|
|
627
654
|
// TODO: this should be a parser plugin
|
|
628
655
|
// Wrap nested lists in HTML card
|
|
629
|
-
parsed.$('ul li ul, ol li ol, ol li ul, ul li ol').forEach(
|
|
656
|
+
parsed.$('ul li ul, ol li ol, ol li ul, ul li ol').forEach(nestedList => {
|
|
630
657
|
const parentList = parents(nestedList, 'ul, ol')[0];
|
|
631
658
|
if (parentList) {
|
|
632
659
|
insertBefore(parentList, '<!--kg-card-begin: html-->');
|
|
@@ -637,7 +664,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
637
664
|
// Handle footnotes
|
|
638
665
|
let footnotesItems = [];
|
|
639
666
|
let footnotesCount = 0;
|
|
640
|
-
parsed.$('.footnote').forEach(
|
|
667
|
+
parsed.$('.footnote').forEach(el => {
|
|
641
668
|
const footnoteAnchor = parsed.$('a', el)[0];
|
|
642
669
|
let footnoteBodyAnchor = attr(footnoteAnchor, 'href');
|
|
643
670
|
|
|
@@ -649,7 +676,10 @@ const processContent = (post, siteUrl, options) => {
|
|
|
649
676
|
const pElements = parsed.$('p', footnoteContent);
|
|
650
677
|
const lastP = pElements[pElements.length - 1];
|
|
651
678
|
if (lastP) {
|
|
652
|
-
lastP.insertAdjacentHTML(
|
|
679
|
+
lastP.insertAdjacentHTML(
|
|
680
|
+
'beforeend',
|
|
681
|
+
` <a href="${footnoteBodyAnchor}" title="Jump back to footnote ${footnoteNumber} in the text.">↩</a>`
|
|
682
|
+
);
|
|
653
683
|
}
|
|
654
684
|
|
|
655
685
|
footnotesItems.push(`<li id="${footnoteID}">${serializeChildren(footnoteContent)}</li>`);
|
|
@@ -665,7 +695,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
665
695
|
}
|
|
666
696
|
|
|
667
697
|
// Wrap content that has footnote anchors in HTML tags to retain the footnote jump anchor
|
|
668
|
-
parsed.$('p, ul, ol').forEach(
|
|
698
|
+
parsed.$('p, ul, ol').forEach(el => {
|
|
669
699
|
if (parsed.$('a.footnote-anchor', el).length > 0) {
|
|
670
700
|
insertBefore(el, '<!--kg-card-begin: html-->');
|
|
671
701
|
insertAfter(el, '<!--kg-card-end: html-->');
|
|
@@ -674,7 +704,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
674
704
|
|
|
675
705
|
// Remove or replace subscribe links on the same domain
|
|
676
706
|
if (options.noSubscribeButtons) {
|
|
677
|
-
parsed.$('a').forEach(
|
|
707
|
+
parsed.$('a').forEach(anchor => {
|
|
678
708
|
let href = attr(anchor, 'href');
|
|
679
709
|
let linkRegex = new RegExp(`^(${escapeStringRegexp(siteUrl)})?(/subscribe)(.*)`, 'gi');
|
|
680
710
|
|
|
@@ -689,7 +719,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
689
719
|
}
|
|
690
720
|
});
|
|
691
721
|
} else if (options.subscribeLink) {
|
|
692
|
-
parsed.$('a').forEach(
|
|
722
|
+
parsed.$('a').forEach(anchor => {
|
|
693
723
|
let href = attr(anchor, 'href');
|
|
694
724
|
let linkRegex = new RegExp(`^(${escapeStringRegexp(siteUrl)})?(/subscribe)(.*)`, 'gi');
|
|
695
725
|
|
|
@@ -707,22 +737,29 @@ const processContent = (post, siteUrl, options) => {
|
|
|
707
737
|
|
|
708
738
|
// Remove or replace signup forms with a Portal signup button
|
|
709
739
|
if (options.noSubscribeButtons) {
|
|
710
|
-
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(
|
|
740
|
+
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(div => {
|
|
711
741
|
div.remove();
|
|
712
742
|
});
|
|
713
743
|
} else if (options.subscribeLink) {
|
|
714
|
-
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(
|
|
744
|
+
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(div => {
|
|
715
745
|
const hasForm = parsed.$('form', div);
|
|
716
746
|
|
|
717
747
|
if (hasForm.length) {
|
|
718
748
|
const submitEl = parsed.$('form input[type="submit"]', div)[0];
|
|
719
749
|
const buttonText = submitEl ? attr(submitEl, 'value') : '';
|
|
720
|
-
|
|
750
|
+
const card = parsed.document.createElement('div');
|
|
751
|
+
card.className = 'kg-card kg-button-card kg-align-center';
|
|
752
|
+
const link = parsed.document.createElement('a');
|
|
753
|
+
link.className = 'kg-btn kg-btn-accent';
|
|
754
|
+
link.setAttribute('href', options.subscribeLink);
|
|
755
|
+
link.textContent = buttonText;
|
|
756
|
+
card.appendChild(link);
|
|
757
|
+
replaceWith(div, card);
|
|
721
758
|
}
|
|
722
759
|
});
|
|
723
760
|
}
|
|
724
761
|
|
|
725
|
-
parsed.$('.embedded-post-wrap').forEach(
|
|
762
|
+
parsed.$('.embedded-post-wrap').forEach(div => {
|
|
726
763
|
const attrs = attr(div, 'data-attrs') || false;
|
|
727
764
|
|
|
728
765
|
if (!attrs) {
|
|
@@ -743,7 +780,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
743
780
|
replaceWith(div, `<!--kg-card-begin: html-->${embeddedHtml}<!--kg-card-end: html-->`);
|
|
744
781
|
});
|
|
745
782
|
|
|
746
|
-
parsed.$('div.instagram').forEach(
|
|
783
|
+
parsed.$('div.instagram').forEach(el => {
|
|
747
784
|
const instagramLink = parsed.$('a.instagram-image', el)[0];
|
|
748
785
|
let src = instagramLink ? attr(instagramLink, 'href') : '';
|
|
749
786
|
|
|
@@ -769,7 +806,7 @@ const processContent = (post, siteUrl, options) => {
|
|
|
769
806
|
});
|
|
770
807
|
|
|
771
808
|
// For each image and link, alter the path to remove cropping & sizing for image paths
|
|
772
|
-
parsed.$('img[src], a[href]').forEach(
|
|
809
|
+
parsed.$('img[src], a[href]').forEach(el => {
|
|
773
810
|
const src = attr(el, 'src');
|
|
774
811
|
const href = attr(el, 'href');
|
|
775
812
|
|
|
@@ -876,11 +913,12 @@ export default async (input, ctx) => {
|
|
|
876
913
|
let postFiles = await getFiles(postsDir);
|
|
877
914
|
let postContent = await readFiles(postFiles, postsDir);
|
|
878
915
|
|
|
879
|
-
input.posts.map(
|
|
916
|
+
input.posts.map(post => {
|
|
917
|
+
// eslint-disable-line array-callback-return
|
|
880
918
|
post.data.html = postContent[post.substackId];
|
|
881
919
|
});
|
|
882
920
|
} catch (error) {
|
|
883
|
-
return new errors.InternalServerError({message:
|
|
921
|
+
return new errors.InternalServerError({message: "Couldn't read post files"});
|
|
884
922
|
}
|
|
885
923
|
}
|
|
886
924
|
|
|
@@ -897,8 +935,4 @@ export default async (input, ctx) => {
|
|
|
897
935
|
return output;
|
|
898
936
|
};
|
|
899
937
|
|
|
900
|
-
export {
|
|
901
|
-
processContent,
|
|
902
|
-
getImageDimensions,
|
|
903
|
-
largeImageUrl
|
|
904
|
-
};
|
|
938
|
+
export {processContent, getImageDimensions, largeImageUrl};
|
package/lib/read-zip.js
CHANGED
|
@@ -3,7 +3,7 @@ import {_base as debugFactory} from '@tryghost/debug';
|
|
|
3
3
|
|
|
4
4
|
const debug = debugFactory('migrate:substack:read-zip');
|
|
5
5
|
|
|
6
|
-
const contentStats = async
|
|
6
|
+
const contentStats = async zipPath => {
|
|
7
7
|
const entries = await fsUtils.readZipEntries(zipPath);
|
|
8
8
|
|
|
9
9
|
const posts = entries.filter(value => /posts\/.*\.html/.test(value)).length;
|
|
@@ -13,7 +13,7 @@ const contentStats = async (zipPath) => {
|
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export default
|
|
16
|
+
export default zipPath => {
|
|
17
17
|
let content = {
|
|
18
18
|
csv: null,
|
|
19
19
|
posts: []
|
|
@@ -34,7 +34,7 @@ export default (zipPath) => {
|
|
|
34
34
|
html: zipEntry.getData().toString('utf8')
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
// Get the posts.csv
|
|
38
38
|
} else if (entryName === 'posts.csv' || entryName.endsWith('/posts.csv')) {
|
|
39
39
|
debug(`Found posts CSV file ${entryName}`);
|
|
40
40
|
content.csv = zipEntry.getData().toString('utf8');
|
|
@@ -44,6 +44,4 @@ export default (zipPath) => {
|
|
|
44
44
|
return content;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
export {
|
|
48
|
-
contentStats
|
|
49
|
-
};
|
|
47
|
+
export {contentStats};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-substack",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -17,9 +17,7 @@
|
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"eslint": "8.57.1"
|
|
22
|
-
},
|
|
20
|
+
"devDependencies": {},
|
|
23
21
|
"dependencies": {
|
|
24
22
|
"@tryghost/debug": "2.2.3",
|
|
25
23
|
"@tryghost/errors": "3.2.4",
|
|
@@ -29,13 +27,11 @@
|
|
|
29
27
|
"html-entities": "2.6.0",
|
|
30
28
|
"simple-dom": "1.4.0",
|
|
31
29
|
"srcset": "5.0.3",
|
|
32
|
-
"@tryghost/mg-utils": "0.11.
|
|
33
|
-
"@tryghost/mg-fs-utils": "0.26.
|
|
30
|
+
"@tryghost/mg-utils": "0.11.3",
|
|
31
|
+
"@tryghost/mg-fs-utils": "0.26.3"
|
|
34
32
|
},
|
|
35
33
|
"scripts": {
|
|
36
34
|
"dev": "echo \"Implement me!\"",
|
|
37
|
-
"test": "node --test 'test/**/*.test.js'"
|
|
38
|
-
"posttest": "pnpm lint",
|
|
39
|
-
"lint": "eslint . --ext .js --cache"
|
|
35
|
+
"test": "node --test 'test/**/*.test.js'"
|
|
40
36
|
}
|
|
41
37
|
}
|