|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Actions; |
| 6 | + |
| 7 | +use App\Support\GitHubActionsOutput; |
| 8 | +use DOMDocument; |
| 9 | +use League\CommonMark\Environment\Environment; |
| 10 | +use League\CommonMark\Event\DocumentParsedEvent; |
| 11 | +use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; |
| 12 | +use League\CommonMark\Extension\CommonMark\Node\Block\Heading; |
| 13 | +use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension; |
| 14 | +use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkProcessor; |
| 15 | +use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkRenderer; |
| 16 | +use League\CommonMark\Node\Block\Document; |
| 17 | +use League\CommonMark\Renderer\HtmlRenderer; |
| 18 | + |
| 19 | +class ExtractPermalinkFragmentFromHeading |
| 20 | +{ |
| 21 | + private GitHubActionsOutput $gitHubActionsOutput; |
| 22 | + |
| 23 | + public function __construct(GitHubActionsOutput $gitHubActionsOutput) |
| 24 | + { |
| 25 | + $this->gitHubActionsOutput = $gitHubActionsOutput; |
| 26 | + } |
| 27 | + |
| 28 | + public function execute(Heading $releaseHeading): string |
| 29 | + { |
| 30 | + $releaseHeading = clone $releaseHeading; |
| 31 | + |
| 32 | + $renderedHtml = $this->attachPermalinkAndRenderAsHtml($releaseHeading); |
| 33 | + $linkFragment = $this->extractLinkFragmentFromRenderedHtml($renderedHtml); |
| 34 | + |
| 35 | + return tap($linkFragment, function (string $linkFragment) { |
| 36 | + $this->gitHubActionsOutput->add('RELEASE_URL_FRAGMENT', $linkFragment); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + protected function attachPermalinkAndRenderAsHtml(Heading $releaseHeading): string |
| 41 | + { |
| 42 | + $environment = $this->prepareCommonmarkEnvironment(); |
| 43 | + |
| 44 | + $document = $this->attachPermalinkToHeading($releaseHeading, $environment); |
| 45 | + |
| 46 | + $renderer = new HtmlRenderer($environment); |
| 47 | + |
| 48 | + return $renderer->renderDocument($document)->getContent(); |
| 49 | + } |
| 50 | + |
| 51 | + protected function prepareCommonmarkEnvironment(): Environment |
| 52 | + { |
| 53 | + $config = [ |
| 54 | + 'heading_permalink' => [ |
| 55 | + 'html_class' => '', |
| 56 | + 'id_prefix' => '', |
| 57 | + 'fragment_prefix' => '', |
| 58 | + 'insert' => 'before', |
| 59 | + 'min_heading_level' => 1, |
| 60 | + 'max_heading_level' => 6, |
| 61 | + 'title' => '', |
| 62 | + 'symbol' => HeadingPermalinkRenderer::DEFAULT_SYMBOL, |
| 63 | + 'aria_hidden' => false, |
| 64 | + ], |
| 65 | + ]; |
| 66 | + |
| 67 | + $environment = new Environment($config); |
| 68 | + $environment->addExtension(new CommonMarkCoreExtension()); |
| 69 | + $environment->addExtension(new HeadingPermalinkExtension()); |
| 70 | + |
| 71 | + return $environment; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Attach Heading Permalink to given ReleaseHeading using |
| 76 | + * the Commonmark Heading Permalink Extension. |
| 77 | + * @param Heading $releaseHeading |
| 78 | + * @param Environment $environment |
| 79 | + * @return Document |
| 80 | + */ |
| 81 | + protected function attachPermalinkToHeading(Heading $releaseHeading, Environment $environment): Document |
| 82 | + { |
| 83 | + $document = new Document(); |
| 84 | + $document->appendChild($releaseHeading); |
| 85 | + |
| 86 | + $documentParsedEvent = new DocumentParsedEvent($document); |
| 87 | + |
| 88 | + $processor = (new HeadingPermalinkProcessor()); |
| 89 | + $processor->setEnvironment($environment); |
| 90 | + $processor->__invoke($documentParsedEvent); |
| 91 | + |
| 92 | + return $document; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Parse the rendered HTML as a DOM Document and extract the |
| 97 | + * href attribute from the generated a-tag. |
| 98 | + * @param string $html |
| 99 | + * @return string|null |
| 100 | + */ |
| 101 | + protected function extractLinkFragmentFromRenderedHtml(string $html): ?string |
| 102 | + { |
| 103 | + $domDocument = new DOMDocument(); |
| 104 | + $domDocument->loadHTML($html); |
| 105 | + |
| 106 | + return $domDocument |
| 107 | + ->getElementsByTagName('a') |
| 108 | + ->item(0) |
| 109 | + ->attributes |
| 110 | + ->getNamedItem('href') |
| 111 | + ->value; |
| 112 | + } |
| 113 | +} |
0 commit comments