-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.go
55 lines (43 loc) · 1.04 KB
/
transform.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
package chess
import (
"bytes"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
type Transformer struct {
}
var _chess = []byte("chess")
// Transform transforms the provided Markdown AST.
func (t *Transformer) Transform(doc *ast.Document, reader text.Reader, _ parser.Context) {
var (
chessBlocks []*ast.FencedCodeBlock
)
// Collect all blocks to be replaced without modifying the tree.
_ = ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
if !enter {
return ast.WalkContinue, nil
}
cb, ok := node.(*ast.FencedCodeBlock)
if !ok {
return ast.WalkContinue, nil
}
lang := cb.Language(reader.Source())
if !bytes.Equal(lang, _chess) {
return ast.WalkContinue, nil
}
chessBlocks = append(chessBlocks, cb)
return ast.WalkContinue, nil
})
if len(chessBlocks) == 0 {
return
}
for _, cb := range chessBlocks {
b := new(ChessBlock)
b.SetLines(cb.Lines())
parent := cb.Parent()
if parent != nil {
parent.ReplaceChild(parent, cb, b)
}
}
}