From 72c2051a10107d8b75d6ec59dd490119b63ff9ff Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 17 Sep 2024 12:35:40 +0200 Subject: [PATCH] rehype-remove-comments: add support for `test` Closes GH-50. --- packages/hast-util-from-string/lib/index.js | 2 +- packages/rehype-remove-comments/index.js | 24 ++++++++++++++++---- packages/rehype-remove-comments/lib/index.js | 16 ++++++++++--- packages/rehype-remove-comments/readme.md | 18 ++++++++++++++- packages/rehype-remove-comments/test.js | 24 ++++++++++++++++++++ 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/packages/hast-util-from-string/lib/index.js b/packages/hast-util-from-string/lib/index.js index 4cd8ad2..5af00c5 100644 --- a/packages/hast-util-from-string/lib/index.js +++ b/packages/hast-util-from-string/lib/index.js @@ -16,7 +16,7 @@ * Nothing. */ export function fromString(node, value) { - const normalized = value === undefined || value === null ? '' : String(value) + const normalized = value === null || value === undefined ? '' : String(value) if ('children' in node) { node.children = [] diff --git a/packages/rehype-remove-comments/index.js b/packages/rehype-remove-comments/index.js index acab358..6d55c8b 100644 --- a/packages/rehype-remove-comments/index.js +++ b/packages/rehype-remove-comments/index.js @@ -30,11 +30,26 @@ * * Configuration (TypeScript type). * - * ##### Fields + * ###### Fields * - * * `removeConditional` (`boolean`, default: `false`) - * — remove conditional comments (default: `false`); the default is to - * leave them + * * `removeConditional` (`boolean`, default: `false`) + * — remove conditional comments (default: `false`); the default is to + * leave them + * * `test` (`Test`, optional) + * — choose which comments to keep (optional) + * + * ### `Test` + * + * Test a comment (TypeScript type). + * + * ###### Parameters + * + * * `value` (`string`) + * — comment value + * + * ###### Returns + * + * Whether to keep the comment (`boolean`, optional). * * @example * {} @@ -44,6 +59,7 @@ /** * @typedef {import('./lib/index.js').Options} Options + * @typedef {import('./lib/index.js').Test} Test */ export {default} from './lib/index.js' diff --git a/packages/rehype-remove-comments/lib/index.js b/packages/rehype-remove-comments/lib/index.js index 0916799..33bafdc 100644 --- a/packages/rehype-remove-comments/lib/index.js +++ b/packages/rehype-remove-comments/lib/index.js @@ -6,8 +6,17 @@ * @typedef Options * Configuration. * @property {boolean | null | undefined} [removeConditional=false] - * Remove conditional comments (default: `false`); the default is to leave - * them. + * Remove conditional comments (default: `false`); + * the default is to leave them. + * @property {Test | null | undefined} [test] + * Choose which comments to keep. + * + * @callback Test + * Test a comment. + * @param {string} value + * Comment value. + * @returns {boolean | null | undefined} + * Whether to keep the comment. */ import {isConditionalComment} from 'hast-util-is-conditional-comment' @@ -38,7 +47,8 @@ export default function rehypeRemoveComments(options) { if ( typeof index === 'number' && parent && - (settings.removeConditional || !isConditionalComment(node)) + (settings.removeConditional || !isConditionalComment(node)) && + (!settings.test || !settings.test(node.value)) ) { parent.children.splice(index, 1) return index diff --git a/packages/rehype-remove-comments/readme.md b/packages/rehype-remove-comments/readme.md index 5bf5e04..00e6b03 100644 --- a/packages/rehype-remove-comments/readme.md +++ b/packages/rehype-remove-comments/readme.md @@ -21,6 +21,7 @@ * [API](#api) * [`unified().use(rehypeRemoveComments[, options])`](#unifieduserehyperemovecomments-options) * [`Options`](#options) + * [`Test`](#test) * [Example](#example) * [Syntax](#syntax) * [Syntax tree](#syntax-tree) @@ -125,11 +126,26 @@ Transform ([`Transformer`](https://github.com/unifiedjs/unified#transformer)). Configuration (TypeScript type). -##### Fields +###### Fields * `removeConditional` (`boolean`, default: `false`) — remove conditional comments (default: `false`); the default is to leave them +* `test` (`Test`, optional) + — choose which comments to keep (optional) + +### `Test` + +Test a comment (TypeScript type). + +###### Parameters + +* `value` (`string`) + — comment value + +###### Returns + +Whether to keep the comment (`boolean`, optional). ## Example diff --git a/packages/rehype-remove-comments/test.js b/packages/rehype-remove-comments/test.js index 08f363a..535f07a 100644 --- a/packages/rehype-remove-comments/test.js +++ b/packages/rehype-remove-comments/test.js @@ -49,4 +49,28 @@ test('rehype-remove-comments', async function (t) { ) } ) + + await t.test( + 'should remove conditional comments w/ `removeConditional`', + async function () { + assert.deepEqual( + rehype() + .use(min, { + test(value) { + return /b/.test(value) + } + }) + .runSync( + h(undefined, [ + h('div', [ + {type: 'comment', value: 'a'}, + {type: 'comment', value: 'b'}, + {type: 'comment', value: 'c'} + ]) + ]) + ), + h(undefined, [h('div', [{type: 'comment', value: 'b'}])]) + ) + } + ) })