-
-
Notifications
You must be signed in to change notification settings - Fork 873
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
Comments
It's hard to know how to address these headers, so I've decided to leave it out of the core. 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}}
/> |
# h1
don't have the anchor #h1
In case it's useful for anyone in the future:
(So lowercase renderer names.) |
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. |
I had to lower-case EDIT: Ah, was already commented @darraghbuckley, sorry... |
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:
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);
} |
What would be the solution for the latest version? |
And for custom id |
renderers is deprecated in version 7.x.x . So I had spent almost an hour looking into why it is not working with renderers only to realize it is deprecated. :-) |
# h1
parse =>
not
The text was updated successfully, but these errors were encountered: