How to force a line break in (Nunjucks) templates

What I’m trying to do

I’m using the Obsidian Kindle plugin to extract highlights from my Kindle, and it’s working fine. The thing is I want to customize the output format, and templates use the Nunjucks templating language for that. The original template comes with horizontal rulers, ---, that effectively separate one highlight from the next, but I want to use line-breaks (two blank lines, to be precise). Thus I want to add forced line breaks to the template, but I can’t find out how to do.

Things I have tried

I’ve tried just entering the line breaks: they get stripped out in the output. I’ve tried \\, \n, <br>…: they all show literally in the output.

Is there any way I can force line breaks (that won’t strip out) in this kind of template file? Thanks!

The way to do a line break in Markdown is 2 spaces at the end of the line, or a common extension which works in Obsidian is a backslash (\) at the end of the line (in either case to get a blank line you’d of course put those characters on a line by themselves). So you could try that.

Thanks! Tried that, but it’s not working either :face_exhaling:

Hm, I don’t really know the plugin or template language or how they interact with Markdown.

If you don’t find an answer here (or in the Obsidian Discord’s #plugins-general channel if you’re on Discord), I’d recommend posting your question as an issue on the plugin’s GitHub.

Would the stackoverflow answer below be applicable to your case?

It suggests doing something like:

{{ "one\ntwo" | nl2br }}

Documentation on nl2br.

Thanks, I’ve tried, but it shows in the Live Preview as:


Will try your suggestions @CawlinTeffid and ask somewhere else, just in case.

Thank you both!

This is more of an issue with live preview than with the actual template commands. In HTML5, it should be legal to use <br />, but it surely seems like live-preview doesn’t like it. Reading view allows it, and interprets it correctly.

According to the answer below from StackOverflow it’s not necessary to use the <br /> in HTML5, but it’s required in XHTML. I think we’re mainly using HTML5, but I’m not certain of it.

What I do know, is that if you enter manually the <br /> it doesn’t work in live-preview, but does work in reading mode. If you change it to <br>, it works in both modes.

So that leaves you with a choice of either trying to get the Nunjucks template command of nl2br to not output that trailing slash, or to get live-preview to accept it (aka get CodeMirror6 to accept it).


Here are two more alternatives which seems to indicate that setting a variable to be something like <br>, and then doing something like {{ variable | safe }} should allow the html code to be passed through without being escaped.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.