forked from teekennedy/goldmark-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.go
More file actions
63 lines (55 loc) · 2.31 KB
/
Copy pathextension.go
File metadata and controls
63 lines (55 loc) · 2.31 KB
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
package markdown
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
type rendererExtension struct{ opts []Option }
// NewExtension returns a new goldmark.Markdown extension that uses the markdown renderer.
func NewExtension(opts ...Option) *rendererExtension {
return &rendererExtension{opts: opts}
}
// Extend implements goldmark.Extension.Extend
func (re *rendererExtension) Extend(md goldmark.Markdown) {
renderer := NewRenderer(re.opts...)
md.SetRenderer(renderer)
if renderer.config.TypographerSubstitutions {
enableTypographicSubstitutions(md)
} else {
disableTypographicSubstitutions(md)
}
}
// enableTypographicSubstitutions configures the typographer extension
// to substitute punctuations with the corresponding unicode character,
// instead of the default HTML escape sequence.
// These substitutions are only applied IF the typographer extension is enabled.
func enableTypographicSubstitutions(md goldmark.Markdown) {
subs := make(extension.TypographicSubstitutions)
subs[extension.LeftSingleQuote] = []byte("‘")
subs[extension.RightSingleQuote] = []byte("’")
subs[extension.LeftDoubleQuote] = []byte("“")
subs[extension.RightDoubleQuote] = []byte("”")
subs[extension.EnDash] = []byte("–")
subs[extension.EmDash] = []byte("—")
subs[extension.Ellipsis] = []byte("…")
subs[extension.LeftAngleQuote] = []byte("«")
subs[extension.RightAngleQuote] = []byte("»")
subs[extension.Apostrophe] = []byte("'")
md.Parser().AddOptions(extension.WithTypographicSubstitutions(subs))
}
// disableTypographicSubstitutions configures the typographer extension
// to substitute punctuations with their original values,
// effectively turning typographer into a no-op.
func disableTypographicSubstitutions(md goldmark.Markdown) {
subs := make(extension.TypographicSubstitutions)
subs[extension.LeftSingleQuote] = []byte("'")
subs[extension.RightSingleQuote] = []byte("'")
subs[extension.LeftDoubleQuote] = []byte("\"")
subs[extension.RightDoubleQuote] = []byte("\"")
subs[extension.EnDash] = []byte("--")
subs[extension.EmDash] = []byte("---")
subs[extension.Ellipsis] = []byte("...")
subs[extension.LeftAngleQuote] = []byte("<<")
subs[extension.RightAngleQuote] = []byte(">>")
subs[extension.Apostrophe] = []byte("'")
md.Parser().AddOptions(extension.WithTypographicSubstitutions(subs))
}