@tryghost/mg-clean-html 0.4.0 → 0.6.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/README.md +7 -7
- package/build/index.d.ts +2 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/build/lib/process.d.ts +7 -0
- package/build/lib/process.js +226 -0
- package/build/lib/process.js.map +1 -0
- package/build/test/process.test.d.ts +1 -0
- package/build/test/process.test.js +207 -0
- package/build/test/process.test.js.map +1 -0
- package/package.json +25 -13
- package/index.js +0 -5
- package/lib/process.js +0 -299
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ To use this package in your own project:
|
|
|
12
12
|
|
|
13
13
|
or
|
|
14
14
|
|
|
15
|
-
`
|
|
15
|
+
`pnpm add @tryghost/mg-clean-html`
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
@@ -21,26 +21,26 @@ or
|
|
|
21
21
|
|
|
22
22
|
## Develop
|
|
23
23
|
|
|
24
|
-
This is a mono repository, managed with [Nx](https://nx.dev/) and
|
|
24
|
+
This is a mono repository, managed with [Nx](https://nx.dev/) and pnpm workspaces.
|
|
25
25
|
|
|
26
26
|
Follow the instructions for the top-level repo.
|
|
27
27
|
1. `git clone` this repo & `cd` into it as usual
|
|
28
|
-
2. Run `
|
|
28
|
+
2. Run `pnpm install` to install top-level dependencies.
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
## Run
|
|
32
32
|
|
|
33
|
-
To run a local development copy, `cd` into this directory, and use `
|
|
33
|
+
To run a local development copy, `cd` into this directory, and use `pnpm dev` instead of `migrate` like so:
|
|
34
34
|
|
|
35
35
|
```sh
|
|
36
|
-
|
|
36
|
+
pnpm dev curate --pathToZip /path/to/export.zip
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
## Test
|
|
41
41
|
|
|
42
|
-
- `
|
|
43
|
-
- `
|
|
42
|
+
- `pnpm lint` run just eslint
|
|
43
|
+
- `pnpm test` run lint and tests
|
|
44
44
|
|
|
45
45
|
|
|
46
46
|
# Copyright & License
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EACH,SAAS,EACZ,CAAC"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { domUtils } from '@tryghost/mg-utils';
|
|
2
|
+
// style-to-object's ESM types don't resolve correctly with NodeNext module resolution
|
|
3
|
+
// eslint-disable-next-line
|
|
4
|
+
import _styleToObject from 'style-to-object';
|
|
5
|
+
const styleToObject = _styleToObject;
|
|
6
|
+
import escapeStringRegexp from 'escape-string-regexp';
|
|
7
|
+
const { serializeChildren, replaceWith, parents, isComment, getCommentData, insertBefore, insertAfter } = domUtils;
|
|
8
|
+
const isAllowed = (el) => {
|
|
9
|
+
if (el.classList.contains('instagram-media')) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (parents(el, '.instagram-media').length) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
17
|
+
const changeTagName = (doc, el, newTag) => {
|
|
18
|
+
const newEl = doc.createElement(newTag);
|
|
19
|
+
for (const attr of Array.from(el.attributes)) {
|
|
20
|
+
newEl.setAttribute(attr.name, attr.value);
|
|
21
|
+
}
|
|
22
|
+
newEl.innerHTML = el.innerHTML;
|
|
23
|
+
replaceWith(el, newEl);
|
|
24
|
+
return newEl;
|
|
25
|
+
};
|
|
26
|
+
const cleanHTML = (args) => {
|
|
27
|
+
let html = args?.html ?? '';
|
|
28
|
+
const opinionated = args?.opinionated ?? false;
|
|
29
|
+
const cards = args?.cards ?? false;
|
|
30
|
+
// If there's nothing to process, return & exit early
|
|
31
|
+
if (html === '') {
|
|
32
|
+
return html;
|
|
33
|
+
}
|
|
34
|
+
return domUtils.processFragment(html, (parsed) => {
|
|
35
|
+
// Remove left, center & right text alignment
|
|
36
|
+
if (opinionated) {
|
|
37
|
+
parsed.$('p[style*="text-align"], li[style*="text-align"]').forEach((el) => {
|
|
38
|
+
if (!isAllowed(el)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let styleAttr = el.getAttribute('style');
|
|
42
|
+
styleAttr = styleAttr.replace(/text-align: ?(left|center|right);?/, '').trim();
|
|
43
|
+
el.setAttribute('style', styleAttr);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// Change inline font-weight to <b> tag or remove altogether
|
|
47
|
+
if (opinionated) {
|
|
48
|
+
parsed.$('span[style*="font-weight"]').forEach((el) => {
|
|
49
|
+
if (!isAllowed(el)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const styleAttr = el.getAttribute('style');
|
|
53
|
+
const styles = styleToObject(styleAttr);
|
|
54
|
+
const fontWeight = styles['font-weight'];
|
|
55
|
+
const weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
56
|
+
if (parseInt(fontWeight) >= 600 || ['bold', 'bolder'].includes(fontWeight)) {
|
|
57
|
+
const newEl = changeTagName(parsed.document, el, 'b');
|
|
58
|
+
newEl.setAttribute('style', weightlessStyleAttr);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
el.setAttribute('style', weightlessStyleAttr);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (opinionated) {
|
|
66
|
+
parsed.$('a[style*="font-weight"], p[style*="font-weight"], li[style*="font-weight"]').forEach((el) => {
|
|
67
|
+
if (!isAllowed(el)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const styleAttr = el.getAttribute('style');
|
|
71
|
+
const styles = styleToObject(styleAttr);
|
|
72
|
+
const fontWeight = styles['font-weight'];
|
|
73
|
+
const weightlessStyleAttr = styleAttr.replace(/font-weight: ?([a-zA-Z0-9-]+);?/, '');
|
|
74
|
+
el.setAttribute('style', weightlessStyleAttr);
|
|
75
|
+
if (['bold', 'bolder'].includes(fontWeight) || parseInt(fontWeight) >= 600) {
|
|
76
|
+
el.innerHTML = `<b>${serializeChildren(el)}</b>`;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
// Change inline font-style to <i> tag or remove altogether
|
|
81
|
+
if (opinionated) {
|
|
82
|
+
parsed.$('span[style*="font-style"]').forEach((el) => {
|
|
83
|
+
if (!isAllowed(el)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const styleAttr = el.getAttribute('style');
|
|
87
|
+
const styles = styleToObject(styleAttr);
|
|
88
|
+
const fontStyle = styles['font-style'];
|
|
89
|
+
const stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
90
|
+
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
91
|
+
const newEl = changeTagName(parsed.document, el, 'i');
|
|
92
|
+
newEl.setAttribute('style', stylelessStyleAttr);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
el.setAttribute('style', stylelessStyleAttr);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (opinionated) {
|
|
100
|
+
parsed.$('a[style*="font-style"], p[style*="font-style"], li[style*="font-style"]').forEach((el) => {
|
|
101
|
+
if (!isAllowed(el)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const styleAttr = el.getAttribute('style');
|
|
105
|
+
const styles = styleToObject(styleAttr);
|
|
106
|
+
const fontStyle = styles['font-style'];
|
|
107
|
+
const stylelessStyleAttr = styleAttr.replace(/font-style: ?([a-zA-Z0-9-]+);?/, '');
|
|
108
|
+
el.setAttribute('style', stylelessStyleAttr);
|
|
109
|
+
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
110
|
+
el.innerHTML = `<i>${serializeChildren(el)}</i>`;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Remove text color declarations
|
|
115
|
+
if (opinionated) {
|
|
116
|
+
parsed.$('[style*="color"]').forEach((el) => {
|
|
117
|
+
if (!isAllowed(el)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const styleAttr = el.getAttribute('style');
|
|
121
|
+
const styles = styleToObject(styleAttr);
|
|
122
|
+
const color = styles?.color ?? false;
|
|
123
|
+
if (!color) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const theReg = new RegExp(`color: ?${escapeStringRegexp(color)};?`);
|
|
127
|
+
const colorlessStyleAttr = styleAttr.replace(theReg, '');
|
|
128
|
+
el.setAttribute('style', colorlessStyleAttr);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Remove background declarations
|
|
132
|
+
if (opinionated) {
|
|
133
|
+
parsed.$('[style*="background"]').forEach((el) => {
|
|
134
|
+
if (!isAllowed(el)) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
let styleAttr = el.getAttribute('style');
|
|
138
|
+
const styles = styleToObject(styleAttr);
|
|
139
|
+
const bgProps = [
|
|
140
|
+
'background', 'background-attachment', 'background-clip',
|
|
141
|
+
'background-color', 'background-image', 'background-origin',
|
|
142
|
+
'background-position', 'background-repeat', 'background-size'
|
|
143
|
+
];
|
|
144
|
+
for (const prop of bgProps) {
|
|
145
|
+
const val = styles?.[prop] ?? false;
|
|
146
|
+
if (val) {
|
|
147
|
+
styleAttr = styleAttr.replace(new RegExp(`${escapeStringRegexp(prop)}: ?${escapeStringRegexp(val)};?`), '');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
el.setAttribute('style', styleAttr);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// Remove empty `style=""` tags
|
|
154
|
+
if (opinionated) {
|
|
155
|
+
parsed.$('[style]').forEach((el) => {
|
|
156
|
+
if (!isAllowed(el)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const styleAttr = (el.getAttribute('style')).trim();
|
|
160
|
+
if (styleAttr.length === 0) {
|
|
161
|
+
el.removeAttribute('style');
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
// Remove formatting tags from headers
|
|
166
|
+
if (opinionated) {
|
|
167
|
+
parsed.$('h1, h2, h3, h4, h5, h6').forEach((el) => {
|
|
168
|
+
if (!isAllowed(el)) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
el.querySelectorAll('*').forEach((ell) => {
|
|
172
|
+
if (['B', 'STRONG', 'I', 'EM', 'SPAN'].includes(ell.tagName)) {
|
|
173
|
+
replaceWith(ell, serializeChildren(ell));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
// This could introduce extra spaces, so trim them
|
|
177
|
+
let headerHTML = serializeChildren(el);
|
|
178
|
+
let headerHTMLTrimmed = headerHTML.replace(/ {2,}/, '').trim();
|
|
179
|
+
el.innerHTML = headerHTMLTrimmed;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// Remove <p> & <li> tags, and remove leading or trailing <br>'s
|
|
183
|
+
if (opinionated) {
|
|
184
|
+
parsed.$('p, li').forEach((el) => {
|
|
185
|
+
if (!isAllowed(el)) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
let content = serializeChildren(el).trim();
|
|
189
|
+
content = content.replace(/^(<br ?\/?>){1,}|(<br ?\/?>){1,}$/gm, '');
|
|
190
|
+
if (content.length === 0) {
|
|
191
|
+
el.remove();
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
el.innerHTML = content;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
// Wrap some lists in a HTML card
|
|
199
|
+
if (cards) {
|
|
200
|
+
parsed.$('ol, ul').forEach((ul) => {
|
|
201
|
+
const outermost = domUtils.lastParent(ul, 'ul, ol') ?? ul;
|
|
202
|
+
const hasStyle = !!outermost.getAttribute('style');
|
|
203
|
+
const hasType = !!outermost.getAttribute('type') || !!outermost.querySelectorAll('[type]').length;
|
|
204
|
+
const hasValue = !!outermost.getAttribute('value') || !!outermost.querySelectorAll('[value]').length;
|
|
205
|
+
const hasStart = !!outermost.getAttribute('start') || !!outermost.querySelectorAll('[start]').length;
|
|
206
|
+
const hasOLList = !!outermost.querySelectorAll('ol').length;
|
|
207
|
+
const hasULList = !!outermost.querySelectorAll('ul').length;
|
|
208
|
+
if (hasStyle || hasType || hasValue || hasStart || hasOLList || hasULList) {
|
|
209
|
+
// If parent is not wrapped in a HTML card, wrap it in one
|
|
210
|
+
const prev = outermost.previousSibling;
|
|
211
|
+
if (!isComment(prev) || getCommentData(prev) !== 'kg-card-begin: html') {
|
|
212
|
+
insertBefore(outermost, '<!--kg-card-begin: html-->');
|
|
213
|
+
insertAfter(outermost, '<!--kg-card-end: html-->');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
// convert HTML back to a string
|
|
219
|
+
html = parsed.html();
|
|
220
|
+
// Remove empty attributes
|
|
221
|
+
html = html.replace(/=""/g, '');
|
|
222
|
+
return html;
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
export { cleanHTML };
|
|
226
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../../src/lib/process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAC5C,sFAAsF;AACtF,2BAA2B;AAC3B,OAAO,cAAc,MAAM,iBAAiB,CAAC;AAC7C,MAAM,aAAa,GAAG,cAA6E,CAAC;AACpG,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AAEtD,MAAM,EAAC,iBAAiB,EAAE,WAAW,EAAE,OAAO,EAAE,SAAS,EAAE,cAAc,EAA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AG,MAAM,CAAC,IAAI,EAAE,CAAC;QAErB,0BAA0B;QAC1B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAEhC,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,OAAO,EACH,SAAS,EACZ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { cleanHTML } from '../index.js';
|
|
4
|
+
describe('Clean HTML', function () {
|
|
5
|
+
it('Can return nothing', function () {
|
|
6
|
+
assert.equal(cleanHTML(), '');
|
|
7
|
+
});
|
|
8
|
+
it('Removes left text alignment', function () {
|
|
9
|
+
let html = '<p style="text-align: left;">Hello</p>';
|
|
10
|
+
let result = cleanHTML({
|
|
11
|
+
html,
|
|
12
|
+
opinionated: true
|
|
13
|
+
});
|
|
14
|
+
assert.equal(result, '<p>Hello</p>');
|
|
15
|
+
});
|
|
16
|
+
it('Removes center text alignment', function () {
|
|
17
|
+
let html = '<p style="text-align: center;">Hello</p>';
|
|
18
|
+
let result = cleanHTML({
|
|
19
|
+
html,
|
|
20
|
+
opinionated: true
|
|
21
|
+
});
|
|
22
|
+
assert.equal(result, '<p>Hello</p>');
|
|
23
|
+
});
|
|
24
|
+
it('Removes right text alignment', function () {
|
|
25
|
+
let html = '<p style="text-align: right;">Hello</p>';
|
|
26
|
+
let result = cleanHTML({
|
|
27
|
+
html,
|
|
28
|
+
opinionated: true
|
|
29
|
+
});
|
|
30
|
+
assert.equal(result, '<p>Hello</p>');
|
|
31
|
+
});
|
|
32
|
+
it('Changes inline font weight to bold tag', function () {
|
|
33
|
+
let html = '<span style="font-weight: bold;">Hello</span> <span style="font-weight: 100;">Hello</span> <span style="font-weight: 800;">Hello</span> <span style="font-weight: revert-layer;">Hello</span>';
|
|
34
|
+
let result = cleanHTML({
|
|
35
|
+
html,
|
|
36
|
+
opinionated: true
|
|
37
|
+
});
|
|
38
|
+
assert.equal(result, '<b>Hello</b> <span>Hello</span> <b>Hello</b> <span>Hello</span>');
|
|
39
|
+
});
|
|
40
|
+
it('Changes bold styles to inner b tag', function () {
|
|
41
|
+
let html = '<a href="https://example.com" style="font-weight: bold;">Hello</a>';
|
|
42
|
+
let result = cleanHTML({
|
|
43
|
+
html,
|
|
44
|
+
opinionated: true
|
|
45
|
+
});
|
|
46
|
+
assert.equal(result, '<a href="https://example.com"><b>Hello</b></a>');
|
|
47
|
+
});
|
|
48
|
+
it('Changes inline font style to italic tag', function () {
|
|
49
|
+
let html = '<span style="font-style: italic;">Hello</span> <span style="font-style: normal;">Hello</span> <span style="font-style: oblique;">Hello</span>';
|
|
50
|
+
let result = cleanHTML({
|
|
51
|
+
html,
|
|
52
|
+
opinionated: true
|
|
53
|
+
});
|
|
54
|
+
assert.equal(result, '<i>Hello</i> <span>Hello</span> <i>Hello</i>');
|
|
55
|
+
});
|
|
56
|
+
it('Changes italic styles to inner b tag', function () {
|
|
57
|
+
let html = '<a href="https://example.com" style="font-style: italic;">Hello</a>';
|
|
58
|
+
let result = cleanHTML({
|
|
59
|
+
html,
|
|
60
|
+
opinionated: true
|
|
61
|
+
});
|
|
62
|
+
assert.equal(result, '<a href="https://example.com"><i>Hello</i></a>');
|
|
63
|
+
});
|
|
64
|
+
it('Removes inline styles and bold & italic tags from headers', function () {
|
|
65
|
+
let html = '<h2>Lorem <b>ipsum</b> <strong>dolor</strong> <i>sit</i> <em>amet</em> <span>consectetur</span> <span style="font-weight: bold;">adipiscing</span> <a href="https://example.com">elit</a> <a href="https://example.com"><b>eiusmod</b></a> </h2>';
|
|
66
|
+
let result = cleanHTML({
|
|
67
|
+
html,
|
|
68
|
+
opinionated: true
|
|
69
|
+
});
|
|
70
|
+
assert.equal(result, '<h2>Lorem ipsum dolor sit amet consectetur adipiscing <a href="https://example.com">elit</a> <a href="https://example.com">eiusmod</a></h2>');
|
|
71
|
+
});
|
|
72
|
+
it('Removes text color decelerations', function () {
|
|
73
|
+
let html = ['<a href="https://example.com" style="color: red;">Hello</a>',
|
|
74
|
+
'<a href="https://example.com" style="color: rgba(34 12 64 / 0.3);">Hello</a>',
|
|
75
|
+
'<a href="https://example.com" style="color: #090;">Hello</a>',
|
|
76
|
+
'<a href="https://example.com" style="color: #009900;">Hello</a>',
|
|
77
|
+
'<a href="https://example.com" style="color: hsla(30, 100%, 50%, 0.6);">Hello</a>',
|
|
78
|
+
'<a href="https://example.com" style="color: hwb(90 10% 10%);">Hello</a>',
|
|
79
|
+
'<a href="https://example.com" style="color: inherit;">Hello</a>'];
|
|
80
|
+
let result = cleanHTML({
|
|
81
|
+
html: html.join(''),
|
|
82
|
+
opinionated: true
|
|
83
|
+
});
|
|
84
|
+
assert.equal(result, '<a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a>');
|
|
85
|
+
});
|
|
86
|
+
it('Removes background color decelerations', function () {
|
|
87
|
+
let html = ['<a href="https://example.com" style="background-color: red;">Hello</a>',
|
|
88
|
+
'<a href="https://example.com" style="background-color: rgba(34 12 64 / 0.3);">Hello</a>',
|
|
89
|
+
'<a href="https://example.com" style="background-color: #090;">Hello</a>',
|
|
90
|
+
'<a href="https://example.com" style="background: #009900;">Hello</a>',
|
|
91
|
+
'<a href="https://example.com" style="background-color: hsla(30, 100%, 50%, 0.6);">Hello</a>',
|
|
92
|
+
'<a href="https://example.com" style="background-color: hwb(90 10% 10%);">Hello</a>',
|
|
93
|
+
'<a href="https://example.com" style="background-attachment: fixed; background-clip: content-box; background-color: #f00; background-image: url(/image.png); background-origin: content-box; background-position: 10% 25%; background-repeat: no-repeat; background-size: 110%;">Hello</a>'];
|
|
94
|
+
let result = cleanHTML({
|
|
95
|
+
html: html.join(''),
|
|
96
|
+
opinionated: true
|
|
97
|
+
});
|
|
98
|
+
assert.equal(result, '<a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a><a href="https://example.com">Hello</a>');
|
|
99
|
+
});
|
|
100
|
+
it('Removes empty <p> tags', function () {
|
|
101
|
+
let html = ['<p></p>',
|
|
102
|
+
'<p>Hello<br>World</p>',
|
|
103
|
+
'<p><br></p>',
|
|
104
|
+
'<p><br/></p>',
|
|
105
|
+
'<p><br><br></p>',
|
|
106
|
+
'<p><br>Again<br></p>',
|
|
107
|
+
'<p><br><br><br></p>',
|
|
108
|
+
'<p> </p>'];
|
|
109
|
+
let result = cleanHTML({
|
|
110
|
+
html: html.join(''),
|
|
111
|
+
opinionated: true
|
|
112
|
+
});
|
|
113
|
+
assert.equal(result, '<p>Hello<br>World</p><p>Again</p>');
|
|
114
|
+
});
|
|
115
|
+
it('Removes empty <li> tags', function () {
|
|
116
|
+
let html = ['<li></li>',
|
|
117
|
+
'<li>Hello<br>World</li>',
|
|
118
|
+
'<li><br></li>',
|
|
119
|
+
'<li><br/></li>',
|
|
120
|
+
'<li><br><br></li>',
|
|
121
|
+
'<li><br>Again<br></li>',
|
|
122
|
+
'<li><br><br><br></li>',
|
|
123
|
+
'<li> </li>'];
|
|
124
|
+
let result = cleanHTML({
|
|
125
|
+
html: html.join(''),
|
|
126
|
+
opinionated: true
|
|
127
|
+
});
|
|
128
|
+
assert.equal(result, '<li>Hello<br>World</li><li>Again</li>');
|
|
129
|
+
});
|
|
130
|
+
it('Wraps special lists in a HTML card', function () {
|
|
131
|
+
let html = '<ol><li value="10">Lorem</li><li>Ipsum</li></ol>';
|
|
132
|
+
let result = cleanHTML({
|
|
133
|
+
html,
|
|
134
|
+
cards: true
|
|
135
|
+
});
|
|
136
|
+
assert.equal(result, '<!--kg-card-begin: html--><ol><li value="10">Lorem</li><li>Ipsum</li></ol><!--kg-card-end: html-->');
|
|
137
|
+
});
|
|
138
|
+
it('Does not double-wrap carded special lists', function () {
|
|
139
|
+
let html = '<!--kg-card-begin: html--><ol><li value="10">Lorem</li><li>Ipsum</li></ol><!--kg-card-end: html-->';
|
|
140
|
+
let result = cleanHTML({
|
|
141
|
+
html,
|
|
142
|
+
cards: true
|
|
143
|
+
});
|
|
144
|
+
assert.equal(result, '<!--kg-card-begin: html--><ol><li value="10">Lorem</li><li>Ipsum</li></ol><!--kg-card-end: html-->');
|
|
145
|
+
});
|
|
146
|
+
it('Does not change Instagram embed', function () {
|
|
147
|
+
let html = '<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="https://www.instagram.com/p/CogY2A-y2_X/?utm_source=ig_embed&utm_campaign=loading" data-instgrm-version="14" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:540px; min-width:326px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:16px;"> <a href="https://www.instagram.com/p/CogY2A-y2_X/?utm_source=ig_embed&utm_campaign=loading" style=" background:#FFFFFF; line-height:0; padding:0 0; text-align:center; text-decoration:none; width:100%;" target="_blank"> <div style=" display: flex; flex-direction: row; align-items: center;"> <div style="background-color: #F4F4F4; border-radius: 50%; flex-grow: 0; height: 40px; margin-right: 14px; width: 40px;"></div> <div style="display: flex; flex-direction: column; flex-grow: 1; justify-content: center;"> <div style=" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 100px;"></div> <div style=" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 60px;"></div></div></div><div style="padding: 19% 0;"></div> <div style="display:block; height:50px; margin:0 auto 12px; width:50px;"><!-- SVG omitted for brevity --></div><div style="padding-top: 8px;"> <div style=" color:#3897f0; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:550; line-height:18px;">View this post on Instagram</div></div><div style="padding: 12.5% 0;"></div> <div style="display: flex; flex-direction: row; margin-bottom: 14px; align-items: center;"><div> <div style="background-color: #F4F4F4; border-radius: 50%; height: 12.5px; width: 12.5px; transform: translateX(0px) translateY(7px);"></div> <div style="background-color: #F4F4F4; height: 12.5px; transform: rotate(-45deg) translateX(3px) translateY(1px); width: 12.5px; flex-grow: 0; margin-right: 14px; margin-left: 2px;"></div> <div style="background-color: #F4F4F4; border-radius: 50%; height: 12.5px; width: 12.5px; transform: translateX(9px) translateY(-18px);"></div></div><div style="margin-left: 8px;"> <div style=" background-color: #F4F4F4; border-radius: 50%; flex-grow: 0; height: 20px; width: 20px;"></div> <div style=" width: 0; height: 0; border-top: 2px solid transparent; border-left: 6px solid #f4f4f4; border-bottom: 2px solid transparent; transform: translateX(16px) translateY(-4px) rotate(30deg)"></div></div><div style="margin-left: auto;"> <div style=" width: 0px; border-top: 8px solid #F4F4F4; border-right: 8px solid transparent; transform: translateY(16px);"></div> <div style=" background-color: #F4F4F4; flex-grow: 0; height: 12px; width: 16px; transform: translateY(-4px);"></div> <div style=" width: 0; height: 0; border-top: 8px solid #F4F4F4; border-left: 8px solid transparent; transform: translateY(-4px) translateX(8px);"></div></div></div> <div style="display: flex; flex-direction: column; flex-grow: 1; justify-content: center; margin-bottom: 24px;"> <div style=" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 224px;"></div> <div style=" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 144px;"></div></div></a><p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;"><a href="https://www.instagram.com/p/CogY2A-y2_X/?utm_source=ig_embed&utm_campaign=loading" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;" target="_blank">A post shared by Cat Lovers Club (@catloversclub)</a></p></div></blockquote> <script async src="//www.instagram.com/embed.js"></script>';
|
|
148
|
+
let result = cleanHTML({
|
|
149
|
+
html,
|
|
150
|
+
opinionated: true,
|
|
151
|
+
cards: true
|
|
152
|
+
});
|
|
153
|
+
assert.equal(result, html);
|
|
154
|
+
});
|
|
155
|
+
it('Skips opinionated cleanup inside Instagram embeds', function () {
|
|
156
|
+
let html = '<blockquote class="instagram-media">'
|
|
157
|
+
+ '<p style="text-align: center;">Centered</p>'
|
|
158
|
+
+ '<span style="font-weight: bold;">Bold</span>'
|
|
159
|
+
+ '<a style="font-weight: bold;">Bold link</a>'
|
|
160
|
+
+ '<span style="font-style: italic;">Italic</span>'
|
|
161
|
+
+ '<a style="font-style: italic;">Italic link</a>'
|
|
162
|
+
+ '<a style="color: red;">Red</a>'
|
|
163
|
+
+ '<a style="background-color: blue;">Blue bg</a>'
|
|
164
|
+
+ '<p style>Empty style</p>'
|
|
165
|
+
+ '<h2><b>Header</b></h2>'
|
|
166
|
+
+ '<p></p>'
|
|
167
|
+
+ '</blockquote>';
|
|
168
|
+
let result = cleanHTML({
|
|
169
|
+
html,
|
|
170
|
+
opinionated: true
|
|
171
|
+
});
|
|
172
|
+
assert.equal(result, html);
|
|
173
|
+
});
|
|
174
|
+
it('Removes non-bold font-weight from a/p/li without wrapping in b tag', function () {
|
|
175
|
+
let html = '<p style="font-weight: 300;">Light text</p>';
|
|
176
|
+
let result = cleanHTML({
|
|
177
|
+
html,
|
|
178
|
+
opinionated: true
|
|
179
|
+
});
|
|
180
|
+
assert.equal(result, '<p>Light text</p>');
|
|
181
|
+
});
|
|
182
|
+
it('Removes non-italic font-style from a/p/li without wrapping in i tag', function () {
|
|
183
|
+
let html = '<p style="font-style: normal;">Normal text</p>';
|
|
184
|
+
let result = cleanHTML({
|
|
185
|
+
html,
|
|
186
|
+
opinionated: true
|
|
187
|
+
});
|
|
188
|
+
assert.equal(result, '<p>Normal text</p>');
|
|
189
|
+
});
|
|
190
|
+
it('Does not wrap plain lists without special attributes', function () {
|
|
191
|
+
let html = '<ul><li>One</li><li>Two</li></ul>';
|
|
192
|
+
let result = cleanHTML({
|
|
193
|
+
html,
|
|
194
|
+
cards: true
|
|
195
|
+
});
|
|
196
|
+
assert.equal(result, '<ul><li>One</li><li>Two</li></ul>');
|
|
197
|
+
});
|
|
198
|
+
it('Removes empty attributes', function () {
|
|
199
|
+
let html = '<script async="" data-not-areal-thing src="/example.js"></script>';
|
|
200
|
+
let result = cleanHTML({
|
|
201
|
+
html,
|
|
202
|
+
cards: true
|
|
203
|
+
});
|
|
204
|
+
assert.equal(result, '<script async data-not-areal-thing src="/example.js"></script>');
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
//# sourceMappingURL=process.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.test.js","sourceRoot":"","sources":["../../src/test/process.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAC,QAAQ,EAAE,EAAE,EAAC,MAAM,WAAW,CAAC;AACvC,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAEtC,QAAQ,CAAC,YAAY,EAAE;IACnB,EAAE,CAAC,oBAAoB,EAAE;QACrB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,6BAA6B,EAAE;QAC9B,IAAI,IAAI,GAAG,wCAAwC,CAAC;QACpD,IAAI,MAAM,GAAG,SAAS,CAAC;YACnB,IAAI;YACJ,WAAW,EAAE,IAAI;SACpB,CAAC,CAAC;QACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+BAA+B,EAAE;QAChC,IAAI,IAAI,GAAG,0CAA0C,CAAC;QACtD,IAAI,MAAM,GAA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I,GAAG,mCAAmC,CAAC;QAC/C,IAAI,MAAM,GAAG,SAAS,CAAC;YACnB,IAAI;YACJ,KAAK,EAAE,IAAI;SACd,CAAC,CAAC;QACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,mCAAmC,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0BAA0B,EAAE;QAC3B,IAAI,IAAI,GAAG,mEAAmE,CAAC;QAC/E,IAAI,MAAM,GAAG,SAAS,CAAC;YACnB,IAAI;YACJ,KAAK,EAAE,IAAI;SACd,CAAC,CAAC;QACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,gEAAgE,CAAC,CAAC;IAC3F,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-clean-html",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -8,27 +8,39 @@
|
|
|
8
8
|
},
|
|
9
9
|
"author": "Ghost Foundation",
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"main": "index.js",
|
|
12
11
|
"type": "module",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"development": "./src/index.ts",
|
|
15
|
+
"default": "./build/index.js"
|
|
16
|
+
}
|
|
18
17
|
},
|
|
19
18
|
"files": [
|
|
20
|
-
"
|
|
21
|
-
"lib"
|
|
19
|
+
"build"
|
|
22
20
|
],
|
|
23
21
|
"publishConfig": {
|
|
24
22
|
"access": "public"
|
|
25
23
|
},
|
|
26
24
|
"devDependencies": {
|
|
27
|
-
"
|
|
25
|
+
"@types/node": "24.12.0",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "8.58.0",
|
|
27
|
+
"@typescript-eslint/parser": "8.58.0",
|
|
28
|
+
"c8": "11.0.0",
|
|
29
|
+
"eslint": "8.57.1",
|
|
30
|
+
"eslint-plugin-ghost": "3.5.0",
|
|
31
|
+
"typescript": "6.0.2"
|
|
28
32
|
},
|
|
29
33
|
"dependencies": {
|
|
30
|
-
"cheerio": "1.2.0",
|
|
31
34
|
"escape-string-regexp": "5.0.0",
|
|
32
|
-
"style-to-object": "1.0.14"
|
|
35
|
+
"style-to-object": "1.0.14",
|
|
36
|
+
"@tryghost/mg-utils": "0.5.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"dev": "echo \"Implement me!\"",
|
|
40
|
+
"build": "rm -rf build && rm -rf tsconfig.tsbuildinfo && tsc --build --sourceMap",
|
|
41
|
+
"build:watch": "tsc --watch --preserveWatchOutput --sourceMap",
|
|
42
|
+
"test": "rm -rf build && pnpm build --force && c8 --src src --all --check-coverage --100 --reporter text --reporter cobertura node --test 'build/test/**/*.test.js'",
|
|
43
|
+
"posttest": "pnpm lint",
|
|
44
|
+
"lint": "eslint src/ --ext .ts --cache"
|
|
33
45
|
}
|
|
34
|
-
}
|
|
46
|
+
}
|
package/index.js
DELETED
package/lib/process.js
DELETED
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import * as cheerio from 'cheerio';
|
|
2
|
-
import styleToObject from 'style-to-object';
|
|
3
|
-
import escapeStringRegexp from 'escape-string-regexp';
|
|
4
|
-
|
|
5
|
-
const isAllowed = ($, el) => {
|
|
6
|
-
const $el = $(el);
|
|
7
|
-
if ($el.hasClass('instagram-media')) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if ($el.parents('.instagram-media').length) {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return true;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const cleanHTML = (args) => {
|
|
19
|
-
let html = args?.html ?? '';
|
|
20
|
-
const opinionated = args?.opinionated ?? false;
|
|
21
|
-
const cards = args?.cards ?? false;
|
|
22
|
-
|
|
23
|
-
const ignoreSelectors = [];
|
|
24
|
-
ignoreSelectors.push('.instagram-media');
|
|
25
|
-
|
|
26
|
-
// If there's nothing to process, return & exit early
|
|
27
|
-
if (html === '') {
|
|
28
|
-
return html;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const $html = cheerio.load(html, {
|
|
32
|
-
xml: {
|
|
33
|
-
xmlMode: false,
|
|
34
|
-
decodeEntities: false
|
|
35
|
-
}
|
|
36
|
-
}, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
|
|
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($html, el)) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let styleAttr = $html(el).attr('style');
|
|
46
|
-
styleAttr = styleAttr.replace(/text-align: ?(left|center|right);?/, '').trim();
|
|
47
|
-
$html(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($html, el)) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let styleAttr = $html(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
|
-
$html(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($html, el)) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
let styleAttr = $html(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
|
-
$html(el).attr('style', weightlessStyleAttr);
|
|
85
|
-
|
|
86
|
-
if (['bold', 'bolder'].includes(fontWeight) || parseInt(fontWeight) >= 600) {
|
|
87
|
-
$html(el).html(`<b>${$html(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($html, el)) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
let styleAttr = $html(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
|
-
$html(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($html, el)) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
let styleAttr = $html(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
|
-
$html(el).attr('style', stylelessStyleAttr);
|
|
124
|
-
|
|
125
|
-
if (['italic', 'oblique'].includes(fontStyle)) {
|
|
126
|
-
$html(el).html(`<i>${$html(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($html, el)) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
let styleAttr = $html(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
|
-
$html(el).attr('style', colorlessStyleAttr);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Remove background decelerations
|
|
154
|
-
if (opinionated) {
|
|
155
|
-
$html('[style*="background"]').each((i, el) => {
|
|
156
|
-
if (!isAllowed($html, el)) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
let styleAttr = $html(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
|
-
$html(el).attr('style', styleAttr);
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Remove empty `style=""` tags
|
|
213
|
-
if (opinionated) {
|
|
214
|
-
$html('[style]').each((i, el) => {
|
|
215
|
-
if (!isAllowed($html, el)) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
let styleAttr = $html(el).attr('style').trim();
|
|
220
|
-
let styleAttrLength = styleAttr.length;
|
|
221
|
-
|
|
222
|
-
if (styleAttrLength === 0) {
|
|
223
|
-
$html(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($html, el)) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
$html(el).find('*').each((ii, ell) => {
|
|
236
|
-
if (['b', 'strong', 'i', 'em', 'span'].includes(ell.tagName)) {
|
|
237
|
-
$html(ell).replaceWith($html(ell).contents());
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
// THis could introduce extra spaces, so trim them
|
|
242
|
-
let headerHTML = $html(el).html();
|
|
243
|
-
let headerHTMLTrimmed = headerHTML.replace(/ {2,}/, '').trim();
|
|
244
|
-
$html(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($html, el)) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
let content = $html(el).html().trim();
|
|
256
|
-
content = content.replace(/^(<br ?\/?>){1,}|(<br ?\/?>){1,}$/gm, '');
|
|
257
|
-
|
|
258
|
-
if (content.length === 0) {
|
|
259
|
-
$html(el).remove();
|
|
260
|
-
} else {
|
|
261
|
-
$html(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 = ($html(ul).parents('ul, ol').last().length) ? $html(ul).parents('ul, ol').last() : $html(ul);
|
|
270
|
-
|
|
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
|
-
|
|
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
|
-
$html($parent).before('<!--kg-card-begin: html-->');
|
|
282
|
-
$html($parent).after('<!--kg-card-end: html-->');
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// convert HTML back to a string
|
|
289
|
-
html = $html.html();
|
|
290
|
-
|
|
291
|
-
// Remove empty attributes
|
|
292
|
-
html = html.replace(/=""/g, '');
|
|
293
|
-
|
|
294
|
-
return html;
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
export {
|
|
298
|
-
cleanHTML
|
|
299
|
-
};
|