React-markdown: HTML a tag not rendered correcly

Created on 16 Nov 2017  路  5Comments  路  Source: remarkjs/react-markdown

Given this JS

import { href } from "cities"
const source = `Go <a href="${href}">here</a>`

<Markdown source={source}/>

The output is not a link but a span with the links "here" only.

馃悰 typbug

Most helpful comment

@sonaye You can use the renderers option like this:

<Markdown
      source={text}
      renderers={{link : this.markdownLinkRenderer}}
    />

And an example of the markdownLinkRenderer function (I use it to add target="_blank" to non relative links):

markdownLinkRenderer(props) {
    return props.href.startsWith("/") ?
      <a href={props.href}>{props.children}</a> :
      <a href={props.href} target="_blank" rel="nofollow noopener noreferrer">{props.children}</a>;
  }

All 5 comments

You'll either have to use markdown links ([title](href)) or wait until someone makes a better HTML integration.

@rexxars Any way around adding target="_blank"?

@sonaye You can use the renderers option like this:

<Markdown
      source={text}
      renderers={{link : this.markdownLinkRenderer}}
    />

And an example of the markdownLinkRenderer function (I use it to add target="_blank" to non relative links):

markdownLinkRenderer(props) {
    return props.href.startsWith("/") ?
      <a href={props.href}>{props.children}</a> :
      <a href={props.href} target="_blank" rel="nofollow noopener noreferrer">{props.children}</a>;
  }

Appears to work if you take:

const source = `Go <a href="${href}">here</a>`

and make it

const source = `<p>Go <a href="${href}">here</a></p>`

Not sure if this works for your context or not, hope it helps.

This is fixed in the upcoming 4.0, given you use the HTML parser plugin.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IonicaBizau picture IonicaBizau  路  4Comments

ScarellaDev picture ScarellaDev  路  3Comments

amalajeyan picture amalajeyan  路  4Comments

jonjaffe picture jonjaffe  路  3Comments

ghost picture ghost  路  4Comments