Skip to content

Commit

Permalink
Removing linkTarget option
Browse files Browse the repository at this point in the history
Closes GH-761.
Closes GH-762.

Reviewed-by: Titus Wormer <[email protected]>
Reviewed-by: Christian Murphy <[email protected]>
Reviewed-by: Remco Haszing <[email protected]>
  • Loading branch information
galabra authored Sep 25, 2023
1 parent 0242d11 commit de29396
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 81 deletions.
20 changes: 0 additions & 20 deletions lib/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@
* @param {string?} title
* @returns {string}
*
* @typedef {import('react').HTMLAttributeAnchorTarget} TransformLinkTargetType
*
* @callback TransformLinkTarget
* @param {string} href
* @param {Array<ElementContent>} children
* @param {string?} title
* @returns {TransformLinkTargetType|undefined}
*
* @typedef {keyof JSX.IntrinsicElements} ReactMarkdownNames
*
* To do: is `data-sourcepos` typeable?
Expand Down Expand Up @@ -96,7 +88,6 @@
* @property {boolean} [includeElementIndex=false]
* @property {null|false|TransformLink} [transformLinkUri]
* @property {TransformImage} [transformImageUri]
* @property {TransformLinkTargetType|TransformLinkTarget} [linkTarget]
* @property {Components} [components]
*/

Expand Down Expand Up @@ -223,17 +214,6 @@ function toReact(context, node, index, parent) {

properties.key = index

if (name === 'a' && options.linkTarget) {
properties.target =
typeof options.linkTarget === 'function'
? options.linkTarget(
String(properties.href || ''),
node.children,
typeof properties.title === 'string' ? properties.title : null
)
: options.linkTarget
}

if (name === 'a' && transform) {
properties.href = transform(
String(properties.href || ''),
Expand Down
1 change: 0 additions & 1 deletion lib/react-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ ReactMarkdown.propTypes = {
skipHtml: PropTypes.bool,
includeElementIndex: PropTypes.bool,
transformLinkUri: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
linkTarget: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
transformImageUri: PropTypes.func,
components: PropTypes.object
}
4 changes: 0 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ The default export is `ReactMarkdown`.
extract (unwrap) the children of not allowed elements, by default, when
`strong` is disallowed, it and it’s children are dropped, but with
`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"…`)
* `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 @@ -632,8 +630,6 @@ Optionally, components will also receive:
— see `rawSourcePos` option
* `index` and `siblingCount` (`number`)
— see `includeElementIndex` option
* `target` on `a` (`string`)
— see `linkTarget` option

## Security

Expand Down
56 changes: 0 additions & 56 deletions test/test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,62 +231,6 @@ test('should handle titles of links', () => {
assert.equal(actual, '<p>Empty: <a href="#" title="x"></a></p>')
})

test('should use target attribute for links if specified', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
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>'
)
})

test('should call function to get target attribute for links if specified', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(
<Markdown
children={input}
linkTarget={(uri) => (uri.startsWith('http') ? '_blank' : undefined)}
/>
)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank">a link</a> to Espen.Codes.</p>'
)
})

test('should handle links with custom target transformer', () => {
const input = 'Empty: []()'

const actual = asHtml(
<Markdown
children={input}
linkTarget={(uri, _, title) => {
assert.equal(uri, '', '`uri` should be an empty string')
assert.equal(title, null, '`title` should be null')
return undefined
}}
/>
)

assert.equal(actual, '<p>Empty: <a href=""></a></p>')
})

test('should handle links w/ titles with custom target transformer', () => {
const input = 'Empty: [](a "b")'

const actual = asHtml(
<Markdown
children={input}
linkTarget={(_, _1, title) => {
assert.equal(title, 'b', '`title` should be given')
return undefined
}}
/>
)

assert.equal(actual, '<p>Empty: <a href="a" title="b"></a></p>')
})

test('should support images without alt, url, or title', () => {
const input = '![]()'
const actual = asHtml(<Markdown children={input} transformLinkUri={null} />)
Expand Down

1 comment on commit de29396

@wooorm
Copy link
Member

@wooorm wooorm commented on de29396 Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not spam commits. Follow the contribution guidelines or I will block you.

Please sign in to comment.