Is it possible to render a link with target="_blank" attribute without using a custom renderer?
Not at the moment. Based on the number of requests for this I'm thinking it would probably be wise to just add an option for it, though.
@rexxars I would really appreciate that! Could you provide an example of a custom renderer which is capable of that until now?
<Markdown
source={yourMarkdownSource}
renderers={{link : props => <a href={props.href} target="_blank">{props.children}</a>}}
/>
You could also make the target attribute optional by appending a string to the href, like:
// Try something like https://example.com{_blank}
export default ({ href, children }) => {
if (/{_blank}$/.test(href)) {
return <a
href={href.replace('{_blank}', '')}
target="_blank"
>
{children}
</a>;
}
return <a href={href}>{children}</a>;
};
Marshall Smith (@marshallds) added a linkTarget prop that now lets you do this without a custom renderer. This was released in 3.5.0, see the README for details.
Most helpful comment