Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\HeadingPermalink;
use League\CommonMark\Node\Inline\AbstractInline;
/**
* Represents an anchor link within a heading
*/
final class HeadingPermalink extends AbstractInline
{
/** @psalm-readonly */
private string $slug;
public function __construct(string $slug)
{
parent::__construct();
$this->slug = $slug;
}
public function getSlug(): string
{
return $this->slug;
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\HeadingPermalink;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ConfigurableExtensionInterface;
use League\Config\ConfigurationBuilderInterface;
use Nette\Schema\Expect;
/**
* Extension which automatically anchor links to heading elements
*/
final class HeadingPermalinkExtension implements ConfigurableExtensionInterface
{
public function configureSchema(ConfigurationBuilderInterface $builder): void
{
$builder->addSchema('heading_permalink', Expect::structure([
'min_heading_level' => Expect::int()->min(1)->max(6)->default(1),
'max_heading_level' => Expect::int()->min(1)->max(6)->default(6),
'insert' => Expect::anyOf(HeadingPermalinkProcessor::INSERT_BEFORE, HeadingPermalinkProcessor::INSERT_AFTER)->default(HeadingPermalinkProcessor::INSERT_BEFORE),
'id_prefix' => Expect::string()->default('content'),
'fragment_prefix' => Expect::string()->default('content'),
'html_class' => Expect::string()->default('heading-permalink'),
'title' => Expect::string()->default('Permalink'),
'symbol' => Expect::string()->default(HeadingPermalinkRenderer::DEFAULT_SYMBOL),
'aria_hidden' => Expect::bool()->default(true),
]));
}
public function register(EnvironmentBuilderInterface $environment): void
{
$environment->addEventListener(DocumentParsedEvent::class, new HeadingPermalinkProcessor(), -100);
$environment->addRenderer(HeadingPermalink::class, new HeadingPermalinkRenderer());
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\HeadingPermalink;
use League\CommonMark\Environment\EnvironmentAwareInterface;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Node\NodeIterator;
use League\CommonMark\Node\RawMarkupContainerInterface;
use League\CommonMark\Node\StringContainerHelper;
use League\CommonMark\Normalizer\TextNormalizerInterface;
use League\Config\ConfigurationInterface;
/**
* Searches the Document for Heading elements and adds HeadingPermalinks to each one
*/
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
{
public const INSERT_BEFORE = 'before';
public const INSERT_AFTER = 'after';
/** @psalm-readonly-allow-private-mutation */
private TextNormalizerInterface $slugNormalizer;
/** @psalm-readonly-allow-private-mutation */
private ConfigurationInterface $config;
public function setEnvironment(EnvironmentInterface $environment): void
{
$this->config = $environment->getConfiguration();
$this->slugNormalizer = $environment->getSlugNormalizer();
}
public function __invoke(DocumentParsedEvent $e): void
{
$min = (int) $this->config->get('heading_permalink/min_heading_level');
$max = (int) $this->config->get('heading_permalink/max_heading_level');
$slugLength = (int) $this->config->get('slug_normalizer/max_length');
foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
$this->addHeadingLink($node, $slugLength);
}
}
}
private function addHeadingLink(Heading $heading, int $slugLength): void
{
$text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
$slug = $this->slugNormalizer->normalize($text, [
'node' => $heading,
'length' => $slugLength,
]);
$headingLinkAnchor = new HeadingPermalink($slug);
switch ($this->config->get('heading_permalink/insert')) {
case self::INSERT_BEFORE:
$heading->prependChild($headingLinkAnchor);
return;
case self::INSERT_AFTER:
$heading->appendChild($headingLinkAnchor);
return;
default:
throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
}
}
}

View File

@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\HeadingPermalink;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use League\CommonMark\Xml\XmlNodeRendererInterface;
use League\Config\ConfigurationAwareInterface;
use League\Config\ConfigurationInterface;
/**
* Renders the HeadingPermalink elements
*/
final class HeadingPermalinkRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
{
public const DEFAULT_SYMBOL = '¶';
/** @psalm-readonly-allow-private-mutation */
private ConfigurationInterface $config;
public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}
/**
* @param HeadingPermalink $node
*
* {@inheritDoc}
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
{
HeadingPermalink::assertInstanceOf($node);
$slug = $node->getSlug();
$idPrefix = (string) $this->config->get('heading_permalink/id_prefix');
if ($idPrefix !== '') {
$idPrefix .= '-';
}
$fragmentPrefix = (string) $this->config->get('heading_permalink/fragment_prefix');
if ($fragmentPrefix !== '') {
$fragmentPrefix .= '-';
}
$attrs = $node->data->getData('attributes');
$attrs->set('id', $idPrefix . $slug);
$attrs->set('href', '#' . $fragmentPrefix . $slug);
$attrs->append('class', $this->config->get('heading_permalink/html_class'));
$hidden = $this->config->get('heading_permalink/aria_hidden');
if ($hidden) {
$attrs->set('aria-hidden', 'true');
}
$attrs->set('title', $this->config->get('heading_permalink/title'));
$symbol = $this->config->get('heading_permalink/symbol');
\assert(\is_string($symbol));
return new HtmlElement('a', $attrs->export(), \htmlspecialchars($symbol), false);
}
public function getXmlTagName(Node $node): string
{
return 'heading_permalink';
}
/**
* @param HeadingPermalink $node
*
* @return array<string, scalar>
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function getXmlAttributes(Node $node): array
{
HeadingPermalink::assertInstanceOf($node);
return [
'slug' => $node->getSlug(),
];
}
}