Make the strip_math_space filter aware of html tags

This fixes #4309

The old `strip_math_space` filter crops some output if no closing `$` is found or if less than 2 line breaks are found after the starting `$`.

Moreover now only spaces and not newlines are stripped.