bueltge/marksimple

View on GitHub
src/Rule/Header.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Bueltge\Marksimple\Rule;

class Header extends AbstractRegexRule
{
    /**
     * Get the regex rule to identify the fromFile for the callback.
     * Header h1 - h6.
     *
     * @return string
     */
    public function rule(): string
    {
        return '#^(\#{1,6})\s*([^\#]+?)\s*\#*$#m';
    }

    /**
     * {@inheritdoc}
     */
    public function render(array $content): string
    {
        $headingLevel = \strlen($content[1]);
        $header = trim($content[2]);
        // Build anker without space, numbers.
        $anker = preg_replace('#[^a-z?!]#', '', strtolower($header));

        return sprintf('<h%d id="%s">%s</h%d>', $headingLevel, $anker, $header, $headingLevel);
    }
}