@tryghost/mg-clean-html 0.2.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/LICENSE +21 -0
- package/README.md +48 -0
- package/index.js +5 -0
- package/lib/process.js +296 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2022 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Clean HTML
|
|
2
|
+
|
|
3
|
+
An opinionated set of cleaning steps, taking HTML full of inline styles and badly structured content and converting it into something cleaner.
|
|
4
|
+
|
|
5
|
+
This is intended to run _after_ platform -specific HTML changes have been made
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
To use this package in your own project:
|
|
10
|
+
|
|
11
|
+
`npm install @tryghost/mg-clean-html --save`
|
|
12
|
+
|
|
13
|
+
or
|
|
14
|
+
|
|
15
|
+
`yarn add @tryghost/mg-clean-html`
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Develop
|
|
23
|
+
|
|
24
|
+
This is a mono repository, managed with [lerna](https://lerna.js.org).
|
|
25
|
+
|
|
26
|
+
Follow the instructions for the top-level repo.
|
|
27
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
28
|
+
2. Run `yarn` to install top-level dependencies.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Run
|
|
32
|
+
|
|
33
|
+
To run a local development copy, `cd` into this directory, and use `yarn dev` instead of `migrate` like so:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
yarn dev curate --pathToZip /path/to/export.zip
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Test
|
|
41
|
+
|
|
42
|
+
- `yarn lint` run just eslint
|
|
43
|
+
- `yarn test` run lint and tests
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# Copyright & License
|
|
47
|
+
|
|
48
|
+
Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
package/lib/process.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import $ from 'cheerio';
|
|
2
|
+
import styleToObject from 'style-to-object';
|
|
3
|
+
import escapeStringRegexp from 'escape-string-regexp';
|
|
4
|
+
|
|
5
|
+
$.prototype.unwrap = function () {
|
|
6
|
+
this.replaceWith(this.html());
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const isAllowed = (el) => {
|
|
10
|
+
if ($(el).hasClass('instagram-media')) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if ($(el).parents('.instagram-media').length) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const cleanHTML = (args) => {
|
|
22
|
+
let html = args?.html ?? '';
|
|
23
|
+
const opinionated = args?.opinionated ?? false;
|
|
24
|
+
const cards = args?.cards ?? false;
|
|
25
|
+
|
|
26
|
+
const ignoreSelectors = [];
|
|
27
|
+
ignoreSelectors.push('.instagram-media');
|
|
28
|
+
|
|
29
|
+
// If there's nothing to process, return & exit early
|
|
30
|
+
if (html === '') {
|
|
31
|
+
return html;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const $html = $.load(html, {
|
|
35
|
+
decodeEntities: false
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Remove left, center & right text alignment
|
|
39
|
+
if (opinionated) {
|
|
40
|
+
$html('p[style*="text-align"], li[style*="text-align"]').each((i, el) => {
|
|
41
|
+
if (!isAllowed(el)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let styleAttr = $(el).attr('style');
|
|
46
|
+
styleAttr = styleAttr.replace(/text-align: ?(left|center|right);?/, '').trim();
|
|
47
|
+
$(el).attr('style', styleAttr);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Change inline font-weight to <b> tag or remove altogether
|
|
52
|
+
if (opinionated) {
|
|
53
|
+
$html('span[style*="font-weight"]').each((i, el) => {
|
|
54
|
+
if (!isAllowed(el)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let styleAttr = $(el).attr('style');
|
|
59
|
+
let styles = styleToObject(styleAttr);
|
|
60
|
+
let fontWeight = styles['font-weight'];
|
|
61
|
+
|
|
62
|
+
if (parseInt(fontWeight) >= 600) {
|
|
63
|
+
el.tagName = 'b';
|
|
64
|
+
} else if (['bold', 'bolder'].includes(fontWeight)) {
|
|
65
|
+
el.tagName = 'b';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
69
|
+
$(el).attr('style', weightlessStyleAttr);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (opinionated) {
|
|
74
|
+
$html('a[style*="font-weight"], p[style*="font-weight"], li[style*="font-weight"]').each((i, el) => {
|
|
75
|
+
if (!isAllowed(el)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let styleAttr = $(el).attr('style');
|
|
80
|
+
let styles = styleToObject(styleAttr);
|
|
81
|
+
let fontWeight = styles['font-weight'];
|
|
82
|
+
|
|
83
|
+
let weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
84
|
+
$(el).attr('style', weightlessStyleAttr);
|
|
85
|
+
|
|
86
|
+
if (['bold', 'bolder'].includes(fontWeight) || parseInt(fontWeight) >= 600) {
|
|
87
|
+
$(el).html(`<b>${$(el).html()}</b>`);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Change inline font-style to <b> tag or remove altogether
|
|
93
|
+
if (opinionated) {
|
|
94
|
+
$html('span[style*="font-style"]').each((i, el) => {
|
|
95
|
+
if (!isAllowed(el)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let styleAttr = $(el).attr('style');
|
|
100
|
+
let styles = styleToObject(styleAttr);
|
|
101
|
+
let fontStyle = styles['font-style'];
|
|
102
|
+
|
|
103
|
+
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
104
|
+
el.tagName = 'i';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
108
|
+
$(el).attr('style', stylelessStyleAttr);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (opinionated) {
|
|
113
|
+
$html('a[style*="font-style"], p[style*="font-style"], li[style*="font-style"]').each((i, el) => {
|
|
114
|
+
if (!isAllowed(el)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let styleAttr = $(el).attr('style');
|
|
119
|
+
let styles = styleToObject(styleAttr);
|
|
120
|
+
let fontStyle = styles['font-style'];
|
|
121
|
+
|
|
122
|
+
let stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
123
|
+
$(el).attr('style', stylelessStyleAttr);
|
|
124
|
+
|
|
125
|
+
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
126
|
+
$(el).html(`<i>${$(el).html()}</i>`);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Remove text color decelerations
|
|
132
|
+
if (opinionated) {
|
|
133
|
+
$html('[style*="color"]').each((i, el) => {
|
|
134
|
+
if (!isAllowed(el)) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let styleAttr = $(el).attr('style');
|
|
139
|
+
let styles = styleToObject(styleAttr);
|
|
140
|
+
let color = styles?.color ?? false;
|
|
141
|
+
|
|
142
|
+
if (!color) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let theReg = new RegExp(`color: ?${escapeStringRegexp(color)};?`);
|
|
147
|
+
let colorlessStyleAttr = styleAttr.replace(theReg, '');
|
|
148
|
+
|
|
149
|
+
$(el).attr('style', colorlessStyleAttr);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Remove background decelerations
|
|
154
|
+
if (opinionated) {
|
|
155
|
+
$html('[style*="background"]').each((i, el) => {
|
|
156
|
+
if (!isAllowed(el)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let styleAttr = $(el).attr('style');
|
|
161
|
+
let styles = styleToObject(styleAttr);
|
|
162
|
+
|
|
163
|
+
let background = styles.background ?? false;
|
|
164
|
+
if (background) {
|
|
165
|
+
styleAttr = styleAttr.replace(new RegExp(`background: ?${escapeStringRegexp(background)};?`), '');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let backgroundAttachment = styles['background-attachment'] ?? false;
|
|
169
|
+
if (backgroundAttachment) {
|
|
170
|
+
styleAttr = styleAttr.replace(new RegExp(`background-attachment: ?${escapeStringRegexp(backgroundAttachment)};?`), '');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let backgroundClip = styles['background-clip'] ?? false;
|
|
174
|
+
if (backgroundClip) {
|
|
175
|
+
styleAttr = styleAttr.replace(new RegExp(`background-clip: ?${escapeStringRegexp(backgroundClip)};?`), '');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let backgroundColor = styles['background-color'] ?? false;
|
|
179
|
+
if (backgroundColor) {
|
|
180
|
+
styleAttr = styleAttr.replace(new RegExp(`background-color: ?${escapeStringRegexp(backgroundColor)};?`), '');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let backgroundImage = styles['background-image'] ?? false;
|
|
184
|
+
if (backgroundImage) {
|
|
185
|
+
styleAttr = styleAttr.replace(new RegExp(`background-image: ?${escapeStringRegexp(backgroundImage)};?`), '');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
let backgroundOrigin = styles['background-origin'] ?? false;
|
|
189
|
+
if (backgroundOrigin) {
|
|
190
|
+
styleAttr = styleAttr.replace(new RegExp(`background-origin: ?${escapeStringRegexp(backgroundOrigin)};?`), '');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let backgroundPosition = styles['background-position'] ?? false;
|
|
194
|
+
if (backgroundPosition) {
|
|
195
|
+
styleAttr = styleAttr.replace(new RegExp(`background-position: ?${escapeStringRegexp(backgroundPosition)};?`), '');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let backgroundRepeat = styles['background-repeat'] ?? false;
|
|
199
|
+
if (backgroundRepeat) {
|
|
200
|
+
styleAttr = styleAttr.replace(new RegExp(`background-repeat: ?${escapeStringRegexp(backgroundRepeat)};?`), '');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let backgroundSize = styles['background-size'] ?? false;
|
|
204
|
+
if (backgroundSize) {
|
|
205
|
+
styleAttr = styleAttr.replace(new RegExp(`background-size: ?${escapeStringRegexp(backgroundSize)};?`), '');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
$(el).attr('style', styleAttr);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Remove empty `style=""` tags
|
|
213
|
+
if (opinionated) {
|
|
214
|
+
$html('[style]').each((i, el) => {
|
|
215
|
+
if (!isAllowed(el)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let styleAttr = $(el).attr('style').trim();
|
|
220
|
+
let styleAttrLength = styleAttr.length;
|
|
221
|
+
|
|
222
|
+
if (styleAttrLength === 0) {
|
|
223
|
+
$(el).removeAttr('style');
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Remove formatting tags from headers
|
|
229
|
+
if (opinionated) {
|
|
230
|
+
$html('h1, h2, h3, h4, h5, h6').each((i, el) => {
|
|
231
|
+
if (!isAllowed(el)) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
$(el).find('*').each((ii, ell) => {
|
|
236
|
+
if (['b', 'strong', 'i', 'em', 'span'].includes(ell.tagName)) {
|
|
237
|
+
$(ell).unwrap();
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// THis could introduce extra spaces, so trim them
|
|
242
|
+
let headerHTML = $(el).html();
|
|
243
|
+
let headerHTMLTrimmed = headerHTML.replace(/ {2,}/, '').trim();
|
|
244
|
+
$(el).html(headerHTMLTrimmed);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Remove <p> & <li> tags, and remove leading or trailing <br>'s
|
|
249
|
+
if (opinionated) {
|
|
250
|
+
$html('p, li').each((i, el) => {
|
|
251
|
+
if (!isAllowed(el)) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let content = $(el).html().trim();
|
|
256
|
+
content = content.replace(/^(<br ?\/?>){1,}|(<br ?\/?>){1,}$/gm, '');
|
|
257
|
+
|
|
258
|
+
if (content.length === 0) {
|
|
259
|
+
$(el).remove();
|
|
260
|
+
} else {
|
|
261
|
+
$(el).html(content);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Wrap some lists in a HTML card
|
|
267
|
+
if (cards) {
|
|
268
|
+
$html('ol, ul').each((i, ul) => {
|
|
269
|
+
let $parent = ($(ul).parents('ul, ol').last().length) ? $(ul).parents('ul, ol').last() : $(ul);
|
|
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;
|
|
277
|
+
|
|
278
|
+
if (hasStyle || hasType || hasValue || hasStart || hasOLList || hasULList) {
|
|
279
|
+
// If parent is not wrapped ina HTML card, wrap it in one
|
|
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-->');
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// convert HTML back to a string
|
|
289
|
+
html = $html.html();
|
|
290
|
+
|
|
291
|
+
return html;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export {
|
|
295
|
+
cleanHTML
|
|
296
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/mg-clean-html",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-clean-html",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"dev": "echo \"Implement me!\"",
|
|
11
|
+
"test": "node --experimental-vm-modules $(yarn bin jest)",
|
|
12
|
+
"lint": "eslint . --ext .js --cache",
|
|
13
|
+
"posttest": "yarn lint"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"lib"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"eslint": "8.34.0",
|
|
24
|
+
"jest": "29.4.3",
|
|
25
|
+
"jest-extended": "3.2.3"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"cheerio": "0.22.0",
|
|
29
|
+
"escape-string-regexp": "5.0.0",
|
|
30
|
+
"style-to-object": "0.4.1"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "c4270f6229caa350155a03a0646b3c0c21eaff5e"
|
|
33
|
+
}
|