@tryghost/mg-clean-html 0.2.22 → 0.2.24
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 +54 -54
- package/package.json +4 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
package/lib/process.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as cheerio from 'cheerio';
|
|
2
2
|
import styleToObject from 'style-to-object';
|
|
3
3
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const isAllowed = (el) => {
|
|
10
|
-
if ($(el).hasClass('instagram-media')) {
|
|
5
|
+
const isAllowed = ($, el) => {
|
|
6
|
+
const $el = $(el);
|
|
7
|
+
if ($el.hasClass('instagram-media')) {
|
|
11
8
|
return false;
|
|
12
9
|
}
|
|
13
10
|
|
|
14
|
-
if ($
|
|
11
|
+
if ($el.parents('.instagram-media').length) {
|
|
15
12
|
return false;
|
|
16
13
|
}
|
|
17
14
|
|
|
@@ -31,31 +28,34 @@ const cleanHTML = (args) => {
|
|
|
31
28
|
return html;
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
const $html =
|
|
35
|
-
|
|
31
|
+
const $html = cheerio.load(html, {
|
|
32
|
+
xml: {
|
|
33
|
+
xmlMode: false,
|
|
34
|
+
decodeEntities: false
|
|
35
|
+
}
|
|
36
36
|
}, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
|
|
37
37
|
|
|
38
38
|
// Remove left, center & right text alignment
|
|
39
39
|
if (opinionated) {
|
|
40
40
|
$html('p[style*="text-align"], li[style*="text-align"]').each((i, el) => {
|
|
41
|
-
if (!isAllowed(el)) {
|
|
41
|
+
if (!isAllowed($html, el)) {
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
let styleAttr = $(el).attr('style');
|
|
45
|
+
let styleAttr = $html(el).attr('style');
|
|
46
46
|
styleAttr = styleAttr.replace(/text-align: ?(left|center|right);?/, '').trim();
|
|
47
|
-
$(el).attr('style', styleAttr);
|
|
47
|
+
$html(el).attr('style', styleAttr);
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Change inline font-weight to <b> tag or remove altogether
|
|
52
52
|
if (opinionated) {
|
|
53
53
|
$html('span[style*="font-weight"]').each((i, el) => {
|
|
54
|
-
if (!isAllowed(el)) {
|
|
54
|
+
if (!isAllowed($html, el)) {
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
let styleAttr = $(el).attr('style');
|
|
58
|
+
let styleAttr = $html(el).attr('style');
|
|
59
59
|
let styles = styleToObject(styleAttr);
|
|
60
60
|
let fontWeight = styles['font-weight'];
|
|
61
61
|
|
|
@@ -66,25 +66,25 @@ const cleanHTML = (args) => {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
let weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
69
|
-
$(el).attr('style', weightlessStyleAttr);
|
|
69
|
+
$html(el).attr('style', weightlessStyleAttr);
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
if (opinionated) {
|
|
74
74
|
$html('a[style*="font-weight"], p[style*="font-weight"], li[style*="font-weight"]').each((i, el) => {
|
|
75
|
-
if (!isAllowed(el)) {
|
|
75
|
+
if (!isAllowed($html, el)) {
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
let styleAttr = $(el).attr('style');
|
|
79
|
+
let styleAttr = $html(el).attr('style');
|
|
80
80
|
let styles = styleToObject(styleAttr);
|
|
81
81
|
let fontWeight = styles['font-weight'];
|
|
82
82
|
|
|
83
83
|
let weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
84
|
-
$(el).attr('style', weightlessStyleAttr);
|
|
84
|
+
$html(el).attr('style', weightlessStyleAttr);
|
|
85
85
|
|
|
86
86
|
if (['bold', 'bolder'].includes(fontWeight) || parseInt(fontWeight) >= 600) {
|
|
87
|
-
$(el).html(`<b>${$(el).html()}</b>`);
|
|
87
|
+
$html(el).html(`<b>${$html(el).html()}</b>`);
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
90
|
}
|
|
@@ -92,11 +92,11 @@ const cleanHTML = (args) => {
|
|
|
92
92
|
// Change inline font-style to <b> tag or remove altogether
|
|
93
93
|
if (opinionated) {
|
|
94
94
|
$html('span[style*="font-style"]').each((i, el) => {
|
|
95
|
-
if (!isAllowed(el)) {
|
|
95
|
+
if (!isAllowed($html, el)) {
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
let styleAttr = $(el).attr('style');
|
|
99
|
+
let styleAttr = $html(el).attr('style');
|
|
100
100
|
let styles = styleToObject(styleAttr);
|
|
101
101
|
let fontStyle = styles['font-style'];
|
|
102
102
|
|
|
@@ -105,25 +105,25 @@ const cleanHTML = (args) => {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
let stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
108
|
-
$(el).attr('style', stylelessStyleAttr);
|
|
108
|
+
$html(el).attr('style', stylelessStyleAttr);
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
if (opinionated) {
|
|
113
113
|
$html('a[style*="font-style"], p[style*="font-style"], li[style*="font-style"]').each((i, el) => {
|
|
114
|
-
if (!isAllowed(el)) {
|
|
114
|
+
if (!isAllowed($html, el)) {
|
|
115
115
|
return;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
let styleAttr = $(el).attr('style');
|
|
118
|
+
let styleAttr = $html(el).attr('style');
|
|
119
119
|
let styles = styleToObject(styleAttr);
|
|
120
120
|
let fontStyle = styles['font-style'];
|
|
121
121
|
|
|
122
122
|
let stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
123
|
-
$(el).attr('style', stylelessStyleAttr);
|
|
123
|
+
$html(el).attr('style', stylelessStyleAttr);
|
|
124
124
|
|
|
125
125
|
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
126
|
-
$(el).html(`<i>${$(el).html()}</i>`);
|
|
126
|
+
$html(el).html(`<i>${$html(el).html()}</i>`);
|
|
127
127
|
}
|
|
128
128
|
});
|
|
129
129
|
}
|
|
@@ -131,11 +131,11 @@ const cleanHTML = (args) => {
|
|
|
131
131
|
// Remove text color decelerations
|
|
132
132
|
if (opinionated) {
|
|
133
133
|
$html('[style*="color"]').each((i, el) => {
|
|
134
|
-
if (!isAllowed(el)) {
|
|
134
|
+
if (!isAllowed($html, el)) {
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
let styleAttr = $(el).attr('style');
|
|
138
|
+
let styleAttr = $html(el).attr('style');
|
|
139
139
|
let styles = styleToObject(styleAttr);
|
|
140
140
|
let color = styles?.color ?? false;
|
|
141
141
|
|
|
@@ -146,18 +146,18 @@ const cleanHTML = (args) => {
|
|
|
146
146
|
let theReg = new RegExp(`color: ?${escapeStringRegexp(color)};?`);
|
|
147
147
|
let colorlessStyleAttr = styleAttr.replace(theReg, '');
|
|
148
148
|
|
|
149
|
-
$(el).attr('style', colorlessStyleAttr);
|
|
149
|
+
$html(el).attr('style', colorlessStyleAttr);
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
// Remove background decelerations
|
|
154
154
|
if (opinionated) {
|
|
155
155
|
$html('[style*="background"]').each((i, el) => {
|
|
156
|
-
if (!isAllowed(el)) {
|
|
156
|
+
if (!isAllowed($html, el)) {
|
|
157
157
|
return;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
let styleAttr = $(el).attr('style');
|
|
160
|
+
let styleAttr = $html(el).attr('style');
|
|
161
161
|
let styles = styleToObject(styleAttr);
|
|
162
162
|
|
|
163
163
|
let background = styles.background ?? false;
|
|
@@ -205,22 +205,22 @@ const cleanHTML = (args) => {
|
|
|
205
205
|
styleAttr = styleAttr.replace(new RegExp(`background-size: ?${escapeStringRegexp(backgroundSize)};?`), '');
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
-
$(el).attr('style', styleAttr);
|
|
208
|
+
$html(el).attr('style', styleAttr);
|
|
209
209
|
});
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
// Remove empty `style=""` tags
|
|
213
213
|
if (opinionated) {
|
|
214
214
|
$html('[style]').each((i, el) => {
|
|
215
|
-
if (!isAllowed(el)) {
|
|
215
|
+
if (!isAllowed($html, el)) {
|
|
216
216
|
return;
|
|
217
217
|
}
|
|
218
218
|
|
|
219
|
-
let styleAttr = $(el).attr('style').trim();
|
|
219
|
+
let styleAttr = $html(el).attr('style').trim();
|
|
220
220
|
let styleAttrLength = styleAttr.length;
|
|
221
221
|
|
|
222
222
|
if (styleAttrLength === 0) {
|
|
223
|
-
$(el).removeAttr('style');
|
|
223
|
+
$html(el).removeAttr('style');
|
|
224
224
|
}
|
|
225
225
|
});
|
|
226
226
|
}
|
|
@@ -228,37 +228,37 @@ const cleanHTML = (args) => {
|
|
|
228
228
|
// Remove formatting tags from headers
|
|
229
229
|
if (opinionated) {
|
|
230
230
|
$html('h1, h2, h3, h4, h5, h6').each((i, el) => {
|
|
231
|
-
if (!isAllowed(el)) {
|
|
231
|
+
if (!isAllowed($html, el)) {
|
|
232
232
|
return;
|
|
233
233
|
}
|
|
234
234
|
|
|
235
|
-
$(el).find('*').each((ii, ell) => {
|
|
235
|
+
$html(el).find('*').each((ii, ell) => {
|
|
236
236
|
if (['b', 'strong', 'i', 'em', 'span'].includes(ell.tagName)) {
|
|
237
|
-
$(ell).
|
|
237
|
+
$html(ell).replaceWith($html(ell).contents());
|
|
238
238
|
}
|
|
239
239
|
});
|
|
240
240
|
|
|
241
241
|
// THis could introduce extra spaces, so trim them
|
|
242
|
-
let headerHTML = $(el).html();
|
|
242
|
+
let headerHTML = $html(el).html();
|
|
243
243
|
let headerHTMLTrimmed = headerHTML.replace(/ {2,}/, '').trim();
|
|
244
|
-
$(el).html(headerHTMLTrimmed);
|
|
244
|
+
$html(el).html(headerHTMLTrimmed);
|
|
245
245
|
});
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
// Remove <p> & <li> tags, and remove leading or trailing <br>'s
|
|
249
249
|
if (opinionated) {
|
|
250
250
|
$html('p, li').each((i, el) => {
|
|
251
|
-
if (!isAllowed(el)) {
|
|
251
|
+
if (!isAllowed($html, el)) {
|
|
252
252
|
return;
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
let content = $(el).html().trim();
|
|
255
|
+
let content = $html(el).html().trim();
|
|
256
256
|
content = content.replace(/^(<br ?\/?>){1,}|(<br ?\/?>){1,}$/gm, '');
|
|
257
257
|
|
|
258
258
|
if (content.length === 0) {
|
|
259
|
-
$(el).remove();
|
|
259
|
+
$html(el).remove();
|
|
260
260
|
} else {
|
|
261
|
-
$(el).html(content);
|
|
261
|
+
$html(el).html(content);
|
|
262
262
|
}
|
|
263
263
|
});
|
|
264
264
|
}
|
|
@@ -266,20 +266,20 @@ const cleanHTML = (args) => {
|
|
|
266
266
|
// Wrap some lists in a HTML card
|
|
267
267
|
if (cards) {
|
|
268
268
|
$html('ol, ul').each((i, ul) => {
|
|
269
|
-
let $parent = ($(ul).parents('ul, ol').last().length) ? $(ul).parents('ul, ol').last() : $(ul);
|
|
269
|
+
let $parent = ($html(ul).parents('ul, ol').last().length) ? $html(ul).parents('ul, ol').last() : $html(ul);
|
|
270
270
|
|
|
271
|
-
let hasStyle = ($($parent).attr('style')) ? true : false;
|
|
272
|
-
let hasType = ($($parent).attr('type') || $($parent).find('[type]').length) ? true : false;
|
|
273
|
-
let hasValue = ($($parent).attr('value') || $($parent).find('[value]').length) ? true : false;
|
|
274
|
-
let hasStart = ($($parent).attr('start') || $($parent).find('[start]').length) ? true : false;
|
|
275
|
-
let hasOLList = ($($parent).find('ol').length) ? true : false;
|
|
276
|
-
let hasULList = ($($parent).find('ul').length) ? true : false;
|
|
271
|
+
let hasStyle = ($html($parent).attr('style')) ? true : false;
|
|
272
|
+
let hasType = ($html($parent).attr('type') || $html($parent).find('[type]').length) ? true : false;
|
|
273
|
+
let hasValue = ($html($parent).attr('value') || $html($parent).find('[value]').length) ? true : false;
|
|
274
|
+
let hasStart = ($html($parent).attr('start') || $html($parent).find('[start]').length) ? true : false;
|
|
275
|
+
let hasOLList = ($html($parent).find('ol').length) ? true : false;
|
|
276
|
+
let hasULList = ($html($parent).find('ul').length) ? true : false;
|
|
277
277
|
|
|
278
278
|
if (hasStyle || hasType || hasValue || hasStart || hasOLList || hasULList) {
|
|
279
279
|
// If parent is not wrapped ina HTML card, wrap it in one
|
|
280
280
|
if ($parent.get(0)?.prev?.data !== 'kg-card-begin: html') {
|
|
281
|
-
$($parent).before('<!--kg-card-begin: html-->');
|
|
282
|
-
$($parent).after('<!--kg-card-end: html-->');
|
|
281
|
+
$html($parent).before('<!--kg-card-begin: html-->');
|
|
282
|
+
$html($parent).after('<!--kg-card-end: html-->');
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-clean-html",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.24",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-clean-html",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"jest-extended": "6.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"cheerio": "1.
|
|
28
|
+
"cheerio": "1.1.2",
|
|
29
29
|
"escape-string-regexp": "5.0.0",
|
|
30
|
-
"style-to-object": "1.0.
|
|
30
|
+
"style-to-object": "1.0.14"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "027225930710c0643603f6ae346dc76504d9bb7a"
|
|
33
33
|
}
|