Skip to content

Commit

Permalink
upgrade to goldmark v1.7.8, handle Node.Text deprecation (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav authored Feb 2, 2025
1 parent 95bb67f commit e6de73e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (c *collector) collectEmbedItem(item *stitch.EmbedItem, cursor tree.Cursor[
// Ignore the heading level in the summary file.
// It'll get whatever the depth of the embed is.
h.Level = 1
id, _ := c.idGen.GenerateID(string(h.Text(summaryFile.Source)))
id, _ := c.idGen.GenerateID(string(goldast.Text(summaryFile.Source, h)))
heading = &markdownHeading{
AST: h,
ID: id,
Expand Down Expand Up @@ -425,7 +425,7 @@ type markdownHeading struct {
}

func (c *collector) newHeading(f *goldast.File, fgen *header.IDGen, h *ast.Heading) *markdownHeading {
text := string(h.Text(f.Source))
text := string(goldast.Text(f.Source, h))
id, _ := c.idGen.GenerateID(text)
oldID, _ := fgen.GenerateID(text)
h.SetAttributeString("id", []byte(id)) // needed for toc.Inspect
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/mattn/go-isatty v0.0.20
github.com/pkg/diff v0.0.0-20241224192749-4e6772a4315c
github.com/stretchr/testify v1.9.0
github.com/yuin/goldmark v1.7.7
github.com/yuin/goldmark v1.7.8
go.abhg.dev/container/ring v0.3.0
go.abhg.dev/goldmark/frontmatter v0.2.0
go.abhg.dev/goldmark/toc v0.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.7.7 h1:5m9rrB1sW3JUMToKFQfb+FGt1U7r57IHu5GrYrG2nqU=
github.com/yuin/goldmark v1.7.7/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.abhg.dev/container/ring v0.3.0 h1:sEYgxqyAsT2X1NkUEPQecR4WY7cZCbKZSGUsERhHyDI=
go.abhg.dev/container/ring v0.3.0/go.mod h1:GoB+vof3ofHqP2h2S97aJkmrMLK/Dyn7UEupDTgfWV8=
go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw=
Expand Down
29 changes: 29 additions & 0 deletions internal/goldast/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goldast

import (
"bytes"
"io"

"github.com/yuin/goldmark/ast"
)

// Text returns the text for the [ast.String] and [ast.Text] nodes
// in the tree of the given goldmark AST node.
func Text(src []byte, n ast.Node) []byte {
var buf bytes.Buffer
writeNodeText(src, &buf, n)
return buf.Bytes()
}

func writeNodeText(src []byte, dst io.Writer, n ast.Node) {
switch n := n.(type) {
case *ast.Text:
_, _ = dst.Write(n.Segment.Value(src))
case *ast.String:
_, _ = dst.Write(n.Value)
default:
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
writeNodeText(src, dst, c)
}
}
}
6 changes: 3 additions & 3 deletions internal/goldast/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func TestWalk(t *testing.T) {
}
if n := nodes[1]; assert.IsType(t, new(ast.Heading), n) {
assert.Equal(t, "foo.md:2:3", posc.Position(OffsetOf(n)).String(), "heading")
assert.Equal(t, "Foo", string(n.Text(f.Source)))
assert.Equal(t, "Foo", string(Text(f.Source, n)))
}
if n := nodes[2]; assert.IsType(t, new(ast.Text), n) {
assert.Equal(t, "foo.md:2:3", posc.Position(OffsetOf(n)).String(), "heading text")
assert.Equal(t, "Foo", string(n.Text(f.Source)))
assert.Equal(t, "Foo", string(Text(f.Source, n)))
}
if n := nodes[3]; assert.IsType(t, new(ast.Paragraph), n) {
assert.Equal(t, "foo.md:3:1", posc.Position(OffsetOf(n)).String(), "paragraph")
}
if n := nodes[4]; assert.IsType(t, new(ast.Text), n) {
assert.Equal(t, "foo.md:3:1", posc.Position(OffsetOf(n)).String(), "paragraph text")
assert.Equal(t, "hello world.", string(n.Text(f.Source)))
assert.Equal(t, "hello world.", string(Text(f.Source, n)))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/stitch/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type LinkItem struct {

func (p *itemTreeParser) parseLinkItem(link *ast.Link) *LinkItem {
return &LinkItem{
Text: string(link.Text(p.src)),
Text: string(goldast.Text(p.src, link)),
Target: filepath.ToSlash(string(link.Destination)),
Depth: p.depth,
AST: link,
Expand Down Expand Up @@ -197,7 +197,7 @@ var _ Item = (*EmbedItem)(nil)

func (p *itemTreeParser) parseEmbedItem(embed *ast.Image) *EmbedItem {
return &EmbedItem{
Text: string(embed.Text(p.src)),
Text: string(goldast.Text(p.src, embed)),
Target: filepath.ToSlash(string(embed.Destination)),
Depth: p.depth,
AST: embed,
Expand Down Expand Up @@ -239,7 +239,7 @@ func (p *itemTreeParser) parseTextItem(text *ast.Text, hasChildren bool) *TextIt
}

return &TextItem{
Text: string(text.Text(p.src)),
Text: string(goldast.Text(p.src, text)),
Depth: p.depth,
AST: text,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/stitch/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p *summaryParser) parseSectionTitle(n ast.Node) (*SectionTitle, ast.Node)
}

return &SectionTitle{
Text: string(h.Text(p.src)),
Text: string(goldast.Text(p.src, h)),
Level: h.Level,
AST: h,
}, n.NextSibling()
Expand Down
2 changes: 1 addition & 1 deletion transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (t *transformer) transformHeading(src []byte, item stitch.Item, h *markdown
para.AppendChild(para, link)

bold := ast.NewEmphasis(2)
bold.AppendChild(bold, ast.NewString(h.AST.Text(src)))
bold.AppendChild(bold, ast.NewString(goldast.Text(src, h.AST)))
para.AppendChild(para, bold)

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

0 comments on commit e6de73e

Please sign in to comment.