1package extension
2
3import (
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.
10type CJKOption func(*cjk)
11
12// WithEastAsianLineBreaks is a functional option that indicates whether softline breaks
13// between east asian wide characters should be ignored.
14func 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.
21func WithEscapedSpace() CJKOption {
22 return func(c *cjk) {
23 c.EscapedSpace = true
24 }
25}
26
27type cjk struct {
28 EastAsianLineBreaks bool
29 EscapedSpace bool
30}
31
32var CJK = NewCJK(WithEastAsianLineBreaks(), WithEscapedSpace())
33
34// NewCJK returns a new extension with given options.
35func NewCJK(opts ...CJKOption) goldmark.Extender {
36 e := &cjk{}
37 for _, opt := range opts {
38 opt(e)
39 }
40 return e
41}
42
43func (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}