PlaceholderParser.php 871 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace App\Services\Markup\Parsing;
  3. use League\CommonMark\Parser\Inline\InlineParserInterface;
  4. use League\CommonMark\Parser\Inline\InlineParserMatch;
  5. use League\CommonMark\Parser\InlineParserContext;
  6. /**
  7. * 把预扫描留下的占位符解析成 {@see PlaceholderNode}。
  8. */
  9. final class PlaceholderParser implements InlineParserInterface
  10. {
  11. public function getMatchDefinition(): InlineParserMatch
  12. {
  13. return InlineParserMatch::regex(Placeholder::OPEN.'(\d+)'.Placeholder::CLOSE);
  14. }
  15. public function parse(InlineParserContext $inlineContext): bool
  16. {
  17. $cursor = $inlineContext->getCursor();
  18. [$index] = $inlineContext->getSubMatches();
  19. $cursor->advanceBy($inlineContext->getFullMatchLength());
  20. $inlineContext->getContainer()->appendChild(new PlaceholderNode((int) $index));
  21. return true;
  22. }
  23. }