How to remove whitespaces from source of posts created through WordPress Gutenberg editor

function remove_white_space( $content ) {
    return preg_replace( '/[\r\n]+/', "\n", $content );
}

add_filter( 'the_content', 'remove_white_space' );

I got a working solution with the help of https://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php