Repository: gohugoio/hugo (language: go)

Issue:
Twitter `og:description` doubly escaped by Hugo 0.125.x
If you build this [small repro (test-site-2024-04-23.zip)](https://drive.google.com/file/d/1VIOiEZ8tdyI2CR1pTkoT9dqw_r5kdBdu) using Hugo 0.124.1 and 0.125.2, you'll see the following difference in the `content` of `<meta property="og:description">` for http://localhost:1313/docs/page:

```html
0.124.1: <meta property="og:description" content="Test line with a single quote: can&rsquo;t." />
0.125.2: <meta property="og:description" content="Test line with a single quote: can&amp;rsquo;t.">
```

Notice that 0.125.2 doubly escapes the `content`, so that:

- `can&rsquo;t` becomes
- `can&amp;rsquo;t`

This seems wrong to me. WDYT?

---

Below I include a bit more header context.

Hugo 0.124.1:

```html
<meta name="description" content="Test line with a single quote: can&amp;rsquo;t.">
<meta property="og:title" content="Some page" />
<meta property="og:description" content="Test line with a single quote: can&rsquo;t." />
...
<meta name="twitter:description" content="Test line with a single quote: can&rsquo;t."/>
```

Hugo 0.125.2:

```html
<meta name="description" content="Test line with a single quote: can&amp;rsquo;t.">
<meta property="og:url" content="http://localhost:1313/docs/page/">
  <meta property="og:site_name" content="My New Hugo Site">
  <meta property="og:title" content="Some page">
  <meta property="og:description" content="Test line with a single quote: can&amp;rsquo;t.">
  ...
<meta name="twitter:description" content="Test line with a single quote: can&rsquo;t.">
```






Discussion / hints:
I'll look at this again. Here's the challenge in the embedded opengraph template:

```text
{{- with or .Description .Summary site.Params.description | plainify }}
  <meta property="og:description" content="{{ . }}">
{{- end }}
```

The input can be plain text, markdown, rendered markdown, HTML, or anything else. If rendered markdown, it might be wrapped in a `p` element or it might not, depending on how the summary was defined.
Going back through issues, PRs, and forum topics, the best that I have come up with for consistent rendering regardless of how the summary is defined (front matter, with summary divider, without summary divider) is:

```text
{{- with or .Description .Summary site.Params.description | .RenderString | plainify | htmlUnescape }}
  <meta property="og:description" content="{{ . }}">
{{- end }}
```

This handles the following cases the same way:

```text
+++
title = 'Post 1'
summary = "Test <em>line</em> with a **single** quote: can't."
+++
```

```text
+++
title = 'Post 1'
+++

Test <em>line</em> with a **single** quote: can't.
```

```text
+++
title = 'Post 1'
+++

Test <em>line</em> with a **single** quote: can't.
<!--more-->
```

All three produce:

```html
<meta property="og:description" content="Test line with a single quote: can’t.">
```

If this is acceptable:

- [ ] Do the same thing for `og:site_name` and `og:title`
- [ ] Make similar changes to the embedded schema and twitter_cards templates



Sounds good, thanks @jmooring. The OTel website has descriptions like the following (which I'm hoping to curate at some point to remove the images, but this is what we have now):

```yaml
description: >-
  <img width="35" class="img-initial" src="/img/logos/32x32/Golang_SDK.svg"
  alt="Go"> A language-specific implementation of OpenTelemetry in Go.
```

And this (markdown):

```yaml
description: >-
  _OTel in Practice_ is a _series_ of talks initiated by some members of the End
  User SIG.
```

Will the solution you propose handle such descriptions as before (or better :)?

/cc @svrnm @theletterf
@chalin 

With the proposed changes, the first is rendered to:

```html
<meta property="og:description" content=" A language-specific implementation of OpenTelemetry in Go.">
```

The second is rendered to:

```html
<meta property="og:description" content="OTel in Practice is a series of talks initiated by some members of the End User SIG.">
```

Thanks @jmooring, that LGTM.

Fix patch:
diff --git a/tpl/tplimpl/embedded/templates/opengraph.html b/tpl/tplimpl/embedded/templates/opengraph.html
index c245e5bd143..a9b348e9ef0 100644
--- a/tpl/tplimpl/embedded/templates/opengraph.html
+++ b/tpl/tplimpl/embedded/templates/opengraph.html
@@ -4,11 +4,11 @@
   <meta property="og:site_name" content="{{ . }}">
 {{- end }}
 
-{{- with or .Title site.Title site.Params.title | plainify}}
+{{- with or .Title site.Title site.Params.title | plainify }}
   <meta property="og:title" content="{{ . }}">
 {{- end }}
 
-{{- with or .Description .Summary site.Params.description | plainify }}
+{{- with or .Description .Summary site.Params.description | plainify | htmlUnescape | chomp }}
   <meta property="og:description" content="{{ . }}">
 {{- end }}
 
@@ -18,7 +18,9 @@
 
 {{- if .IsPage }}
   <meta property="og:type" content="article">
-  <meta property="article:section" content="{{ .Section }}">
+  {{- with .Section }}
+    <meta property="article:section" content="{{ . }}">
+  {{- end }}
   {{- $ISO8601 := "2006-01-02T15:04:05-07:00" }}
   {{- with .PublishDate }}
     <meta property="article:published_time" {{ .Format $ISO8601 | printf "content=%q" | safeHTMLAttr }}>


Test patch:
diff --git a/tpl/tplimpl/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go
index 28d442e0d63..6b2664c4d84 100644
--- a/tpl/tplimpl/tplimpl_integration_test.go
+++ b/tpl/tplimpl/tplimpl_integration_test.go
@@ -305,3 +305,109 @@ title: p2
 		"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n  xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n  <url>\n    <loc>/p2/</loc>\n  </url>\n</urlset>\n",
 	)
 }
