markup.goldmark.renderer.hardWraps not working

[markup.goldmark.renderer]
      hardWraps = true

When I set this I get no <br>s, even if I add some empty lines and also when I try to add 2 spaces at the end of each empty line.

What am I missing to make this work? It makes no difference if I add also unsafe: true.

config.toml

baseURL = "http://example.org/"
languageCode = "en-us"
title = "Test Site"

This markdown:

This line has 2 spaces after the period.  
This line has nothing after the period.
This line has a backslash after the period.\
This line has nothing after the period.
This line has nothing after the period.

Produces this HTML:

<p>
This line has 2 spaces after the period.<br>
This line has nothing after the period.
This line has a backslash after the period.<br>
This line has nothing after the period.
This line has nothing after the period.
</p>

If I add this to config.toml

[markup.goldmark.renderer]
hardWraps = true

The same markup produces this HTML:

<p>
This line has 2 spaces after the period.<br>
This line has nothing after the period.<br>
This line has a backslash after the period.<br>
This line has nothing after the period.<br>
This line has nothing after the period.
</p>

I’m not sure what you’re doing differently. Perhaps your editor is configured to strip trailing spaces from each line upon save.

2 Likes

No, my IDE (VSCode) does not remove any whitespace on save.

I wanted this:

some line


some line

to become

some line
<br>
<br>
<br>
some line

But it seems this does not work.

So I guess the only option is the unsafe config plus to add <br> manually? A bit overcomplicated but ok.

You could setup your IDE to NOT remove whitespace in markdown files. Mine (IntelliJ) can do that :wink:

Other than that: adding three br’s looks a lot like you are trying to add a bottom margin to something. That is task of the “display” part of your website and this is done in CSS. Quite easy with margin-bottom: 1rem; for instance (change the 1rem).

Please see my earlier example.

The CommonMark specification allows you to insert hard breaks with either (a) two or more spaces at the end of a line, or (b) a blackslash (\). Spaces at the beginning of a line are ignored.

This markdown:

someline\
\
\
someline

Produces this HTML:

<p>
someline<br>
<br>
<br>
someline
</p>

I already tried this (with hugo server -D), no difference with Hugo 0.79 on my side. My IDE does not change anything.

My IDE does not remove any whitespace :wink:
Not exactly, I just need one more line break in a longer article to get a bit more space and improve the readability.

Try this:

git clone --single-branch -b hugo-forum-topic-32049 https://github.com/jmooring/hugo-testing hugo-forum-topic-32049
cd hugo-forum-topic-32049
hugo server

Thanks, "\" definitely works, but " " (two spaces) doesn’t:

Bildschirmfoto 2021-03-30 um 19.19.52

Bildschirmfoto 2021-03-30 um 19.19.15

Bildschirmfoto 2021-03-30 um 19.20.21

Bildschirmfoto 2021-03-30 um 19.20.33

fyi, I have updated to Hugo 0.82 and the results are like in the screenshots.

hugo config yields:

baseurl = "http://example.org/"
builddrafts = false
buildexpired = false
buildfuture = false
cachedir = "/var/folders/60/r5b76ddd5m110ngbjxpbggq40000gn/T/hugo_cache/"
canonifyurls = false
cleandestinationdir = false
debug = false
defaultcontentlanguage = "en"
defaultcontentlanguageinsubdir = false
disablealiases = false
disablefastrender = false
disablelivereload = false
disablepathtolower = false
enableemoji = false
enablegitinfo = false
enableinlineshortcodes = false
enablemissingtranslationplaceholders = false
environment = "production"
footnoteanchorprefix = ""
footnotereturnlinkcontents = ""
forcesyncstatic = false
hascjklanguage = false
ignorecache = false
ignorefiles = []
indexes = map[category:categories tag:tags]
languagecode = "en-us"
newcontenteditor = ""
paginate = 10
paginatepath = "page"
permalinks = map[]
pluralizelisttitles = true
publishdir = "public"
pygmentscodefencesguesssyntax = false
relativeurls = false
removepathaccents = false
resourcedir = "resources"
rsslimit = -1
sectionpagesmenu = ""
sitemap = {ChangeFreq: Priority:-1 Filename:sitemap.xml}
summarylength = 70
taxonomies = map[category:categories tag:tags]
themesdir = "themes"
timeout = "30s"
title = "Hugo Forum Topic #32049"
titlecasestyle = "AP"
uglyurls = false
verbose = false
watch = false
workingdir = "/Users/danielruf/projects/hugo-forum-topic-32049"

hugo version yields:

hugo v0.82.0+extended darwin/amd64 BuildDate=unknown

This is a convenient way to test your markdown.

1 Like

Ok, thanks. But it seems this is very buggy, at least commonmark does the same.

Even LaTeX does it better =)
Is there some repo to report this flaw in the commonmark specification? Because to me, this makes no sense at all.

I guess it is better to add a custom class, pure HTML (that’s something that I want to avoid) or do some other magic trick.

I found Issues · commonmark/commonmark-spec · GitHub, I will clarify this there. Thanks for your helpful input so far.

My final solution is now to create a custom raw-html shortcode which contains only {{ .Inner }} that I use like this: {{< raw-html >}}<br>{{< / raw-html >}}

If you need to do this frequently…

This markdown:

Foo
{{< br >}}
# Title
{{< br 3 >}}
Bar

Produces this HTML:

<p>
  Foo
  <br>
</p>
<h1 id="title">Title</h1>
<p>
  <br><br><br>
  Bar
</p>

layouts/shortcodes/br.html

{{- $msg := "The optional argument passed to the %s shortcode must be an integer greater than or equal to zero. See %s" -}}
{{- if ne 0 (.Get 0) -}}
  {{- with .Get 0 -}}
    {{- if eq (printf "%T" .) "int" -}}
      {{- if lt . 0 -}}
        {{- errorf $msg $.Name $.Position -}}
      {{- else -}}
        {{- range seq 1 . -}}
          <br>
        {{- end -}}
      {{- end -}}
    {{- else -}}
      {{- errorf $msg $.Name $.Position -}}
    {{- end -}}
  {{- else -}}
    <br>
  {{- end -}}
{{- end -}}
1 Like

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