-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the missing anchors on the website homepage + cleanup (#141)
- Loading branch information
Showing
5 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig\Extension; | ||
|
||
use App\Helper\StringHelper; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
use function Symfony\Component\String\u; | ||
|
||
final class MarkdownExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private readonly StringHelper $stringHelper, | ||
) { | ||
} | ||
|
||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('add_headers_anchors', $this->addHeadersAnchors(...)), | ||
]; | ||
} | ||
|
||
/** | ||
* Add the missing anchors on demo website homepage displaying the Githb README. | ||
* | ||
* @see https://microsymfony.ovh | ||
* @see https://github.com/strangebuzz/MicroSymfony/blob/main/README.md?plain=1 | ||
*/ | ||
public function addHeadersAnchors(string $html): string | ||
{ | ||
$dom = new \DOMDocument(); | ||
$dom->loadHTML(mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, ~0], 'UTF-8'), \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD); | ||
|
||
/** @var \DOMNodeList<\DOMNode> $tags */ | ||
$tags = (new \DOMXPath($dom))->query('//h1 | //h2 | //h3 | //h4 | //h5 | //h6'); | ||
// Allow to have the same "buggy" anchors as GitHub | ||
foreach ($tags as $headerTag) { | ||
$slug = $this->stringHelper->slugify($headerTag->textContent); | ||
/** @var \DOMElement $headerTag */ | ||
$headerTag->setAttribute('id', $slug.(u($slug)->length() !== u($headerTag->textContent)->length() ? '-' : '')); // add "-" when we have a final Emoji. | ||
} | ||
|
||
return (string) $dom->saveHTML(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/Integration/Twig/Extension/MarkdownExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Integration\Twig\Extension; | ||
|
||
use App\Twig\Extension\MarkdownExtension; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
final class MarkdownExtensionTest extends KernelTestCase | ||
{ | ||
public function testRoutingExtensionInvalidArgumentException(): void | ||
{ | ||
self::bootKernel(); | ||
$extension = self::getContainer()->get(MarkdownExtension::class); | ||
self::assertNotEmpty($extension->getFilters()); | ||
} | ||
} |