2
0

AnnotationTokenParser.php 855 B

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