Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions extension/cjk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,50 @@ func TestEastAsianLineBreaks(t *testing.T) {
t,
)

// Tests with EastAsianLineBreaksStyleSimple (for Chinese)
markdown = goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
goldmark.WithExtensions(
NewCJK(WithEastAsianLineBreaks(EastAsianLineBreaksSimple)),
),
)
no = 20
testutil.DoTestCase(
markdown,
testutil.MarkdownTestCase{
No: no,
Description: "中文汉字之间的软回车应被忽略",
Markdown: "被分开成两行\n写的一句话。",
Expected: "<p>被分开成两行写的一句话。</p>",
},
t,
)
no = 21
testutil.DoTestCase(
markdown,
testutil.MarkdownTestCase{
No: no,
Description: "中文常用标点符号之间的软回车应被忽略(换行符前)",
Markdown: "一,\n二。\n三?\n四!\n五:\n六;\n七。\n八【\n九】\n十『\n九』\n八‘\n七’\n六“\n五”\n四……\n三、\n二",
Expected: "<p>一,二。三?四!五:六;七。八【九】十『九』八‘七’六“五”四……三、二</p>",
},
t,
)

// 注意:按照中文标点符号规范,中文标点符号与其后的英文字母之间不应该有空格。
// 但是目前的实现不支持这种判断,所以还是会有空格。
no = 22
testutil.DoTestCase(
markdown,
testutil.MarkdownTestCase{
No: no,
Description: "中文与英文混合",
Markdown: "一,\na\n二。\nb",
Expected: "<p>一,\na\n二。\nb</p>",
},
t,
)
}
15 changes: 13 additions & 2 deletions util/util_cjk.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var cjkSymbolsAndPunctuation = &unicode.RangeTable{
},
}

var unicodeGeneralPunctuation = &unicode.RangeTable{
R16: []unicode.Range16{
{0x2000, 0x206F, 1},
},
}

var hiragana = &unicode.RangeTable{
R16: []unicode.Range16{
{0x3040, 0x309F, 1},
Expand Down Expand Up @@ -177,14 +183,19 @@ var cjkUnifiedIdeographsExtensionG = &unicode.RangeTable{
},
}

// IsEastAsianWideRune returns trhe if the given rune is an east asian wide character, otherwise false.
// IsEastAsianWideRune returns true if the given rune is an east asian wide character, otherwise false.
//
// Note for halfwidthAndFullwidthForms: some characters in this range are not wide characters,
// but soft line breaks are ignored for them too.
func IsEastAsianWideRune(r rune) bool {
return unicode.Is(unicode.Hiragana, r) ||
unicode.Is(unicode.Katakana, r) ||
unicode.Is(unicode.Han, r) ||
unicode.Is(unicode.Lm, r) ||
unicode.Is(unicode.Hangul, r) ||
unicode.Is(cjkSymbolsAndPunctuation, r)
unicode.Is(cjkSymbolsAndPunctuation, r) ||
unicode.Is(halfwidthAndFullwidthForms, r) ||
unicode.Is(unicodeGeneralPunctuation, r)
}

// IsSpaceDiscardingUnicodeRune returns true if the given rune is space-discarding unicode character, otherwise false.
Expand Down
Loading