@tryghost/mg-blogger 0.1.0 → 0.1.2

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 CHANGED
@@ -55,6 +55,9 @@ It's possible to pass more options, in order to achieve a better migration file
55
55
  - **`--addTag`**
56
56
  - string - default: `null`
57
57
  - Provide a tag slug which should be added to every post in this migration
58
+ - **`--firstImageAsFeatured`**
59
+ - Use the first image as the post's feature_image
60
+ - bool - default: `true`
58
61
  - **`--fallBackHTMLCard`**
59
62
  - Fall back to convert to HTMLCard, if standard Mobiledoc convert fails
60
63
  - bool - default: `true`
package/lib/process.js CHANGED
@@ -1,3 +1,4 @@
1
+ import {basename} from 'node:path';
1
2
  import $ from 'cheerio';
2
3
  import autop from 'autop';
3
4
  import sanitizeHtml from 'sanitize-html';
@@ -8,6 +9,10 @@ import imageCard from '@tryghost/kg-default-cards/lib/cards/image.js';
8
9
 
9
10
  const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
10
11
 
12
+ $.prototype.unwrap = function () {
13
+ this.replaceWith(this.html());
14
+ };
15
+
11
16
  const cleanExcerpt = (htmlContent) => {
12
17
  // Convert to text only
13
18
  const cleanedContent = sanitizeHtml(htmlContent, {
@@ -27,7 +32,10 @@ const slugFromURL = (url) => {
27
32
  const increaseImageSize = (src) => {
28
33
  // Increases the image size and removes special flags
29
34
  // https://developers.google.com/photos/library/guides/access-media-items
30
- let updatedSrc = src.replace(/s[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, 's2000');
35
+ let updatedSrc = src.replace(/\/s[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, '/s2000');
36
+ updatedSrc = updatedSrc.replace(/=s[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, '=s2000');
37
+ updatedSrc = updatedSrc.replace(/\/w[0-9]{2,5}-h[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, '/w2000-h2000');
38
+ updatedSrc = updatedSrc.replace(/=w[0-9]{2,5}-h[0-9]{2,5}(-[a-z0-9#,*]{1,4})?/g, '=w2000-h2000');
31
39
  return updatedSrc;
32
40
  };
33
41
 
@@ -37,8 +45,45 @@ const getAllAttributes = function (el) {
37
45
  );
38
46
  };
39
47
 
48
+ const handleFirstImage = (args) => {
49
+ let {postData, html} = args;
50
+
51
+ const $html = $.load(html);
52
+ const firstContentElement = $html('*').first();
53
+
54
+ if (firstContentElement[0].name === 'img') {
55
+ $(firstContentElement).remove();
56
+ postData.feature_image = $(firstContentElement).attr('src');
57
+ } else if ($(firstContentElement).text().trim() === '') {
58
+ const firstContentElementImgSrc = $(firstContentElement).find('img').attr('src');
59
+ const firstContentElementImgBasename = firstContentElementImgSrc ? basename(firstContentElementImgSrc) : null;
60
+ const firstContentElementAHref = $(firstContentElement).find('a').attr('href');
61
+ const firstContentElementAHrefBasename = firstContentElementAHref ? basename(firstContentElementAHref) : null;
62
+
63
+ if (firstContentElementImgBasename === firstContentElementAHrefBasename) {
64
+ $(firstContentElement).remove();
65
+ postData.feature_image = firstContentElementImgSrc;
66
+ } else if (firstContentElementImgSrc && !firstContentElementAHref) {
67
+ $(firstContentElement).remove();
68
+ postData.feature_image = firstContentElementImgSrc;
69
+ } else {
70
+ const firstContentElementImgBasenameNoSize = firstContentElementImgBasename.replace(/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/, '$1');
71
+ const firstContentElementAHrefBasenameNoSize = firstContentElementAHrefBasename.replace(/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/, '$1');
72
+
73
+ if (firstContentElementImgBasenameNoSize === firstContentElementAHrefBasenameNoSize) {
74
+ $(firstContentElement).remove();
75
+ postData.feature_image = firstContentElementImgSrc;
76
+ }
77
+ }
78
+ }
79
+
80
+ postData.html = $html.html().trim();
81
+
82
+ return postData;
83
+ };
84
+
40
85
  const processHTMLContent = async (args) => {
41
- let {html} = args;
86
+ let {postData, html, options} = args;
42
87
 
43
88
  html = autop(html);
44
89
 
@@ -53,6 +98,8 @@ const processHTMLContent = async (args) => {
53
98
  const largerSrc = increaseImageSize(imgSrc);
54
99
 
55
100
  $(el).attr('src', largerSrc);
101
+ $(el).removeAttr('width');
102
+ $(el).removeAttr('height');
56
103
  });
57
104
 
58
105
  $html('.tr-caption-container').each((i, el) => {
@@ -140,17 +187,40 @@ const processHTMLContent = async (args) => {
140
187
  $(el).replaceWith(`<p>${$(el).html().trim()}</p>`);
141
188
  });
142
189
 
190
+ $html('h1, h2, h3, h4, h5, h6').each((i, el) => {
191
+ const attrs = getAllAttributes(el);
192
+
193
+ const styleIndex = attrs.findIndex(e => e.name === 'style');
194
+ if (styleIndex > -1) {
195
+ if (attrs[styleIndex].value === 'text-align: left;') {
196
+ $(el).removeAttr(`style`);
197
+ }
198
+ }
199
+ });
200
+
201
+ $html('h3 b').each((i, el) => {
202
+ $(el).unwrap();
203
+ });
204
+
143
205
  $html('div, p').each((i, el) => {
144
206
  const attrs = getAllAttributes(el);
145
207
  const children = $(el).children();
146
208
 
147
209
  if ($(el).html().trim() === '' && attrs.length === 0) {
148
210
  $(el).remove();
211
+ } else if ($(el).html() === '&#xA0;') {
212
+ $(el).remove();
149
213
  } else if (attrs.length === 0 && children.length === 1 && children[0].name === 'img') {
150
214
  $(el).replaceWith($(el).html().trim());
151
215
  }
152
216
  });
153
217
 
218
+ $html('p').each((i, el) => {
219
+ if ($(el).html().trim() === '' || $(el).html() === '&#xA0;') {
220
+ $(el).remove();
221
+ }
222
+ });
223
+
154
224
  $html('div').each((i, el) => {
155
225
  const attrs = getAllAttributes(el);
156
226
  const children = $(el).children();
@@ -160,6 +230,11 @@ const processHTMLContent = async (args) => {
160
230
  }
161
231
  });
162
232
 
233
+ $html('iframe[src*="youtube.com"]').each((i, el) => {
234
+ $(el).attr('width', 160);
235
+ $(el).attr('height', 90);
236
+ });
237
+
163
238
  // Convert back to plain HTML
164
239
  html = $html.html();
165
240
 
@@ -169,7 +244,16 @@ const processHTMLContent = async (args) => {
169
244
  // Remove first element(s) if <hr>
170
245
  html = html.replace(/^(<hr\/?> ?)+/gm, '').trim();
171
246
 
172
- return html;
247
+ postData.html = html;
248
+
249
+ if (options?.firstImageAsFeatured) {
250
+ postData = handleFirstImage({
251
+ postData,
252
+ html
253
+ });
254
+ }
255
+
256
+ return postData;
173
257
  };
174
258
 
175
259
  const processPost = async (postData, options) => {
@@ -189,8 +273,10 @@ const processPost = async (postData, options) => {
189
273
  }
190
274
  };
191
275
 
192
- post.data.html = await processHTMLContent({
193
- html: postData.content
276
+ post.data = await processHTMLContent({
277
+ postData: post.data,
278
+ html: postData.content,
279
+ options
194
280
  });
195
281
 
196
282
  if (postData.author) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-blogger",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-blogger",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -24,13 +24,13 @@
24
24
  "jest-extended": "3.2.4"
25
25
  },
26
26
  "dependencies": {
27
- "@tryghost/errors": "1.2.22",
27
+ "@tryghost/errors": "1.2.25",
28
28
  "@tryghost/kg-default-cards": "9.0.1",
29
29
  "autop": "1.0.1",
30
30
  "cheerio": "0.22.0",
31
31
  "node-fetch": "^3.3.1",
32
- "sanitize-html": "2.10.0",
32
+ "sanitize-html": "2.11.0",
33
33
  "simple-dom": "1.4.0"
34
34
  },
35
- "gitHead": "36b15a65cacdbb4002c195b06a434cb9742349b6"
35
+ "gitHead": "858b985d1e54eaf41b3a53dbccdc074b57246f9c"
36
36
  }