Having trouble with plain text output

Hi all,

tl;dr : parse markdown to text but keep line breaks.

I’m trying to produce plain text versions of a particular group of pages, and am almost there (I’ve checked old threads and can’t find anything that helps).

I’ve got Hugo to produce the plain text, and set up templates for the shortcodes I’m using, and they look great. The problem I have is the main page content.

So far I’ve tried:

  1. {{ .Content }} in the template gives me HTML output, which makes sense.
  2. {{ .RawContent }} doesn’t parse at all, also as expected.
  3. {{ .Content | plainify }} is the one I assume I want - but it puts all the text on one line (in a single string) which is ugly.

I must be missing something obvious, but how do I parse the markdown and turn it into text that isn’t a single string? Even just ignoring the markdown and preserving line breaks would be fine.

Thanks in advance.

Have you tried .Plain?

Thanks.

That’s the best yet - but it still seems to strip out consecutive line breaks (presumably by design) and that means that it will join a new paragraph onto the end of a heading, no matter how I try:

# Header

First line

Second line

becomes:

Header First Line
Second line

And it does the same to the plain text I’ve formatted myself in shortcodes, too. I think it’s OK for what I need, but still not quite what I would expect.

Thanks for the help,

Mark

How about this:

{{ with .RawContent }}
    {{ $content := . }}
    {{ $content = replaceRE "\\s*\\{#[^}]+\\}" "" $content }}
    {{ $content = replaceRE "\\n" "<br/>" $content }}
    {{ $content | safeHTML }}
{{ end }}


https://plain-text-output--hugo-mwe.netlify.app/posts/hello/

5 Likes

Have you looked into Custom Output Formats?

Here’s a crude example:

Thanks very much folks, I think I’ve managed with your help!

I’ll mark this as solved, much appreciated.

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