Browse Source

Ca marche avec des paragraphes à l\'intérieur du block

Sangfroid 1 tháng trước cách đây
mục cha
commit
998007ee8d

+ 15 - 2
src/Markdown/Block/IntroBlock.php

@@ -3,16 +3,29 @@
 namespace App\Markdown\Block;
 
 use League\CommonMark\Node\Block\AbstractBlock;
+use League\CommonMark\Node\StringContainerInterface;
 
-class IntroBlock extends AbstractBlock
+class IntroBlock extends AbstractBlock implements StringContainerInterface
 {
+    protected string $literal;
+
     public function __construct(protected string $className)
     {
-        
+        $this->literal = '';
     }
 
     public function getClassName(): string
     {
         return $this->className;
     }
+    
+    public function setLiteral(string $literal): void
+    {
+        $this->literal = $literal;
+    }
+
+    public function getLiteral(): string
+    {
+        return $this->literal;
+    }
 }

+ 2 - 2
src/Markdown/Extension/IntroExtension.php

@@ -12,7 +12,7 @@ class IntroExtension implements ExtensionInterface
 {
     public function register(EnvironmentBuilderInterface $environment): void
     {
-        $environment->addBlockStartParser(IntroParser::createBlockStartParser(), 100);
-        $environment->addRenderer(IntroBlock::class, new IntroRenderer(), 10);
+        $environment->addBlockStartParser(IntroParser::createBlockStartParser(), 0);
+        $environment->addRenderer(IntroBlock::class, new IntroRenderer(), 0);
     }
 }

+ 37 - 14
src/Markdown/Parser/IntroParser.php

@@ -15,17 +15,18 @@ use League\CommonMark\Parser\Block\BlockStart;
 use League\CommonMark\Parser\Block\BlockStartParserInterface;
 use League\CommonMark\Parser\InlineParserEngineInterface;
 use League\CommonMark\Parser\MarkdownParserStateInterface;
+use Twig\Node\TextNode;
 
 class IntroParser extends AbstractBlockContinueParser implements BlockContinueParserWithInlinesInterface
 {
     protected IntroBlock $introBlock;
-    protected Paragraph $paragraph;
+    // protected Paragraph $paragraph;
 
     public function __construct(string $className ='')
     {
         $this->introBlock = new IntroBlock($className);
-        $this->paragraph = new Paragraph();
-        $this->introBlock->appendChild($this->paragraph);
+        // $this->paragraph = new Paragraph();
+        // $this->introBlock->appendChild($this->paragraph);
     }
 
     public function getBlock(): AbstractBlock
@@ -35,28 +36,50 @@ class IntroParser extends AbstractBlockContinueParser implements BlockContinuePa
 
     public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
     {
-        if ($cursor->getLine() === ':::') {
+        if (trim($cursor->getLine()) === ':::') {
             return BlockContinue::finished();
         }
-        $line = $cursor->getRemainder();
-        $this->paragraph->appendChild(new Text($line));
+        // $line = $cursor->getRemainder();
+        // $this->paragraph->appendChild(new Text($line));
+        //$this->introBlock->appendChild(new Text($cursor->getRemainder()));
 
+        $line = $cursor->getRemainder();
+        $paragraph =new Paragraph();
+        $paragraph->appendChild(new Text($line));
+        $this->introBlock->appendChild($paragraph);
         return BlockContinue::at($cursor);
     }
 
     public function parseInlines(InlineParserEngineInterface $inlineParser): void
     {
-        $textContent = '';
-        foreach($this->paragraph->children() as $child) {
-            if ($child instanceof Text) {
-                $textContent .= $child->getLiteral();
+        foreach($this->introBlock->children() as $child) {
+            if ($child instanceof Paragraph) {
+                $textContent = '';
+
+                foreach($child->children() as $textNode) {
+                    if ($textNode instanceof Text) {
+                        $textContent .= $textNode->getLiteral();
+                    }
+                }
+
+                foreach($child->children() as $textNode) {
+                    $textNode->detach();
+                }
+
+                $inlineParser->parse($textContent, $child);
             }
         }
-        foreach($this->paragraph->children() as $child) {
-            $child->detach();
-        }
+    //     $textContent = '';
+    //     foreach($this->paragraph->children() as $child) {
+    //         if ($child instanceof Text) {
+    //             $textContent .= $child->getLiteral();
+    //         }
+    //     }
+    //     foreach($this->paragraph->children() as $child) {
+    //         $child->detach();
+    //     }
 
-        $inlineParser->parse($textContent, $this->paragraph);
+    //     $inlineParser->parse($textContent, $this->paragraph);
     }
 
     public static function createBlockStartParser(): BlockStartParserInterface

+ 0 - 1
src/Markdown/Render/IntroRenderer.php

@@ -15,7 +15,6 @@ class IntroRenderer implements NodeRendererInterface
         IntroBlock::assertInstanceOf($node);
         /** @var IntroBlock $node */
         $attrs = ['class' => $node->getClassName()];
-        dump($node);
         return new HtmlElement('div', $attrs, $childRenderer->renderNodes($node->children()));
     }
 }