From 74ab839ccb397ea9df1c96d71c5347f49f2b231a Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Thu, 25 Apr 2024 08:18:40 -0700 Subject: [PATCH] tpl/tplimpl: Plainify title and description in twitter_cards.html Closes #12433 Improves #10900 --- .../embedded/templates/twitter_cards.html | 24 +++--- tpl/tplimpl/tplimpl_integration_test.go | 77 +++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/tpl/tplimpl/embedded/templates/twitter_cards.html b/tpl/tplimpl/embedded/templates/twitter_cards.html index 14c92274b..6f156c7a7 100644 --- a/tpl/tplimpl/embedded/templates/twitter_cards.html +++ b/tpl/tplimpl/embedded/templates/twitter_cards.html @@ -1,12 +1,18 @@ -{{- $images := partial "_funcs/get-page-images" . -}} -{{- with index $images 0 -}} - - -{{- else -}} - -{{- end -}} - - +{{- $images := partial "_funcs/get-page-images" . }} +{{- with index $images 0 }} + + +{{- else }} + +{{- end }} + +{{- with or .Title site.Title site.Params.title | plainify }} + +{{- end }} + +{{- with or .Description .Summary site.Params.description | plainify | htmlUnescape | chomp }} + +{{- end }} {{- $twitterSite := "" }} {{- with site.Params.social }} diff --git a/tpl/tplimpl/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go index b30965be9..41257912d 100644 --- a/tpl/tplimpl/tplimpl_integration_test.go +++ b/tpl/tplimpl/tplimpl_integration_test.go @@ -493,3 +493,80 @@ title: p5 ``, ) } + +// 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 n 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 b and **c** can't. +-- content/s1/p2.md -- +--- +title: p2 +--- +d e and **f** can't. + +-- content/s1/p3.md -- +--- +title: p3 +summary: g h and **i** can't. +--- +-- content/s1/p4.md -- +--- +title: p4 +description: j k and **l** can't. +--- +-- content/s1/p5.md -- +--- +title: p5 +--- +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/s1/p1/index.html", ` + + + + + + `, + ) + + b.AssertFileContent("public/s1/p2/index.html", + ``, + ``, + ) + + b.AssertFileContent("public/s1/p3/index.html", + ``, + ) + + // The markdown is intentionally not rendered to HTML. + b.AssertFileContent("public/s1/p4/index.html", + ``, + ) + + // The markdown is intentionally not rendered to HTML. + b.AssertFileContent("public/s1/p5/index.html", + ``, + ) +}