|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Markdown\Alerts; |
| 4 | + |
| 5 | +use League\CommonMark\Node\Block\AbstractBlock; |
| 6 | +use League\CommonMark\Parser\Block\BlockContinue; |
| 7 | +use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
| 8 | +use League\CommonMark\Parser\Cursor; |
| 9 | +use League\CommonMark\Util\RegexHelper; |
| 10 | + |
| 11 | +final class AlertBlockParser implements BlockContinueParserInterface |
| 12 | +{ |
| 13 | + protected $block; |
| 14 | + protected $finished = false; |
| 15 | + |
| 16 | + public function __construct( |
| 17 | + protected string $alertType, |
| 18 | + protected string $title, |
| 19 | + ) { |
| 20 | + $this->block = new AlertBlock($alertType, $title); |
| 21 | + } |
| 22 | + |
| 23 | + public function addLine(string $line): void |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + public function getBlock(): AbstractBlock |
| 28 | + { |
| 29 | + return $this->block; |
| 30 | + } |
| 31 | + |
| 32 | + public function isContainer(): bool |
| 33 | + { |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + public function canContain(AbstractBlock $childBlock): bool |
| 38 | + { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + public function canHaveLazyContinuationLines(): bool |
| 43 | + { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + public function parseInlines(): bool |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
| 53 | + { |
| 54 | + if ($cursor->isIndented()) { |
| 55 | + return BlockContinue::at($cursor); |
| 56 | + } |
| 57 | + |
| 58 | + $match = RegexHelper::matchFirst('/^:::$/', $cursor->getLine()); |
| 59 | + if ($match !== null) { |
| 60 | + $this->finished = true; |
| 61 | + return BlockContinue::finished(); |
| 62 | + } |
| 63 | + |
| 64 | + return BlockContinue::at($cursor); |
| 65 | + } |
| 66 | + |
| 67 | + public function closeBlock(): void |
| 68 | + { |
| 69 | + // Nothing to do here |
| 70 | + } |
| 71 | + |
| 72 | + public function isFinished(): bool |
| 73 | + { |
| 74 | + return $this->finished; |
| 75 | + } |
| 76 | +} |
0 commit comments