]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/tplimpl: Plainify title and description in twitter_cards.html
authorJoe Mooring <joe.mooring@veriphor.com>
Thu, 25 Apr 2024 15:18:40 +0000 (08:18 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 14 May 2024 12:45:04 +0000 (14:45 +0200)
Closes #12433
Improves #10900

tpl/tplimpl/embedded/templates/twitter_cards.html
tpl/tplimpl/tplimpl_integration_test.go

index 14c92274bf13cf6dc9b6f753a214bf304ec6db84..6f156c7a7775ecd636a88ecee2405abd30af6034 100644 (file)
@@ -1,12 +1,18 @@
-{{- $images := partial "_funcs/get-page-images" . -}}
-{{- with index $images 0 -}}
-<meta name="twitter:card" content="summary_large_image">
-<meta name="twitter:image" content="{{ .Permalink }}">
-{{- else -}}
-<meta name="twitter:card" content="summary">
-{{- end -}}
-<meta name="twitter:title" content="{{ .Title }}">
-<meta name="twitter:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end -}}">
+{{- $images := partial "_funcs/get-page-images" . }}
+{{- with index $images 0 }}
+  <meta name="twitter:card" content="summary_large_image">
+  <meta name="twitter:image" content="{{ .Permalink }}">
+{{- else }}
+  <meta name="twitter:card" content="summary">
+{{- end }}
+
+{{- with or .Title site.Title site.Params.title | plainify }}
+  <meta name="twitter:title" content="{{ . }}">
+{{- end }}
+
+{{- with or .Description .Summary site.Params.description | plainify | htmlUnescape | chomp }}
+  <meta name="twitter:description" content="{{ . }}">
+{{- end }}
 
 {{- $twitterSite := "" }}
 {{- with site.Params.social }}
index b30965be938fa175aa4213196dd67398172c4de1..41257912d20ad20110619e342cc0c0b283367b2e 100644 (file)
@@ -493,3 +493,80 @@ title: p5
                `<meta itemprop="description" content="m n and **o** can&#39;t.">`,
        )
 }
+
+// Issue 12433
+func TestTwitterCards(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+capitalizeListTitles = false
+disableKinds = ['rss','sitemap','taxonomy','term']
+[markup.goldmark.renderer]
+unsafe = true
+[params]
+description = "m <em>n</em> and **o** can't."
+[params.social]
+twitter = 'foo'
+-- layouts/_default/list.html --
+{{ template "_internal/twitter_cards.html" . }}
+-- layouts/_default/single.html --
+{{ template "_internal/twitter_cards.html" . }}
+-- content/s1/p1.md --
+---
+title: p1
+images: [a.jpg,b.jpg]
+---
+a <em>b</em> and **c** can't.
+-- content/s1/p2.md --
+---
+title: p2
+---
+d <em>e</em> and **f** can't.
+<!--more-->
+-- content/s1/p3.md --
+---
+title: p3
+summary: g <em>h</em> and **i** can't.
+---
+-- content/s1/p4.md --
+---
+title: p4
+description: j <em>k</em> and **l** can't.
+---
+-- content/s1/p5.md --
+---
+title: p5
+---
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/s1/p1/index.html", `
+               <meta name="twitter:card" content="summary_large_image">
+               <meta name="twitter:image" content="/a.jpg">
+               <meta name="twitter:title" content="p1">
+               <meta name="twitter:description" content="a b and c can’t.">
+               <meta name="twitter:site" content="@foo">
+               `,
+       )
+
+       b.AssertFileContent("public/s1/p2/index.html",
+               `<meta name="twitter:card" content="summary">`,
+               `<meta name="twitter:description" content="d e and f can’t.">`,
+       )
+
+       b.AssertFileContent("public/s1/p3/index.html",
+               `<meta name="twitter:description" content="g h and i can’t.">`,
+       )
+
+       // The markdown is intentionally not rendered to HTML.
+       b.AssertFileContent("public/s1/p4/index.html",
+               `<meta name="twitter: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 name="twitter:description" content="m n and **o** can&#39;t.">`,
+       )
+}