Newline Character in JSON Markdown File

I create my markdown files in JSON (instead of yaml or toml). And I place the main content / body as a value of a json key. I call that value via params variables.

It works reasonably well. EXCEPT when that content includes the newline character \n

There is nothing terrible happening. There just is no newline or <p> in the generated html. The sentences are all together in one line which is not visually appealing.

Has anyone dealt with this problem? What was your hack / solution?

FYI :

  • I have tried this solution. But having double slashes or \\n instead of \n results in \\n appearing the generated html which is a worse outcome.
  • Including multiple \n or \n\n instead of \n has no effect at all on generated html.
  • I have tried writing newline as unicode with this \u000d based on this proposal. But it doesn’t work and has not noticeable effect.
  • Including U+000A based on this has not noticeable effect either.
  • Apart from this, hugo handles unicode characters in the JSON value well.
1 Like

SOLUTION:

I am replying to my own question just in case somebody comes across this problem in the future.

The solution I found was to use the replace function in hugo (which I was entirely unaware of). You can choose to replace all the newline characters with an html tag like
. Something along these lines when declaring Content in the templates:
{{ (replace .Content "\n" "<br/>") | safeHTML}}

Inspiration was this.

1 Like

Thanks! It works for me!