cjk.go (1299B)
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 // A CJKOption sets options for CJK support mostly for HTML based renderers. 10 type CJKOption func(*cjk) 11 12 // WithEastAsianLineBreaks is a functional option that indicates whether softline breaks 13 // between east asian wide characters should be ignored. 14 func WithEastAsianLineBreaks() CJKOption { 15 return func(c *cjk) { 16 c.EastAsianLineBreaks = true 17 } 18 } 19 20 // WithEscapedSpace is a functional option that indicates that a '\' escaped half-space(0x20) should not be rendered. 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 // NewCJK returns a new extension with given options. 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 }