Skip to content

Apostrophe character replacement not working in renderer #971

Closed

Description

@shwne1

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?

Activity

  1. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member
  2. Feder1co5oave commented on Dec 27, 2017

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

  3. joshbruce commented on Dec 27, 2017

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions