Skip to content

Commit

Permalink
Added a default value of link's rel, when the link's target it "_blank"
Browse files Browse the repository at this point in the history
  • Loading branch information
galabra committed Aug 24, 2023
1 parent 0242d11 commit 9c48565
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
* @property {null|false|TransformLink} [transformLinkUri]
* @property {TransformImage} [transformImageUri]
* @property {TransformLinkTargetType|TransformLinkTarget} [linkTarget]
* @property {string} [linkRel]
* @property {Components} [components]
*/

Expand Down Expand Up @@ -232,6 +233,10 @@ function toReact(context, node, index, parent) {
typeof properties.title === 'string' ? properties.title : null
)
: options.linkTarget

properties.rel =
options.linkRel ||
(properties.target === '_blank' ? 'noopener noreferrer' : undefined)
}

if (name === 'a' && transform) {
Expand Down
1 change: 1 addition & 0 deletions lib/react-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ ReactMarkdown.propTypes = {
includeElementIndex: PropTypes.bool,
transformLinkUri: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
linkTarget: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
linkRel: PropTypes.string,
transformImageUri: PropTypes.func,
components: PropTypes.object
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ The default export is `ReactMarkdown`.
`unwrapDisallowed` the element itself is replaced by its children
* `linkTarget` (`string` or `(href, children, title) => string`, optional)\
target to use on links (such as `_blank` for `<a target="_blank"…`)
* `linkRel` (`string`, optional)\
rel to use on links (will be set to `noopener noreferrer` by default,
if `linkTarget` is set to `_blank`)
* `transformLinkUri` (`(href, children, title) => string`, default:
[`uriTransformer`][uri-transformer], optional)\
change URLs on links, pass `null` to allow all URLs, see [security][]
Expand Down Expand Up @@ -634,6 +637,8 @@ Optionally, components will also receive:
— see `includeElementIndex` option
* `target` on `a` (`string`)
— see `linkTarget` option
* `rel` on `a` (`string`)
— see `linkRel` option

## Security

Expand Down
24 changes: 22 additions & 2 deletions test/test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,27 @@ test('should use target attribute for links if specified', () => {
const actual = asHtml(<Markdown children={input} linkTarget="_blank" />)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank">a link</a> to Espen.Codes.</p>'
'<p>This is <a href="https://espen.codes/" target="_blank" rel="noopener noreferrer">a link</a> to Espen.Codes.</p>'
)
})

test('should use custom default rel attribute for links if specified, when target is _blank', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(
<Markdown children={input} linkTarget="_blank" linkRel="foo" />
)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank" rel="foo">a link</a> to Espen.Codes.</p>'
)
})

test('should not use default rel attribute for links if specified, when target is not _blank', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(<Markdown children={input} linkTarget="_top" />)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_top">a link</a> to Espen.Codes.</p>'
)
})

Expand All @@ -250,7 +270,7 @@ test('should call function to get target attribute for links if specified', () =
)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank">a link</a> to Espen.Codes.</p>'
'<p>This is <a href="https://espen.codes/" target="_blank" rel="noopener noreferrer">a link</a> to Espen.Codes.</p>'
)
})

Expand Down

0 comments on commit 9c48565

Please sign in to comment.