From f14261f6ff0ae8db70f543ba2230633d0f95d3da Mon Sep 17 00:00:00 2001 From: Liam Bigelow Date: Thu, 3 Feb 2022 09:59:11 +1300 Subject: [PATCH] fix: match hugo's (bizzare) handling of short markdown strings --- .../helpers/bookshop_modified_content.go | 28 +++++++++++++++++++ .../hugo-renderer/tpl/transform/transform.go | 3 ++ .../hugo_bookshop_live_filters.feature | 15 ++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/javascript-modules/engines/hugo-engine/hugo-renderer/helpers/bookshop_modified_content.go b/javascript-modules/engines/hugo-engine/hugo-renderer/helpers/bookshop_modified_content.go index 2dcc5035..c38c8560 100644 --- a/javascript-modules/engines/hugo-engine/hugo-renderer/helpers/bookshop_modified_content.go +++ b/javascript-modules/engines/hugo-engine/hugo-renderer/helpers/bookshop_modified_content.go @@ -18,6 +18,7 @@ package helpers import ( + "bytes" "html/template" "strings" "unicode" @@ -25,6 +26,13 @@ import ( bp "github.com/gohugoio/hugo/bufferpool" ) +var ( + openingPTag = []byte("

") + closingPTag = []byte("

") + paragraphIndicator = []byte("", "\n", "
", "\n", "
", "\n") // StripHTML accepts a string, strips out all HTML tags and returns it. @@ -68,3 +76,23 @@ func StripHTML(s string) string { func BytesToHTML(b []byte) template.HTML { return template.HTML(string(b)) } + +// TrimShortHTML removes the

/

tags from HTML input in the situation +// where said tags are the only

tags in the input and enclose the content +// of the input (whitespace excluded). +// Bookshop: Changed this to _not_ be a method of *ContentSpec +func TrimShortHTML(input []byte) []byte { + firstOpeningP := bytes.Index(input, paragraphIndicator) + lastOpeningP := bytes.LastIndex(input, paragraphIndicator) + + lastClosingP := bytes.LastIndex(input, closingPTag) + lastClosing := bytes.LastIndex(input, closingIndicator) + + if firstOpeningP == lastOpeningP && lastClosingP == lastClosing { + input = bytes.TrimSpace(input) + input = bytes.TrimPrefix(input, openingPTag) + input = bytes.TrimSuffix(input, closingPTag) + input = bytes.TrimSpace(input) + } + return input +} diff --git a/javascript-modules/engines/hugo-engine/hugo-renderer/tpl/transform/transform.go b/javascript-modules/engines/hugo-engine/hugo-renderer/tpl/transform/transform.go index 548f4a24..f46d6b10 100644 --- a/javascript-modules/engines/hugo-engine/hugo-renderer/tpl/transform/transform.go +++ b/javascript-modules/engines/hugo-engine/hugo-renderer/tpl/transform/transform.go @@ -99,6 +99,9 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { // This includes using a different (smaller) markdown implementation. output := markdown.ToHTML([]byte(ss), nil, nil) + // Strip if this is a short inline type of text. + output = helpers.TrimShortHTML(output) + return helpers.BytesToHTML(output), nil } diff --git a/javascript-modules/integration-tests/features/hugo/live_editing/hugo_bookshop_live_filters.feature b/javascript-modules/integration-tests/features/hugo/live_editing/hugo_bookshop_live_filters.feature index 6561e4cf..82497954 100644 --- a/javascript-modules/integration-tests/features/hugo/live_editing/hugo_bookshop_live_filters.feature +++ b/javascript-modules/integration-tests/features/hugo/live_editing/hugo_bookshop_live_filters.feature @@ -18,11 +18,12 @@ Feature: Hugo Bookshop CloudCannon Live Editing Filters and Functions Scenario: Bookshop live renders markdown Given a component-lib/components/beetroot/beetroot.hugo.html file containing: """ -

{{ .md | markdownify }}
+
{{ .md | markdownify }}
""" Given [front_matter]: """ md: title + multi_md: title """ And a site/content/_index.md file containing: """ @@ -36,6 +37,9 @@ Feature: Hugo Bookshop CloudCannon Live Editing Filters and Functions {{ partial "bookshop_bindings" `(dict "md" .Params.md )` }} {{ partial "bookshop" (slice "beetroot" (dict "md" .Params.md )) }} + + {{ partial "bookshop_bindings" `(dict "md" .Params.multi_md )` }} + {{ partial "bookshop" (slice "beetroot" (dict "md" .Params.multi_md )) }} """ @@ -43,8 +47,15 @@ Feature: Hugo Bookshop CloudCannon Live Editing Filters and Functions When 🌐 CloudCannon pushes new yaml: """ md: "**bold** title" + multi_md: >- + # Hello + + World """ Then 🌐 There should be no errors * 🌐 There should be no logs - * 🌐 The selector code should match "

bold title

" + # Hugo strips single

tags + * 🌐 The selector div:nth-of-type(1) should match "bold title" + * 🌐 The selector div:nth-of-type(2) should match "

Hello

" + * 🌐 The selector div:nth-of-type(2) should match "

World

" \ No newline at end of file