Skip to content

Commit

Permalink
Remove boolean support from options.target
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed Jul 10, 2022
1 parent 0c87b75 commit aae9e39
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
11 changes: 5 additions & 6 deletions index.js
Expand Up @@ -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>|string|false} Rel
* @typedef {Array<string>} Protocols
* @typedef {ElementChild|Array<ElementChild>} Content
Expand Down Expand Up @@ -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.
Expand All @@ -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']

Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Expand Up @@ -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)')

Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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'
Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions test.js
Expand Up @@ -84,11 +84,11 @@ test('rehypeExternalLinks', async (t) => {
String(
await rehype()
.use({settings: {fragment: true}})
.use(rehypeExternalLinks, {target: false})
.use(rehypeExternalLinks)
.process('<a href="http://example.com">http</a>')
),
'<a href="http://example.com" rel="nofollow">http</a>',
'should not add a `[target]` w/ `target: false`'
'should not add a `[target]` by default'
)

t.equal(
Expand All @@ -110,29 +110,29 @@ test('rehypeExternalLinks', async (t) => {
.process('<a href="http://example.com">http</a>')
),
'<a href="http://example.com" target="_parent">http</a>',
'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('<a href="http://example.com">http</a>')
),
'<a href="http://example.com" rel="nofollow">http</a>',
'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('<a href="http://example.com">http</a>')
),
'<a href="http://example.com" rel="nofollow">http</a>',
'should not add a `[rel]` w/ `rel` set to an array'
'should add a `[rel]` w/ `rel` set to an array'
)

t.equal(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aae9e39

Please sign in to comment.