craftcms / anchors

Add anchor links to headings in your Craft CMS website content.
MIT License
48 stars 7 forks source link

Reverse the order of h tag content #12

Closed LouisCuvelier closed 3 years ago

LouisCuvelier commented 4 years ago

To apply correctly a float: left;, the link should be at the first position in the h tag. The link is on the first position just everywhere (Github, React Doc, Craft CMS Doc)

So, in src/Parser.php, the change would be :

public function parseHtml($html, $tags = 'h1,h2,h3', string $language = null): string
    {
        if (is_string($tags)) {
            $tags = StringHelper::split($tags);
        }

        return preg_replace_callback('/<('.implode('|', $tags).')([^>]*)>(.+?)<\/\1>/', function(array $match) use ($language) {
            $anchorName = $this->generateAnchorName($match[3], $language);
            $heading = strip_tags(str_replace(['&nbsp;', ' '], ' ', $match[3]));

            return '<a'.($this->anchorClass ? ' class="'.$this->anchorClass.'"' : '').' id="'.$anchorName.'"></a>'.
                '<'.$match[1].$match[2].'>'.
// I inverted this two lines
                ' <a'.($this->anchorLinkClass ? ' class="'.$this->anchorLinkClass.'"' : '').' href="#'.$anchorName.'" title="'.Craft::t('anchors', $this->anchorLinkTitleText, ['heading' => $heading]).'">'.$this->anchorLinkText.'</a>'.
               $match[3].
                '</'.$match[1].'>';
        }, $html);
    }

Can you make the change please ? 🙂

brandonkelly commented 3 years ago

Just released Anchors 2.3.0 with a new anchorLinkPosition setting, which determines where the anchor link should be added.

To get the link to be added before the heading text, create a config/anchors.php file with this in it:

<?php

return [
    'anchorLinkPosition' => craft\anchors\Settings::POS_BEFORE,
];