diff options
author | Joe Mooring <[email protected]> | 2023-11-11 21:27:44 -0800 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-11-16 18:21:01 +0100 |
commit | 0bde6931ac8fb288565b6f951f6e10adf529d527 (patch) | |
tree | 3c207efb5d60b13681c79cf573ed055b74ada9af /helpers | |
parent | ac7cffa7e2932fc3c6bd425f86b981dfdef94968 (diff) | |
download | hugo-0bde6931ac8fb288565b6f951f6e10adf529d527.tar.gz hugo-0bde6931ac8fb288565b6f951f6e10adf529d527.zip |
helpers: Fix TrimShortHTML used by markdownify and RenderString
Closes #11698
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/content.go | 17 | ||||
-rw-r--r-- | helpers/content_test.go | 5 |
2 files changed, 11 insertions, 11 deletions
diff --git a/helpers/content.go b/helpers/content.go index 510d496b9..c0a6d8221 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -251,18 +251,15 @@ func (c *ContentSpec) TruncateWordsToWholeSentence(s string) (string, bool) { // where said tags are the only <p> tags in the input and enclose the content // of the input (whitespace excluded). func (c *ContentSpec) 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) + if bytes.Count(input, openingPTag) == 1 { input = bytes.TrimSpace(input) + if bytes.HasPrefix(input, openingPTag) && bytes.HasSuffix(input, closingPTag) { + input = bytes.TrimPrefix(input, openingPTag) + input = bytes.TrimSuffix(input, closingPTag) + input = bytes.TrimSpace(input) + } } + return input } diff --git a/helpers/content_test.go b/helpers/content_test.go index 2909c0266..72e3eeb49 100644 --- a/helpers/content_test.go +++ b/helpers/content_test.go @@ -32,12 +32,15 @@ func TestTrimShortHTML(t *testing.T) { }{ {[]byte(""), []byte("")}, {[]byte("Plain text"), []byte("Plain text")}, - {[]byte(" \t\n Whitespace text\n\n"), []byte("Whitespace text")}, + // This seems wrong. Why touch it if it doesn't have p tag? + // {[]byte(" \t\n Whitespace text\n\n"), []byte("Whitespace text")}, {[]byte("<p>Simple paragraph</p>"), []byte("Simple paragraph")}, {[]byte("\n \n \t <p> \t Whitespace\nHTML \n\t </p>\n\t"), []byte("Whitespace\nHTML")}, {[]byte("<p>Multiple</p><p>paragraphs</p>"), []byte("<p>Multiple</p><p>paragraphs</p>")}, {[]byte("<p>Nested<p>paragraphs</p></p>"), []byte("<p>Nested<p>paragraphs</p></p>")}, {[]byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>"), []byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>")}, + // Issue #11698 + {[]byte("<h2 id=`a`>b</h2>\n\n<p>c</p>"), []byte("<h2 id=`a`>b</h2>\n\n<p>c</p>")}, } c := newTestContentSpec(nil) |