@tryghost/mg-clean-html 0.13.2 → 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.
package/build/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { cleanHTML } from './lib/process.js';
2
- export { cleanHTML };
2
+ import { removeNonKoenigComments } from './lib/remove-comments.js';
3
+ export { cleanHTML, removeNonKoenigComments };
package/build/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import { cleanHTML } from './lib/process.js';
2
- export { cleanHTML };
2
+ import { removeNonKoenigComments } from './lib/remove-comments.js';
3
+ export { cleanHTML, removeNonKoenigComments };
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EAAC,SAAS,EAAC,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,7 @@
1
+ import { domUtils } from '@tryghost/mg-utils';
2
+ /**
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.
5
+ */
6
+ declare const removeNonKoenigComments: (html?: string | domUtils.ParsedFragment | null) => string;
7
+ export { removeNonKoenigComments };
@@ -0,0 +1,38 @@
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
+ const marker = getCommentData(child).trim();
7
+ if (!marker.startsWith('kg-') && marker !== 'members-only') {
8
+ child.remove();
9
+ }
10
+ }
11
+ else {
12
+ removeCommentNodes(child);
13
+ }
14
+ }
15
+ };
16
+ const cleanFragment = (parsed) => {
17
+ removeCommentNodes(parsed.body);
18
+ const cleaned = parsed.html();
19
+ return cleaned.trim() === '' ? '' : cleaned;
20
+ };
21
+ /**
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.
24
+ */
25
+ const removeNonKoenigComments = (html) => {
26
+ if (html && typeof html !== 'string') {
27
+ return cleanFragment(html);
28
+ }
29
+ if (!html || html.trim() === '') {
30
+ return '';
31
+ }
32
+ if (!html.includes('<!--')) {
33
+ return html;
34
+ }
35
+ return domUtils.processFragment(html, cleanFragment);
36
+ };
37
+ export { removeNonKoenigComments };
38
+ //# 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,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,MAAM,KAAK,cAAc,EAAE,CAAC;gBACzD,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,MAAM,aAAa,GAAG,CAAC,MAA+B,EAAU,EAAE;IAC9D,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IAC9B,OAAO,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AAChD,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,uBAAuB,GAAG,CAAC,IAA8C,EAAU,EAAE;IACvF,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QACnC,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,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,OAAO,QAAQ,CAAC,eAAe,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;AACzD,CAAC,CAAC;AAEF,OAAO,EAAC,uBAAuB,EAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,33 @@
1
+ import assert from 'node:assert/strict';
2
+ import { describe, it } from 'node:test';
3
+ import { domUtils } from '@tryghost/mg-utils';
4
+ import { removeNonKoenigComments } from '../index.js';
5
+ describe('removeNonKoenigComments', function () {
6
+ it('Returns an empty string for missing or empty HTML', function () {
7
+ assert.equal(removeNonKoenigComments(), '');
8
+ assert.equal(removeNonKoenigComments(null), '');
9
+ assert.equal(removeNonKoenigComments(''), '');
10
+ assert.equal(removeNonKoenigComments(' \n\t '), '');
11
+ });
12
+ it('Returns HTML without comments unchanged', function () {
13
+ assert.equal(removeNonKoenigComments('<p>Hello</p>'), '<p>Hello</p>');
14
+ });
15
+ it('Removes non-Koenig comments, including nested ones', function () {
16
+ const html = '<!-- wp:paragraph --><p>Hello<!-- inline --></p><!-- /wp:paragraph --><div><!-- nested --><span>World</span></div>';
17
+ assert.equal(removeNonKoenigComments(html), '<p>Hello</p><div><span>World</span></div>');
18
+ });
19
+ it('Keeps Koenig card comments', function () {
20
+ const html = '<!-- wp:html --><!--kg-card-begin: html--><div>Card<!-- inside --></div><!--kg-card-end: html--><!-- /wp:html -->';
21
+ assert.equal(removeNonKoenigComments(html), '<!--kg-card-begin: html--><div>Card</div><!--kg-card-end: html-->');
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
+ });
29
+ it('Returns an empty string when only comments and whitespace remain', function () {
30
+ assert.equal(removeNonKoenigComments('<!-- wp:paragraph -->\n\n<!-- /wp:paragraph -->'), '');
31
+ });
32
+ });
33
+ //# 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,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAC,uBAAuB,EAAC,MAAM,aAAa,CAAC;AAEpD,QAAQ,CAAC,yBAAyB,EAAE;IAChC,EAAE,CAAC,mDAAmD,EAAE;QACpD,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,EAAE,EAAE,CAAC,CAAC;QAC5C,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,IAAI,CAA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@tryghost/mg-clean-html",
3
- "version": "0.13.2",
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.2",
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
  },