YAML newline

I found a few very long and detailed articles on how to create multinline in YAML string - for instance
https://yaml-multiline.info/’,
https://www.purestorage.com/knowledge/what-is-yaml-multiline-string.html’,
YAML Superpowers, part 2: Multiline Strings – Crunchy Development’,
How To Break a Single String Over Multi Lines in Yaml
and
syntax - How do I break a string in YAML over multiple lines? - Stack Overflow
-but none of them work.
Does that mean that Goldmark engine requires special configuration, or all those methods will not work whatever you do?
BTW newlines work in this article - I think it’s Markdown.

How does it work here?!

Thanks

Mmh…not sure what you want to do.

Have a multiline string in a frontmatter field?

Goldmark is processing the markdown, not YAML.

The frontmatter is parsed by hugo and so a data file would.

Multiline definitely works with hugo and goldmark. See this topic from 2022. Rendering of string using markdownify not working

Guess It would be good to provide an example code that shows your problem.

Sorry, my bad - indeed YAML is not Markdown!
This YAML in the frontmatter:

    description: |
      Lorem ipsum dolor sit amet, consectetur adipiscing elit:
      - Cras condimentum vestibulum accumsan.
      - Duis nibh mauris, fringilla in sapien at, rutrum condimentum est.
      - Sed porttitor urna metus, a efficitur justo volutpat sit amet

does not make newlines whether indented with 2 or 4 spaces.
But if posted in the Content area (below the ending ---) , it will definitely work.

I just wonder what did you mean saying “Multiline definitely works with hugo and goldmark”.

Multiline definitely works with hugo and goldmark. See this topic from 2022. Rendering of string using markdownify not working

YAML multiline works fine.

I did a quick test here:

Result:

I don’t know what’s going on with my setup.
I have these versions:

$ go version
go version go1.22.5 linux/amd64

$ hugo version
hugo v0.129.0-e85be29867d71e09ce48d293ad9d1f715bc09bb9+extended linux/amd64 BuildDate=2024-07-17T13:29:16Z VendorInfo=gohugoio

config.toml markup settings:

[markup]
  defaultMarkdownHandler = "goldmark"
  unsafe=true
  noClasses=false
  angledQuotes = false
  [markup.goldmark]
    [markup.goldmark.parser]
      autoHeadingID = true
      [markup.goldmark.parser.attribute]
        block = true
        title = true
      [markup.goldmark.parser.newline]
        hardLineBreak = true

All nice samples described by multiple sources as working, like this:

---
dimensions:
  width: "3 metres"
  height: "8 metres"
---

-don’t work in my setup - they are just invisible.

The sample offered by jmooring is not read by my setup altogether - it is invisible:

myvar: |
  Lorem ipsum dolor sit amet, consectetur adipiscing elit:
  - Cras condimentum vestibulum accumsan.
  - Duis nibh mauris, fringilla in sapien at, rutrum condimentum est.
  - Sed porttitor urna metus, a efficitur justo volutpat sit amet

However I’ve found an online YAML editor at
YAML Editor Online

and this sample:

dimensions:
  width: "3 metres"
  height: "8 metres"

works there very well!
I’d love to have that result on my screen!

The sample offered by jmooring as is doesn’t work there as well, but starts working fine if the “|” operator is removed:

myvar:
  Lorem ipsum dolor sit amet, consectetur adipiscing elit:
  - Cras condimentum vestibulum accumsan.
  - Duis nibh mauris, fringilla in sapien at, rutrum condimentum est.
  - Sed porttitor urna metus, a efficitur justo volutpat sit amet

I think my setup is misconfigured. I should find a sample config file.

Your post is illegible. Please wrap code, data, or configurations in triple backticks or use the </> button in the menu.

I suspect you are not converting the markdown to HTML when you render the field in your template. You should be doing something like this:

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Description | .RenderString }}
  {{ .Content }}
{{ end }}

Indeed, I didn’t have the line

{{ .Description | .RenderString }}

-I added it, but so far see no changes.

Perhaps my wrong is that I was trying to render the output not in the

{{ .Content }}

section, but in a section like this:

    {{ with .Params }}
      <div class="col-lg-12">
        <div class="content text-center">
          <h1 class="mb-2">{{ .title | markdownify | upper }}</h1>
          <p class="mb-3">{{ .description }}</p>
        </div>
    etc.

Is the description field in the root of your front matter, or under the params key?


Also, your markup configuration contains invalid and superfluous values:

[markup]
  defaultMarkdownHandler = "goldmark" # superfluous, delete
  unsafe=true # invalid, does nothing, delete
  noClasses=false # invalid, does nothing, delete
  angledQuotes = false # invalid, does nothing, delete
  [markup.goldmark]
    [markup.goldmark.parser]
      autoHeadingID = true # superfluous, delete
      [markup.goldmark.parser.attribute]
        block = true 
        title = true # superfluous, delete
      [markup.goldmark.parser.newline] # invalid, does nothing, delete
        hardLineBreak = true # invalid, does nothing, delete

You can replace all of the above with:

[markup.goldmark.parser.attribute]
block = true

See https://gohugo.io/getting-started/configuration-markup/#goldmark

1 Like

Thank you Joe.
AFAIK to write in Markdown I should use the space under the ending “—” line and output it to the {{ .Content }} field.
But I am already using the key “features:” to write under the ending “—” line to output it to the {{ .Content }} field in Markdown.
Is it possible to output contents of the “description:” key in the front matter to another {{ .Content }} field?
How to separate one {{ .Content }} from another?

I don’t understand your question. Perhaps you can answer my question from my previous comment.

Is the description field in the root of your front matter, or under the params key?

Sorry, I don’t know what you call “the root of front matter”.
Front matter is data and section, contained between the starting 3 dashes and closing 3 dashes:

---
Front matter section, YAML
---
Content section, Markdown

AFAIK there may be any number of keys in the front matter and the data from any front matter key can be posted to any HTML field as unformatted plain text.

But there is only 1 Content section in the .md file, data from which will be posted to only 1 {{ .Content }} HTML field and have markdown formatting.

I have “description:” and “features:” keys in the front matter:

---
description: ""
features: ""
---

I am already using the Content section to post the “features:” data.

My guess was, that YAML in my “description:” don’t have newlines because it’s posted not into the {{ .Content }} HTML field.

My questions are: is there a way to post my “description:” YAML so that it would have newlines?

Thanks!

Mmh, looks like there is some missunderstanding about .Content:

.Content is not field but a page method which prints the rendered content to the target page.

There is only one filnal content!

The content usually is generated by

  • markdown source (.md)
  • a layout template (usually single.html)

You may include frontmatter values into the rendered content by:

  • add the field to the layout template

    {{ .description }}

  • use param shortcode in Markdown

    ### Description:
    
     {{% param description %}}
    

For your multinine field you might have to adjust something… but

It might be a good idea to post a working repo that shows your problem. Including source, config, layouts… (especially the description part). And not parts of parts of your code.

Will be easier to spot the root cause, solve your problem. And will save time


there possibilities to split content generation into seperate parts, but I estimate that this is not needed here.

irkode and jmooring, thank you very much!
I will think it over, but I’ve already found the solution using the markdownify function - it lets me have newlines in YAML!

<span class="description">{{ .description | markdownify | safeHTMLAttr }}</span>'

Thanks once again!

Better use RenderString | Hugo. See Deprecate markdownify?.

Thank you very much!

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