Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default value to link's "rel" when link's "target" is "_blank" #761

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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