-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopt_path_resolver.go
50 lines (40 loc) · 1.16 KB
/
opt_path_resolver.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
package img64
import (
"path/filepath"
"strings"
"github.com/yuin/goldmark/renderer"
)
type PathResolver func(string) string
// WithPathResolver adds custom behavior to read image source path.
// For example, relative paths could be converted into absolute paths by adding the parent directory path.
func WithPathResolver(r PathResolver) interface {
renderer.Option
Img64Option
} {
return &withPathResolver{r}
}
type withPathResolver struct {
r PathResolver
}
func (o *withPathResolver) SetConfig(c *renderer.Config) {
c.Options[optImg64PathResolver] = o.r
}
func (o *withPathResolver) SetImg64Option(c *Img64Config) {
c.PathResolver = o.r
}
func DefaultPathResolver() PathResolver {
return defaultPathResolver
}
func defaultPathResolver(path string) string {
return path
}
// ParentLocalPathResolver adds parent directory path (ex. /var + target.md = /var/target.md).
// This is one example of PathResolver implementations.
func ParentLocalPathResolver(parentPath string) PathResolver {
return func(path string) string {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return path
}
return filepath.Join(parentPath, path)
}
}