Skip to content

Commit 85c1727

Browse files
committed
markup/goldmark: Fix panic on stray attribute nodes
1 parent 641403f commit 85c1727

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

markup/goldmark/internal/extensions/attributes/attributes.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ func (a *transformer) isFragmentNode(n ast.Node) bool {
117117

118118
func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
119119
var attributes []ast.Node
120+
var solitaryAttributeNodes []ast.Node
120121
if a.cfg.Attribute.Block {
121-
attributes = make([]ast.Node, 0, 500)
122+
attributes = make([]ast.Node, 0, 100)
122123
}
123124
ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
124125
if !entering {
@@ -141,8 +142,7 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
141142
attributes = append(attributes, node)
142143
return ast.WalkSkipChildren, nil
143144
} else {
144-
// remove attributes node
145-
node.Parent().RemoveChild(node.Parent(), node)
145+
solitaryAttributeNodes = append(solitaryAttributeNodes, node)
146146
}
147147
}
148148

@@ -161,6 +161,11 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
161161
// remove attributes node
162162
attr.Parent().RemoveChild(attr.Parent(), attr)
163163
}
164+
165+
// Remove any solitary attribute nodes.
166+
for _, n := range solitaryAttributeNodes {
167+
n.Parent().RemoveChild(n.Parent(), n)
168+
}
164169
}
165170

166171
func (a *transformer) generateAutoID(n ast.Node, reader text.Reader, pc parser.Context) {

markup/goldmark/internal/extensions/attributes/attributes_integration_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,36 @@ Second line
7575
"|Identifiers: [a-a-a-a-a-a-c-c-c-c-c-c-c-c-d base-name base-name-1 example-hyperlink-in-a-header foo-something-bar foobar my-title my-title-1 second-line term title-with-id title-with-id]|",
7676
)
7777
}
78+
79+
func TestSolitaryAttributesCrash(t *testing.T) {
80+
t.Parallel()
81+
82+
files := `
83+
-- hugo.toml --
84+
[markup.goldmark.parser.attribute]
85+
block = true
86+
-- layouts/_default/single.html --
87+
Content: {{ .Content }}
88+
-- content/p1.md --
89+
---
90+
title: "Title"
91+
---
92+
93+
1. a
94+
95+
{.x}
96+
97+
1. b
98+
99+
{.x}
100+
101+
102+
103+
`
104+
105+
b := hugolib.Test(t, files)
106+
107+
b.AssertFileContent("public/p1/index.html",
108+
` <li>a</li>`,
109+
)
110+
}

0 commit comments

Comments
 (0)