markup/goldmark: Add CJK extension

Fixes #10472
This commit is contained in:
OMOTO Tsukasa 2023-08-30 20:08:45 +09:00 committed by GitHub
parent 9bf76fd7e8
commit d7dcc76d27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 0 deletions

View file

@ -1037,6 +1037,9 @@ config:
rightAngleQuote: '»' rightAngleQuote: '»'
rightDoubleQuote: '”' rightDoubleQuote: '”'
rightSingleQuote: '’' rightSingleQuote: '’'
CJK:
eastAsianLineBreaks: false
escapedSpace: false
parser: parser:
attribute: attribute:
block: false block: false

View file

@ -136,6 +136,19 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
extensions = append(extensions, extension.Footnote) extensions = append(extensions, extension.Footnote)
} }
if cfg.Extensions.CJK.Enable {
opts := []extension.CJKOption{}
if cfg.Extensions.CJK.EastAsianLineBreaks {
opts = append(opts, extension.WithEastAsianLineBreaks())
}
if cfg.Extensions.CJK.EscapedSpace {
opts = append(opts, extension.WithEscapedSpace())
}
c := extension.NewCJK(opts...)
extensions = append(extensions, c)
}
if cfg.Parser.AutoHeadingID { if cfg.Parser.AutoHeadingID {
parserOptions = append(parserOptions, parser.WithAutoHeadingID()) parserOptions = append(parserOptions, parser.WithAutoHeadingID())
} }

View file

@ -626,3 +626,76 @@ unsafe = false
return testconfig.GetTestConfig(nil, cfg) return testconfig.GetTestConfig(nil, cfg)
} }
func TestConvertCJK(t *testing.T) {
c := qt.New(t)
content := `
私は太郎です
プログラミングが好きです\ 運動が苦手です
`
confStr := `
[markup]
[markup.goldmark]
`
cfg := config.FromTOMLConfigString(confStr)
conf := testconfig.GetTestConfig(nil, cfg)
b := convert(c, conf, content)
got := string(b.Bytes())
c.Assert(got, qt.Contains, "<p>私は太郎です。\nプログラミングが好きです。\\ 運動が苦手です。</p>\n")
}
func TestConvertCJKWithExtensionWithEastAsianLineBreaksOption(t *testing.T) {
c := qt.New(t)
content := `
私は太郎です
プログラミングが好きで
運動が苦手です
`
confStr := `
[markup]
[markup.goldmark]
[markup.goldmark.extensions.CJK]
enable=true
eastAsianLineBreaks=true
`
cfg := config.FromTOMLConfigString(confStr)
conf := testconfig.GetTestConfig(nil, cfg)
b := convert(c, conf, content)
got := string(b.Bytes())
c.Assert(got, qt.Contains, "<p>私は太郎です。プログラミングが好きで、運動が苦手です。</p>\n")
}
func TestConvertCJKWithExtensionWithEscapedSpaceOption(t *testing.T) {
c := qt.New(t)
content := `
私は太郎です
プログラミングが好きです\ 運動が苦手です
`
confStr := `
[markup]
[markup.goldmark]
[markup.goldmark.extensions.CJK]
enable=true
escapedSpace=true
`
cfg := config.FromTOMLConfigString(confStr)
conf := testconfig.GetTestConfig(nil, cfg)
b := convert(c, conf, content)
got := string(b.Bytes())
c.Assert(got, qt.Contains, "<p>私は太郎です。\nプログラミングが好きです。運動が苦手です。</p>\n")
}

View file

@ -43,6 +43,11 @@ var Default = Config{
Linkify: true, Linkify: true,
LinkifyProtocol: "https", LinkifyProtocol: "https",
TaskList: true, TaskList: true,
CJK: CJK{
Enable: false,
EastAsianLineBreaks: false,
EscapedSpace: false,
},
}, },
Renderer: Renderer{ Renderer: Renderer{
Unsafe: false, Unsafe: false,
@ -76,6 +81,7 @@ type Extensions struct {
Linkify bool Linkify bool
LinkifyProtocol string LinkifyProtocol string
TaskList bool TaskList bool
CJK CJK
} }
// Typographer holds typographer configuration. // Typographer holds typographer configuration.
@ -105,6 +111,17 @@ type Typographer struct {
Apostrophe string Apostrophe string
} }
type CJK struct {
// Whether to enable CJK support.
Enable bool
// Whether softline breaks between east asian wide characters should be ignored.
EastAsianLineBreaks bool
// Whether a '\' escaped half-space(0x20) should not be rendered.
EscapedSpace bool
}
type Renderer struct { type Renderer struct {
// Whether softline breaks should be rendered as '<br>' // Whether softline breaks should be rendered as '<br>'
HardWraps bool HardWraps bool