Skip to content

Commit

Permalink
fix: match hugo's (bizzare) handling of short markdown strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Feb 2, 2022
1 parent b13997d commit f14261f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
package helpers

import (
"bytes"
"html/template"
"strings"
"unicode"

bp "github.com/gohugoio/hugo/bufferpool"
)

var (
openingPTag = []byte("<p>")
closingPTag = []byte("</p>")
paragraphIndicator = []byte("<p")
closingIndicator = []byte("</")
)

var stripHTMLReplacer = strings.NewReplacer("\n", " ", "</p>", "\n", "<br>", "\n", "<br />", "\n")

// StripHTML accepts a string, strips out all HTML tags and returns it.
Expand Down Expand Up @@ -68,3 +76,23 @@ func StripHTML(s string) string {
func BytesToHTML(b []byte) template.HTML {
return template.HTML(string(b))
}

// TrimShortHTML removes the <p>/</p> tags from HTML input in the situation
// where said tags are the only <p> 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<div><code>{{ .md | markdownify }}</code></div>
<div>{{ .md | markdownify }}</div>
"""
Given [front_matter]:
"""
md: title
multi_md: title
"""
And a site/content/_index.md file containing:
"""
Expand All @@ -36,15 +37,25 @@ Feature: Hugo Bookshop CloudCannon Live Editing Filters and Functions
<body>
{{ 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 )) }}
</body>
</html>
"""
And 🌐 I have loaded my site in CloudCannon
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 "<p><strong>bold</strong> title</p>"
# Hugo strips single <p> tags
* 🌐 The selector div:nth-of-type(1) should match "<strong>bold</strong> title"
* 🌐 The selector div:nth-of-type(2) should match "<h1>Hello</h1>"
* 🌐 The selector div:nth-of-type(2) should match "<p>World</p>"

0 comments on commit f14261f

Please sign in to comment.