dafi / tocmd-generator

Table of Contents Generator for Markdown pages
MIT License
65 stars 11 forks source link

Make generated anchors less brittle so they can be externally linked #1

Open dchambers opened 9 years ago

dchambers commented 9 years ago

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.

dafi commented 9 years ago

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