Sfoglia il codice sorgente

On ajoute une extension Markdown pour la couleur mais on utilise pas

Sangfroid 2 mesi fa
parent
commit
b435d1ff02

+ 15 - 0
src/Markdown/ColorExtension.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Markdown;
+
+use League\CommonMark\Environment\EnvironmentBuilderInterface;
+use League\CommonMark\Extension\ExtensionInterface;
+
+class ColorExtension implements ExtensionInterface
+{
+    public function register(EnvironmentBuilderInterface $environment): void
+    {
+        $environment->addInlineParser(new ColorParser(), 200);
+        $environment->addRenderer(ColorText::class, new ColorRenderer());
+    }
+}

+ 37 - 0
src/Markdown/ColorParser.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Markdown;
+
+use League\CommonMark\Parser\Inline\InlineParserInterface;
+use League\CommonMark\Parser\Inline\InlineParserMatch;
+use League\CommonMark\Parser\InlineParserContext;
+
+class ColorParser implements InlineParserInterface
+{
+    public function getMatchDefinition(): InlineParserMatch
+    {
+        return InlineParserMatch::regex("!([a-z]+):(.+?)!");
+    }
+
+    public function parse(InlineParserContext $inlineContext): bool
+    {
+        $matches = $inlineContext->getMatches();
+        
+        if (count($matches) === 3) {
+            // La couleur est capturée dans le premier groupe, et le texte dans le second
+            $color = $matches[1];
+            $text = $matches[2];
+            
+            $colorNode = new ColorText($text, $color);
+
+            $inlineContext->getContainer()->appendChild($colorNode);
+            $inlineContext->getCursor()->advanceBy(mb_strlen($matches[0]));
+            dump($matches[0]);
+            
+            return true;
+        }
+
+        return false;
+    }
+
+}

+ 24 - 0
src/Markdown/ColorRenderer.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Markdown;
+
+use League\CommonMark\Node\Node;
+use League\CommonMark\Renderer\ChildNodeRendererInterface;
+use League\CommonMark\Renderer\NodeRendererInterface;
+use League\CommonMark\Util\HtmlElement;
+
+class ColorRenderer implements NodeRendererInterface
+{
+    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
+    {
+        if (!($node instanceof ColorText)) {
+            throw  new \InvalidArgumentException('Le noeud doit être une instance de ColorText');
+        }
+
+        $attrs = [
+            'style' => 'color: ' . htmlspecialchars($node->getColor(), ENT_QUOTES, 'UTF-8')
+        ];
+
+        return new HtmlElement('span', $attrs, $node->getText());
+    }
+}

+ 26 - 0
src/Markdown/ColorText.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Markdown;
+
+use League\CommonMark\Node\Inline\AbstractInline;
+
+class ColorText extends AbstractInline
+{
+    public function __construct(
+        private string $text,
+        private string $color
+    )
+    {
+        parent::__construct();
+    }
+
+    public function getColor(): string
+    {
+        return $this->color;
+    }
+
+    public function getText(): string
+    {
+        return $this->text;
+    }
+}