Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 991
Zeile 20... Zeile 20...
20
use League\CommonMark\Node\NodeIterator;
20
use League\CommonMark\Node\NodeIterator;
21
use League\CommonMark\Node\RawMarkupContainerInterface;
21
use League\CommonMark\Node\RawMarkupContainerInterface;
22
use League\CommonMark\Node\StringContainerHelper;
22
use League\CommonMark\Node\StringContainerHelper;
23
use League\CommonMark\Normalizer\TextNormalizerInterface;
23
use League\CommonMark\Normalizer\TextNormalizerInterface;
24
use League\Config\ConfigurationInterface;
24
use League\Config\ConfigurationInterface;
-
 
25
use League\Config\Exception\InvalidConfigurationException;
Zeile 25... Zeile 26...
25
 
26
 
26
/**
27
/**
27
 * Searches the Document for Heading elements and adds HeadingPermalinks to each one
28
 * Searches the Document for Heading elements and adds HeadingPermalinks to each one
28
 */
29
 */
29
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
30
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
30
{
31
{
31
    public const INSERT_BEFORE = 'before';
32
    public const INSERT_BEFORE = 'before';
-
 
33
    public const INSERT_AFTER  = 'after';
Zeile 32... Zeile 34...
32
    public const INSERT_AFTER  = 'after';
34
    public const INSERT_NONE   = 'none';
33
 
35
 
Zeile 34... Zeile 36...
34
    /** @psalm-readonly-allow-private-mutation */
36
    /** @psalm-readonly-allow-private-mutation */
Zeile 43... Zeile 45...
43
        $this->slugNormalizer = $environment->getSlugNormalizer();
45
        $this->slugNormalizer = $environment->getSlugNormalizer();
44
    }
46
    }
Zeile 45... Zeile 47...
45
 
47
 
46
    public function __invoke(DocumentParsedEvent $e): void
48
    public function __invoke(DocumentParsedEvent $e): void
47
    {
49
    {
48
        $min = (int) $this->config->get('heading_permalink/min_heading_level');
50
        $min            = (int) $this->config->get('heading_permalink/min_heading_level');
-
 
51
        $max            = (int) $this->config->get('heading_permalink/max_heading_level');
-
 
52
        $applyToHeading = (bool) $this->config->get('heading_permalink/apply_id_to_heading');
-
 
53
        $idPrefix       = (string) $this->config->get('heading_permalink/id_prefix');
-
 
54
        $slugLength     = (int) $this->config->get('slug_normalizer/max_length');
Zeile -... Zeile 55...
-
 
55
        $headingClass   = (string) $this->config->get('heading_permalink/heading_class');
49
        $max = (int) $this->config->get('heading_permalink/max_heading_level');
56
 
-
 
57
        if ($idPrefix !== '') {
Zeile 50... Zeile 58...
50
 
58
            $idPrefix .= '-';
51
        $slugLength = (int) $this->config->get('slug_normalizer/max_length');
59
        }
52
 
60
 
53
        foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
61
        foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
54
            if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
62
            if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
55
                $this->addHeadingLink($node, $slugLength);
63
                $this->addHeadingLink($node, $slugLength, $idPrefix, $applyToHeading, $headingClass);
Zeile 56... Zeile 64...
56
            }
64
            }
57
        }
65
        }
58
    }
66
    }
59
 
67
 
60
    private function addHeadingLink(Heading $heading, int $slugLength): void
68
    private function addHeadingLink(Heading $heading, int $slugLength, string $idPrefix, bool $applyToHeading, string $headingClass): void
61
    {
69
    {
62
        $text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
70
        $text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
Zeile -... Zeile 71...
-
 
71
        $slug = $this->slugNormalizer->normalize($text, [
-
 
72
            'node' => $heading,
-
 
73
            'length' => $slugLength,
-
 
74
        ]);
-
 
75
 
-
 
76
        if ($applyToHeading) {
-
 
77
            $heading->data->set('attributes/id', $idPrefix . $slug);
-
 
78
        }
63
        $slug = $this->slugNormalizer->normalize($text, [
79
 
Zeile 64... Zeile 80...
64
            'node' => $heading,
80
        if ($headingClass !== '') {
65
            'length' => $slugLength,
81
            $heading->data->append('attributes/class', $headingClass);
66
        ]);
82
        }
Zeile 74... Zeile 90...
74
                return;
90
                return;
75
            case self::INSERT_AFTER:
91
            case self::INSERT_AFTER:
76
                $heading->appendChild($headingLinkAnchor);
92
                $heading->appendChild($headingLinkAnchor);
Zeile 77... Zeile 93...
77
 
93
 
-
 
94
                return;
-
 
95
            case self::INSERT_NONE:
78
                return;
96
                return;
79
            default:
97
            default:
80
                throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
98
                throw new InvalidConfigurationException("Invalid configuration value for heading_permalink/insert; expected 'before', 'after', or 'none'");
81
        }
99
        }
82
    }
100
    }