Skip to content

Commit

Permalink
rehype-remove-comments: add support for test
Browse files Browse the repository at this point in the history
Closes GH-50.
  • Loading branch information
wooorm committed Sep 17, 2024
1 parent 94e970b commit 72c2051
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/hast-util-from-string/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
24 changes: 20 additions & 4 deletions packages/rehype-remove-comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* {}
Expand All @@ -44,6 +59,7 @@

/**
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').Test} Test
*/

export {default} from './lib/index.js'
16 changes: 13 additions & 3 deletions packages/rehype-remove-comments/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion packages/rehype-remove-comments/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [API](#api)
* [`unified().use(rehypeRemoveComments[, options])`](#unifieduserehyperemovecomments-options)
* [`Options`](#options)
* [`Test`](#test)
* [Example](#example)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
Expand Down Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions packages/rehype-remove-comments/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}])])
)
}
)
})

0 comments on commit 72c2051

Please sign in to comment.