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

Apostrophe character replacement not working in renderer #971

Closed
S-W-Williams opened this issue Dec 11, 2017 · 3 comments
Closed

Apostrophe character replacement not working in renderer #971

S-W-Williams opened this issue Dec 11, 2017 · 3 comments

Comments

@S-W-Williams
Copy link

S-W-Williams commented Dec 11, 2017

I'm currently trying to make the parsed heading ids match GitHub's markdown anchor conversion rules.

My regular expressions work when tested, but the moment I include it in the render apostrophes stop working.

By apostrophe I mean unicode character 39, not 8217.

var renderer = new marked.Renderer();

renderer.heading = function (heading, level) {
    return '<h' + level + ' id=' 
    + heading.toLowerCase().replace(/(\s|,)+/g, '-').replace(/("|'|`|’)/g, "").replace(/\(|\)/g, "") + '>' 
    + heading + '</h' + level + '>';
},

For example the string "Dijkstra's Shortest Path Algorithm" becomes "dijkstra's-shortest-path-algorithm" in the parsed html, but running

"Dijkstra's Shortest Path Algorithm".toLowerCase().replace(/(\s|,)+/g, '-').replace(/("|'|`|’)/g, "").replace(/\(|\)/g, "") 

properly returns "dijkstras-shortest-path-algorithm".

Am I doing something wrong here? Is this intended or a bug?

@joshbruce
Copy link
Member

#981

@Feder1co5oave
Copy link
Contributor

You must generate ids from the third argument passed to the heading generator, which contains the textual (unescaped) content of the heading, like this:

renderer.heading = function(heading, level, raw) {
  return '<h' + level + ' id=' 
  + raw.toLowerCase().replace(/(\s|,)+/g, '-').replace(/("|'|`|’)/g, "").replace(/\(|\)/g, "") + '>'
  + heading + '</h' + level + '>';
};
marked("# Dijkstra's Shortest Path Algorithm", {renderer: renderer})

This prints

<h1 id=dijkstras-shortest-path-algorithm>Dijkstra&#39;s Shortest Path Algorithm</h1>

@joshbruce we can close this

@joshbruce
Copy link
Member

@Feder1co5oave: You're giving folks a lot of great information. Starting to wonder if updating docs would be of value to consumers of Marked. Closing.

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

No branches or pull requests

3 participants