markedjs / marked

A markdown parser and compiler. Built for speed.
https://marked.js.org
Other
33.1k stars 3.39k forks source link

Apostrophe character replacement not working in renderer #971

Closed S-W-Williams closed 6 years ago

S-W-Williams commented 6 years ago

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 commented 6 years ago

981

Feder1co5oave commented 6 years ago

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 commented 6 years ago

@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.