123456789101112131415161718192021222324252627282930313233343536 |
- <?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]));
-
- return true;
- }
- return false;
- }
- }
|