From 6049ba99f006389c444aaecee57ae88c1f21abfa Mon Sep 17 00:00:00 2001
From: Joe Mooring ")
- closingPTag = []byte(" / tags in the input and enclose the content
-// of the input (whitespace excluded).
-func (c *ContentSpec) TrimShortHTML(input []byte) []byte {
- if bytes.Count(input, openingPTag) == 1 {
+// TrimShortHTML removes the outer tags from HTML input where (a) the opening
+// tag is present only once with the input, and (b) the opening and closing
+// tags wrap the input after white space removal.
+func (c *ContentSpec) TrimShortHTML(input []byte, markup string) []byte {
+ openingTag := []byte(" ")
+ closingTag := []byte(" ")
+ closingTag = []byte(" Simple paragraph \t Whitespace\nHTML \n\t Multiple paragraphs Multiple paragraphs Nested paragraphs
Nested
paragraphs
")}, - {[]byte("Hello
\nHello
\nc
"), []byte("c
")}, + {"markdown", []byte(""), []byte("")}, + {"markdown", []byte("Plain text"), []byte("Plain text")}, + {"markdown", []byte("Simple paragraph
"), []byte("Simple paragraph")}, + {"markdown", []byte("\n \n \t\t Whitespace\nHTML \n\t
\n\t"), []byte("Whitespace\nHTML")}, + {"markdown", []byte("Multiple
paragraphs
"), []byte("Multiple
paragraphs
")}, + {"markdown", []byte("Nested
paragraphs
"), []byte("Nested
paragraphs
")}, + {"markdown", []byte("Hello
\nHello
\nc
"), []byte("c
")}, + // Issue 12369 + {"markdown", []byte("foo
\nfoo
\nfoo
\nSimple paragraph
")) + c.TrimShortHTML([]byte("Simple paragraph
"), "markdown") } } diff --git a/hugolib/page__content.go b/hugolib/page__content.go index 54f7be961..799fc89b6 100644 --- a/hugolib/page__content.go +++ b/hugolib/page__content.go @@ -778,7 +778,7 @@ func (c *cachedContent) contentPlain(ctx context.Context, cp *pageContentOutput) if err != nil { return nil, err } - html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes()) + html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes(), cp.po.p.m.pageConfig.Markup) result.summary = helpers.BytesToHTML(html) } else { var summary string diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go index 7220c86fe..fac719ea9 100644 --- a/hugolib/page__per_output.go +++ b/hugolib/page__per_output.go @@ -363,9 +363,11 @@ func (pco *pageContentOutput) RenderString(ctx context.Context, args ...any) (te } if opts.Display == "inline" { - // We may have to rethink this in the future when we get other - // renderers. - rendered = pco.po.p.s.ContentSpec.TrimShortHTML(rendered) + markup := pco.po.p.m.pageConfig.Markup + if opts.Markup != "" { + markup = pco.po.p.s.ContentSpec.ResolveMarkup(opts.Markup) + } + rendered = pco.po.p.s.ContentSpec.TrimShortHTML(rendered, markup) } return template.HTML(string(rendered)), nil diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go index 7054c6988..5ef9bff21 100644 --- a/tpl/transform/transform.go +++ b/tpl/transform/transform.go @@ -167,7 +167,7 @@ func (ns *Namespace) Markdownify(ctx context.Context, s any) (template.HTML, err } // Strip if this is a short inline type of text. - bb := ns.deps.ContentSpec.TrimShortHTML([]byte(ss)) + bb := ns.deps.ContentSpec.TrimShortHTML([]byte(ss), "markdown") return helpers.BytesToHTML(bb), nil }