-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
95 lines (80 loc) · 1.92 KB
/
renderer.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
94
95
package renderer
import (
"io"
"sync"
"github.com/dissipative/opabinia/internal/infra/config"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
)
type Logger interface {
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
type Config interface {
CodeDefaultLang() string
SyntaxTheme() string
TmplConf() *config.Template
}
type Renderer struct {
*html.Renderer
h *highlighter
m *sync.Mutex
state *renderState
opts Options
logger Logger
}
type Options struct {
html.RendererOptions
SyntaxDefaultLang string
SyntaxTheme string
TmplConf *config.Template
}
type renderState struct {
documentMatter ast.DocumentMatters
}
func NewRenderer(opts Options, logger Logger) (*Renderer, error) {
h, err := newHighlighter(opts.SyntaxTheme)
if err != nil {
return nil, err
}
return &Renderer{
Renderer: html.NewRenderer(opts.RendererOptions),
h: h,
state: &renderState{},
opts: opts,
logger: logger,
m: new(sync.Mutex),
}, nil
}
func NewDefaultRenderer(conf Config, logger Logger) (*Renderer, error) {
return NewRenderer(
Options{
RendererOptions: html.RendererOptions{Flags: html.CommonFlags | html.HrefTargetBlank},
SyntaxDefaultLang: conf.CodeDefaultLang(),
SyntaxTheme: conf.SyntaxTheme(),
TmplConf: conf.TmplConf(),
},
logger,
)
}
// RenderNode process images, links, and code blocks differently than default renderer
func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus {
switch n := node.(type) {
case *ast.Link:
r.Link(w, n, entering)
return ast.GoToNext
case *ast.Image:
if r.opts.Flags&html.SkipImages != 0 {
return ast.SkipChildren
}
r.Image(w, n, entering)
return ast.GoToNext
case *ast.CodeBlock:
r.CodeBlock(w, n)
return ast.GoToNext
case *ast.Code:
r.Code(w, n)
return ast.GoToNext
}
return r.Renderer.RenderNode(w, node, entering)
}