]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix extra newline/paragraphs issue with .RenderShortcodes
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 16 Nov 2024 08:56:25 +0000 (09:56 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 16 Nov 2024 16:51:11 +0000 (17:51 +0100)
Fixes #13051

hugolib/rendershortcodes_test.go
markup/goldmark/goldmark_integration_test.go
markup/goldmark/hugocontext/hugocontext.go

index 0eebf46eb3ee268571aae5e57d7dd819547f5625..d8b51d3ed47945c570fa73211f92f9fd4a8e89c5 100644 (file)
@@ -434,16 +434,16 @@ code_p3
        b := TestRunning(t, files, TestOptWarn())
 
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-1000.</p>\n<code>code_p2</code><p>Foo.\n</p>\n<code>code_p3</code><p></p>\n<code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
+       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-1000.</p>\n<code>code_p2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
        b.EditFileReplaceAll("content/p1.md", "id-1000.", "id-100.").Build()
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>code_p2</code><p>Foo.\n</p>\n<code>code_p3</code><p></p>\n<code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
+       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>code_p2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
        b.EditFileReplaceAll("content/p2.md", "code_p2", "codep2").Build()
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.\n</p>\n<code>code_p3</code><p></p>\n<code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
+       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
        b.EditFileReplaceAll("content/p3.md", "code_p3", "code_p3_edited").Build()
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.\n</p>\n<code>code_p3_edited</code><p></p>\n<code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
+       b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.</p>\n<code>code_p3_edited</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
 }
 
 // Issue 13004.
@@ -475,8 +475,55 @@ This is some **markup**.
 `
        b := TestRunning(t, files)
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<p></p>\n<h3 id=\"p2-h1\">p2-h1</h3>\n<p>This is some <strong>markup</strong>.\n</p>\n")
+       b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<h3 id=\"p2-h1\">p2-h1</h3>\n<p>This is some <strong>markup</strong>.</p>\n")
        b.EditFileReplaceAll("content/second/p2.md", "p2-h1", "p2-h1-edited").Build()
        b.AssertNoRenderShortcodesArtifacts()
-       b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<p></p>\n<h3 id=\"p2-h1-edited\">p2-h1-edited</h3>\n<p>This is some <strong>markup</strong>.\n</p>\n")
+       b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<h3 id=\"p2-h1-edited\">p2-h1-edited</h3>\n<p>This is some <strong>markup</strong>.</p>\n")
+}
+
+// Issue 13051.
+func TestRenderShortcodesEmptyParagraph(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['section','rss','sitemap','taxonomy','term']
+-- layouts/_default/home.html --
+{{ .Content }}
+-- layouts/_default/single.html --
+{{ .Content }}
+-- layouts/shortcodes/include.html --
+ {{ with site.GetPage (.Get 0) }}
+  {{ .RenderShortcodes }}
+{{ end }}
+-- content/_index.md --
+---
+title: home
+---
+
+a
+
+{{% include "/snippet" %}}
+
+b
+
+-- content/snippet.md --
+---
+title: snippet
+build:
+  render: never
+  list: never
+---
+
+_emphasized_
+
+not emphasized
+
+`
+
+       b := Test(t, files)
+       b.AssertNoRenderShortcodesArtifacts()
+       b.AssertFileContentEquals("public/index.html",
+               "<p>a</p>\n<p><em>emphasized</em></p>\n<p>not emphasized</p>\n<p>b</p>\n",
+       )
 }
index 794f34150a433a737e4efa96dfdf1afe7bea6139..591226dc2fe23f2692f3c227b4a31ab95bc68ef2 100644 (file)
@@ -575,7 +575,7 @@ sc3_begin|{{ .Inner }}|sc3_end
                // Issue #7332
                "<span>:x:\n</span>",
                // Issue #11587
-               "<p>&#x2714;&#xfe0f;\n</p>",
+               "<p>&#x2714;&#xfe0f;</p>",
                // Should not be converted to emoji
                "sc1_begin|:smiley:|sc1_end",
                // Should be converted to emoji
index b1f149d0b8637eaec12005e91c4d4cfbacd7c8da..601014b37055ed53a035ef98c2a8d83a84cac17b 100644 (file)
@@ -242,6 +242,39 @@ func (r *hugoContextRenderer) handleHugoContext(w util.BufWriter, source []byte,
        return ast.WalkContinue, nil
 }
 
+type hugoContextTransformer struct{}
+
+var _ parser.ASTTransformer = (*hugoContextTransformer)(nil)
+
+func (a *hugoContextTransformer) Transform(n *ast.Document, reader text.Reader, pc parser.Context) {
+       ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
+               s := ast.WalkContinue
+               if !entering || n.Kind() != kindHugoContext {
+                       return s, nil
+               }
+
+               if p, ok := n.Parent().(*ast.Paragraph); ok {
+                       if p.ChildCount() == 1 {
+                               // Avoid empty paragraphs.
+                               p.Parent().ReplaceChild(p.Parent(), p, n)
+                       } else {
+                               if t, ok := n.PreviousSibling().(*ast.Text); ok {
+                                       // Remove the newline produced by the Hugo context markers.
+                                       if t.SoftLineBreak() {
+                                               if t.Segment.Len() == 0 {
+                                                       p.RemoveChild(p, t)
+                                               } else {
+                                                       t.SetSoftLineBreak(false)
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               return s, nil
+       })
+}
+
 type hugoContextExtension struct {
        logger loggers.Logger
 }
@@ -251,6 +284,7 @@ func (a *hugoContextExtension) Extend(m goldmark.Markdown) {
                parser.WithInlineParsers(
                        util.Prioritized(&hugoContextParser{}, 50),
                ),
+               parser.WithASTTransformers(util.Prioritized(&hugoContextTransformer{}, 10)),
        )
 
        m.Renderer().AddOptions(