-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.go
93 lines (72 loc) · 2.39 KB
/
modify.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package markdown
import (
"bytes"
"log"
"strings"
markdown "github.com/teekennedy/goldmark-markdown"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type imgReplaceTransformer struct {
Replacer replacer
}
func (t *imgReplaceTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
// Walk the AST in depth-first fashion and apply transformations
err := ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
// Each node will be visited twice, once when it is first encountered (entering), and again
// after all the node's children have been visited (if any). Skip the latter.
if !entering {
return ast.WalkContinue, nil
}
if node.Kind() == ast.KindImage {
imgNode := node.(*ast.Image)
shouldReplace, newValue := t.Replacer(string(imgNode.Destination))
if shouldReplace {
imgNode.Destination = []byte(newValue)
}
}
return ast.WalkContinue, nil
})
if err != nil {
log.Fatal("Error encountered while transforming AST:", err)
}
}
func NewModifier(t parser.ASTTransformer) goldmark.Markdown {
// Goldmark supports multiple AST transformers and runs them sequentially in order of priority.
prioritizedTransformer := util.Prioritized(t, 0)
gm := goldmark.New(
goldmark.WithRenderer(markdown.NewRenderer()),
goldmark.WithParserOptions(parser.WithASTTransformers(prioritizedTransformer)),
)
return gm
}
func ReplaceImageUrls(md string, replace replacer) string {
t := &imgReplaceTransformer{
Replacer: replace,
}
gm := NewModifier(t) // Output buffer
buf := bytes.Buffer{}
// Convert parses the source, applies transformers, and renders output to the given io.Writer
err := gm.Convert([]byte(md), &buf)
if err != nil {
log.Fatalf("Encountered Markdown conversion error: %v", err)
}
return strings.TrimSpace(buf.String())
}
func ImportReplacer(renameMap map[string]string, existingMap map[string]struct{}) replacer {
return func(in string) (bool, string) {
destRaw := in
dest := strings.ToLower(destRaw)
// all this dance is only to make sure wrong case does not fool us
// to treat an existing image as a new one
if _, ok := existingMap[dest]; ok && dest != destRaw {
return true, dest
} else if newName, ok := renameMap[dest]; ok {
return true, newName
}
return false, in
}
}