code: function (wholeMatch, before, inner, afterInner, id, end) {
      inner = inner.replace(regex, getLink);
      if (defsToAdd[id]) {
        addDefNumber(defsToAdd[id]);
        return before + inner + afterInner + refNumber + end;
      }
      return wholeMatch;
    }
docs: note that a) the recursive call to getLink cannot go infinite, because by definition of regex, inner is always a proper substring of wholeMatch, and b) more than one level of nesting is neither supported by the regex nor making a lot of sense (the only use case for nesting is a linked image)