@tryghost/mg-clean-html 0.13.2 → 0.13.3
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/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build/lib/remove-comments.d.ts +8 -0
- package/build/lib/remove-comments.js +36 -0
- package/build/lib/remove-comments.js.map +1 -0
- package/build/test/remove-comments.test.d.ts +1 -0
- package/build/test/remove-comments.test.js +26 -0
- package/build/test/remove-comments.test.js.map +1 -0
- package/package.json +2 -2
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAC,uBAAuB,EAAC,MAAM,0BAA0B,CAAC;AAEjE,OAAO,EAAC,SAAS,EAAE,uBAAuB,EAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove every HTML comment except Koenig card markers (comments starting with `kg-`, such as
|
|
3
|
+
* `<!--kg-card-begin: html-->`). Content made of only comments (e.g. WordPress block comments
|
|
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.
|
|
6
|
+
*/
|
|
7
|
+
declare const removeNonKoenigComments: (html?: string | null) => string;
|
|
8
|
+
export { removeNonKoenigComments };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { domUtils } from '@tryghost/mg-utils';
|
|
2
|
+
const { isComment, getCommentData } = domUtils;
|
|
3
|
+
const removeCommentNodes = (node) => {
|
|
4
|
+
for (const child of Array.from(node.childNodes)) {
|
|
5
|
+
if (isComment(child)) {
|
|
6
|
+
if (!getCommentData(child).trim().startsWith('kg-')) {
|
|
7
|
+
child.remove();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
removeCommentNodes(child);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Remove every HTML comment except Koenig card markers (comments starting with `kg-`, such as
|
|
17
|
+
* `<!--kg-card-begin: html-->`). Content made of only comments (e.g. WordPress block comments
|
|
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.
|
|
20
|
+
*/
|
|
21
|
+
const removeNonKoenigComments = (html) => {
|
|
22
|
+
// Whitespace-only content also converts to an empty Lexical root
|
|
23
|
+
if (!html || html.trim() === '') {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
if (!html.includes('<!--')) {
|
|
27
|
+
return html;
|
|
28
|
+
}
|
|
29
|
+
const cleaned = domUtils.processFragment(html, parsed => {
|
|
30
|
+
removeCommentNodes(parsed.body);
|
|
31
|
+
return parsed.html();
|
|
32
|
+
});
|
|
33
|
+
return cleaned.trim() === '' ? '' : cleaned;
|
|
34
|
+
};
|
|
35
|
+
export { removeNonKoenigComments };
|
|
36
|
+
//# sourceMappingURL=remove-comments.js.map
|
|
@@ -0,0 +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,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClD,KAAK,CAAC,MAAM,EAAE,CAAC;YACnB,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;IACL,CAAC;AACL,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,uBAAuB,GAAG,CAAC,IAAoB,EAAU,EAAE;IAC7D,iEAAiE;IACjE,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9B,OAAO,EAAE,CAAC;IACd,CAAC;IAED,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE;QACpD,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAChC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;IACzB,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AAChD,CAAC,CAAC;AAEF,OAAO,EAAC,uBAAuB,EAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { removeNonKoenigComments } from '../index.js';
|
|
4
|
+
describe('removeNonKoenigComments', function () {
|
|
5
|
+
it('Returns an empty string for missing or empty HTML', function () {
|
|
6
|
+
assert.equal(removeNonKoenigComments(), '');
|
|
7
|
+
assert.equal(removeNonKoenigComments(null), '');
|
|
8
|
+
assert.equal(removeNonKoenigComments(''), '');
|
|
9
|
+
assert.equal(removeNonKoenigComments(' \n\t '), '');
|
|
10
|
+
});
|
|
11
|
+
it('Returns HTML without comments unchanged', function () {
|
|
12
|
+
assert.equal(removeNonKoenigComments('<p>Hello</p>'), '<p>Hello</p>');
|
|
13
|
+
});
|
|
14
|
+
it('Removes non-Koenig comments, including nested ones', function () {
|
|
15
|
+
const html = '<!-- wp:paragraph --><p>Hello<!-- inline --></p><!-- /wp:paragraph --><div><!-- nested --><span>World</span></div>';
|
|
16
|
+
assert.equal(removeNonKoenigComments(html), '<p>Hello</p><div><span>World</span></div>');
|
|
17
|
+
});
|
|
18
|
+
it('Keeps Koenig card comments', function () {
|
|
19
|
+
const html = '<!-- wp:html --><!--kg-card-begin: html--><div>Card<!-- inside --></div><!--kg-card-end: html--><!-- /wp:html -->';
|
|
20
|
+
assert.equal(removeNonKoenigComments(html), '<!--kg-card-begin: html--><div>Card</div><!--kg-card-end: html-->');
|
|
21
|
+
});
|
|
22
|
+
it('Returns an empty string when only comments and whitespace remain', function () {
|
|
23
|
+
assert.equal(removeNonKoenigComments('<!-- wp:paragraph -->\n\n<!-- /wp:paragraph -->'), '');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=remove-comments.test.js.map
|
|
@@ -0,0 +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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.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.3",
|
|
32
32
|
"escape-string-regexp": "5.0.0",
|
|
33
33
|
"style-to-object": "2.0.2"
|
|
34
34
|
},
|