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 onlytags 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 }}