Getting the text of a fenced Code Block #398
Replies: 3 comments 1 reply
-
So far I have this (very messy)
which is showing each line of the code block, but surely there should be some kind of convenience method that allows for this? |
Beta Was this translation helpful? Give feedback.
-
You need to walk the AST to search for the ast.Walk(document, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
cb, ok := node.(*ast.FencedCodeBlock)
if !ok {
return ast.WalkContinue, nil
}
// do stuff with FencedCodeBlock
return ast.WalkContinue, nil
}) Once you've found it, use var code bytes.Buffer
lines := cb.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
code.Write(line.Value(src))
}
fmt.Println("Code:\n", code.String()) |
Beta Was this translation helpful? Give feedback.
-
@abhinav hey thanks for the explanation super helpful. can you please elaborate how some nodes have Also whats the difference between Text Block, and Paragraph? Similary Text vs String? I went through the docs but didnt find anything helpful there. Thanks in advance 🙏🏾 |
Beta Was this translation helpful? Give feedback.
-
It really is not clear to me how to retrieve the text of a fenced code block. I have the following code:
But the pointer only shows the Text for Paragraph blocks, when there are CodeBlocks, it returns nothing.
Is there anyone who has an idea of how to retrieve text of code blocks?
When I run document.Dump(source), it is clear that it can parse the text and display it.
@yuin
Beta Was this translation helpful? Give feedback.
All reactions