+
+// Issue 12418
+func TestOpengraph(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+capitalizeListTitles = false
+disableKinds = ['rss','sitemap']
+languageCode = 'en-US'
+[markup.goldmark.renderer]
+unsafe = true
+[params]
+description = "m <em>n</em> and **o** can't."
+[params.social]
+facebook_admin = 'foo'
+[taxonomies]
+series = 'series'
+tag = 'tags'
+-- layouts/_default/list.html --
+{{ template "_internal/opengraph.html" . }}
+-- layouts/_default/single.html --
+{{ template "_internal/opengraph.html" . }}
+-- content/s1/p1.md --
+---
+title: p1
+date: 2024-04-24T08:00:00-07:00
+lastmod: 2024-04-24T11:00:00-07:00
+images: [a.jpg,b.jpg]
+audio: [c.mp3,d.mp3]
+videos: [e.mp4,f.mp4]
+series: [series-1]
+tags: [t1,t2]
+---
+a <em>b</em> and **c** can't.
+-- content/s1/p2.md --
+---
+title: p2
+series: [series-1]
+---
+d <em>e</em> and **f** can't.
+<!--more-->
+-- content/s1/p3.md --
+---
+title: p3
+series: [series-1]
+summary: g <em>h</em> and **i** can't.
+---
+-- content/s1/p4.md --
+---
+title: p4
+series: [series-1]
+description: j <em>k</em> and **l** can't.
+---
+-- content/s1/p5.md --
+---
+title: p5
+series: [series-1]
+---
+`
+
+	b := hugolib.Test(t, files)
+
+	b.AssertFileContent("public/s1/p1/index.html", `
+		<meta property="og:url" content="/s1/p1/">
+		<meta property="og:title" content="p1">
+		<meta property="og:description" content="a b and c can’t.">
+		<meta property="og:locale" content="en-US">
+		<meta property="og:type" content="article">
+		<meta property="article:section" content="s1">
+		<meta property="article:published_time" content="2024-04-24T08:00:00-07:00">
+		<meta property="article:modified_time" content="2024-04-24T11:00:00-07:00">
+		<meta property="article:tag" content="t1">
+		<meta property="article:tag" content="t2">
+		<meta property="og:image" content="/a.jpg">
+		<meta property="og:image" content="/b.jpg">
+		<meta property="og:audio" content="/c.mp3">
+		<meta property="og:audio" content="/d.mp3">
+		<meta property="og:video" content="/e.mp4">
+		<meta property="og:video" content="/f.mp4">
+		<meta property="og:see_also" content="/s1/p2/">
+		<meta property="og:see_also" content="/s1/p3/">
+		<meta property="og:see_also" content="/s1/p4/">
+		<meta property="og:see_also" content="/s1/p5/">
+		<meta property="fb:admins" content="foo">
+		`,
+	)
+
+	b.AssertFileContent("public/s1/p2/index.html",
+		`<meta property="og:description" content="d e and f can’t.">`,
+	)
+
+	b.AssertFileContent("public/s1/p3/index.html",
+		`<meta property="og:description" content="g h and i can’t.">`,
+	)
+
+	// The markdown is intentionally not rendered to HTML.
+	b.AssertFileContent("public/s1/p4/index.html",
+		`<meta property="og:description" content="j k and **l** can&#39;t.">`,
+	)
+
+	// The markdown is intentionally not rendered to HTML.
+	b.AssertFileContent("public/s1/p5/index.html",
+		`<meta property="og:description" content="m n and **o** can&#39;t.">`,
+	)
+}