Enhancing generalizability in static and temporal link prediction with applications in drug discovery