Skip to content

Anchor generation and headers with HTML #685

Closed
@Haraldson

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 + '>';
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    L2 - annoyingSimilar to L1 - broken but there is a known workaround available for the issueproposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions