From aae9e3903b3b88d264ed5e760360b5d04631fe91 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 10 Jul 2022 13:00:40 +0200 Subject: [PATCH] Remove `boolean` support from `options.target` Now that there is no default anymore, a boolean of `true` no longer makes sense, and `false` is equivalent to `null` or `undefined`. Use `null` or `undefined` instead. --- index.js | 11 +++++------ readme.md | 12 ++++++------ test.js | 16 ++++++++-------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 6a8528a..9638bec 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ * * @typedef {Element['children'][number]} ElementChild * - * @typedef {'_self'|'_blank'|'_parent'|'_top'|false} Target + * @typedef {'_self'|'_blank'|'_parent'|'_top'} Target * @typedef {Array|string|false} Rel * @typedef {Array} Protocols * @typedef {ElementChild|Array} Content @@ -33,10 +33,10 @@ * * @typedef Options * Configuration. - * @property {Target|TargetCallback} [target='_blank'] + * @property {Target|TargetCallback} [target] * How to display referenced documents (`string?`: `_self`, `_blank`, * `_parent`, or `_top`, default: `_blank`). - * Pass `false` to not set `target`s on links. + * The default (nothing) is to not set `target`s on links. * @property {Rel|RelCallback} [rel=['nofollow', 'noopener', 'noreferrer']] * Link types to hint about the referenced documents. * Pass `false` to not set `rel`s on links. @@ -60,7 +60,6 @@ import {parse} from 'space-separated-tokens' import absolute from 'is-absolute-url' import extend from 'extend' -const defaultTarget = false const defaultRel = ['nofollow'] const defaultProtocols = ['http', 'https'] @@ -109,8 +108,8 @@ export default function rehypeExternalLinks(options = {}) { callIfNeeded(options.contentProperties, node) || {} if (absolute(url) && protocols.includes(protocol)) { - if (target !== false) { - node.properties.target = target || defaultTarget + if (target) { + node.properties.target = target } if (rel !== false) { diff --git a/readme.md b/readme.md index fdb068c..aeef634 100644 --- a/readme.md +++ b/readme.md @@ -86,7 +86,7 @@ import rehypeStringify from 'rehype-stringify' const file = await unified() .use(remarkParse) .use(remarkRehype) - .use(rehypeExternalLinks, {target: false, rel: ['nofollow']}) + .use(rehypeExternalLinks, {rel: ['nofollow']}) .use(rehypeStringify) .process('[rehype](https://github.com/rehypejs/rehype)') @@ -115,12 +115,12 @@ Configuration (optional). ###### `options.target` How to open external documents (`string?`: `_self`, `_blank`, `_parent`, -or `_top`, default: `false`). +or `_top`, default: `undefined`). Can also be a function called with the current element to get `target` dynamically. -Pass `false` to not set `target`s on links. +The default (nothing) is to not set `target`s on links. -> 👉 **Note**: [you should likely pass `false`][css-tricks]. +> 👉 **Note**: [you should likely not configure this][css-tricks]. ###### `options.rel` @@ -176,7 +176,7 @@ Taking the above `example.js` and applying the following diff: const file = await unified() .use(remarkParse) .use(remarkRehype) -- .use(rehypeExternalLinks, {target: false, rel: ['nofollow']}) +- .use(rehypeExternalLinks, {rel: ['nofollow']}) + .use(rehypeExternalLinks, { + target(element) { + return element.properties && element.properties.id === '5' @@ -189,7 +189,7 @@ Taking the above `example.js` and applying the following diff: .process('[rehype](https://github.com/rehypejs/rehype)') ``` -Changes to only apply `target="_blank"` on the element with an `id="5"`. +Changes to apply `target="_blank"` on the element with an `id="5"`. ## Types diff --git a/test.js b/test.js index c7472b2..9c12110 100644 --- a/test.js +++ b/test.js @@ -84,11 +84,11 @@ test('rehypeExternalLinks', async (t) => { String( await rehype() .use({settings: {fragment: true}}) - .use(rehypeExternalLinks, {target: false}) + .use(rehypeExternalLinks) .process('http') ), 'http', - 'should not add a `[target]` w/ `target: false`' + 'should not add a `[target]` by default' ) t.equal( @@ -110,29 +110,29 @@ test('rehypeExternalLinks', async (t) => { .process('http') ), 'http', - 'should not add a `[target]` w/ `target` set to a known target' + 'should add a `[target]` w/ `target` set to a known target' ) t.equal( String( await rehype() .use({settings: {fragment: true}}) - .use(rehypeExternalLinks, {target: false, rel: 'nofollow'}) + .use(rehypeExternalLinks, {rel: 'nofollow'}) .process('http') ), 'http', - 'should not add a `[rel]` w/ `rel` set to a string' + 'should add a `[rel]` w/ `rel` set to a string' ) t.equal( String( await rehype() .use({settings: {fragment: true}}) - .use(rehypeExternalLinks, {target: false, rel: ['nofollow']}) + .use(rehypeExternalLinks, {rel: ['nofollow']}) .process('http') ), 'http', - 'should not add a `[rel]` w/ `rel` set to an array' + 'should add a `[rel]` w/ `rel` set to an array' ) t.equal( @@ -256,7 +256,7 @@ test('rehypeExternalLinks', async (t) => { return true }) - return noImage ? '_blank' : false + return noImage ? '_blank' : undefined }, rel(node) { // True, If node doesn't contains an image