Skip to content

Commit

Permalink
markup/goldmark: Fix attribute nilpointer
Browse files Browse the repository at this point in the history
Fixes 9819
  • Loading branch information
bep committed Apr 27, 2022
1 parent 13ceef7 commit d7b54a4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
48 changes: 48 additions & 0 deletions markup/goldmark/codeblocks/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,51 @@ Attributes: {{ .Attributes }}|Options: {{ .Options }}|
testLanguage("bash", "Attributes: map[]|Options: map[style:monokai]|")
testLanguage("hugo", "Attributes: map[style:monokai]|Options: map[]|")
}

func TestPanics(t *testing.T) {

files := `
-- config.toml --
[markup]
[markup.goldmark]
[markup.goldmark.parser]
autoHeadingID = true
autoHeadingIDType = "github"
[markup.goldmark.parser.attribute]
block = true
title = true
-- content/p1.md --
---
title: "p1"
---
BLOCK
Common
-- layouts/_default/single.html --
{{ .Content }}
`

for _, test := range []struct {
name string
markdown string
}{
{"issue-9819", "asdf\n: {#myid}"},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: strings.ReplaceAll(files, "BLOCK", test.markdown),
},
).Build()

b.AssertFileContent("public/p1/index.html", "Common")
})
}

}
18 changes: 0 additions & 18 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@ package goldmark

import (
"bytes"
"fmt"
"path/filepath"
"runtime/debug"

"github.com/gohugoio/hugo/markup/goldmark/codeblocks"
"github.com/gohugoio/hugo/markup/goldmark/internal/extensions/attributes"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"

"github.com/gohugoio/hugo/identity"

"github.com/pkg/errors"

"github.com/spf13/afero"

"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark"
Expand Down Expand Up @@ -178,16 +170,6 @@ func (c converterResult) GetIdentities() identity.Identities {
var converterIdentity = identity.KeyValueIdentity{Key: "goldmark", Value: "converter"}

func (c *goldmarkConverter) Convert(ctx converter.RenderContext) (result converter.Result, err error) {
defer func() {
if r := recover(); r != nil {
dir := afero.GetTempDir(hugofs.Os, "hugo_bugs")
name := fmt.Sprintf("goldmark_%s.txt", c.ctx.DocumentID)
filename := filepath.Join(dir, name)
afero.WriteFile(hugofs.Os, filename, ctx.Src, 07555)
fmt.Print(string(debug.Stack()))
err = errors.Errorf("[BUG] goldmark: %s: create an issue on GitHub attaching the file in: %s", r, filename)
}
}()

buf := &render.BufWriter{Buffer: &bytes.Buffer{}}
result = buf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
// Attributes for fenced code blocks are handled in their own extension,
// but note that we currently only support code block attributes when
// CodeFences=true.
if node.PreviousSibling().Kind() != ast.KindFencedCodeBlock && !node.HasBlankPreviousLines() {
if node.PreviousSibling() != nil && node.PreviousSibling().Kind() != ast.KindFencedCodeBlock && !node.HasBlankPreviousLines() {
attributes = append(attributes, node)
return ast.WalkSkipChildren, nil
}
Expand Down

0 comments on commit d7b54a4

Please sign in to comment.