tocmd-generator icon indicating copy to clipboard operation
tocmd-generator copied to clipboard

Make generated anchors less brittle so they can be externally linked

Open dchambers opened this issue 10 years ago • 1 comments

If an anchor isn't explicitly supplied then one is generated, but based on the headings' current position within the document, rather than the name of the heading. I've worked around this issue locally by replacing:

anchorId = config.anchorPrefix + tocLevel + '-' + tocSection;

with:

anchorId = encodeURIComponent(config.anchorPrefix + el.text().toLowerCase().replace(/ /g, '-'));

but obviously any proper solution would need to handle the case where the same heading name had been used multiple times (e.g. for a sub-heading), in which case the positional information could be included again to disambiguate.

dchambers avatar Nov 26 '14 11:11 dchambers

Your idea is very interesting, maybe the plugin can be modified to create the anchor calling an user defined callback receiving as arguments level, section and element so callback can decide what to do, a simply call to encodeURIComponent or any other custom behavior

dafi avatar Nov 27 '14 14:11 dafi