toc icon indicating copy to clipboard operation
toc copied to clipboard

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

Open lazharichir opened this issue 2 years ago • 1 comments

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>?

lazharichir avatar Mar 09 '23 16:03 lazharichir

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!

caseyamcl avatar Mar 11 '23 16:03 caseyamcl