...
1 package extension
2
3 import (
4 "github.com/yuin/goldmark"
5 "github.com/yuin/goldmark/parser"
6 "github.com/yuin/goldmark/renderer/html"
7 )
8
9
10 type CJKOption func(*cjk)
11
12
13
14 func WithEastAsianLineBreaks() CJKOption {
15 return func(c *cjk) {
16 c.EastAsianLineBreaks = true
17 }
18 }
19
20
21 func WithEscapedSpace() CJKOption {
22 return func(c *cjk) {
23 c.EscapedSpace = true
24 }
25 }
26
27 type cjk struct {
28 EastAsianLineBreaks bool
29 EscapedSpace bool
30 }
31
32 var CJK = NewCJK(WithEastAsianLineBreaks(), WithEscapedSpace())
33
34
35 func NewCJK(opts ...CJKOption) goldmark.Extender {
36 e := &cjk{}
37 for _, opt := range opts {
38 opt(e)
39 }
40 return e
41 }
42
43 func (e *cjk) Extend(m goldmark.Markdown) {
44 if e.EastAsianLineBreaks {
45 m.Renderer().AddOptions(html.WithEastAsianLineBreaks())
46 }
47 if e.EscapedSpace {
48 m.Renderer().AddOptions(html.WithWriter(html.NewWriter(html.WithEscapedSpace())))
49 m.Parser().AddOptions(parser.WithEscapedSpace())
50 }
51 }
52
View as plain text