I've came up with the following solution, imitating most parts of the gfm newline behavior. It passes all the relevant tests on the page mentioned in the original post. Please note that the code below preprocesses markdown and outputs flavored markdown.