@tryghost/mg-clean-html 0.13.3 → 0.13.4
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.
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { domUtils } from '@tryghost/mg-utils';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* like `<!-- wp:paragraph -->`) converts to an empty Lexical root, which Ghost rejects, whereas
|
|
5
|
-
* an empty string imports fine. If nothing but whitespace remains, an empty string is returned.
|
|
3
|
+
* Strip ordinary comments, preserving Koenig markers and the members-only paywall.
|
|
4
|
+
* A supplied fragment is cleaned in place. Return an empty string for a blank body.
|
|
6
5
|
*/
|
|
7
|
-
declare const removeNonKoenigComments: (html?: string | null) => string;
|
|
6
|
+
declare const removeNonKoenigComments: (html?: string | domUtils.ParsedFragment | null) => string;
|
|
8
7
|
export { removeNonKoenigComments };
|
|
@@ -3,7 +3,8 @@ const { isComment, getCommentData } = domUtils;
|
|
|
3
3
|
const removeCommentNodes = (node) => {
|
|
4
4
|
for (const child of Array.from(node.childNodes)) {
|
|
5
5
|
if (isComment(child)) {
|
|
6
|
-
|
|
6
|
+
const marker = getCommentData(child).trim();
|
|
7
|
+
if (!marker.startsWith('kg-') && marker !== 'members-only') {
|
|
7
8
|
child.remove();
|
|
8
9
|
}
|
|
9
10
|
}
|
|
@@ -12,25 +13,26 @@ const removeCommentNodes = (node) => {
|
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
};
|
|
16
|
+
const cleanFragment = (parsed) => {
|
|
17
|
+
removeCommentNodes(parsed.body);
|
|
18
|
+
const cleaned = parsed.html();
|
|
19
|
+
return cleaned.trim() === '' ? '' : cleaned;
|
|
20
|
+
};
|
|
15
21
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* like `<!-- wp:paragraph -->`) converts to an empty Lexical root, which Ghost rejects, whereas
|
|
19
|
-
* an empty string imports fine. If nothing but whitespace remains, an empty string is returned.
|
|
22
|
+
* Strip ordinary comments, preserving Koenig markers and the members-only paywall.
|
|
23
|
+
* A supplied fragment is cleaned in place. Return an empty string for a blank body.
|
|
20
24
|
*/
|
|
21
25
|
const removeNonKoenigComments = (html) => {
|
|
22
|
-
|
|
26
|
+
if (html && typeof html !== 'string') {
|
|
27
|
+
return cleanFragment(html);
|
|
28
|
+
}
|
|
23
29
|
if (!html || html.trim() === '') {
|
|
24
30
|
return '';
|
|
25
31
|
}
|
|
26
32
|
if (!html.includes('<!--')) {
|
|
27
33
|
return html;
|
|
28
34
|
}
|
|
29
|
-
|
|
30
|
-
removeCommentNodes(parsed.body);
|
|
31
|
-
return parsed.html();
|
|
32
|
-
});
|
|
33
|
-
return cleaned.trim() === '' ? '' : cleaned;
|
|
35
|
+
return domUtils.processFragment(html, cleanFragment);
|
|
34
36
|
};
|
|
35
37
|
export { removeNonKoenigComments };
|
|
36
38
|
//# sourceMappingURL=remove-comments.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-comments.js","sourceRoot":"","sources":["../../src/lib/remove-comments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAE5C,MAAM,EAAC,SAAS,EAAE,cAAc,EAAC,GAAG,QAAQ,CAAC;AAE7C,MAAM,kBAAkB,GAAG,CAAC,IAAU,EAAQ,EAAE;IAC5C,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC9C,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACnB,
|
|
1
|
+
{"version":3,"file":"remove-comments.js","sourceRoot":"","sources":["../../src/lib/remove-comments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAE5C,MAAM,EAAC,SAAS,EAAE,cAAc,EAAC,GAAG,QAAQ,CAAC;AAE7C,MAA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CAAC;AACzD,CAAC,CAAC;AAEF,OAAO,EAAC,uBAAuB,EAAC,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
2
|
import { describe, it } from 'node:test';
|
|
3
|
+
import { domUtils } from '@tryghost/mg-utils';
|
|
3
4
|
import { removeNonKoenigComments } from '../index.js';
|
|
4
5
|
describe('removeNonKoenigComments', function () {
|
|
5
6
|
it('Returns an empty string for missing or empty HTML', function () {
|
|
@@ -19,6 +20,12 @@ describe('removeNonKoenigComments', function () {
|
|
|
19
20
|
const html = '<!-- wp:html --><!--kg-card-begin: html--><div>Card<!-- inside --></div><!--kg-card-end: html--><!-- /wp:html -->';
|
|
20
21
|
assert.equal(removeNonKoenigComments(html), '<!--kg-card-begin: html--><div>Card</div><!--kg-card-end: html-->');
|
|
21
22
|
});
|
|
23
|
+
it('Preserves the paywall when cleaning an existing fragment in place', function () {
|
|
24
|
+
domUtils.processFragment('<!-- wp:more --><!--members-only--><!-- /wp:more -->', parsed => {
|
|
25
|
+
assert.equal(removeNonKoenigComments(parsed), '<!--members-only-->');
|
|
26
|
+
assert.equal(parsed.html(), '<!--members-only-->');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
22
29
|
it('Returns an empty string when only comments and whitespace remain', function () {
|
|
23
30
|
assert.equal(removeNonKoenigComments('<!-- wp:paragraph -->\n\n<!-- /wp:paragraph -->'), '');
|
|
24
31
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-comments.test.js","sourceRoot":"","sources":["../../src/test/remove-comments.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAC,QAAQ,EAAE,EAAE,EAAC,MAAM,WAAW,CAAC;AACvC,OAAO,EAAC,uBAAuB,EAAC,MAAM,aAAa,CAAC;AAEpD,QAAQ,CAAC,yBAAyB,EAAE;IAChC,EAAE,CAAC,mDAAmD,EAAE;QACpD,MAAM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,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kEAAkE,EAAE;QACnE,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,iDAAiD,CAAC,EAAE,EAAE,CAAC,CAAC;IACjG,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.13.
|
|
3
|
+
"version": "0.13.4",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript": "7.0.2"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@tryghost/mg-utils": "0.12.
|
|
31
|
+
"@tryghost/mg-utils": "0.12.4",
|
|
32
32
|
"escape-string-regexp": "5.0.0",
|
|
33
33
|
"style-to-object": "2.0.2"
|
|
34
34
|
},
|