Retain line breaks in plain text paragraph

I’m in the process of converting a Wordpress blog to use hugo instead. Many of my posts are code related and make use of client side syntax highlighting in code blocks. However one of my posts is basically in the form of a poem, and I would like linebreaks to be retained, but code blocks don’t give the presentation I’m looking for, and they also try to auto-highlight the text which definitely isn’t what I want. In Wordpress it basically did this for me, by inserting <br/> at the end of each line. What I’d like is basically this:

I went for a walk.
And then some stuff happened.
And then I went home.

Interestingly, this discussion forum appears to do exactly what I want without doing anything special.

I could of course just add <br/> to the end of each line manually, or apparently use the markdown “two or more spaces at the end of the line” to mean the same thing, but is there a shortcode I could use (or create) which would format the text block this way for me automatically? Can I iterate through input lines in a shortcode and replace line endings with the required markup? Or is there some CSS I could wrap the block in to achieve the same presentation?

Thanks

1 Like

If you use the latest version (from minutes ago) (either by building it from source or wait for a release), you can add a shortcode to fix this:

Add a poem.html (my suggestion, maybe better with something more generic) in /layouts/shortcodes with this content:

{{ replace .Inner "\n" "<br/>" | safeHtml }}

If you’re running Windows, you might have to replace “\n” with “\r\n” - test it.

Then from the content file:

{{< poem >}}
I went for a walk. 
And then some stuff happened.
And then I went home.
{{< / poem >}}
5 Likes

Excellent, I’ll give it a try!

At preset, hugo 0.15 alreday supports github flavor markdown linebreak:
Insert below lines into config.toml
[blackfriday]
extensions = [“hardLineBreak”]

3 Likes