Diagrams in category theory: formalizing a concept in diagram-chasing

Lemma 1.6.11. Suppose $$f_1,...,f_n$$ is a composable sequence - a "path" - of morphisms in a category. If the composite $$f_kf_{k-1}...f_{i+1}f_i$$ equals $$g_m...g_1$$ for another composable sequence of morphsism $$g_1,...,g_m$$, then $$f_n...f_1 = f_n...f_{k+1}g_m...g_1f_{i-1}...f_1$$

... In such cases, Lemma 1.6.11 and transitivity of equality implies that commutativity of the entire diagram may be checked by establishing commutativity of each minimal subdiagram in the directed graph. Here, a minimal subdiagram corresponds to a composition relation $$h_n...h_1 = k_m...k_1$$ that cannot be factored into a relation between shorter paths of composable morphisms. The graph corresponding to a minimal relation is a "directed polygon" with a commutative triangle being the simplest case.

This is from a book "Category Theory in Context" by Emily Riehl. As an example, the author gives the case of a commutative cube (a cube of objects and morphisms in a category) such as this:

The formal definition of a diagram in a category $$\mathcal{C}$$ is that it is a functor $$F\colon\mathcal{I}\to\mathcal{C}$$ for some category $$\mathcal{I}$$, which is called the shape of the diagram. It is defined the same way in the aforementioned book.

What I wish to know if whether we can formalize the aforementioned concept of a "minimal subdiagram" as it is called by Riehl with respect to an aribtrary functor $$D\colon\mathcal{I}\to\mathcal{C}$$ considered as a diagram of shape $$\mathcal{I}$$ in a category $$\mathcal{C}$$.

If $$f$$ is a morphism and $$f = g\circ h$$, then $$a \circ f \circ b = a \circ g \circ h \circ b$$, wherever $$a \circ f \circ b$$ is defined.
Since we're in a category, this is equivalent to the lemma given. To reach any path expandable-to from $$f$$ you just recursively apply this lemma in-place.