haxeflixel.com icon indicating copy to clipboard operation
haxeflixel.com copied to clipboard

Documentation: Add anchors to <h>-tags

Open Gama11 opened this issue 10 years ago • 5 comments

That way, you can link to an invidual section.

Gama11 avatar Jan 21 '14 13:01 Gama11

Meh. It seems like this might not work with javascript and has to be done at compile time with .eco.

Here's what I came up with:

function generateAnchors()
{
    var docs = document.getElementsByClassName("doc-content")[0];

    addAnchorsToTag(docs, "h1");
    addAnchorsToTag(docs, "h2");
    addAnchorsToTag(docs, "h3");
    addAnchorsToTag(docs, "h4");
}

function addAnchorsToTag(docs, tag)
{
    var tags = docs.getElementsByTagName(tag);

    for (var i = 0; i < tags.length; i++)
    {
        var tag = tags[i];
        var anchorName = tag.innerHTML;
        tag.id = anchorName;
        $(tag).wrap("<a class='inSiteAnchor' href='#" + anchorName + "'></a>");
    }
}

The links and everything are working fine, but when loading a site with an anchor, it does not work, probably because the anchors don't exist right away (they are generated with <body onload="generateAnchors();">.

@impaler Any ideas?

Gama11 avatar Jan 22 '14 12:01 Gama11

Best handled in a docpad plugin that way there is no clientside js.

impaler avatar Jan 22 '14 21:01 impaler

I'm guessing this ticket is closed as well since we've ruled out adding logic to the client-side?

RichardBray avatar Oct 21 '20 10:10 RichardBray

This issue is still open.

Gama11 avatar Oct 21 '20 16:10 Gama11

Ah okay, so someone needs to make a docpad plugin with this functionality. I don't know how easy it is to create a docpad plugin but I feel like it might be easier to create a custom npm package. I might look into this.

RichardBray avatar Oct 22 '20 08:10 RichardBray