@tryghost/mg-blogger 0.1.28 → 0.1.29
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/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/process.js +58 -57
- package/package.json +4 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
package/lib/process.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {basename} from 'node:path';
|
|
2
|
-
import
|
|
2
|
+
import * as cheerio from 'cheerio';
|
|
3
3
|
import autop from 'autop';
|
|
4
4
|
import sanitizeHtml from 'sanitize-html';
|
|
5
5
|
import {slugify} from '@tryghost/string';
|
|
@@ -9,10 +9,6 @@ import imageCard from '@tryghost/kg-default-cards/lib/cards/image.js';
|
|
|
9
9
|
|
|
10
10
|
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
|
|
11
11
|
|
|
12
|
-
$.prototype.unwrap = function () {
|
|
13
|
-
this.replaceWith(this.html());
|
|
14
|
-
};
|
|
15
|
-
|
|
16
12
|
const cleanExcerpt = (htmlContent) => {
|
|
17
13
|
// Convert to text only
|
|
18
14
|
const cleanedContent = sanitizeHtml(htmlContent, {
|
|
@@ -48,36 +44,37 @@ const getAllAttributes = function (el) {
|
|
|
48
44
|
const handleFirstImage = (args) => {
|
|
49
45
|
let {postData, html} = args;
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
const $html = cheerio.load(html, {
|
|
48
|
+
xml: {
|
|
49
|
+
xmlMode: false,
|
|
50
|
+
decodeEntities: false,
|
|
51
|
+
scriptingEnabled: false
|
|
52
|
+
}
|
|
56
53
|
}, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
|
|
57
54
|
|
|
58
55
|
const firstContentElement = $html('*').first();
|
|
59
56
|
|
|
60
57
|
if (firstContentElement[0].name === 'img') {
|
|
61
|
-
$(firstContentElement).remove();
|
|
62
|
-
postData.feature_image = $(firstContentElement).attr('src');
|
|
63
|
-
} else if ($(firstContentElement).text().trim() === '') {
|
|
64
|
-
const firstContentElementImgSrc = $(firstContentElement).find('img').attr('src');
|
|
58
|
+
$html(firstContentElement).remove();
|
|
59
|
+
postData.feature_image = $html(firstContentElement).attr('src');
|
|
60
|
+
} else if ($html(firstContentElement).text().trim() === '') {
|
|
61
|
+
const firstContentElementImgSrc = $html(firstContentElement).find('img').attr('src');
|
|
65
62
|
const firstContentElementImgBasename = firstContentElementImgSrc ? basename(firstContentElementImgSrc) : null;
|
|
66
|
-
const firstContentElementAHref = $(firstContentElement).find('a').attr('href');
|
|
63
|
+
const firstContentElementAHref = $html(firstContentElement).find('a').attr('href');
|
|
67
64
|
const firstContentElementAHrefBasename = firstContentElementAHref ? basename(firstContentElementAHref) : null;
|
|
68
65
|
|
|
69
66
|
if (firstContentElementImgBasename === firstContentElementAHrefBasename) {
|
|
70
|
-
$(firstContentElement).remove();
|
|
67
|
+
$html(firstContentElement).remove();
|
|
71
68
|
postData.feature_image = firstContentElementImgSrc;
|
|
72
69
|
} else if (firstContentElementImgSrc && !firstContentElementAHref) {
|
|
73
|
-
$(firstContentElement).remove();
|
|
70
|
+
$html(firstContentElement).remove();
|
|
74
71
|
postData.feature_image = firstContentElementImgSrc;
|
|
75
72
|
} else {
|
|
76
73
|
const firstContentElementImgBasenameNoSize = firstContentElementImgBasename.replace(/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/, '$1');
|
|
77
74
|
const firstContentElementAHrefBasenameNoSize = firstContentElementAHrefBasename.replace(/(.*)((-=|-|=)w[0-9]{2,5}-h[0-9]{2,5}|(-=|-|=)s[0-9]{2,5})/, '$1');
|
|
78
75
|
|
|
79
76
|
if (firstContentElementImgBasenameNoSize === firstContentElementAHrefBasenameNoSize) {
|
|
80
|
-
$(firstContentElement).remove();
|
|
77
|
+
$html(firstContentElement).remove();
|
|
81
78
|
postData.feature_image = firstContentElementImgSrc;
|
|
82
79
|
}
|
|
83
80
|
}
|
|
@@ -93,29 +90,32 @@ const processHTMLContent = async (args) => {
|
|
|
93
90
|
|
|
94
91
|
html = autop(html);
|
|
95
92
|
|
|
96
|
-
const $html =
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
const $html = cheerio.load(html, {
|
|
94
|
+
xml: {
|
|
95
|
+
xmlMode: false,
|
|
96
|
+
decodeEntities: false,
|
|
97
|
+
scriptingEnabled: false
|
|
98
|
+
}
|
|
99
99
|
}, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
|
|
100
100
|
|
|
101
101
|
$html('div.separator').each((i, el) => {
|
|
102
|
-
$(el).replaceWith(`<hr><div>${$(el).html().trim()}</div>`);
|
|
102
|
+
$html(el).replaceWith(`<hr><div>${$html(el).html().trim()}</div>`);
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
$html('img').each((i, el) => {
|
|
106
|
-
const imgSrc = $(el).attr('src');
|
|
106
|
+
const imgSrc = $html(el).attr('src');
|
|
107
107
|
const largerSrc = increaseImageSize(imgSrc);
|
|
108
108
|
|
|
109
|
-
$(el).attr('src', largerSrc);
|
|
110
|
-
$(el).removeAttr('width');
|
|
111
|
-
$(el).removeAttr('height');
|
|
109
|
+
$html(el).attr('src', largerSrc);
|
|
110
|
+
$html(el).removeAttr('width');
|
|
111
|
+
$html(el).removeAttr('height');
|
|
112
112
|
});
|
|
113
113
|
|
|
114
114
|
$html('.tr-caption-container').each((i, el) => {
|
|
115
|
-
const hasLink = $(el).find('img').parent('a').length;
|
|
116
|
-
const href = (hasLink) ? $(el).find('img').parent('a').attr('href') : null;
|
|
117
|
-
const imgSrc = $(el).find('img').attr('src');
|
|
118
|
-
const caption = $(el).find('.tr-caption').html();
|
|
115
|
+
const hasLink = $html(el).find('img').parent('a').length;
|
|
116
|
+
const href = (hasLink) ? $html(el).find('img').parent('a').attr('href') : null;
|
|
117
|
+
const imgSrc = $html(el).find('img').attr('src');
|
|
118
|
+
const caption = $html(el).find('.tr-caption').html();
|
|
119
119
|
const cleanedExcerpt = cleanExcerpt(caption);
|
|
120
120
|
|
|
121
121
|
if (!imgSrc || imgSrc.length === 0) {
|
|
@@ -143,33 +143,33 @@ const processHTMLContent = async (args) => {
|
|
|
143
143
|
|
|
144
144
|
const newCard = serializer.serialize(imageCard.render(cardOpts));
|
|
145
145
|
|
|
146
|
-
$(el).replaceWith(newCard);
|
|
146
|
+
$html(el).replaceWith(newCard);
|
|
147
147
|
});
|
|
148
148
|
|
|
149
149
|
$html('a > img').each((i, el) => {
|
|
150
|
-
const aHref = $(el).parent().attr('href');
|
|
151
|
-
const imgSrc = $(el).attr('src');
|
|
150
|
+
const aHref = $html(el).parent().attr('href');
|
|
151
|
+
const imgSrc = $html(el).attr('src');
|
|
152
152
|
|
|
153
153
|
const updatedaHref = increaseImageSize(aHref);
|
|
154
154
|
const updatedImgSrc = increaseImageSize(imgSrc);
|
|
155
155
|
|
|
156
156
|
// Don't touch Koenig cards
|
|
157
|
-
if ($(el).parent().attr('class')?.includes('kg-') || $(el).attr('class')?.includes('kg-')) {
|
|
157
|
+
if ($html(el).parent().attr('class')?.includes('kg-') || $html(el).attr('class')?.includes('kg-')) {
|
|
158
158
|
return;
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
if (updatedaHref === updatedImgSrc) {
|
|
162
|
-
$(el).parent().replaceWith(`<img src="${imgSrc}">`);
|
|
162
|
+
$html(el).parent().replaceWith(`<img src="${imgSrc}">`);
|
|
163
163
|
}
|
|
164
164
|
});
|
|
165
165
|
|
|
166
166
|
// Convert weird lists to real lists
|
|
167
167
|
$html('span[style*="white-space: pre"]').each((i, el) => {
|
|
168
|
-
const parent = $(el).parent('div');
|
|
168
|
+
const parent = $html(el).parent('div');
|
|
169
169
|
if (parent.html()?.includes('•') || parent.html()?.includes('•')) {
|
|
170
170
|
parent[0].tagName = 'li';
|
|
171
171
|
parent.find('span[style*="white-space: pre"]').each((ii, ell) => {
|
|
172
|
-
$(ell).remove();
|
|
172
|
+
$html(ell).remove();
|
|
173
173
|
});
|
|
174
174
|
parent.html(parent.html().replace('•', '').trim());
|
|
175
175
|
}
|
|
@@ -177,23 +177,23 @@ const processHTMLContent = async (args) => {
|
|
|
177
177
|
|
|
178
178
|
// Wrap <li>s in <ul>
|
|
179
179
|
$html('li').each((i, el) => {
|
|
180
|
-
if ($(el).prev('ul').length) {
|
|
181
|
-
$(el).prev('ul').append($(el));
|
|
180
|
+
if ($html(el).prev('ul').length) {
|
|
181
|
+
$html(el).prev('ul').append($html(el));
|
|
182
182
|
} else {
|
|
183
|
-
$(el).wrap('<ul></ul>');
|
|
183
|
+
$html(el).wrap('<ul></ul>');
|
|
184
184
|
}
|
|
185
185
|
});
|
|
186
186
|
|
|
187
187
|
$html('div[style*="color"][style*="font-family"][style*="font-size"][style*="white-space: pre-line"]').each((i, el) => {
|
|
188
|
-
$(el).replaceWith(`<p>${$(el).html().trim()}</p>`);
|
|
188
|
+
$html(el).replaceWith(`<p>${$html(el).html().trim()}</p>`);
|
|
189
189
|
});
|
|
190
190
|
|
|
191
191
|
$html('span[style*="background-color"][style*="color"][style*="font-family"][style*="font-size"][style*="white-space: pre-line"]').each((i, el) => {
|
|
192
|
-
$(el).replaceWith(`<p>${$(el).html().trim()}</p>`);
|
|
192
|
+
$html(el).replaceWith(`<p>${$html(el).html().trim()}</p>`);
|
|
193
193
|
});
|
|
194
194
|
|
|
195
195
|
$html('div[style="text-align: center;"]').each((i, el) => {
|
|
196
|
-
$(el).replaceWith(`<p>${$(el).html().trim()}</p>`);
|
|
196
|
+
$html(el).replaceWith(`<p>${$html(el).html().trim()}</p>`);
|
|
197
197
|
});
|
|
198
198
|
|
|
199
199
|
$html('h1, h2, h3, h4, h5, h6').each((i, el) => {
|
|
@@ -202,46 +202,47 @@ const processHTMLContent = async (args) => {
|
|
|
202
202
|
const styleIndex = attrs.findIndex(e => e.name === 'style');
|
|
203
203
|
if (styleIndex > -1) {
|
|
204
204
|
if (attrs[styleIndex].value === 'text-align: left;') {
|
|
205
|
-
$(el).removeAttr(`style`);
|
|
205
|
+
$html(el).removeAttr(`style`);
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
});
|
|
209
209
|
|
|
210
210
|
$html('h3 b').each((i, el) => {
|
|
211
|
-
$(el)
|
|
211
|
+
const $el = $html(el);
|
|
212
|
+
$el.replaceWith($el.html());
|
|
212
213
|
});
|
|
213
214
|
|
|
214
215
|
$html('div, p').each((i, el) => {
|
|
215
216
|
const attrs = getAllAttributes(el);
|
|
216
|
-
const children = $(el).children();
|
|
217
|
+
const children = $html(el).children();
|
|
217
218
|
|
|
218
|
-
if ($(el).html().trim() === '' && attrs.length === 0) {
|
|
219
|
-
$(el).remove();
|
|
220
|
-
} else if ($(el).html() === ' ') {
|
|
221
|
-
$(el).remove();
|
|
219
|
+
if ($html(el).html().trim() === '' && attrs.length === 0) {
|
|
220
|
+
$html(el).remove();
|
|
221
|
+
} else if ($html(el).html() === ' ') {
|
|
222
|
+
$html(el).remove();
|
|
222
223
|
} else if (attrs.length === 0 && children.length === 1 && children[0].name === 'img') {
|
|
223
|
-
$(el).replaceWith($(el).html().trim());
|
|
224
|
+
$html(el).replaceWith($html(el).html().trim());
|
|
224
225
|
}
|
|
225
226
|
});
|
|
226
227
|
|
|
227
228
|
$html('p').each((i, el) => {
|
|
228
|
-
if ($(el).html().trim() === '' || $(el).html() === ' ' || $(el).html().trim() === ' ') {
|
|
229
|
-
$(el).remove();
|
|
229
|
+
if ($html(el).html().trim() === '' || $html(el).html() === ' ' || $html(el).html().trim() === ' ') {
|
|
230
|
+
$html(el).remove();
|
|
230
231
|
}
|
|
231
232
|
});
|
|
232
233
|
|
|
233
234
|
$html('div').each((i, el) => {
|
|
234
235
|
const attrs = getAllAttributes(el);
|
|
235
|
-
const children = $(el).children();
|
|
236
|
+
const children = $html(el).children();
|
|
236
237
|
|
|
237
238
|
if (attrs.length === 0 && children.length === 0) {
|
|
238
|
-
$(el).replaceWith(`<p>${$(el).html().trim()}</p>`);
|
|
239
|
+
$html(el).replaceWith(`<p>${$html(el).html().trim()}</p>`);
|
|
239
240
|
}
|
|
240
241
|
});
|
|
241
242
|
|
|
242
243
|
$html('iframe[src*="youtube.com"]').each((i, el) => {
|
|
243
|
-
$(el).attr('width', 160);
|
|
244
|
-
$(el).attr('height', 90);
|
|
244
|
+
$html(el).attr('width', 160);
|
|
245
|
+
$html(el).attr('height', 90);
|
|
245
246
|
});
|
|
246
247
|
|
|
247
248
|
// Convert back to plain HTML
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-blogger",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.29",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-blogger",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@tryghost/errors": "1.3.8",
|
|
28
|
-
"@tryghost/kg-default-cards": "10.2.
|
|
28
|
+
"@tryghost/kg-default-cards": "10.2.3",
|
|
29
29
|
"autop": "1.0.1",
|
|
30
|
-
"cheerio": "1.
|
|
30
|
+
"cheerio": "1.1.2",
|
|
31
31
|
"node-fetch": "^3.3.1",
|
|
32
32
|
"sanitize-html": "2.17.0",
|
|
33
33
|
"simple-dom": "1.4.0"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "027225930710c0643603f6ae346dc76504d9bb7a"
|
|
36
36
|
}
|