@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.
- package/README.md +81 -7
- package/build/index.d.ts +5 -0
- package/build/index.js +4 -0
- package/build/index.js.map +1 -0
- package/build/lib/ingest.d.ts +18 -0
- package/build/lib/ingest.js +62 -0
- package/build/lib/ingest.js.map +1 -0
- package/build/lib/map.d.ts +24 -0
- package/build/lib/map.js +302 -0
- package/build/lib/map.js.map +1 -0
- package/build/lib/process.d.ts +15 -0
- package/{lib → build/lib}/process.js +128 -483
- package/build/lib/process.js.map +1 -0
- package/build/lib/types.d.ts +80 -0
- package/build/lib/types.js +3 -0
- package/build/lib/types.js.map +1 -0
- package/build/test/helpers.d.ts +16 -0
- package/build/test/helpers.js +58 -0
- package/build/test/helpers.js.map +1 -0
- package/build/test/ingest.test.d.ts +1 -0
- package/build/test/ingest.test.js +96 -0
- package/build/test/ingest.test.js.map +1 -0
- package/build/test/map.test.d.ts +1 -0
- package/build/test/map.test.js +338 -0
- package/build/test/map.test.js.map +1 -0
- package/build/test/process.test.d.ts +1 -0
- package/build/test/process.test.js +882 -0
- package/build/test/process.test.js.map +1 -0
- package/package.json +25 -13
- package/index.js +0 -29
- package/lib/mapper.js +0 -169
- package/lib/read-zip.js +0 -47
|
@@ -1,294 +1,209 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {join, basename} from 'node:path';
|
|
1
|
+
import { basename } from 'node:path';
|
|
3
2
|
import url from 'node:url';
|
|
4
|
-
import {domUtils,
|
|
5
|
-
import errors from '@tryghost/errors';
|
|
3
|
+
import { domUtils, cardUtils, stringUtils } from '@tryghost/mg-utils';
|
|
6
4
|
import SimpleDom from 'simple-dom';
|
|
7
|
-
import {decode} from 'html-entities';
|
|
8
|
-
import {parseSrcset} from 'srcset';
|
|
9
|
-
import {_base as debugFactory} from '@tryghost/debug';
|
|
10
|
-
import {slugify} from '@tryghost/string';
|
|
5
|
+
import { decode } from 'html-entities';
|
|
6
|
+
import { parseSrcset } from 'srcset';
|
|
7
|
+
import { _base as debugFactory } from '@tryghost/debug';
|
|
11
8
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
|
|
15
|
-
const {startCase} = stringUtils;
|
|
16
|
-
|
|
9
|
+
const { parseFragment, serializeChildren, replaceWith, insertBefore, insertAfter, attr, parents } = domUtils;
|
|
10
|
+
const { escapeHTML } = stringUtils;
|
|
17
11
|
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
|
|
12
|
+
const serialize = (node) => serializer.serialize(node);
|
|
18
13
|
const debug = debugFactory('migrate:substack:process');
|
|
19
14
|
const imageCard = cardUtils.getCard('image');
|
|
20
15
|
const audioCard = cardUtils.getCard('audio');
|
|
21
16
|
const galleryCard = cardUtils.getCard('gallery');
|
|
22
17
|
const bookmarkCard = cardUtils.getCard('bookmark');
|
|
23
18
|
const fileCard = cardUtils.getCard('file');
|
|
24
|
-
|
|
25
|
-
const getFiles = async filePath => {
|
|
26
|
-
let filenames = await fs.readdir(filePath);
|
|
27
|
-
|
|
28
|
-
return filenames.filter(filename => filename.match(/\.html/));
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const readContent = async filePath => {
|
|
32
|
-
return fs.readFile(filePath, 'utf-8');
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const readFiles = async (files, postsDir) => {
|
|
36
|
-
const postContent = {};
|
|
37
|
-
for (const file of files) {
|
|
38
|
-
const substackId = file.replace(/\.html/, '');
|
|
39
|
-
|
|
40
|
-
postContent[substackId] = await readContent(join(postsDir, file));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return postContent;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const largeImageUrl = path => {
|
|
19
|
+
const largeImageUrl = (path) => {
|
|
47
20
|
const substackCdnUrl = URL.parse(path);
|
|
48
21
|
if (substackCdnUrl?.hostname === 'substackcdn.com' && substackCdnUrl.pathname.startsWith('/image/fetch/')) {
|
|
49
22
|
path = decodeURIComponent(path.split('/').pop());
|
|
50
23
|
}
|
|
51
|
-
|
|
52
24
|
const bucketeerUrl = URL.parse(path);
|
|
53
25
|
if (bucketeerUrl?.hostname.startsWith('bucketeer-')) {
|
|
54
26
|
path = path.replace(/https:\/\/[^/]*\.s3\.amazonaws\.com/gim, 'https://substack-post-media.s3.amazonaws.com');
|
|
55
27
|
}
|
|
56
|
-
|
|
57
28
|
return path;
|
|
58
29
|
};
|
|
59
|
-
|
|
60
|
-
const getUnsizedImageName = str => {
|
|
30
|
+
const getUnsizedImageName = (str) => {
|
|
61
31
|
// Match the size suffix (e.g. `_1200x800.jpg`) directly rather than with a
|
|
62
32
|
// leading `(.*)` capture, which is vulnerable to polynomial backtracking.
|
|
63
33
|
const sizeSuffixRegex = /_[0-9]{1,4}x[0-9]{1,4}.[a-z]{2,4}/gi;
|
|
64
34
|
let srcParts = str.split(/\/|%2F/);
|
|
65
35
|
let last = srcParts.slice(-1)[0];
|
|
66
|
-
|
|
67
36
|
// Greedy `(.*)` settled on the last occurrence, so mirror that here
|
|
68
37
|
let lastMatch = null;
|
|
69
38
|
for (const match of last.matchAll(sizeSuffixRegex)) {
|
|
70
39
|
lastMatch = match;
|
|
71
40
|
}
|
|
72
|
-
|
|
73
41
|
if (lastMatch) {
|
|
74
42
|
return last.slice(0, lastMatch.index);
|
|
75
|
-
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
76
45
|
return str;
|
|
77
46
|
}
|
|
78
47
|
};
|
|
79
|
-
|
|
80
|
-
const getImageDimensions = str => {
|
|
48
|
+
const getImageDimensions = (str) => {
|
|
81
49
|
const imageSizeRegexp = /_([0-9]{2,5})x([0-9]{2,5}).[a-zA-Z]{2,4}/;
|
|
82
50
|
const matches = str.match(imageSizeRegexp);
|
|
83
|
-
|
|
84
|
-
if (matches && matches[1] && matches[2]) {
|
|
51
|
+
if (matches) {
|
|
85
52
|
return {
|
|
86
53
|
width: parseInt(matches[1]),
|
|
87
54
|
height: parseInt(matches[2])
|
|
88
55
|
};
|
|
89
|
-
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
90
58
|
return false;
|
|
91
59
|
}
|
|
92
60
|
};
|
|
93
|
-
|
|
94
|
-
const largestSrc = imageElem => {
|
|
61
|
+
const largestSrc = (imageElem) => {
|
|
95
62
|
const src = attr(imageElem, 'src');
|
|
96
63
|
const srcset = attr(imageElem, 'srcset');
|
|
97
|
-
|
|
98
|
-
if (!src || !src.length) {
|
|
64
|
+
if (!src) {
|
|
99
65
|
return '';
|
|
100
66
|
}
|
|
101
|
-
|
|
102
67
|
let srcToUse = src;
|
|
103
|
-
|
|
104
68
|
if (srcset) {
|
|
105
69
|
const parsedSrcset = parseSrcset(srcset);
|
|
106
|
-
|
|
107
|
-
if (parsedSrcset && parsedSrcset.length > 0) {
|
|
70
|
+
if (parsedSrcset.length > 0) {
|
|
108
71
|
srcToUse = parsedSrcset[0].url;
|
|
109
72
|
}
|
|
110
73
|
}
|
|
111
|
-
|
|
112
74
|
// Remove the width from the image URL
|
|
113
75
|
srcToUse = srcToUse.replace(/w_[0-9]{2,4},c_limit,/, '');
|
|
114
|
-
|
|
115
76
|
return srcToUse;
|
|
116
77
|
};
|
|
117
|
-
|
|
78
|
+
/** Strip the query string from a URL, as the legacy `url.format()` round trip does. */
|
|
79
|
+
const withoutSearch = (src) => {
|
|
80
|
+
const parsedUrl = url.parse(src);
|
|
81
|
+
parsedUrl.search = null;
|
|
82
|
+
return url.format(parsedUrl);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Convert Substack's export HTML into Ghost-compatible HTML, in place on `post.data`.
|
|
86
|
+
*
|
|
87
|
+
* Reads `post.data.html`, the optional scraped `og_image` and `podcast_audio_src`, and `post.substackPodcastURL`
|
|
88
|
+
* (from `posts.csv`). Writes `post.data.html`, and `feature_image`/`feature_image_caption` when the first image
|
|
89
|
+
* becomes the feature image.
|
|
90
|
+
*/
|
|
118
91
|
const processContent = (post, siteUrl, options) => {
|
|
119
|
-
const {substackPodcastURL} = post;
|
|
120
|
-
const {useMetaImage, useFirstImage} = options;
|
|
121
|
-
|
|
122
|
-
let html = post.data?.html;
|
|
123
|
-
|
|
92
|
+
const { substackPodcastURL } = post;
|
|
93
|
+
const { useMetaImage, useFirstImage } = options;
|
|
94
|
+
let html = post.data.html;
|
|
124
95
|
// If there's no HTML, exit & return an empty string to avoid errors
|
|
125
96
|
if (!html) {
|
|
126
97
|
debug(`Post ${post.data.slug} has no HTML content`);
|
|
127
98
|
post.data.html = '';
|
|
128
99
|
return post;
|
|
129
100
|
}
|
|
130
|
-
|
|
131
101
|
// Parse the HTML inside a wrapper element so we have a global wrapper to target later on
|
|
132
102
|
const parsed = parseFragment(`<div class="migrate-substack-wrapper">${html}</div>`);
|
|
133
|
-
|
|
134
103
|
// Change paywall card to comment
|
|
135
104
|
parsed.$('.paywall-jump').forEach(el => {
|
|
136
105
|
replaceWith(el, '<!--members-only-->');
|
|
137
106
|
});
|
|
138
|
-
|
|
139
107
|
// Empty text elements are commonplace and are not needed
|
|
140
108
|
parsed.$('p').forEach(el => {
|
|
141
109
|
let content = serializeChildren(el).trim();
|
|
142
|
-
|
|
143
110
|
if (content.length === 0) {
|
|
144
111
|
el.remove();
|
|
145
112
|
}
|
|
146
113
|
});
|
|
147
|
-
|
|
148
114
|
parsed
|
|
149
115
|
.$('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
116
|
.forEach(el => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
117
|
+
replaceWith(el, serializeChildren(el));
|
|
118
|
+
});
|
|
154
119
|
// Wrap these in a HTML card so they can be handled by publishers as needed
|
|
155
120
|
parsed.$('div.latex-rendered').forEach(el => {
|
|
156
121
|
insertBefore(el, '<!--kg-card-begin: html-->');
|
|
157
122
|
insertAfter(el, '<!--kg-card-end: html-->');
|
|
158
123
|
});
|
|
159
|
-
|
|
160
124
|
// We don't currently handle these, so remove them to clean up the document
|
|
161
125
|
parsed.$('div.native-video-embed').forEach(el => {
|
|
162
126
|
el.remove();
|
|
163
127
|
});
|
|
164
|
-
|
|
165
128
|
parsed.$('div.poll-embed').forEach(el => {
|
|
166
129
|
el.remove();
|
|
167
130
|
});
|
|
168
|
-
|
|
169
131
|
parsed.$('.image3').forEach(el => {
|
|
170
|
-
const
|
|
171
|
-
const attrsObj = JSON.parse(attrs);
|
|
172
|
-
|
|
132
|
+
const attrsObj = JSON.parse(attr(el, 'data-attrs'));
|
|
173
133
|
let cardOpts = {
|
|
174
|
-
env: {dom: new SimpleDom.Document()},
|
|
134
|
+
env: { dom: new SimpleDom.Document() },
|
|
175
135
|
payload: {
|
|
176
136
|
src: largeImageUrl(attrsObj.src),
|
|
177
137
|
alt: attrsObj.title,
|
|
178
138
|
caption: attrsObj.title
|
|
179
139
|
}
|
|
180
140
|
};
|
|
181
|
-
|
|
182
|
-
replaceWith(el, serializer.serialize(imageCard.render(cardOpts)));
|
|
141
|
+
replaceWith(el, serialize(imageCard.render(cardOpts)));
|
|
183
142
|
});
|
|
184
|
-
|
|
185
143
|
// 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
|
|
186
|
-
if (post.data
|
|
144
|
+
if (post.data.og_image) {
|
|
187
145
|
if (useMetaImage) {
|
|
188
146
|
post.data.feature_image = largeImageUrl(post.data.og_image);
|
|
189
147
|
}
|
|
190
|
-
|
|
191
148
|
let firstElement = parsed.$('.migrate-substack-wrapper *')[0];
|
|
192
|
-
|
|
193
149
|
if (firstElement) {
|
|
194
150
|
const isImg = firstElement.tagName === 'IMG';
|
|
195
151
|
const hasImg = !isImg && parsed.$('img', firstElement).length > 0;
|
|
196
|
-
|
|
197
152
|
if (isImg || hasImg) {
|
|
198
153
|
let theElementItself = isImg ? firstElement : parsed.$('img', firstElement)[0];
|
|
199
154
|
let firstImgSrc = attr(theElementItself, 'src');
|
|
200
|
-
|
|
201
155
|
if (firstImgSrc.length > 0) {
|
|
202
156
|
let unsizedFirstSrc = getUnsizedImageName(firstImgSrc);
|
|
203
|
-
|
|
204
157
|
let ogImgSrc = post.data.og_image;
|
|
205
158
|
let unsizedOgSrc = getUnsizedImageName(ogImgSrc);
|
|
206
|
-
|
|
207
159
|
if (unsizedFirstSrc === unsizedOgSrc) {
|
|
208
160
|
if (parsed.$('figcaption', firstElement).length) {
|
|
209
|
-
post.data.feature_image_caption = serializeChildren(
|
|
210
|
-
parsed.$('figcaption', firstElement)[0]
|
|
211
|
-
);
|
|
161
|
+
post.data.feature_image_caption = serializeChildren(parsed.$('figcaption', firstElement)[0]);
|
|
212
162
|
}
|
|
213
|
-
|
|
214
163
|
firstElement.remove();
|
|
215
164
|
}
|
|
216
165
|
}
|
|
217
166
|
}
|
|
218
167
|
}
|
|
219
168
|
}
|
|
220
|
-
|
|
221
169
|
if (useFirstImage && !post.data.feature_image) {
|
|
222
170
|
let firstElement = parsed.$('.migrate-substack-wrapper *')[0];
|
|
223
|
-
|
|
224
171
|
if (firstElement) {
|
|
225
172
|
const isImg = firstElement.tagName === 'IMG';
|
|
226
173
|
const hasImg = !isImg && parsed.$('img', firstElement).length > 0;
|
|
227
|
-
|
|
228
174
|
if (isImg || hasImg) {
|
|
229
175
|
let theElementItself = isImg ? firstElement : parsed.$('img', firstElement)[0];
|
|
230
176
|
let firstImgSrc = attr(theElementItself, 'src');
|
|
231
|
-
|
|
232
|
-
if (
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
post.data.feature_image = unsizedFirstSrc;
|
|
237
|
-
|
|
238
|
-
if (parsed.$('figcaption', firstElement).length) {
|
|
239
|
-
post.data.feature_image_caption = serializeChildren(
|
|
240
|
-
parsed.$('figcaption', firstElement)[0]
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
firstElement.remove();
|
|
177
|
+
const unsizedFirstSrc = firstImgSrc.length > 0 ? largeImageUrl(firstImgSrc) : '';
|
|
178
|
+
if (unsizedFirstSrc) {
|
|
179
|
+
post.data.feature_image = unsizedFirstSrc;
|
|
180
|
+
if (parsed.$('figcaption', firstElement).length) {
|
|
181
|
+
post.data.feature_image_caption = serializeChildren(parsed.$('figcaption', firstElement)[0]);
|
|
245
182
|
}
|
|
183
|
+
firstElement.remove();
|
|
246
184
|
}
|
|
247
185
|
}
|
|
248
186
|
}
|
|
249
187
|
}
|
|
250
|
-
|
|
251
188
|
// If we have a podcast URL `posts.csv`, embed an audio card at the start of the document
|
|
252
189
|
// Else, if we have scraped a podcast URL, embed that instead
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
env: {dom: new SimpleDom.Document()},
|
|
267
|
-
payload: {
|
|
268
|
-
src: audioSrc,
|
|
269
|
-
title: post.data.title
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
const buildCard = audioCard.render(cardOpts);
|
|
274
|
-
const cardHTML = buildCard.nodeValue;
|
|
275
|
-
|
|
276
|
-
const wrapper = parsed.$('.migrate-substack-wrapper')[0];
|
|
277
|
-
wrapper.insertAdjacentHTML('afterbegin', cardHTML);
|
|
278
|
-
}
|
|
190
|
+
const audioSrc = substackPodcastURL || post.data.podcast_audio_src;
|
|
191
|
+
if (audioSrc) {
|
|
192
|
+
debug(`Post ${post.data.slug} has podcast episode from ${substackPodcastURL ? 'CSV' : 'scrape'}`);
|
|
193
|
+
let cardOpts = {
|
|
194
|
+
env: { dom: new SimpleDom.Document() },
|
|
195
|
+
payload: {
|
|
196
|
+
src: audioSrc,
|
|
197
|
+
title: post.data.title
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const cardHTML = audioCard.render(cardOpts).nodeValue;
|
|
201
|
+
const wrapper = parsed.$('.migrate-substack-wrapper')[0];
|
|
202
|
+
wrapper.insertAdjacentHTML('afterbegin', cardHTML);
|
|
279
203
|
}
|
|
280
|
-
|
|
281
204
|
parsed.$('div.tweet').forEach(el => {
|
|
282
205
|
let childAnchors = parsed.$(':scope > a', el);
|
|
283
|
-
let src = attr(childAnchors[0], 'href');
|
|
284
|
-
let parsedUrl = url.parse(src);
|
|
285
|
-
|
|
286
|
-
if (parsedUrl.search) {
|
|
287
|
-
// remove possible query params
|
|
288
|
-
parsedUrl.search = null;
|
|
289
|
-
}
|
|
290
|
-
src = url.format(parsedUrl, {search: false});
|
|
291
|
-
|
|
206
|
+
let src = withoutSearch(attr(childAnchors[0], 'href'));
|
|
292
207
|
const tweetTextEl = parsed.$('.tweet-text', el)[0];
|
|
293
208
|
let tweetText = tweetTextEl ? serializeChildren(tweetTextEl).replace(/(?:\r\n|\r|\n)/g, '<br>') : false;
|
|
294
209
|
const tweetAuthorNameEl = parsed.$('.tweet-author-name', el)[0];
|
|
@@ -296,117 +211,78 @@ const processContent = (post, siteUrl, options) => {
|
|
|
296
211
|
const tweetAuthorHandleEl = parsed.$('.tweet-author-handle', el)[0];
|
|
297
212
|
let tweetAuthorHandle = tweetAuthorHandleEl ? serializeChildren(tweetAuthorHandleEl).trim() : false;
|
|
298
213
|
const tweetDateTimeEl = parsed.$('.tweet-date', el)[0];
|
|
299
|
-
let tweetDateTime = tweetDateTimeEl ?
|
|
214
|
+
let tweetDateTime = tweetDateTimeEl ? tweetDateTimeEl.textContent.trim() : false;
|
|
300
215
|
const tweetLinkEl = parsed.$('.tweet-link-top', el)[0];
|
|
301
216
|
let tweetURL = tweetLinkEl ? attr(tweetLinkEl, 'href') : false;
|
|
302
|
-
|
|
303
217
|
let theHtml = [];
|
|
304
|
-
|
|
305
218
|
if (tweetText) {
|
|
306
219
|
theHtml.push(`<p lang="en" dir="ltr">${tweetText}</p>`);
|
|
307
220
|
}
|
|
308
|
-
|
|
309
221
|
theHtml.push(`—`);
|
|
310
|
-
|
|
311
222
|
if (tweetAuthorName) {
|
|
312
223
|
theHtml.push(`${tweetAuthorName}`);
|
|
313
224
|
}
|
|
314
|
-
|
|
315
225
|
if (tweetAuthorHandle) {
|
|
316
226
|
theHtml.push(`(${tweetAuthorHandle})`);
|
|
317
227
|
}
|
|
318
|
-
|
|
319
228
|
if (tweetURL) {
|
|
320
229
|
theHtml.push(`<a href="${tweetURL}">`);
|
|
321
230
|
}
|
|
322
|
-
|
|
323
231
|
if (tweetDateTime) {
|
|
324
232
|
theHtml.push(`${tweetDateTime}`);
|
|
325
233
|
}
|
|
326
|
-
|
|
327
234
|
theHtml.push(`</a>`);
|
|
328
|
-
|
|
329
235
|
const tweetHtml = `<figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet">${theHtml.join(' ')}<a href="${src}"></a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></figure>`;
|
|
330
236
|
replaceWith(el, tweetHtml);
|
|
331
237
|
});
|
|
332
|
-
|
|
333
238
|
parsed.$('.image-gallery-embed').forEach(el => {
|
|
334
|
-
const
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
let items = [];
|
|
338
|
-
|
|
339
|
-
attrsObj.gallery.images.forEach(item => {
|
|
239
|
+
const attrsObj = JSON.parse(attr(el, 'data-attrs'));
|
|
240
|
+
const items = attrsObj.gallery.images.map((item, index) => {
|
|
340
241
|
const dimensions = getImageDimensions(item.src);
|
|
341
|
-
|
|
342
|
-
items.push({
|
|
242
|
+
return {
|
|
343
243
|
fileName: basename(item.src),
|
|
344
244
|
src: largeImageUrl(item.src),
|
|
345
245
|
width: dimensions ? dimensions.width : '100',
|
|
346
|
-
height: dimensions ? dimensions.height : '100'
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
items = items.map((item, index) => {
|
|
351
|
-
return {
|
|
352
|
-
...item,
|
|
246
|
+
height: dimensions ? dimensions.height : '100',
|
|
353
247
|
row: Math.floor(index / 3)
|
|
354
248
|
};
|
|
355
249
|
});
|
|
356
|
-
|
|
357
250
|
let cardOpts = {
|
|
358
|
-
env: {dom: new SimpleDom.Document()},
|
|
251
|
+
env: { dom: new SimpleDom.Document() },
|
|
359
252
|
payload: {
|
|
360
253
|
images: items,
|
|
361
254
|
caption: attrsObj.gallery.caption
|
|
362
255
|
}
|
|
363
256
|
};
|
|
364
|
-
|
|
365
|
-
replaceWith(el, serializer.serialize(galleryCard.render(cardOpts)));
|
|
257
|
+
replaceWith(el, serialize(galleryCard.render(cardOpts)));
|
|
366
258
|
});
|
|
367
|
-
|
|
368
259
|
parsed.$('[class*="ImageGallery-module__imageGallery"]').forEach(el => {
|
|
369
260
|
const rows = parsed.$('[class*="ImageGallery-module__imageRow"]', el);
|
|
370
261
|
const figcaptionEl = parsed.$('figcaption', el)[0];
|
|
371
262
|
const caption = figcaptionEl ? serializeChildren(figcaptionEl) : null;
|
|
372
|
-
|
|
373
|
-
let items = [];
|
|
374
|
-
|
|
263
|
+
const items = [];
|
|
375
264
|
rows.forEach(row => {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
pictures.forEach(picture => {
|
|
379
|
-
const img = parsed.$('img', picture)[0];
|
|
380
|
-
const src = largestSrc(img);
|
|
265
|
+
parsed.$('picture', row).forEach(picture => {
|
|
266
|
+
const src = largestSrc(parsed.$('img', picture)[0]);
|
|
381
267
|
const dimensions = getImageDimensions(src);
|
|
382
|
-
|
|
383
268
|
items.push({
|
|
384
269
|
fileName: basename(src),
|
|
385
270
|
src: src,
|
|
386
271
|
width: dimensions ? dimensions.width : '100',
|
|
387
|
-
height: dimensions ? dimensions.height : '100'
|
|
272
|
+
height: dimensions ? dimensions.height : '100',
|
|
273
|
+
row: Math.floor(items.length / 3)
|
|
388
274
|
});
|
|
389
275
|
});
|
|
390
276
|
});
|
|
391
|
-
|
|
392
|
-
items = items.map((item, index) => {
|
|
393
|
-
return {
|
|
394
|
-
...item,
|
|
395
|
-
row: Math.floor(index / 3)
|
|
396
|
-
};
|
|
397
|
-
});
|
|
398
|
-
|
|
399
277
|
let cardOpts = {
|
|
400
|
-
env: {dom: new SimpleDom.Document()},
|
|
278
|
+
env: { dom: new SimpleDom.Document() },
|
|
401
279
|
payload: {
|
|
402
280
|
images: items,
|
|
403
281
|
caption: caption
|
|
404
282
|
}
|
|
405
283
|
};
|
|
406
|
-
|
|
407
|
-
replaceWith(el, serializer.serialize(galleryCard.render(cardOpts)));
|
|
284
|
+
replaceWith(el, serialize(galleryCard.render(cardOpts)));
|
|
408
285
|
});
|
|
409
|
-
|
|
410
286
|
parsed.$('.captioned-image-container').forEach(div => {
|
|
411
287
|
const imgAltEl = parsed.$('img[alt]', div)[0];
|
|
412
288
|
const imgAlt = imgAltEl ? attr(imgAltEl, 'alt') : '';
|
|
@@ -416,47 +292,39 @@ const processContent = (post, siteUrl, options) => {
|
|
|
416
292
|
const imgCaption = figcaptionEl ? serializeChildren(figcaptionEl) : false;
|
|
417
293
|
const imgEl = parsed.$('img', div)[0];
|
|
418
294
|
const imageSrc = largestSrc(imgEl);
|
|
419
|
-
|
|
420
295
|
let cardOpts = {
|
|
421
|
-
env: {dom: new SimpleDom.Document()},
|
|
296
|
+
env: { dom: new SimpleDom.Document() },
|
|
422
297
|
payload: {
|
|
423
298
|
src: imageSrc,
|
|
424
299
|
alt: imgAlt,
|
|
425
300
|
caption: imgCaption
|
|
426
301
|
}
|
|
427
302
|
};
|
|
428
|
-
|
|
429
303
|
if (imageSrc !== linkHref) {
|
|
430
304
|
cardOpts.payload.href = linkHref;
|
|
431
305
|
}
|
|
432
|
-
|
|
433
|
-
replaceWith(div, serializer.serialize(imageCard.render(cardOpts)));
|
|
306
|
+
replaceWith(div, serialize(imageCard.render(cardOpts)));
|
|
434
307
|
});
|
|
435
|
-
|
|
436
308
|
parsed.$('.image-link').forEach(anchor => {
|
|
437
309
|
const imgAltEl = parsed.$('img[alt]', anchor)[0];
|
|
438
310
|
const imgAlt = imgAltEl ? attr(imgAltEl, 'alt') : '';
|
|
439
311
|
const linkHref = attr(anchor, 'href');
|
|
440
312
|
const imgEl = parsed.$('img', anchor)[0];
|
|
441
313
|
const imageSrc = largestSrc(imgEl);
|
|
442
|
-
|
|
443
314
|
let cardOpts = {
|
|
444
|
-
env: {dom: new SimpleDom.Document()},
|
|
315
|
+
env: { dom: new SimpleDom.Document() },
|
|
445
316
|
payload: {
|
|
446
317
|
src: imageSrc,
|
|
447
318
|
alt: imgAlt
|
|
448
319
|
}
|
|
449
320
|
};
|
|
450
|
-
|
|
451
321
|
// If the anchor links to the image itself
|
|
452
322
|
const splitRegexp = /public\/images\/|public%2Fimages%2F/;
|
|
453
323
|
if (imageSrc.split(splitRegexp)[1] !== linkHref.split(splitRegexp)[1]) {
|
|
454
324
|
cardOpts.payload.href = linkHref;
|
|
455
325
|
}
|
|
456
|
-
|
|
457
|
-
replaceWith(anchor, serializer.serialize(imageCard.render(cardOpts)));
|
|
326
|
+
replaceWith(anchor, serialize(imageCard.render(cardOpts)));
|
|
458
327
|
});
|
|
459
|
-
|
|
460
328
|
parsed.$('.file-embed-wrapper').forEach(el => {
|
|
461
329
|
const fileSrcEl = parsed.$('.file-embed-button', el)[0];
|
|
462
330
|
const fileSrc = attr(fileSrcEl, 'href');
|
|
@@ -464,34 +332,16 @@ const processContent = (post, siteUrl, options) => {
|
|
|
464
332
|
const fileTitle = fileTitleEl ? fileTitleEl.textContent : '';
|
|
465
333
|
const fileDetailsEl = parsed.$('.file-embed-details-h2', el)[0];
|
|
466
334
|
const fileDetails = fileDetailsEl ? fileDetailsEl.textContent : '';
|
|
467
|
-
|
|
468
335
|
const fileSizeMatch = fileDetails.match(/([\d.]+)([KMGT]B)/i);
|
|
469
336
|
let fileSizeBytes = 0;
|
|
470
|
-
|
|
471
337
|
if (fileSizeMatch) {
|
|
472
338
|
const size = parseFloat(fileSizeMatch[1]);
|
|
473
339
|
const unit = fileSizeMatch[2].toUpperCase();
|
|
474
|
-
|
|
475
|
-
|
|
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;
|
|
490
|
-
}
|
|
340
|
+
const powers = { KB: 1, MB: 2, GB: 3, TB: 4 };
|
|
341
|
+
fileSizeBytes = size * 1024 ** powers[unit];
|
|
491
342
|
}
|
|
492
|
-
|
|
493
343
|
const cardOpts = {
|
|
494
|
-
env: {dom: new SimpleDom.Document()},
|
|
344
|
+
env: { dom: new SimpleDom.Document() },
|
|
495
345
|
payload: {
|
|
496
346
|
src: fileSrc,
|
|
497
347
|
fileTitle: fileTitle,
|
|
@@ -499,130 +349,84 @@ const processContent = (post, siteUrl, options) => {
|
|
|
499
349
|
fileSize: fileSizeBytes
|
|
500
350
|
}
|
|
501
351
|
};
|
|
502
|
-
|
|
503
|
-
replaceWith(el, serializer.serialize(fileCard.render(cardOpts)));
|
|
352
|
+
replaceWith(el, serialize(fileCard.render(cardOpts)));
|
|
504
353
|
});
|
|
505
|
-
|
|
506
354
|
parsed.$('.comment').forEach(el => {
|
|
507
355
|
el.remove();
|
|
508
356
|
});
|
|
509
|
-
|
|
510
357
|
parsed.$('.mention-wrap').forEach(el => {
|
|
511
|
-
const attrsRaw = attr(el, 'data-attrs');
|
|
512
|
-
|
|
513
358
|
let attrs;
|
|
514
|
-
|
|
515
359
|
try {
|
|
516
|
-
attrs = JSON.parse(
|
|
517
|
-
}
|
|
360
|
+
attrs = JSON.parse(attr(el, 'data-attrs'));
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
518
363
|
return;
|
|
519
364
|
}
|
|
520
|
-
|
|
521
365
|
const name = attrs.name;
|
|
522
366
|
const id = attrs.id;
|
|
523
|
-
|
|
524
367
|
if (name && id) {
|
|
525
|
-
replaceWith(el, `<a href="https://open.substack.com/users/${id}">${name}</a>`);
|
|
368
|
+
replaceWith(el, `<a href="https://open.substack.com/users/${escapeHTML(String(id))}">${escapeHTML(String(name))}</a>`);
|
|
526
369
|
}
|
|
527
370
|
});
|
|
528
|
-
|
|
529
371
|
parsed.$('.digest-post-embed').forEach(el => {
|
|
530
|
-
const attrsRaw = attr(el, 'data-attrs');
|
|
531
|
-
|
|
532
372
|
let attrs;
|
|
533
|
-
|
|
534
373
|
// Return early if JSON is invalid
|
|
535
374
|
try {
|
|
536
|
-
attrs = JSON.parse(
|
|
537
|
-
}
|
|
375
|
+
attrs = JSON.parse(attr(el, 'data-attrs'));
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
538
378
|
return;
|
|
539
379
|
}
|
|
540
|
-
|
|
541
380
|
const postUrl = attrs.canonical_url;
|
|
542
|
-
const postTitle = attrs.title;
|
|
543
|
-
const postCaption = attrs.caption;
|
|
544
|
-
const postImage = attrs.cover_image;
|
|
545
|
-
const postIcon = attrs.publication_logo_url;
|
|
546
|
-
const postAuthor = attrs?.publishedBylines[0]?.name ?? null;
|
|
547
|
-
const postPubName = attrs.publication_name;
|
|
548
|
-
|
|
549
381
|
let cardOpts = {
|
|
550
|
-
env: {dom: new SimpleDom.Document()},
|
|
382
|
+
env: { dom: new SimpleDom.Document() },
|
|
551
383
|
payload: {
|
|
552
384
|
url: postUrl,
|
|
553
385
|
metadata: {
|
|
554
386
|
url: postUrl,
|
|
555
|
-
title:
|
|
556
|
-
description:
|
|
557
|
-
icon: largeImageUrl(
|
|
558
|
-
thumbnail:
|
|
559
|
-
publisher:
|
|
560
|
-
author:
|
|
387
|
+
title: attrs.title,
|
|
388
|
+
description: attrs.caption,
|
|
389
|
+
icon: largeImageUrl(attrs.publication_logo_url),
|
|
390
|
+
thumbnail: attrs.cover_image,
|
|
391
|
+
publisher: attrs.publication_name,
|
|
392
|
+
author: attrs?.publishedBylines[0]?.name ?? null
|
|
561
393
|
},
|
|
562
394
|
caption: null
|
|
563
395
|
}
|
|
564
396
|
};
|
|
565
|
-
|
|
566
|
-
const bookmarkHtml = serializer.serialize(bookmarkCard.render(cardOpts));
|
|
567
|
-
|
|
568
|
-
replaceWith(el, bookmarkHtml);
|
|
397
|
+
replaceWith(el, serialize(bookmarkCard.render(cardOpts)));
|
|
569
398
|
});
|
|
570
|
-
|
|
571
399
|
parsed.$('a > style').forEach(style => {
|
|
572
400
|
style.remove();
|
|
573
401
|
});
|
|
574
|
-
|
|
575
402
|
parsed.$('ul, ol').forEach(list => {
|
|
576
403
|
if (parsed.$('img, div, figure, blockquote, .button-wrapper', list).length) {
|
|
577
404
|
insertBefore(list, '<!--kg-card-begin: html-->');
|
|
578
405
|
insertAfter(list, '<!--kg-card-end: html-->');
|
|
579
406
|
}
|
|
580
407
|
});
|
|
581
|
-
|
|
582
408
|
// Remove Substack share buttons
|
|
583
409
|
parsed.$('p.button-wrapper').forEach(button => {
|
|
584
410
|
let shareLinks = parsed.$(':scope > a.button', button);
|
|
585
411
|
if (shareLinks.length === 1 && siteUrl) {
|
|
586
|
-
let
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
// If it's a share button, there's no use for it and completely remove the button
|
|
591
|
-
if (parsedUrl.search && parsedUrl.search.indexOf('action=share') >= 0) {
|
|
592
|
-
button.remove();
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
// If it's a gift button, there's no use for it and completely remove the button
|
|
597
|
-
if (parsedUrl.search && parsedUrl.search.indexOf('gift=true') >= 0) {
|
|
412
|
+
let search = url.parse(attr(shareLinks[0], 'href')).search;
|
|
413
|
+
// If it's a share or gift button, there's no use for it, so completely remove the button
|
|
414
|
+
if (search && (search.indexOf('action=share') >= 0 || search.indexOf('gift=true') >= 0)) {
|
|
598
415
|
button.remove();
|
|
599
|
-
return;
|
|
600
416
|
}
|
|
601
417
|
}
|
|
602
418
|
});
|
|
603
|
-
|
|
604
419
|
// Update button elements
|
|
605
420
|
parsed.$('p.button-wrapper').forEach(button => {
|
|
606
421
|
let buttons = parsed.$(':scope > a.button', button);
|
|
607
422
|
if (buttons.length === 1 && siteUrl) {
|
|
608
|
-
let siteRegex = new RegExp(
|
|
609
|
-
`^(?:${escapeStringRegexp(siteUrl)}(?:\\/?)(?:p\\/)?)([a-zA-Z-_\\d]*)(?:\\/?)`,
|
|
610
|
-
'gi'
|
|
611
|
-
);
|
|
423
|
+
let siteRegex = new RegExp(`^(?:${escapeStringRegexp(siteUrl)}(?:\\/?)(?:p\\/)?)([a-zA-Z-_\\d]*)(?:\\/?)`, 'gi');
|
|
612
424
|
let buttonLink = buttons[0];
|
|
613
|
-
let buttonHref = attr(buttonLink, 'href');
|
|
614
425
|
let buttonText = buttonLink.textContent;
|
|
615
|
-
let
|
|
616
|
-
|
|
617
|
-
// remove possible query params
|
|
618
|
-
parsedUrl.search = null;
|
|
619
|
-
|
|
620
|
-
buttonHref = url.format(parsedUrl, {search: false});
|
|
621
|
-
|
|
426
|
+
let buttonHref = withoutSearch(attr(buttonLink, 'href'));
|
|
622
427
|
if (buttonHref.match(siteRegex)) {
|
|
623
428
|
buttonHref = buttonHref.replace(siteRegex, '/$1/');
|
|
624
429
|
}
|
|
625
|
-
|
|
626
430
|
if (buttonHref === '/subscribe/') {
|
|
627
431
|
if (options.noSubscribeButtons) {
|
|
628
432
|
button.remove();
|
|
@@ -630,16 +434,13 @@ const processContent = (post, siteUrl, options) => {
|
|
|
630
434
|
}
|
|
631
435
|
buttonHref = options.subscribeLink || '#/portal/signup';
|
|
632
436
|
}
|
|
633
|
-
|
|
634
437
|
if (buttonHref.endsWith('/comments')) {
|
|
635
438
|
buttonHref = options.commentLink || '#ghost-comments-root';
|
|
636
|
-
|
|
637
439
|
if (!options.comments) {
|
|
638
440
|
button.remove();
|
|
639
441
|
return;
|
|
640
442
|
}
|
|
641
443
|
}
|
|
642
|
-
|
|
643
444
|
const card = parsed.document.createElement('div');
|
|
644
445
|
card.className = 'kg-card kg-button-card kg-align-center';
|
|
645
446
|
const link = parsed.document.createElement('a');
|
|
@@ -650,50 +451,34 @@ const processContent = (post, siteUrl, options) => {
|
|
|
650
451
|
replaceWith(button, card);
|
|
651
452
|
}
|
|
652
453
|
});
|
|
653
|
-
|
|
654
454
|
// TODO: this should be a parser plugin
|
|
655
455
|
// Wrap nested lists in HTML card
|
|
656
456
|
parsed.$('ul li ul, ol li ol, ol li ul, ul li ol').forEach(nestedList => {
|
|
657
457
|
const parentList = parents(nestedList, 'ul, ol')[0];
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
insertAfter(parentList, '<!--kg-card-end: html-->');
|
|
661
|
-
}
|
|
458
|
+
insertBefore(parentList, '<!--kg-card-begin: html-->');
|
|
459
|
+
insertAfter(parentList, '<!--kg-card-end: html-->');
|
|
662
460
|
});
|
|
663
|
-
|
|
664
461
|
// Handle footnotes
|
|
665
462
|
let footnotesItems = [];
|
|
666
|
-
let footnotesCount = 0;
|
|
667
463
|
parsed.$('.footnote').forEach(el => {
|
|
668
464
|
const footnoteAnchor = parsed.$('a', el)[0];
|
|
669
465
|
let footnoteBodyAnchor = attr(footnoteAnchor, 'href');
|
|
670
|
-
|
|
671
466
|
let footnoteID = attr(el, 'id') || attr(footnoteAnchor, 'id');
|
|
672
|
-
|
|
673
467
|
let footnoteNumber = footnoteID.replace('footnote-', '');
|
|
674
468
|
let footnoteContent = parsed.$('.footnote-content', el)[0];
|
|
675
|
-
|
|
676
469
|
const pElements = parsed.$('p', footnoteContent);
|
|
677
470
|
const lastP = pElements[pElements.length - 1];
|
|
678
471
|
if (lastP) {
|
|
679
|
-
lastP.insertAdjacentHTML(
|
|
680
|
-
'beforeend',
|
|
681
|
-
` <a href="${footnoteBodyAnchor}" title="Jump back to footnote ${footnoteNumber} in the text.">↩</a>`
|
|
682
|
-
);
|
|
472
|
+
lastP.insertAdjacentHTML('beforeend', ` <a href="${footnoteBodyAnchor}" title="Jump back to footnote ${footnoteNumber} in the text.">↩</a>`);
|
|
683
473
|
}
|
|
684
|
-
|
|
685
474
|
footnotesItems.push(`<li id="${footnoteID}">${serializeChildren(footnoteContent)}</li>`);
|
|
686
475
|
el.remove();
|
|
687
|
-
|
|
688
|
-
footnotesCount = footnotesCount + 1;
|
|
689
476
|
});
|
|
690
|
-
|
|
691
|
-
if (footnotesCount > 0) {
|
|
477
|
+
if (footnotesItems.length > 0) {
|
|
692
478
|
let footnotedHTML = `<div class="footnotes"><hr><ol>${footnotesItems.join('')}</ol></div>`;
|
|
693
479
|
const wrapper = parsed.$('.migrate-substack-wrapper')[0];
|
|
694
480
|
wrapper.insertAdjacentHTML('beforeend', `<!--kg-card-begin: html-->${footnotedHTML}<!--kg-card-end: html-->`);
|
|
695
481
|
}
|
|
696
|
-
|
|
697
482
|
// Wrap content that has footnote anchors in HTML tags to retain the footnote jump anchor
|
|
698
483
|
parsed.$('p, ul, ol').forEach(el => {
|
|
699
484
|
if (parsed.$('a.footnote-anchor', el).length > 0) {
|
|
@@ -701,49 +486,34 @@ const processContent = (post, siteUrl, options) => {
|
|
|
701
486
|
insertAfter(el, '<!--kg-card-end: html-->');
|
|
702
487
|
}
|
|
703
488
|
});
|
|
704
|
-
|
|
705
489
|
// Remove or replace subscribe links on the same domain
|
|
706
|
-
if (options.noSubscribeButtons) {
|
|
490
|
+
if (options.noSubscribeButtons || options.subscribeLink) {
|
|
707
491
|
parsed.$('a').forEach(anchor => {
|
|
708
492
|
let href = attr(anchor, 'href');
|
|
709
493
|
let linkRegex = new RegExp(`^(${escapeStringRegexp(siteUrl)})?(/subscribe)(.*)`, 'gi');
|
|
710
|
-
|
|
711
494
|
if (!href) {
|
|
712
495
|
return;
|
|
713
496
|
}
|
|
714
|
-
|
|
715
497
|
let matches = href.replace(linkRegex, '$2');
|
|
716
|
-
|
|
717
498
|
if (matches === '/subscribe') {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
let linkRegex = new RegExp(`^(${escapeStringRegexp(siteUrl)})?(/subscribe)(.*)`, 'gi');
|
|
725
|
-
|
|
726
|
-
if (!href) {
|
|
727
|
-
return;
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
let matches = href.replace(linkRegex, '$2');
|
|
731
|
-
|
|
732
|
-
if (matches === '/subscribe') {
|
|
733
|
-
attr(anchor, 'href', options.subscribeLink);
|
|
499
|
+
if (options.noSubscribeButtons) {
|
|
500
|
+
anchor.remove();
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
attr(anchor, 'href', options.subscribeLink);
|
|
504
|
+
}
|
|
734
505
|
}
|
|
735
506
|
});
|
|
736
507
|
}
|
|
737
|
-
|
|
738
508
|
// Remove or replace signup forms with a Portal signup button
|
|
739
509
|
if (options.noSubscribeButtons) {
|
|
740
510
|
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(div => {
|
|
741
511
|
div.remove();
|
|
742
512
|
});
|
|
743
|
-
}
|
|
513
|
+
}
|
|
514
|
+
else if (options.subscribeLink) {
|
|
744
515
|
parsed.$('.subscription-widget-wrap, .subscription-widget-wrap-editor').forEach(div => {
|
|
745
516
|
const hasForm = parsed.$('form', div);
|
|
746
|
-
|
|
747
517
|
if (hasForm.length) {
|
|
748
518
|
const submitEl = parsed.$('form input[type="submit"]', div)[0];
|
|
749
519
|
const buttonText = submitEl ? attr(submitEl, 'value') : '';
|
|
@@ -758,181 +528,56 @@ const processContent = (post, siteUrl, options) => {
|
|
|
758
528
|
}
|
|
759
529
|
});
|
|
760
530
|
}
|
|
761
|
-
|
|
762
531
|
parsed.$('.embedded-post-wrap').forEach(div => {
|
|
763
|
-
const attrs = attr(div, 'data-attrs')
|
|
764
|
-
|
|
532
|
+
const attrs = attr(div, 'data-attrs');
|
|
765
533
|
if (!attrs) {
|
|
766
534
|
return;
|
|
767
535
|
}
|
|
768
|
-
|
|
769
536
|
// CASE: Some embedded posts are double-encoded, so we need to decode twice
|
|
770
537
|
let bookmarkJSON = JSON.parse(decode(decode(attrs)));
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
let
|
|
774
|
-
let
|
|
775
|
-
let
|
|
776
|
-
let
|
|
777
|
-
|
|
538
|
+
// The fields are decoded text, so they are escaped before they go back into HTML.
|
|
539
|
+
const field = (name) => escapeHTML(String(bookmarkJSON[name]));
|
|
540
|
+
let bookmarkLink = field('url');
|
|
541
|
+
let bookmarkPubName = field('publication_name');
|
|
542
|
+
let bookmarkPubIcon = field('publication_logo_url');
|
|
543
|
+
let bookmarkTitle = field('title');
|
|
544
|
+
let bookmarkContent = field('truncated_body_text');
|
|
778
545
|
const embeddedHtml = `<figure class="kg-card kg-bookmark-card"><a class="kg-bookmark-container" href="${bookmarkLink}"><div class="kg-bookmark-content"><div class="kg-bookmark-title">${bookmarkTitle}</div><div class="kg-bookmark-description">${bookmarkContent}</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="${bookmarkPubIcon}"><span class="kg-bookmark-author">${bookmarkPubName}</span></div></div></a></figure>`;
|
|
779
|
-
|
|
780
546
|
replaceWith(div, `<!--kg-card-begin: html-->${embeddedHtml}<!--kg-card-end: html-->`);
|
|
781
547
|
});
|
|
782
|
-
|
|
783
548
|
parsed.$('div.instagram').forEach(el => {
|
|
784
549
|
const instagramLink = parsed.$('a.instagram-image', el)[0];
|
|
785
550
|
let src = instagramLink ? attr(instagramLink, 'href') : '';
|
|
786
|
-
|
|
787
551
|
if (!src) {
|
|
788
552
|
return;
|
|
789
553
|
}
|
|
790
|
-
|
|
791
|
-
let parsedUrl = url.parse(src);
|
|
792
|
-
|
|
793
|
-
if (parsedUrl.search) {
|
|
794
|
-
// remove possible query params
|
|
795
|
-
parsedUrl.search = null;
|
|
796
|
-
}
|
|
797
|
-
src = url.format(parsedUrl, {search: false});
|
|
798
|
-
|
|
554
|
+
src = withoutSearch(src);
|
|
799
555
|
// Trim the trailing slash from src if it exists
|
|
800
556
|
if (src.endsWith('/')) {
|
|
801
557
|
src = src.slice(0, -1);
|
|
802
558
|
}
|
|
803
|
-
|
|
804
559
|
const instagramHtml = `<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="${src}/embed/captioned/"></iframe><script async="" src="//www.instagram.com/embed.js"></script></figure>`;
|
|
805
560
|
replaceWith(el, instagramHtml);
|
|
806
561
|
});
|
|
807
|
-
|
|
808
562
|
// For each image and link, alter the path to remove cropping & sizing for image paths
|
|
809
563
|
parsed.$('img[src], a[href]').forEach(el => {
|
|
810
564
|
const src = attr(el, 'src');
|
|
811
565
|
const href = attr(el, 'href');
|
|
812
|
-
|
|
813
566
|
if (src) {
|
|
814
567
|
attr(el, 'src', largeImageUrl(src));
|
|
815
568
|
}
|
|
816
|
-
|
|
817
569
|
if (href) {
|
|
818
570
|
attr(el, 'href', largeImageUrl(href));
|
|
819
571
|
}
|
|
820
572
|
});
|
|
821
|
-
|
|
822
573
|
// convert HTML back to a string
|
|
823
574
|
const wrapper = parsed.$('.migrate-substack-wrapper')[0];
|
|
824
575
|
html = serializeChildren(wrapper);
|
|
825
|
-
|
|
826
576
|
// Remove empty attributes
|
|
827
577
|
html = html.replace(/=""/g, '');
|
|
828
|
-
|
|
829
578
|
// Apply our new HTML back to the post object
|
|
830
579
|
post.data.html = html.trim();
|
|
831
|
-
|
|
832
580
|
return post;
|
|
833
581
|
};
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
// Add tags to the post
|
|
837
|
-
const typeSlug = slugify(post.substackData.type);
|
|
838
|
-
const visibilitySlug = slugify(post.substackData.audience);
|
|
839
|
-
const {addTag, addPlatformTag, addTypeTag, addAccessTag} = options;
|
|
840
|
-
|
|
841
|
-
// Add a type tag, e.g. Newsletter, Podcast, etc.
|
|
842
|
-
const typeSlugSlugify = slugify(typeSlug);
|
|
843
|
-
post.data.tags.push({
|
|
844
|
-
url: `${siteUrl}/tag/${typeSlugSlugify}`,
|
|
845
|
-
data: {
|
|
846
|
-
slug: typeSlugSlugify,
|
|
847
|
-
name: startCase(typeSlug)
|
|
848
|
-
}
|
|
849
|
-
});
|
|
850
|
-
|
|
851
|
-
// Add a custom tag if one is provided
|
|
852
|
-
if (addTag) {
|
|
853
|
-
let trimmedTag = addTag.trim();
|
|
854
|
-
let trimmedTagSlug = slugify(trimmedTag);
|
|
855
|
-
post.data.tags.push({
|
|
856
|
-
url: `${siteUrl}/tag/${trimmedTagSlug}`,
|
|
857
|
-
data: {
|
|
858
|
-
slug: trimmedTagSlug,
|
|
859
|
-
name: trimmedTag
|
|
860
|
-
}
|
|
861
|
-
});
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
// Add a platform tag
|
|
865
|
-
if (addPlatformTag) {
|
|
866
|
-
post.data.tags.push({
|
|
867
|
-
url: `migrator-added-tag`,
|
|
868
|
-
data: {
|
|
869
|
-
slug: `hash-substack`,
|
|
870
|
-
name: `#substack`
|
|
871
|
-
}
|
|
872
|
-
});
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
// Add an internal tag based on the type of post
|
|
876
|
-
if (addTypeTag) {
|
|
877
|
-
post.data.tags.push({
|
|
878
|
-
url: `migrator-added-tag-substack-type-${typeSlug}`,
|
|
879
|
-
data: {
|
|
880
|
-
slug: `hash-substack-type-${typeSlug}`,
|
|
881
|
-
name: `#substack-type-${typeSlug}`
|
|
882
|
-
}
|
|
883
|
-
});
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
// Add tags based on post visibility
|
|
887
|
-
if (addAccessTag) {
|
|
888
|
-
post.data.tags.push({
|
|
889
|
-
url: `migrator-added-tag-substack-access-${visibilitySlug}`,
|
|
890
|
-
data: {
|
|
891
|
-
slug: `hash-substack-access-${visibilitySlug}`,
|
|
892
|
-
name: `#substack-access-${visibilitySlug}`
|
|
893
|
-
}
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
// And now process the HTML
|
|
898
|
-
post = processContent(post, siteUrl, options);
|
|
899
|
-
|
|
900
|
-
return post;
|
|
901
|
-
};
|
|
902
|
-
|
|
903
|
-
export default async (input, ctx) => {
|
|
904
|
-
let {postsDir, options} = ctx;
|
|
905
|
-
let {url: siteUrl} = options;
|
|
906
|
-
const output = {};
|
|
907
|
-
|
|
908
|
-
// console.log('in default');
|
|
909
|
-
// console.log(input.posts);
|
|
910
|
-
|
|
911
|
-
if (postsDir) {
|
|
912
|
-
try {
|
|
913
|
-
let postFiles = await getFiles(postsDir);
|
|
914
|
-
let postContent = await readFiles(postFiles, postsDir);
|
|
915
|
-
|
|
916
|
-
input.posts.map(post => {
|
|
917
|
-
// eslint-disable-line array-callback-return
|
|
918
|
-
post.data.html = postContent[post.substackId];
|
|
919
|
-
});
|
|
920
|
-
} catch (error) {
|
|
921
|
-
return new errors.InternalServerError({message: "Couldn't read post files"});
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
if (input.posts && input.posts.length > 0) {
|
|
926
|
-
output.posts = [];
|
|
927
|
-
for (let i = 0; i < input.posts.length; i++) {
|
|
928
|
-
const post = input.posts[i];
|
|
929
|
-
if (post) {
|
|
930
|
-
output.posts.push(processPost(post, siteUrl, options));
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
return output;
|
|
936
|
-
};
|
|
937
|
-
|
|
938
|
-
export {processContent, getImageDimensions, largeImageUrl};
|
|
582
|
+
export { processContent, getImageDimensions, largeImageUrl };
|
|
583
|
+
//# sourceMappingURL=process.js.map
|