Repository: gohugoio/hugo (language: go)

Issue:
Softbreaks in the alt text of images are eaten 
I have [asked the question on the forum](https://discourse.gohugo.io/t/how-to-preserve-softbreaks-in-the-alt-text-of-images/47019?u=yihui) and was suggested to file an issue here.

---

Originally this was [an issue in goldmark](https://github.com/yuin/goldmark/issues/416) but it has been fixed recently (in 1.6.0), which has been [picked up](https://github.com/gohugoio/hugo/commit/db14238ba323279b28e7ad4cfff5aa10909ccd18) by Hugo 0.120.3. However, when I render an image like this:

```md
![alt
tex](logo.png)
```

it still gives me

```html
<img src="logo.png" alt="alttext">
```

That is, the softbreak was still eaten. I'm hoping to get

```html
<img src="logo.png" alt="alt
text">
```

---

From @jmooring:

> I’ve verified that (a) v0.120.3 is using yuin/goldmark v1.6.0, and (b) yuin/goldmark v1.6.0 resolves the reported problem. So, this looks like a bug in Hugo’s parser.

---

### What version of Hugo are you using (`hugo version`)?

```
$ hugo version
hugo v0.120.3-a4892a07b41b7b3f1f143140ee4ec0a9a5cf3970+extended darwin/arm64 BuildDate=2023-11-01T17:57:00Z VendorInfo=gohugoio
```

### Does this issue reproduce with the latest release?

Yes.


Discussion / hints:
@yihui Until this is resolved, you can use this image render hook...

layouts/_default/_markup/render-image.html

```text
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{- with .Title }} title="{{ . }}"{{ end }}>
{{- /**/ -}}
```

Fix patch:
diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go
index 60a3a1efee3..ecdd7f91e98 100644
--- a/markup/goldmark/render_hooks.go
+++ b/markup/goldmark/render_hooks.go
@@ -484,6 +484,9 @@ func nodeToHTMLText(n ast.Node, source []byte) []byte {
 			buf.Write(s.Text(source))
 		} else if !c.HasChildren() {
 			buf.Write(util.EscapeHTML(c.Text(source)))
+			if t, ok := c.(*ast.Text); ok && t.SoftLineBreak() {
+				buf.WriteByte('\n')
+			}
 		} else {
 			buf.Write(nodeToHTMLText(c, source))
 		}


Test patch: