-
Notifications
You must be signed in to change notification settings - Fork 4
/
transformer.go
53 lines (41 loc) · 988 Bytes
/
transformer.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
package d2
import (
"bytes"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
type Transformer struct {
}
var _d2 = []byte("d2")
func (s *Transformer) Transform(doc *ast.Document, reader text.Reader, pctx parser.Context) {
var blocks []*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, _d2) {
return ast.WalkContinue, nil
}
blocks = append(blocks, cb)
return ast.WalkContinue, nil
})
// Nothing to do.
if len(blocks) == 0 {
return
}
for _, cb := range blocks {
b := new(Block)
b.SetLines(cb.Lines())
parent := cb.Parent()
if parent != nil {
parent.ReplaceChild(parent, cb, b)
}
}
}