@tryghost/mg-blogger 0.11.1 → 0.11.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/README.md +2 -2
- package/lib/fetch.js +3 -2
- package/lib/process.js +74 -40
- package/package.json +4 -8
package/README.md
CHANGED
|
@@ -95,8 +95,8 @@ pnpm dev blogger --apiKey abcd --blogID 1234
|
|
|
95
95
|
|
|
96
96
|
## Test
|
|
97
97
|
|
|
98
|
-
- `pnpm
|
|
99
|
-
-
|
|
98
|
+
- `pnpm test` runs the package tests
|
|
99
|
+
- Linting and formatting are run from the repo root (`pnpm lint`, `pnpm format`)
|
|
100
100
|
|
|
101
101
|
|
|
102
102
|
# Copyright & License
|
package/lib/fetch.js
CHANGED
|
@@ -57,10 +57,11 @@ const tasks = async ({apiKey, blogID}, ctx) => {
|
|
|
57
57
|
posts: []
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
blogID.forEach(
|
|
60
|
+
blogID.forEach(singleblogID => {
|
|
61
61
|
tasks.push({
|
|
62
62
|
title: `Fetching posts for ${singleblogID}`,
|
|
63
|
-
task: async
|
|
63
|
+
task: async ctx => {
|
|
64
|
+
// eslint-disable-line no-shadow
|
|
64
65
|
try {
|
|
65
66
|
let response = await cachedFetch(ctx.fileCache, apiKey, singleblogID, 'posts');
|
|
66
67
|
|
package/lib/process.js
CHANGED
|
@@ -11,7 +11,7 @@ const {serializeChildren, replaceWith, wrap} = domUtils;
|
|
|
11
11
|
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
|
|
12
12
|
const imageCard = cardUtils.getCard('image');
|
|
13
13
|
|
|
14
|
-
const cleanExcerpt =
|
|
14
|
+
const cleanExcerpt = htmlContent => {
|
|
15
15
|
// Convert to text only
|
|
16
16
|
const cleanedContent = sanitizeHtml(htmlContent, {
|
|
17
17
|
allowedTags: ['b', 'i', 'em', 'strong', 'a'],
|
|
@@ -23,11 +23,11 @@ const cleanExcerpt = (htmlContent) => {
|
|
|
23
23
|
return cleanedContent.trim();
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const slugFromURL =
|
|
26
|
+
const slugFromURL = url => {
|
|
27
27
|
return url.split('/').pop().replace('.html', '');
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
const increaseImageSize =
|
|
30
|
+
const increaseImageSize = src => {
|
|
31
31
|
// Increases the image size and removes special flags
|
|
32
32
|
// https://developers.google.com/photos/library/guides/access-media-items
|
|
33
33
|
let updatedSrc = src.replace(/\/s[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, '/s2000');
|
|
@@ -37,10 +37,10 @@ const increaseImageSize = (src) => {
|
|
|
37
37
|
return updatedSrc;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
-
const handleFirstImage =
|
|
40
|
+
const handleFirstImage = args => {
|
|
41
41
|
let {postData, html} = args;
|
|
42
42
|
|
|
43
|
-
return domUtils.processFragment(html,
|
|
43
|
+
return domUtils.processFragment(html, parsed => {
|
|
44
44
|
const firstContentElement = parsed.body.children[0];
|
|
45
45
|
|
|
46
46
|
if (!firstContentElement) {
|
|
@@ -55,9 +55,13 @@ const handleFirstImage = (args) => {
|
|
|
55
55
|
const img = firstContentElement.querySelector('img');
|
|
56
56
|
const a = firstContentElement.querySelector('a');
|
|
57
57
|
const firstContentElementImgSrc = img?.getAttribute('src') ?? null;
|
|
58
|
-
const firstContentElementImgBasename = firstContentElementImgSrc
|
|
58
|
+
const firstContentElementImgBasename = firstContentElementImgSrc
|
|
59
|
+
? basename(firstContentElementImgSrc)
|
|
60
|
+
: null;
|
|
59
61
|
const firstContentElementAHref = a?.getAttribute('href') ?? null;
|
|
60
|
-
const firstContentElementAHrefBasename = firstContentElementAHref
|
|
62
|
+
const firstContentElementAHrefBasename = firstContentElementAHref
|
|
63
|
+
? basename(firstContentElementAHref)
|
|
64
|
+
: null;
|
|
61
65
|
|
|
62
66
|
if (firstContentElementImgBasename === firstContentElementAHrefBasename) {
|
|
63
67
|
firstContentElement.remove();
|
|
@@ -66,8 +70,14 @@ const handleFirstImage = (args) => {
|
|
|
66
70
|
firstContentElement.remove();
|
|
67
71
|
postData.feature_image = firstContentElementImgSrc;
|
|
68
72
|
} else {
|
|
69
|
-
const firstContentElementImgBasenameNoSize = firstContentElementImgBasename.replace(
|
|
70
|
-
|
|
73
|
+
const firstContentElementImgBasenameNoSize = firstContentElementImgBasename.replace(
|
|
74
|
+
/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/,
|
|
75
|
+
'$1'
|
|
76
|
+
);
|
|
77
|
+
const firstContentElementAHrefBasenameNoSize = firstContentElementAHrefBasename.replace(
|
|
78
|
+
/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/,
|
|
79
|
+
'$1'
|
|
80
|
+
);
|
|
71
81
|
|
|
72
82
|
if (firstContentElementImgBasenameNoSize === firstContentElementAHrefBasenameNoSize) {
|
|
73
83
|
firstContentElement.remove();
|
|
@@ -81,17 +91,17 @@ const handleFirstImage = (args) => {
|
|
|
81
91
|
});
|
|
82
92
|
};
|
|
83
93
|
|
|
84
|
-
const processHTMLContent = async
|
|
94
|
+
const processHTMLContent = async args => {
|
|
85
95
|
let {postData, html, options} = args;
|
|
86
96
|
|
|
87
97
|
html = autop(html);
|
|
88
98
|
|
|
89
|
-
html = domUtils.processFragment(html,
|
|
90
|
-
parsed.$('div.separator').forEach(
|
|
99
|
+
html = domUtils.processFragment(html, parsed => {
|
|
100
|
+
parsed.$('div.separator').forEach(el => {
|
|
91
101
|
replaceWith(el, `<hr><div>${serializeChildren(el).trim()}</div>`);
|
|
92
102
|
});
|
|
93
103
|
|
|
94
|
-
parsed.$('img').forEach(
|
|
104
|
+
parsed.$('img').forEach(el => {
|
|
95
105
|
const imgSrc = el.getAttribute('src');
|
|
96
106
|
const largerSrc = increaseImageSize(imgSrc);
|
|
97
107
|
|
|
@@ -100,7 +110,7 @@ const processHTMLContent = async (args) => {
|
|
|
100
110
|
el.removeAttribute('height');
|
|
101
111
|
});
|
|
102
112
|
|
|
103
|
-
parsed.$('.tr-caption-container').forEach(
|
|
113
|
+
parsed.$('.tr-caption-container').forEach(el => {
|
|
104
114
|
const img = el.querySelector('img');
|
|
105
115
|
const imgParentA = img?.closest('a');
|
|
106
116
|
const hasLink = !!imgParentA;
|
|
@@ -138,7 +148,7 @@ const processHTMLContent = async (args) => {
|
|
|
138
148
|
replaceWith(el, newCard);
|
|
139
149
|
});
|
|
140
150
|
|
|
141
|
-
parsed.$('a > img').forEach(
|
|
151
|
+
parsed.$('a > img').forEach(el => {
|
|
142
152
|
const parentA = el.parentElement;
|
|
143
153
|
if (!parentA || parentA.tagName !== 'A') {
|
|
144
154
|
return;
|
|
@@ -161,7 +171,7 @@ const processHTMLContent = async (args) => {
|
|
|
161
171
|
});
|
|
162
172
|
|
|
163
173
|
// Convert weird lists to real lists
|
|
164
|
-
parsed.$('span[style*="white-space: pre"]').forEach(
|
|
174
|
+
parsed.$('span[style*="white-space: pre"]').forEach(el => {
|
|
165
175
|
const parentDiv = el.closest('div');
|
|
166
176
|
if (!parentDiv) {
|
|
167
177
|
return;
|
|
@@ -171,18 +181,40 @@ const processHTMLContent = async (args) => {
|
|
|
171
181
|
if (parentHTML.includes('•') || parentHTML.includes('•')) {
|
|
172
182
|
// Change div to li by replacing with a new li element
|
|
173
183
|
const newLi = parsed.document.createElement('li');
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
// Move the div's child nodes into the li. The div is replaced
|
|
185
|
+
// below, so moving (rather than serializing back into innerHTML)
|
|
186
|
+
// is safe and avoids reinterpreting DOM text as HTML.
|
|
187
|
+
newLi.replaceChildren(...parentDiv.childNodes);
|
|
188
|
+
// Remove the whitespace-pre spans
|
|
189
|
+
newLi.querySelectorAll('span[style*="white-space: pre"]').forEach(span => {
|
|
177
190
|
span.remove();
|
|
178
191
|
});
|
|
179
|
-
|
|
192
|
+
// Trim leading/trailing whitespace from the list item. Merge
|
|
193
|
+
// adjacent text nodes first, then strip whitespace directly from
|
|
194
|
+
// the boundary text nodes so no serialized markup is re-parsed.
|
|
195
|
+
// Only ASCII whitespace is trimmed (matching String.trim over the
|
|
196
|
+
// serialized HTML); non-breaking spaces are left untouched.
|
|
197
|
+
newLi.normalize();
|
|
198
|
+
const firstChild = newLi.firstChild;
|
|
199
|
+
if (firstChild && firstChild.nodeType === 3) {
|
|
200
|
+
firstChild.data = firstChild.data.replace(/^[ \t\n\r\f\v]+/, '');
|
|
201
|
+
if (firstChild.data === '') {
|
|
202
|
+
firstChild.remove();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const lastChild = newLi.lastChild;
|
|
206
|
+
if (lastChild && lastChild.nodeType === 3) {
|
|
207
|
+
lastChild.data = lastChild.data.replace(/[ \t\n\r\f\v]+$/, '');
|
|
208
|
+
if (lastChild.data === '') {
|
|
209
|
+
lastChild.remove();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
180
212
|
replaceWith(parentDiv, newLi);
|
|
181
213
|
}
|
|
182
214
|
});
|
|
183
215
|
|
|
184
216
|
// Wrap <li>s in <ul>
|
|
185
|
-
parsed.$('li').forEach(
|
|
217
|
+
parsed.$('li').forEach(el => {
|
|
186
218
|
const prevSibling = el.previousElementSibling;
|
|
187
219
|
if (prevSibling && prevSibling.tagName === 'UL') {
|
|
188
220
|
prevSibling.appendChild(el);
|
|
@@ -191,30 +223,36 @@ const processHTMLContent = async (args) => {
|
|
|
191
223
|
}
|
|
192
224
|
});
|
|
193
225
|
|
|
194
|
-
parsed
|
|
195
|
-
|
|
196
|
-
|
|
226
|
+
parsed
|
|
227
|
+
.$('div[style*="color"][style*="font-family"][style*="font-size"][style*="white-space: pre-line"]')
|
|
228
|
+
.forEach(el => {
|
|
229
|
+
replaceWith(el, `<p>${serializeChildren(el).trim()}</p>`);
|
|
230
|
+
});
|
|
197
231
|
|
|
198
|
-
parsed
|
|
199
|
-
|
|
200
|
-
|
|
232
|
+
parsed
|
|
233
|
+
.$(
|
|
234
|
+
'span[style*="background-color"][style*="color"][style*="font-family"][style*="font-size"][style*="white-space: pre-line"]'
|
|
235
|
+
)
|
|
236
|
+
.forEach(el => {
|
|
237
|
+
replaceWith(el, `<p>${serializeChildren(el).trim()}</p>`);
|
|
238
|
+
});
|
|
201
239
|
|
|
202
|
-
parsed.$('div[style="text-align: center;"]').forEach(
|
|
240
|
+
parsed.$('div[style="text-align: center;"]').forEach(el => {
|
|
203
241
|
replaceWith(el, `<p>${serializeChildren(el).trim()}</p>`);
|
|
204
242
|
});
|
|
205
243
|
|
|
206
|
-
parsed.$('h1, h2, h3, h4, h5, h6').forEach(
|
|
244
|
+
parsed.$('h1, h2, h3, h4, h5, h6').forEach(el => {
|
|
207
245
|
const styleAttr = el.getAttribute('style');
|
|
208
246
|
if (styleAttr === 'text-align: left;') {
|
|
209
247
|
el.removeAttribute('style');
|
|
210
248
|
}
|
|
211
249
|
});
|
|
212
250
|
|
|
213
|
-
parsed.$('h3 b').forEach(
|
|
251
|
+
parsed.$('h3 b').forEach(el => {
|
|
214
252
|
replaceWith(el, serializeChildren(el));
|
|
215
253
|
});
|
|
216
254
|
|
|
217
|
-
parsed.$('div, p').forEach(
|
|
255
|
+
parsed.$('div, p').forEach(el => {
|
|
218
256
|
const attrs = el.attributes;
|
|
219
257
|
const children = el.children;
|
|
220
258
|
|
|
@@ -227,14 +265,14 @@ const processHTMLContent = async (args) => {
|
|
|
227
265
|
}
|
|
228
266
|
});
|
|
229
267
|
|
|
230
|
-
parsed.$('p').forEach(
|
|
268
|
+
parsed.$('p').forEach(el => {
|
|
231
269
|
const innerHtml = serializeChildren(el);
|
|
232
270
|
if (innerHtml.trim() === '' || innerHtml === ' ' || innerHtml.trim() === ' ') {
|
|
233
271
|
el.remove();
|
|
234
272
|
}
|
|
235
273
|
});
|
|
236
274
|
|
|
237
|
-
parsed.$('div').forEach(
|
|
275
|
+
parsed.$('div').forEach(el => {
|
|
238
276
|
const attrs = el.attributes;
|
|
239
277
|
const children = el.children;
|
|
240
278
|
|
|
@@ -243,7 +281,7 @@ const processHTMLContent = async (args) => {
|
|
|
243
281
|
}
|
|
244
282
|
});
|
|
245
283
|
|
|
246
|
-
parsed.$('iframe[src*="youtube.com"]').forEach(
|
|
284
|
+
parsed.$('iframe[src*="youtube.com"]').forEach(el => {
|
|
247
285
|
el.setAttribute('width', '160');
|
|
248
286
|
el.setAttribute('height', '90');
|
|
249
287
|
});
|
|
@@ -311,7 +349,7 @@ const processPost = async (postData, options) => {
|
|
|
311
349
|
}
|
|
312
350
|
|
|
313
351
|
if (postData.labels) {
|
|
314
|
-
postData.labels.forEach(
|
|
352
|
+
postData.labels.forEach(label => {
|
|
315
353
|
const cleanedLabel = label.replace('&', '&');
|
|
316
354
|
const labelSlug = slugify(cleanedLabel);
|
|
317
355
|
|
|
@@ -393,8 +431,4 @@ export default {
|
|
|
393
431
|
all
|
|
394
432
|
};
|
|
395
433
|
|
|
396
|
-
export {
|
|
397
|
-
cleanExcerpt,
|
|
398
|
-
slugFromURL,
|
|
399
|
-
increaseImageSize
|
|
400
|
-
};
|
|
434
|
+
export {cleanExcerpt, slugFromURL, increaseImageSize};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-blogger",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
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/errors": "3.2.4",
|
|
25
23
|
"@tryghost/string": "0.3.5",
|
|
@@ -27,12 +25,10 @@
|
|
|
27
25
|
"node-fetch": "3.3.2",
|
|
28
26
|
"sanitize-html": "2.17.5",
|
|
29
27
|
"simple-dom": "1.4.0",
|
|
30
|
-
"@tryghost/mg-utils": "0.11.
|
|
28
|
+
"@tryghost/mg-utils": "0.11.3"
|
|
31
29
|
},
|
|
32
30
|
"scripts": {
|
|
33
31
|
"dev": "echo \"Implement me!\"",
|
|
34
|
-
"test": "node --test 'test/**/*.test.js'"
|
|
35
|
-
"posttest": "pnpm lint",
|
|
36
|
-
"lint": "eslint . --ext .js --cache"
|
|
32
|
+
"test": "node --test 'test/**/*.test.js'"
|
|
37
33
|
}
|
|
38
34
|
}
|