matcornic / hugo-theme-learn

Porting Grav Learn theme to Hugo
https://learn.netlify.com/en/
MIT License
1.6k stars 1.28k forks source link

invalid index.json with multiline descriptions #517

Open garz75 opened 2 years ago

garz75 commented 2 years ago

When using a yaml front matter with a multiline description that uses a Block Style Indicator (> or |)

---
title: Simple title
chapter: false
weight: 40
description: |
  This is the beginning of a multiline description...
---

The JSON file generated by the index.json template is invalid: The description contains a \n at the end:

[
{
        "uri": "/example.html",
        "title": "Simple title",
        "tags": [],
        "description": "This is the beginning of a multiline description...
",
        "content": "blah"
}
]

My attempt at fixing this issue is to trim the extra\n in the template:

diff --git a/layouts/index.json b/layouts/index.json
index 367f3b3..011b7c8 100644
--- a/layouts/index.json
+++ b/layouts/index.json
@@ -5,7 +5,7 @@
        "uri": "{{ $page.Permalink }}",
        "title": "{{ htmlEscape $page.Title}}",
        "tags": [{{ range $tindex, $tag := $page.Params.tags }}{{ if $tindex }}, {{ end }}"{{ $tag| htmlEscape }}"{{ end }}],
-       "description": "{{ htmlEscape .Description}}",
+       "description": "{{ trim .Description "\n" | htmlEscape }}",
        "content": {{$page.Plain | jsonify}}
 }
 {{- end -}}

This seems to work for me but I am very new at Hugo templating... Am I on the right track ? Is there a way to fix this in a better way ?

Thank you in Advance.

McShelby commented 2 years ago

Seems to work in the Relearn fork.