forked from yuin/goldmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown.go
140 lines (116 loc) · 3.64 KB
/
markdown.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Package goldmark implements functions to convert markdown text to a desired format.
package goldmark
import (
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"io"
)
// DefaultParser returns a new Parser that is configured by default values.
func DefaultParser() parser.Parser {
return parser.NewParser(parser.WithBlockParsers(parser.DefaultBlockParsers()...),
parser.WithInlineParsers(parser.DefaultInlineParsers()...),
parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...),
)
}
// DefaultRenderer returns a new Renderer that is configured by default values.
func DefaultRenderer() renderer.Renderer {
return renderer.NewRenderer(renderer.WithNodeRenderers(util.Prioritized(html.NewRenderer(), 1000)))
}
var defaultMarkdown = New()
// Convert interprets a UTF-8 bytes source in Markdown and
// write rendered contents to a writer w.
func Convert(source []byte, w io.Writer, opts ...parser.ParseOption) error {
return defaultMarkdown.Convert(source, w, opts...)
}
// A Markdown interface offers functions to convert Markdown text to
// a desired format.
type Markdown interface {
// Convert interprets a UTF-8 bytes source in Markdown and write rendered
// contents to a writer w.
Convert(source []byte, writer io.Writer, opts ...parser.ParseOption) error
// Parser returns a Parser that will be used for conversion.
Parser() parser.Parser
// SetParser sets a Parser to this object.
SetParser(parser.Parser)
// Parser returns a Renderer that will be used for conversion.
Renderer() renderer.Renderer
// SetRenderer sets a Renderer to this object.
SetRenderer(renderer.Renderer)
}
// Option is a functional option type for Markdown objects.
type Option func(*markdown)
// WithExtensions adds extensions.
func WithExtensions(ext ...Extender) Option {
return func(m *markdown) {
m.extensions = append(m.extensions, ext...)
}
}
// WithParser allows you to override the default parser.
func WithParser(p parser.Parser) Option {
return func(m *markdown) {
m.parser = p
}
}
// WithParserOptions applies options for the parser.
func WithParserOptions(opts ...parser.Option) Option {
return func(m *markdown) {
m.parser.AddOptions(opts...)
}
}
// WithRenderer allows you to override the default renderer.
func WithRenderer(r renderer.Renderer) Option {
return func(m *markdown) {
m.renderer = r
}
}
// WithRendererOptions applies options for the renderer.
func WithRendererOptions(opts ...renderer.Option) Option {
return func(m *markdown) {
m.renderer.AddOptions(opts...)
}
}
type markdown struct {
parser parser.Parser
renderer renderer.Renderer
extensions []Extender
}
// New returns a new Markdown with given options.
func New(options ...Option) Markdown {
md := &markdown{
parser: DefaultParser(),
renderer: DefaultRenderer(),
extensions: []Extender{},
}
for _, opt := range options {
opt(md)
}
for _, e := range md.extensions {
e.Extend(md)
}
return md
}
func (m *markdown) Convert(source []byte, writer io.Writer, opts ...parser.ParseOption) error {
reader := text.NewReader(source)
doc := m.parser.Parse(reader, opts...)
return m.renderer.Render(writer, source, doc)
}
func (m *markdown) Parser() parser.Parser {
return m.parser
}
func (m *markdown) SetParser(v parser.Parser) {
m.parser = v
}
func (m *markdown) Renderer() renderer.Renderer {
return m.renderer
}
func (m *markdown) SetRenderer(v renderer.Renderer) {
m.renderer = v
}
// An Extender interface is used for extending Markdown.
type Extender interface {
// Extend extends the Markdown.
Extend(Markdown)
}