| 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 |
type EastAsianLineBreaks int |
| 14 |
|
| 15 |
const ( |
| 16 |
|
| 17 |
EastAsianLineBreaksNone EastAsianLineBreaks = iota |
| 18 |
|
| 19 |
|
| 20 |
EastAsianLineBreaksSimple |
| 21 |
|
| 22 |
|
| 23 |
EastAsianLineBreaksCSS3Draft |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func WithEastAsianLineBreaks(style ...EastAsianLineBreaks) CJKOption { |
| 30 |
return func(c *cjk) { |
| 31 |
if len(style) == 0 { |
| 32 |
c.EastAsianLineBreaks = EastAsianLineBreaksSimple |
| 33 |
return |
| 34 |
} |
| 35 |
c.EastAsianLineBreaks = style[0] |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func WithEscapedSpace() CJKOption { |
| 41 |
return func(c *cjk) { |
| 42 |
c.EscapedSpace = true |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
type cjk struct { |
| 47 |
EastAsianLineBreaks EastAsianLineBreaks |
| 48 |
EscapedSpace bool |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
var CJK = NewCJK(WithEastAsianLineBreaks(), WithEscapedSpace()) |
| 53 |
|
| 54 |
|
| 55 |
func NewCJK(opts ...CJKOption) goldmark.Extender { |
| 56 |
e := &cjk{ |
| 57 |
EastAsianLineBreaks: EastAsianLineBreaksNone, |
| 58 |
} |
| 59 |
for _, opt := range opts { |
| 60 |
opt(e) |
| 61 |
} |
| 62 |
return e |
| 63 |
} |
| 64 |
|
| 65 |
func (e *cjk) Extend(m goldmark.Markdown) { |
| 66 |
m.Renderer().AddOptions(html.WithEastAsianLineBreaks( |
| 67 |
html.EastAsianLineBreaks(e.EastAsianLineBreaks))) |
| 68 |
if e.EscapedSpace { |
| 69 |
m.Renderer().AddOptions(html.WithWriter(html.NewWriter(html.WithEscapedSpace()))) |
| 70 |
m.Parser().AddOptions(parser.WithEscapedSpace()) |
| 71 |
} |
| 72 |
} |