public static function consume()

in src/markdown-extensions/ExampleIncludeBlock.php [29:75]


  public static function consume(
    UnparsedBlocks\Context $context,
    UnparsedBlocks\Lines $lines,
  ): ?(UnparsedBlocks\Block, UnparsedBlocks\Lines) {
    list($first, $rest) = $lines->getFirstLineAndRest();
    $matches = darray[];
    if (\preg_match_with_matches(self::PATTERN, $first, inout $matches) !== 1) {
      return null;
    }

    $dir = (string)$matches['dir'];
    $file = (string)$matches['file'];

    if (Str\starts_with($dir, '/')) {
      $file = $dir.$file;
    } else {
      $md = $context->getFilePath();
      invariant($md !== null, 'must have file path set on block context');
      $root = \dirname($md);
      $subdir = Str\strip_suffix(\basename($md), '.md');

      // 01-foo/examples/bar.php is referenced in the markdown as
      // foo/examples/bar.php, to make re-ordering easier
      $match = Str\trim_left($subdir, '1234567890-');
      if (!Str\starts_with($dir, $match)) {
        return null;
      }

      $file = $root.'/'.$subdir.Str\strip_prefix($dir, $match).$file;
    }


    invariant(
      \file_exists($file),
      'failed to find file %s, referenced by %s',
      $file,
      $context->getFilePath(),
    );

    return tuple(
      UnparsedBlocks\BlockSequence::flatten(
        self::getExampleBlock($file),
        self::getOutputBlock($file),
      ),
      $rest,
    );
  }