Skip to content

Anchor generation and headers with HTML #685

Closed

Description

@Haraldson

This may well be an edge case, but I’m making an issue anyway.

In my markdown, I sometimes want to customize the output a tad by adding HTML, such as adding an icon in front of a heading; # <span class="font-icon font-icon-logo"></span>Heading text.

This works just fine, but the anchor generation is too stupid in this case, as it will generate the following output; <h1 id="span-class-font-icon-font-icon-logo-span-heading-text">Heading text</h1>. Hardly what I want and certainly not what I expect.

I made a renderer to get around the problem, but would prefer if Marked was smart enough to handle it all by itself. And without jQuery as a dependency, obviously.

renderer.heading = function(text, level)
{
    var stripped = $('<div />').html(text).text();
    var escaped = stripped.toLowerCase().replace(/[^\w]+/g, '-');
    return '<h' + level + ' id="' + escaped + '">' + text + '</h' + level + '>';
};

Activity

  1. added
    L2 - annoying
    on Dec 23, 2017
  2. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    L2 - annoyingproposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions