React-markdown: How can I render anchors?

Created on 11 Dec 2017  路  3Comments  路  Source: remarkjs/react-markdown

Since internal HTMl is broken: How can I address a specific topic in my document?

This

[Link](#bla)

produces a correct link to a topic

## bla

But the link doesn't work

Most helpful comment

You could define a custom heading renderer.
Something along the lines of this? (psuedo code, untested, and you probably want a proper slug function instead of this one)

function Heading(props) {
  const text = props.children.reduce(reduceTextChildren, []).join(' ')
  const slug = text
    .toLowerCase()
    .replace(/\s+/g, '-')
    .replace(/[^a-z-]/g, '')

  return React.createElement(`h${props.level}`, {id: slug}, props.children)
}

<Markdown source="# Some *heading*" renderers={{heading: Heading}} />

// <h1 id="some-heading">Some <em>heading</em></h1>

All 3 comments

You could define a custom heading renderer.
Something along the lines of this? (psuedo code, untested, and you probably want a proper slug function instead of this one)

function Heading(props) {
  const text = props.children.reduce(reduceTextChildren, []).join(' ')
  const slug = text
    .toLowerCase()
    .replace(/\s+/g, '-')
    .replace(/[^a-z-]/g, '')

  return React.createElement(`h${props.level}`, {id: slug}, props.children)
}

<Markdown source="# Some *heading*" renderers={{heading: Heading}} />

// <h1 id="some-heading">Some <em>heading</em></h1>

Sorry, what is reduceTextChildren?

Disregard, stupid question

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexLerman picture AlexLerman  路  3Comments

guilhermecastro-hotmart picture guilhermecastro-hotmart  路  3Comments

garrilla picture garrilla  路  4Comments

ScarellaDev picture ScarellaDev  路  3Comments

IonicaBizau picture IonicaBizau  路  4Comments