From 812688fc2f3e220ac35cad9f0445a2548f0cc603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 14 Nov 2019 09:57:37 +0100 Subject: [PATCH] hugolib: Fix emoji handling inside shortcodes Fixes #6504 --- hugolib/shortcode.go | 11 +++++++++++ hugolib/shortcode_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 700ac5bd8..5e916aeec 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -18,6 +18,8 @@ import ( "fmt" "strconv" + "github.com/gohugoio/hugo/helpers" + "html/template" "path" @@ -517,6 +519,15 @@ Loop: return sc, nil case currItem.IsText(): sc.inner = append(sc.inner, currItem.ValStr()) + case currItem.Type == pageparser.TypeEmoji: + // TODO(bep) avoid the duplication of these "text cases", to prevent + // more of #6504 in the future. + val := currItem.ValStr() + if emoji := helpers.Emoji(val); emoji != nil { + sc.inner = append(sc.inner, string(emoji)) + } else { + sc.inner = append(sc.inner, val) + } case currItem.IsShortcodeName(): sc.name = currItem.ValStr() diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 01fa61512..eb763b2a0 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -1171,6 +1171,33 @@ title: "Hugo Rocks!" ) } +// https://github.com/gohugoio/hugo/issues/6504 +func TestShortcodeEmoji(t *testing.T) { + t.Parallel() + + v := viper.New() + v.Set("enableEmoji", true) + + builder := newTestSitesBuilder(t).WithViper(v) + + builder.WithContent("page.md", `--- +title: "Hugo Rocks!" +--- + +# doc + +{{< event >}}10:30-11:00 My :smile: Event {{< /event >}} + + +`).WithTemplatesAdded( + "layouts/shortcodes/event.html", `
{{ "\u29BE" }} {{ .Inner }}
`) + + builder.Build(BuildCfg{}) + builder.AssertFileContent("public/page/index.html", + "⦾ 10:30-11:00 My 😄 Event", + ) +} + func TestShortcodeTypedParams(t *testing.T) { t.Parallel() c := qt.New(t)