diff --git a/README.md b/README.md index cb8cb77..dfcb6da 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,8 @@ Parser and Renderer options - [PHP Markdown Extra: Footnotes](https://michelf.ca/projects/php-markdown/extra/#footnotes) - `extension.Typographer` - This extension substitutes punctuations with typographic entities like [smartypants](https://daringfireball.net/projects/smartypants/). +- `extension.CJK` + - This extension is a shortcut for CJK related functionalities. ### Attributes The `parser.WithAttribute` option allows you to define attributes on some elements. @@ -369,6 +371,17 @@ footnote-prefix: article1 # My article ``` + +### CJK extension +CommonMark gives compatibilities a high priority and original markdown was designed by westerners. So CommonMark lacks considerations for languages like CJK. + +This extension provides additional options for CJK users. + +| Functional option | Type | Description | +| ----------------- | ---- | ----------- | +| `extension.WithEastAsianLineBreaks` | `-` | Soft line breaks are rendered as a newline. Some asian users will see it as an unnecessary space. With this option, soft line breaks between east asian wide characters will be ignored. | +| `extension.WithEscapedSpace` | `-` | Without spaces around an emphasis started with east asian punctuations, it is not interpreted as an emphasis(as defined in CommonMark spec). With this option, you can avoid this inconvenient behavior by putting 'not rendered' spaces around an emphasis like `太郎は\ **「こんにちわ」**\ といった`. | + Security -------------------- diff --git a/ast/inline.go b/ast/inline.go index fa6fc34..7da098f 100644 --- a/ast/inline.go +++ b/ast/inline.go @@ -91,7 +91,7 @@ func (n *Text) SetSoftLineBreak(v bool) { if v { n.flags |= textSoftLineBreak } else { - n.flags = n.flags &^ textHardLineBreak + n.flags = n.flags &^ textSoftLineBreak } } diff --git a/extension/cjk.go b/extension/cjk.go new file mode 100644 index 0000000..60e07ff --- /dev/null +++ b/extension/cjk.go @@ -0,0 +1,49 @@ +package extension + +import ( + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/renderer/html" +) + +// A CJKOption sets options for CJK support mostly for HTML based renderers. +type CJKOption func(*cjk) + +// WithEastAsianLineBreaks is a functional option that indicates whether softline breaks +// between east asian wide characters should be ignored. +func WithEastAsianLineBreaks() CJKOption { + return func(c *cjk) { + c.EastAsianLineBreaks = true + } +} + +// WithEscapedSpace is a functional option that indicates that a '\' escaped half-space(0x20) should not be rendered. +func WithEscapedSpace() CJKOption { + return func(c *cjk) { + c.EscapedSpace = true + } +} + +type cjk struct { + EastAsianLineBreaks bool + EscapedSpace bool +} + +var CJK = NewCJK(WithEastAsianLineBreaks(), WithEscapedSpace()) + +// NewCJK returns a new extension with given options. +func NewCJK(opts ...CJKOption) goldmark.Extender { + e := &cjk{} + for _, opt := range opts { + opt(e) + } + return e +} + +func (e *cjk) Extend(m goldmark.Markdown) { + if e.EastAsianLineBreaks { + m.Renderer().AddOptions(html.WithEastAsianLineBreaks()) + } + if e.EscapedSpace { + m.Renderer().AddOptions(html.WithWriter(html.NewWriter(html.WithEscapedSpace()))) + } +} diff --git a/extension/cjk_test.go b/extension/cjk_test.go new file mode 100644 index 0000000..8863a10 --- /dev/null +++ b/extension/cjk_test.go @@ -0,0 +1,154 @@ +package extension + +import ( + "testing" + + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" +) + +func TestEscapedSpace(t *testing.T) { + markdown := goldmark.New(goldmark.WithRendererOptions( + html.WithXHTML(), + html.WithUnsafe(), + )) + no := 1 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Without spaces around an emphasis started with east asian punctuations, it is not interpreted as an emphasis(as defined in CommonMark spec)", + Markdown: "太郎は**「こんにちわ」**と言った\nんです", + Expected: "
太郎は**「こんにちわ」**と言った\nんです
", + }, + t, + ) + + no = 2 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "With spaces around an emphasis started with east asian punctuations, it is interpreted as an emphasis(but remains unnecessary spaces)", + Markdown: "太郎は **「こんにちわ」** と言った\nんです", + Expected: "太郎は 「こんにちわ」 と言った\nんです
", + }, + t, + ) + + // Enables EscapedSpace + markdown = goldmark.New(goldmark.WithRendererOptions( + html.WithXHTML(), + html.WithUnsafe(), + ), + goldmark.WithExtensions(NewCJK(WithEscapedSpace())), + ) + + no = 3 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "With spaces around an emphasis started with east asian punctuations,it is interpreted as an emphasis", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nんです", + Expected: "太郎は「こんにちわ」と言った\nんです
", + }, + t, + ) +} + +func TestEastAsianLineBreaks(t *testing.T) { + markdown := goldmark.New(goldmark.WithRendererOptions( + html.WithXHTML(), + html.WithUnsafe(), + )) + no := 1 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Soft line breaks are rendered as a newline, so some asian users will see it as an unnecessary space", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言った\nんです
", + }, + t, + ) + + // Enables EastAsianLineBreaks + + markdown = goldmark.New(goldmark.WithRendererOptions( + html.WithXHTML(), + html.WithUnsafe(), + ), + goldmark.WithExtensions(NewCJK(WithEastAsianLineBreaks())), + ) + + no = 2 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Soft line breaks between east asian wide characters are ignored", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言ったんです
", + }, + t, + ) + + no = 3 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Soft line breaks between western characters are rendered as a newline", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言ったa\nbんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言ったa\nbんです
", + }, + t, + ) + + no = 4 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Soft line breaks between a western character and an east asian wide character are rendered as a newline", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言ったa\nんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言ったa\nんです
", + }, + t, + ) + + no = 5 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "Soft line breaks between an east asian wide character and a western character are rendered as a newline", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nbんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言った\nbんです
", + }, + t, + ) + + // WithHardWraps take precedence over WithEastAsianLineBreaks + markdown = goldmark.New(goldmark.WithRendererOptions( + html.WithHardWraps(), + html.WithXHTML(), + html.WithUnsafe(), + ), + goldmark.WithExtensions(NewCJK(WithEastAsianLineBreaks())), + ) + no = 6 + testutil.DoTestCase( + markdown, + testutil.MarkdownTestCase{ + No: no, + Description: "WithHardWraps take precedence over WithEastAsianLineBreaks", + Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nんです", + Expected: "太郎は\\ 「こんにちわ」\\ と言った
\nんです