caseyamcl / toc

Table of Contents Generator for HTML Markup
MIT License
84 stars 15 forks source link

Is there an option to remove "numbered" prefixes for headings' anchors? #21

Open lazharichir opened 1 year ago

lazharichir commented 1 year ago

Wondering what you would recommend to automatically remove the "1. " or "1) " of the headings from the slugified anchor?

For instance, so that <h2>1. How To Do This</h2> becomes <h2 id="how-to-do-this">1. How To Do This</h2> and not <h2 id="1-how-to-do-this">1. How To Do This</h2>?

caseyamcl commented 1 year ago

Hi @lazharichir,

There is no "batteries included" way, but there is indeed a way to do this if you're willing to do a bit of extra coding.

You can extend the UniqueSlugify class and override the slugify method with your own implementation; something like this:

use TOC\UniqueSlugify;

class MySlugifier extends UniqueSlugify
{
    public function slugify($string, $options = null): string
    {
        // write your own logic to remove the numeric prefix here...

        parent::slugify($string, $options);
    }
}

Then, when instantiating the MarkupFixer class, simply pass your own instance of the Slugifier to the constructor:

$markupFixer  = new TOC\MarkupFixer(null, new MySlugifier());

Hope this helps!