How to skeep/break a line for a txt output

My question is perhaps very naive, but I can’t find a way to force a break line for a TXT output in Hugo.

Here the context and my specific needs:

  • I have few different outputs, and a TXT one
  • I need to add a break line before every heading in the TXT output
  • I try several options (like {{ " " }}, a loop, etc.) in my layouts/_default/_markup/render-heading.txt.txt file, but none of them work

I assume you have a config like:

[outputs]
page = ['html','text']

[outputFormats.text]
mediaType = 'text/plain'

And a template such as layouts/_default/single.text.txt.

What’s in that template? Are you rendering RawContent, Content, or something else?

My content structure is a bit more complex but yep it’s very similar. In the equivalent of the layouts/_default/single.text.txt I have: {{ .Content | markdownify | plainify | htmlUnescape }}

My repo is here: ~antoinentl/t - Dépôt de la thèse d'Antoine Fauchié. https://these.quaternum.net - sourcehut git
(It’s a work in progress: ~antoinentl/t (87-version-txt): layouts/_default/index.txt - sourcehut git)

First, don’t pass .Content through markdownify. The .Content is rendered markdown; don’t process it twice.

Second, use a render hook to render the headings as a paragraphs.

layouts/_default/_markup/render-heading.text.txt

<p>{{ .Text }}</p>

This works too:

{{ .Text }}<br>

It seems weird but it’s the only way I found to convert apostrophes/single quotes (l'élémentl’élément) without rendering it on special characters.

Both of it does anything in my case, do I miss something :thinking: ?

git clone --single-branch -b hugo-forum-topic-48430 https://github.com/jmooring/hugo-testing hugo-forum-topic-48430
cd hugo-forum-topic-48430
hugo && cat public/posts/post-1/index.txt 

This markdown:

The front matter color param is... {{< param color >}}.

## Heading 1

This is paragraph 1.

## Heading 2

This is paragraph 2.

Is rendered to:

The front matter color param is… red.
Heading 1
This is paragraph 1.
Heading 2
This is paragraph 2.

If you want blank lines before and after each heading, change your render hook to:

<p>&nbsp;</p>
{{ .Text }}<br>
<p>&nbsp;</p>

Ok <p>&nbsp;</p> do the job, thanks for that!

I’m not sure the <br> does something.

In my first example, without that, there is no newline between the heading and the following paragraph. Play with it a bit…

Although I’m sure you’ve thought about this, you might consider rendering .RawContent instead. The plainify function will turn things like markdown tables into gibberish.

Wouldn’t that be better handled by setting margin-top/margin-bottom on the headings?

This is a text file output. There is no CSS.

No I admit that I never thought about .RawMarkdown, and I don’t see how to implement it, any advice?

Get rid of the render hook, and in layouts/_default/single.text.txt do:

{{ .RawContent}}

Sorry, it’s named RawContent.

Ho ok, but no .RawContent don’t manage single quotes like a need…

1 Like

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