yuin / goldmark

:trophy: A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
MIT License
3.68k stars 255 forks source link

ast: Empty Text() from code blocks #459

Closed cbednarski closed 3 weeks ago

cbednarski commented 3 months ago

Hello, goldmark is great and I've been enjoying using it for several projects now. Thanks for working on this!

I've run into a snag with the ast where I can't read node.Text() for code blocks. Headings work OK, and I can see the data using Dump() and Lines() has the right offsets.

Here's my repro case. Thanks!

main.go

package main

import (
    "fmt"

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

var data = []byte(`# A heading

    some code in a code block
`)

func main() {
    rootNode := goldmark.New().Parser().Parse(text.NewReader(data))

    if err := ast.Walk(rootNode, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
        if entering {
            return ast.WalkContinue, nil
        }
        switch n.Kind() {
        case ast.KindHeading:
            fmt.Println("heading:", string(n.Text(data)))
        case ast.KindCodeBlock:
            fmt.Println("codeblock:", string(n.Text(data)))

            for _, line := range n.Lines().Sliced(0, n.Lines().Len()) {
                fmt.Println(line.Value(data))
            }

            pos := n.Lines().At(0)
            fmt.Println("start:", pos.Start,
                "stop:", pos.Stop,
                "data:", string(data[pos.Start:pos.Stop]))
        }

        return ast.WalkContinue, nil
    }); err != nil {
        panic(err)
    }
}

Output:

$ go run .
heading: A heading
codeblock:                   <-------------- missing data!
start: 14 stop: 39 data: some code in a code block

Please answer the following before submitting your issue:

  1. What version of goldmark are you using? : github.com/yuin/goldmark v1.7.4
  2. What version of Go are you using? : go version go1.23rc2
  3. What operating system and processor architecture are you using? : linux/amd64
github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 30 days with no activity.

cbednarski commented 2 months ago

Not stale. Still an issue.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 30 days with no activity.

cbednarski commented 1 month ago

Still here, thanks