Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headings are missing anchors / ids #69

Closed
ycjcl868 opened this issue Mar 28, 2017 · 10 comments
Closed

Headings are missing anchors / ids #69

ycjcl868 opened this issue Mar 28, 2017 · 10 comments

Comments

@ycjcl868
Copy link

ycjcl868 commented Mar 28, 2017

# h1

parse =>

<h1>h1</h1>

not

<h1 id="h1">h1</h1>

image

@rexxars
Copy link
Collaborator

rexxars commented Mar 28, 2017

It's hard to know how to address these headers, so I've decided to leave it out of the core.
You can achieve the same functionality by using a Header renderer:

function flatten(text, child) {
  return typeof child === 'string'
    ? text + child
    : React.Children.toArray(child.props.children).reduce(flatten, text)
}

function HeadingRenderer(props) {
  var children = React.Children.toArray(props.children)
  var text = children.reduce(flatten, '')
  var slug = text.toLowerCase().replace(/\W/g, '-')
  return React.createElement('h' + props.level, {id: slug}, props.children)
}

<Markdown
  source="# Some heading!\n\nAnd a paragraph"
  renderers={{Heading: HeadingRenderer}}
/>

JSbin example here

@rexxars rexxars changed the title # h1 don't have the anchor #h1 Headings are missing anchors / ids Mar 28, 2017
@rexxars rexxars closed this as completed Mar 28, 2017
@darraghbuckley
Copy link

In case it's useful for anyone in the future:

renderers={{Heading: HeadingRenderer}} now wants to be renderers={{heading: HeadingRenderer}}

(So lowercase renderer names.)

@Ratstail91
Copy link

This should be a core feature. I shouldn't have to go out of my way to implement something that is a core markdown feature.

@neilyoung
Copy link

neilyoung commented Mar 8, 2020

I had to lower-case heading with react-markdown 4.3.1

EDIT: Ah, was already commented @darraghbuckley, sorry...

@tsaldanha
Copy link

tsaldanha commented Apr 7, 2020

How to get different IDs when you have same titles?

For exemple, I am using it to render markdown as a developers documentation, and one page can have multiple end points that are the same for example:

# route a
## GET - List
## POST - Create

# route b
## GET - List
## POST - Create

The solution proposed will generate same ids for the h2 generated and my Table of Contents will not work.

@liuxinqiong
Copy link

How to get different IDs when you have same titles?

For exemple, I am using it to render markdown as a developers documentation, and one page can have multiple end points that are the same for example:

# route a
## GET - List
## POST - Create

# route b
## GET - List
## POST - Create

The solution proposed will generate same ids for the h2 generated and my Table of Contents will not work.

I think you can simplify it. Just generate a unique id by yourself. Here is my example, hope can help you.

const generateId = (() => {
  let i = 0;
  return (prefix = '') => {
    i += 1;
    return `${prefix}-${i}`;
  };
})();

function HeadingRenderer(props: any) {
  const slug = `h${props.level}-${generateId('titile')}`;
  return createElement(`h${props.level}`, { id: slug }, props.children);
}

@justanotheranonymoususer

What would be the solution for the latest version?

@ChristianMurphy
Copy link
Member

@firmart
Copy link

firmart commented Sep 23, 2021

And for custom id # h1 {#myHeading}: https://github.com/imcuttle/remark-heading-id

@ericjj94
Copy link

ericjj94 commented Oct 6, 2021

renderers is deprecated in version 7.x.x . So components should be used instead.
For example components = {{h2: HeadingRenderer}}

I had spent almost an hour looking into why it is not working with renderers only to realize it is deprecated. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests