There's a utility function in cnx-epub called `flatten_to_documents` which does this