Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions internal/mdplain/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ func (r *TextRender) Render(w io.Writer, source []byte, n ast.Node) error {
// we want to write the text of the
// link before the url
child := node.FirstChild()
if child != nil {
t, ok := child.(*ast.Text)
if ok {
out.Write(t.Value(source))
}
err := r.renderText(source, out, child)
if err != nil {
return ast.WalkStop, err
}

if !isRelativeLink(node.Destination) {
Expand Down Expand Up @@ -91,6 +89,32 @@ func (r *TextRender) Render(w io.Writer, source []byte, n ast.Node) error {
return nil
}

// renderText writes any encountered ast.Text node value to the buffer,
// ignoring all other node types. This is intended to be a rough replacement
// of the deprecated (*BaseNode).Text() method in the yuin/goldmark library.
func (r *TextRender) renderText(source []byte, out *bytes.Buffer, n ast.Node) error {
err := ast.Walk(n, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering || node.Type() == ast.TypeDocument {
return ast.WalkContinue, nil
}

switch node := node.(type) {
case *ast.Text:
out.Write(node.Value(source))
if node.SoftLineBreak() {
doubleSpace(out)
}
return ast.WalkContinue, nil

}
return ast.WalkContinue, nil
})
if err != nil {
return err
}
return nil
}

func (r *TextRender) AddOptions(...renderer.Option) {}

func doubleSpace(out *bytes.Buffer) {
Expand Down
2 changes: 2 additions & 0 deletions internal/mdplain/testdata/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ These are the elements outlined in John Gruber’s original design document. All

Plain URL: https://www.markdownguide.org

[`Code Link`](https://www.markdownguide.org)

Copy link
Member

@austinvalle austinvalle Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another change in behavior that at first glance seems unrelated to this fix, I only noticed it because of the random provider which now the only change showing is this:
image

You can actually recreate this behavior pretty easily in this test by adding:

Suggested change
[Link_With_Underscores](https://www.markdownguide.org)

The expected output would be:

Link_With_Underscores https://www.markdownguide.org

But after upgrading goldmark, we get:

Link_ https://www.markdownguide.org

Looking a little deeper it seems like the underscore is causing the link text node to get split up, which may be a bug in Goldmark? Perhaps it should be treating all link text inside those code braces as literal, maybe it thinks the link text is italicized 🤔

The workaround is pretty simple (which is probably what we would do with the random provider), you can just add backticks to the link text:

[`Link_With_Underscores`](https://www.markdownguide.org)

Will produce:

Link_With_Underscores https://www.markdownguide.org

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I do think the change might be related to this PR. It looks like each text segment delineated by _ is a sibling text node and I'm only rendering the children of the link node's first child recursively. Looping through all of the link node's children and call renderText() on each child node should fix the problem 🤞🏾

### Image

![alt text](https://www.markdownguide.org/assets/images/tux.png)
Expand Down
1 change: 1 addition & 0 deletions internal/mdplain/testdata/mdplain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Link
Markdown Guide https://www.markdownguide.org
Relative Link
Plain URL: https://www.markdownguide.org
Code Link https://www.markdownguide.org
Image

Extended Syntax
Expand Down