-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (56 loc) · 1.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
)
type visualizer struct{}
func (v *visualizer) AddOptions(...renderer.Option) {}
func (v *visualizer) Render(w io.Writer, source []byte, n ast.Node) error {
return renderWithIndent(w, source, n, 0)
}
func renderWithIndent(w io.Writer, source []byte, n ast.Node, indent int) error {
fmt.Fprintln(w, strings.Repeat("\t", indent), toNodeSummary(source, n))
if n.HasChildren() {
siblingCount := n.ChildCount()
for child := n.FirstChild(); siblingCount > 0; siblingCount-- {
renderWithIndent(w, source, child, indent+1)
child = child.NextSibling()
}
}
return nil
}
func toNodeSummary(source []byte, n ast.Node) string {
if text := string(n.Text(source)); text != "" {
return fmt.Sprintf("Type: %s, Kind: %s, Text: %s", toNodeTypeName(n.Type()), n.Kind().String(), text)
}
return fmt.Sprintf("Type: %s, Kind: %s, (no text)", toNodeTypeName(n.Type()), n.Kind().String())
}
func toNodeTypeName(t ast.NodeType) string {
switch t {
case ast.TypeBlock:
return "Block"
case ast.TypeInline:
return "Inline"
case ast.TypeDocument:
return "Doc"
}
return "?"
}
func main() {
in, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
VisualizeMarkdown(in)
}
func VisualizeMarkdown(in []byte) {
md := goldmark.New(goldmark.WithRenderer(&visualizer{}))
if err := md.Convert(in, os.Stdout); err != nil {
panic(err)
}
}