-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
352 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: "<p>太郎は**「こんにちわ」**と言った\nんです</p>", | ||
}, | ||
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: "<p>太郎は <strong>「こんにちわ」</strong> と言った\nんです</p>", | ||
}, | ||
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: "<p>太郎は<strong>「こんにちわ」</strong>と言った\nんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言った\nんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言ったんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言ったa\nbんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言ったa\nんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言った\nbんです</p>", | ||
}, | ||
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: "<p>太郎は\\ <strong>「こんにちわ」</strong>\\ と言った<br />\nんです</p>", | ||
}, | ||
t, | ||
) | ||
} |
Oops, something went wrong.