Skip to content

Commit e6de73e

Browse files
authored
upgrade to goldmark v1.7.8, handle Node.Text deprecation (#163)
1 parent 95bb67f commit e6de73e

File tree

8 files changed

+42
-13
lines changed

8 files changed

+42
-13
lines changed

collect.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (c *collector) collectEmbedItem(item *stitch.EmbedItem, cursor tree.Cursor[
380380
// Ignore the heading level in the summary file.
381381
// It'll get whatever the depth of the embed is.
382382
h.Level = 1
383-
id, _ := c.idGen.GenerateID(string(h.Text(summaryFile.Source)))
383+
id, _ := c.idGen.GenerateID(string(goldast.Text(summaryFile.Source, h)))
384384
heading = &markdownHeading{
385385
AST: h,
386386
ID: id,
@@ -425,7 +425,7 @@ type markdownHeading struct {
425425
}
426426

427427
func (c *collector) newHeading(f *goldast.File, fgen *header.IDGen, h *ast.Heading) *markdownHeading {
428-
text := string(h.Text(f.Source))
428+
text := string(goldast.Text(f.Source, h))
429429
id, _ := c.idGen.GenerateID(text)
430430
oldID, _ := fgen.GenerateID(text)
431431
h.SetAttributeString("id", []byte(id)) // needed for toc.Inspect

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/mattn/go-isatty v0.0.20
1010
github.com/pkg/diff v0.0.0-20241224192749-4e6772a4315c
1111
github.com/stretchr/testify v1.9.0
12-
github.com/yuin/goldmark v1.7.7
12+
github.com/yuin/goldmark v1.7.8
1313
go.abhg.dev/container/ring v0.3.0
1414
go.abhg.dev/goldmark/frontmatter v0.2.0
1515
go.abhg.dev/goldmark/toc v0.10.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
2222
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
2323
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2424
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
25-
github.com/yuin/goldmark v1.7.7 h1:5m9rrB1sW3JUMToKFQfb+FGt1U7r57IHu5GrYrG2nqU=
26-
github.com/yuin/goldmark v1.7.7/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
25+
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
26+
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
2727
go.abhg.dev/container/ring v0.3.0 h1:sEYgxqyAsT2X1NkUEPQecR4WY7cZCbKZSGUsERhHyDI=
2828
go.abhg.dev/container/ring v0.3.0/go.mod h1:GoB+vof3ofHqP2h2S97aJkmrMLK/Dyn7UEupDTgfWV8=
2929
go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw=

internal/goldast/text.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package goldast
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
"github.com/yuin/goldmark/ast"
8+
)
9+
10+
// Text returns the text for the [ast.String] and [ast.Text] nodes
11+
// in the tree of the given goldmark AST node.
12+
func Text(src []byte, n ast.Node) []byte {
13+
var buf bytes.Buffer
14+
writeNodeText(src, &buf, n)
15+
return buf.Bytes()
16+
}
17+
18+
func writeNodeText(src []byte, dst io.Writer, n ast.Node) {
19+
switch n := n.(type) {
20+
case *ast.Text:
21+
_, _ = dst.Write(n.Segment.Value(src))
22+
case *ast.String:
23+
_, _ = dst.Write(n.Value)
24+
default:
25+
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
26+
writeNodeText(src, dst, c)
27+
}
28+
}
29+
}

internal/goldast/walk_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ func TestWalk(t *testing.T) {
3131
}
3232
if n := nodes[1]; assert.IsType(t, new(ast.Heading), n) {
3333
assert.Equal(t, "foo.md:2:3", posc.Position(OffsetOf(n)).String(), "heading")
34-
assert.Equal(t, "Foo", string(n.Text(f.Source)))
34+
assert.Equal(t, "Foo", string(Text(f.Source, n)))
3535
}
3636
if n := nodes[2]; assert.IsType(t, new(ast.Text), n) {
3737
assert.Equal(t, "foo.md:2:3", posc.Position(OffsetOf(n)).String(), "heading text")
38-
assert.Equal(t, "Foo", string(n.Text(f.Source)))
38+
assert.Equal(t, "Foo", string(Text(f.Source, n)))
3939
}
4040
if n := nodes[3]; assert.IsType(t, new(ast.Paragraph), n) {
4141
assert.Equal(t, "foo.md:3:1", posc.Position(OffsetOf(n)).String(), "paragraph")
4242
}
4343
if n := nodes[4]; assert.IsType(t, new(ast.Text), n) {
4444
assert.Equal(t, "foo.md:3:1", posc.Position(OffsetOf(n)).String(), "paragraph text")
45-
assert.Equal(t, "hello world.", string(n.Text(f.Source)))
45+
assert.Equal(t, "hello world.", string(Text(f.Source, n)))
4646
}
4747
}
4848

internal/stitch/item.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type LinkItem struct {
152152

153153
func (p *itemTreeParser) parseLinkItem(link *ast.Link) *LinkItem {
154154
return &LinkItem{
155-
Text: string(link.Text(p.src)),
155+
Text: string(goldast.Text(p.src, link)),
156156
Target: filepath.ToSlash(string(link.Destination)),
157157
Depth: p.depth,
158158
AST: link,
@@ -197,7 +197,7 @@ var _ Item = (*EmbedItem)(nil)
197197

198198
func (p *itemTreeParser) parseEmbedItem(embed *ast.Image) *EmbedItem {
199199
return &EmbedItem{
200-
Text: string(embed.Text(p.src)),
200+
Text: string(goldast.Text(p.src, embed)),
201201
Target: filepath.ToSlash(string(embed.Destination)),
202202
Depth: p.depth,
203203
AST: embed,
@@ -239,7 +239,7 @@ func (p *itemTreeParser) parseTextItem(text *ast.Text, hasChildren bool) *TextIt
239239
}
240240

241241
return &TextItem{
242-
Text: string(text.Text(p.src)),
242+
Text: string(goldast.Text(p.src, text)),
243243
Depth: p.depth,
244244
AST: text,
245245
}

internal/stitch/summary.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (p *summaryParser) parseSectionTitle(n ast.Node) (*SectionTitle, ast.Node)
132132
}
133133

134134
return &SectionTitle{
135-
Text: string(h.Text(p.src)),
135+
Text: string(goldast.Text(p.src, h)),
136136
Level: h.Level,
137137
AST: h,
138138
}, n.NextSibling()

transform.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func (t *transformer) transformHeading(src []byte, item stitch.Item, h *markdown
399399
para.AppendChild(para, link)
400400

401401
bold := ast.NewEmphasis(2)
402-
bold.AppendChild(bold, ast.NewString(h.AST.Text(src)))
402+
bold.AppendChild(bold, ast.NewString(goldast.Text(src, h.AST)))
403403
para.AppendChild(para, bold)
404404

405405
if parent := h.AST.Parent(); parent != nil {

0 commit comments

Comments
 (0)