public static function consume()

in src/markdown-extensions/AutoLinkifyInline.php [26:119]


  public static function consume(
    Inlines\Context $context,
    string $markdown,
    int $offset,
  ): ?(Inlines\Link, int) {
    if (self::$disabled) {
      return null;
    }

    // If there is a link at the current position, parse it using the standard
    // Inlines\Link, but with auto-linkifying disabled because nested links are
    // invalid HTML.
    self::$disabled = true;
    try {
      $result = Inlines\Link::consume($context, $markdown, $offset);
    } finally {
      self::$disabled = false;
    }
    if ($result is nonnull) {
      return $result;
    }

    $result = Inlines\CodeSpan::consume($context, $markdown, $offset);
    if ($result === null) {
      return null;
    }
    list($quoted, $offset) = $result;

    $content = $quoted->getCode();
    if ($content === null) {
      return null;
    }

    if (Str\starts_with($content, '$')) {
      return null;
    }

    $matches = Regex\first_match($content, re"/^[^(<]+/");
    if ($matches is null) {
      return null;
    }
    $definition = $matches[0];

    if (Str\contains($definition, ' ')) {
      return null;
    }

    $block_context = $context->getBlockContext();
    invariant(
      $block_context is BlockContext,
      'Expected block context to be a %s',
      BlockContext::class,
    );
    $meta = $block_context->getYamlMeta();

    $name = ($meta['name'] ?? null);
    if ($name !== null && Str\ends_with($name, $definition)) {
      return null;
    }

    $method = self::getMethodTarget($meta, $definition);

    $prefixes = vec[
      $meta['namespace'] ?? null,
      '',
      "HH",
      "HH\\Lib",
      "HH\\Lib\\Experimental",
    ]
      |> Vec\filter_nulls($$);

    $suffixes = vec[$definition, $method]
      |> Vec\filter_nulls($$);

    $to_try = self::product2($prefixes, $suffixes)
      |> Vec\map($$, $parts ==> {
        // Concat namespace parts and ignore parts that are empty
        list($prefix, $suffix) = $parts;
        return vec[$prefix, $suffix]
          |> Vec\filter($$, $part ==> !Str\is_empty($part))
          |> Str\join($$, '\\');
      });

    $index = self::getIndex();

    foreach ($to_try as $def) {
      $target = $index[$def] ?? null;
      if ($target !== null) {
        return tuple(self::makeAutoLink($quoted, $target), $offset);
      }
    }

    return null;
  }