How to parse markdown without newlines?

I’m creating a plugin that serializes Kirby content. One step is to convert Markdown to HTML. However, since that data will be saved in a file, the size of it matters. The Markdown class of Kirby parses it with newlines.

If I have the following markdown:

# Hello

- foo
- bar
- baz

It will be exported as:

<h1>Hello</h1>\n<ul>\n<li>foo</li>\n<li>bar</li>\n<li>baz</li>\n</ul>

I suppose the newlines are added for better readability, but in my case, that doesn’t matter. Is there a way I can get the following output instead:

<h1>Hello</h1><ul><li>foo</li><li>bar</li><li>baz</li></ul>

:thinking:

Remove new lines with str_replace()?

str_replace("\n", '', $parsetext))
1 Like

Well, yeah, but isn’t this risky? Could the parser output legitimate newlines that actually need to be there? Or all necessary line breaks are replaced with <br>?

Probably, depending on what content your text contains, thinking of scripts etc., e.g. if people do not use semicolons at the end of lines. You’d probably need a HTML minifying library.

This would be less risky, I guess:

str_replace(">\n<", '><', $output);

I’m gonna go with that.