@NotNull
  public static Pair<PsiElement, PsiElement> createLinkDeclarationAndReference(Project project, PsiElement link, String referenceText) {
    String text = null;
    String title = null;
    String url = getUrl(link);

    if (PsiUtilCore.getElementType(link) == INLINE_LINK) {
      SyntaxTraverser<PsiElement> syntaxTraverser = SyntaxTraverser.psiTraverser();

      PsiElement textElement = syntaxTraverser.children(link).find(child -> PsiUtilCore.getElementType(child) == LINK_TEXT);
      if (textElement != null) {
        text = textElement.getText();
        assert text.startsWith("[") && text.endsWith("]");
        text = text.substring(1, text.length() - 1);
      }

      PsiElement titleElement =
        syntaxTraverser.children(link).find(child -> PsiUtilCore.getElementType(child) == LINK_TITLE);
      if (titleElement != null) {
        title = titleElement.getText();
      }
    }

    assert url != null;

    if (text == null) {
      text = url;
    }

    return MarkdownPsiElementFactory.createLinkDeclarationAndReference(project, url, text, title, referenceText);
  }
@NotNull
  public static Pair<PsiElement, PsiElement> createLinkDeclarationAndReference(Project project, PsiElement link, String referenceText)