There are 2 kinds of code tags, inline and block:
This is `inline code`
```
This is a code block
```
The button in the markdown toolbar should automatically use the block tag if you select multiple lines of text.
If you use the inline code but insert new lines within, it will produce the result in your first screenshot.
I'm not really sure why TextFormatter doesn't just ignore an inline code tag if it isn't terminated on the same line, but I guess it would be unneeded complexity.