-
-
Notifications
You must be signed in to change notification settings - Fork 904
Option for Setting target="_blank" on Links #12
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
A bit unsure. It's easy enough to add, but I don't know if the API makes sense. I think there are two options:
I'm leaning towards the first, as that will allow all sorts of flexibility to be added without bloating the renderer with options. |
I agree. The first option you listed sounds the best way to go and would open up the API to be much more flexible. Do you have any inkling as to when such a feature could be ready? |
I don't think it should be too much work, so it's more a question of when I'll have the time to prioritize it. I'm hoping it'll be this week, at least. |
That would be perfect. I really like this package as I don't have to use the Keep up the good work on this package 👍 |
Wouldn't the most optimal case be that any valid attribute would be valid and applied? When rendering the container, you could just spread the props with Then you could just do |
I've implemented custom renderers in <ReactMarkdown
source="Click [here](https://espen.codes/) to visit external site"
renderers={{Link: props => <a href={props.href} target="_blank">{props.children</a>}}
/> |
Version 4 is now out, and the above example should work for this use case. |
I wonder how to do a paragraph like this? I end up This seems to occur when using the Update - can be fixed by overriding the softBreak renderer with |
In case what I found helps others: I needed something similar within an Electron app and went with this solution before I found this issue:
...though this makes all links on the page get opened by the user's default browser, not just links rendered by |
shouldn't it be
|
Dont forget to add rel="noopener noreferrer" |
I'm currently using it like so:
maybe this should be built in? so we could do something like ...
|
Whats the reason you have What else can it be? |
Never mind. Just realised that there are a plethora of protocols. That said, I would not recommend blindly constructing a link. A safer option would be: const renderLink = (props: Object) => {
if (!props.href.startsWith('http')) {
return props.children;
}
return <a href={props.href} rel='nofollow noreferrer noopener' target='_blank'>{props.children}</a>;
}; |
@gajus |
Sorry, this should have been
And yes, that would just render plain text link. |
@gajus |
oh, I see. Forgot about that possibility. |
having a i'd prefer the json solution. |
For anyone wondering, in version 4.0.6 the <ReactMarkdown
source={source}
renderers={{link: props => <a href={props.href} target="_blank">{props.children}</a>}}
/> |
This has now been changed, |
Here is how you can add
|
This method is not a good one because the anchor link will also open in a new tab. <ReactMarkdown
components={{
a: ({ node, children, ...props }) => {
if (props.href?.includes('http')) {
props.target = '_blank'
props.rel = 'noopener noreferrer'
}
return <a {...props}>{children}</a>
},
}}
>
{`[External Link](https://example.com)\n\n[Internal Link](#anchor)`}
</ReactMarkdown> Rendered as: <p>
<a href="https://example.com" target="_blank" rel="noopener noreferrer">External Link</a>
</p>
<p>
<a href="#anchor">Internal Link</a>
</p> |
Appreciate all the code snippets everyone's shared above. I wanted to share an improved version that I'm now using, that are more robust than checking whether the URL contains If you want to check whether the link points to the same domain, regardless of whether it's an absolute URL or not: <ReactMarkdown
components={{
a({ node, children, ...props }) {
let url = new URL(props.href ?? "", location.href);
if (url.origin !== location.origin) {
props.target = "_blank";
props.rel = "noopener noreferrer";
}
return <a {...props}>{children}</a>;
},
}}
>
{`[External Link](https://example.com)\n\n[Internal Link](#anchor)`}
</ReactMarkdown> If you just want to check whether the link is an absolute URL: <ReactMarkdown
components={{
a({ node, children, ...props }) {
try {
new URL(props.href ?? "");
// If we don't get an error, then it's an absolute URL.
props.target = "_blank";
props.rel = "noopener noreferrer";
} catch (e) {}
return <a {...props}>{children}</a>;
},
}}
>
{`[External Link](https://example.com)\n\n[Internal Link](#anchor)`}
</ReactMarkdown> |
Is it feasible to allow this as an option for this package?
Something like the following:
The text was updated successfully, but these errors were encountered: