React-markdown: Feature request: Set default target for links

Created on 22 Feb 2018  路  5Comments  路  Source: remarkjs/react-markdown

Is it possible to render a link with target="_blank" attribute without using a custom renderer?

help wanted 馃檹

Most helpful comment

<Markdown
  source={yourMarkdownSource}
  renderers={{link : props => <a href={props.href} target="_blank">{props.children}</a>}}
/>

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings