]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix nilpointer regression with empty blockquotes
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Aug 2024 08:58:59 +0000 (10:58 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Aug 2024 09:46:25 +0000 (11:46 +0200)
Fixes #12756

markup/goldmark/blockquotes/blockquotes.go
markup/goldmark/blockquotes/blockquotes_integration_test.go

index f4c908e0ba9f2f0ad6344eb57ac4ef22dfdf19e9..2f5629d87dbd6c90d1bbb8bcc8af22c6e962d62d 100644 (file)
@@ -75,20 +75,6 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
        text := ctx.Buffer.Bytes()[pos:]
        ctx.Buffer.Truncate(pos)
 
-       // Extract a source sample to use for position information.
-       nn := n.FirstChild()
-       var start, stop int
-       for i := 0; i < nn.Lines().Len() && i < 2; i++ {
-               line := nn.Lines().At(i)
-               if i == 0 {
-                       start = line.Start
-               }
-               stop = line.Stop
-       }
-
-       // We do not mutate the source, so this is safe.
-       sourceRef := src[start:stop]
-
        ordinal := ctx.GetAndIncrementOrdinal(ast.KindBlockquote)
 
        texts := string(text)
@@ -110,6 +96,22 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
                texts = "<p>" + texts[strings.Index(texts, "\n")+1:]
        }
 
+       var sourceRef []byte
+
+       // Extract a source sample to use for position information.
+       if nn := n.FirstChild(); nn != nil {
+               var start, stop int
+               for i := 0; i < nn.Lines().Len() && i < 2; i++ {
+                       line := nn.Lines().At(i)
+                       if i == 0 {
+                               start = line.Start
+                       }
+                       stop = line.Stop
+               }
+               // We do not mutate the source, so this is safe.
+               sourceRef = src[start:stop]
+       }
+
        bqctx := &blockquoteContext{
                page:             ctx.DocumentContext().Document,
                pageInner:        r.getPageInner(ctx),
index 45c7caa0fc27fc00fa7942188735c7abd0967239..45c031098fcb449bc6bdc6fa85b2e33e0617ec4a 100644 (file)
@@ -81,3 +81,23 @@ title: "p1"
                "Blockquote Alert Page: |<p>This is a tip with attributes.</p>\n|p1|p1|",
        )
 }
+
+func TestBlockquoteEmptyIssue12756(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+-- content/p1.md --
+---
+title: "p1"
+---
+
+>
+-- layouts/_default/single.html --
+Content: {{ .Content }}
+
+`
+
+       b := hugolib.Test(t, files)
+       b.AssertFileContent("public/p1/index.html", "Content: <blockquote>\n</blockquote>\n")
+}