From 78970da2b7782bb2d5e22d8438575ad126e5772a Mon Sep 17 00:00:00 2001 From: COil Date: Sat, 21 Dec 2024 08:57:13 +0100 Subject: [PATCH] feat: add the missing anchors on the website homepage + cleanup --- README.md | 9 ++-- src/Twig/Extension/MarkdownExtension.php | 49 +++++++++++++++++++ templates/App/Controller/HomeAction.html.twig | 10 ++-- templates/base.html.twig | 2 +- .../Twig/Extension/MarkdownExtensionTest.php | 18 +++++++ 5 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 src/Twig/Extension/MarkdownExtension.php create mode 100644 tests/Integration/Twig/Extension/MarkdownExtensionTest.php diff --git a/README.md b/README.md index 5939512..4df42f9 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ You can also directly use the [FrankenPHP](https://github.com/strangebuzz/MicroS * The [Symfony CLI](https://symfony.com/download) -### Optional requirements ⚙ +### Optional requirements 🚦 * The [Xdebug](https://xdebug.org/) PHP extension if you want to run the code coverage report * [Castor](https://github.com/jolicode/castor) task runner if you don't want to use @@ -122,11 +122,12 @@ You can also directly use the [FrankenPHP](https://github.com/strangebuzz/MicroS ## Stack 🔗 +* [PHP 8.2](https://www.php.net/releases/8.2/en.php) to [8.4](https://www.php.net/releases/8.4/en.php) * [Symfony 7.2](https://symfony.com/7) * [Twig 3.8](https://twig.symfony.com) -* Hotwired [stimulus 3.2](https://stimulus.hotwired.dev/) and [Turbo 8.0](https://turbo.hotwired.dev/) -* [PHPUnit 11.0](https://phpunit.de/announcements/phpunit-11.html) -* The [Pico CSS](https://picocss.com) framework +* [Hotwired](https://hotwired.dev/) [Stimulus 3.2](https://stimulus.hotwired.dev/) and [Turbo 8.0](https://turbo.hotwired.dev/) +* [PHPUnit 11.5](https://phpunit.de/announcements/phpunit-11.html) +* [Pico CSS 2.0](https://picocss.com) ## Features 🚀 diff --git a/src/Twig/Extension/MarkdownExtension.php b/src/Twig/Extension/MarkdownExtension.php new file mode 100644 index 0000000..811a9ae --- /dev/null +++ b/src/Twig/Extension/MarkdownExtension.php @@ -0,0 +1,49 @@ +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(); + } +} diff --git a/templates/App/Controller/HomeAction.html.twig b/templates/App/Controller/HomeAction.html.twig index f6159d6..029e988 100644 --- a/templates/App/Controller/HomeAction.html.twig +++ b/templates/App/Controller/HomeAction.html.twig @@ -8,12 +8,14 @@
- {{ readme|raw|markdown_to_html|raw }} + {{ readme|raw|markdown_to_html|add_headers_anchors|raw }} - {# Uncomment this and adapat it to your needs. + {# Uncomment and adapt to your needs.

MicroSymfony 🎶

+

About 🖋

+

Table of Contents 📖

Demos 🌈

@@ -21,7 +23,7 @@

Quick-start ⚡

Stack 🔗

- +🔆

Features 🚀

Notes 📒

@@ -37,6 +39,8 @@

Credits 🙏

License ⚖️

+ +

Built with MicroSymfony 🛠️️

#} {% endblock %} \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 4a07823..ad2108a 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -57,7 +57,7 @@ GitHub
  • - 🌞 + 🔆 🌘
  • diff --git a/tests/Integration/Twig/Extension/MarkdownExtensionTest.php b/tests/Integration/Twig/Extension/MarkdownExtensionTest.php new file mode 100644 index 0000000..32d74e8 --- /dev/null +++ b/tests/Integration/Twig/Extension/MarkdownExtensionTest.php @@ -0,0 +1,18 @@ +get(MarkdownExtension::class); + self::assertNotEmpty($extension->getFilters()); + } +}