Skip to content

Commit

Permalink
Fix bug that escaped space not working with Linkfy extension
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Sep 25, 2022
1 parent 1dd67d5 commit ae42b91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extension/cjk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package extension

import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)

Expand Down Expand Up @@ -45,5 +46,6 @@ func (e *cjk) Extend(m goldmark.Markdown) {
}
if e.EscapedSpace {
m.Renderer().AddOptions(html.WithWriter(html.NewWriter(html.WithEscapedSpace())))
m.Parser().AddOptions(parser.WithEscapedSpace())
}
}
25 changes: 25 additions & 0 deletions extension/cjk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ func TestEscapedSpace(t *testing.T) {
},
t,
)

// ' ' triggers Linkify extension inline parser.
// Escaped spaces should not trigger the inline parser.

markdown = goldmark.New(goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
goldmark.WithExtensions(
NewCJK(WithEscapedSpace()),
Linkify,
),
)

no = 4
testutil.DoTestCase(
markdown,
testutil.MarkdownTestCase{
No: no,
Description: "Escaped space and linkfy extension",
Markdown: "太郎は\\ **「こんにちわ」**\\ と言った\nんです",
Expected: "<p>太郎は<strong>「こんにちわ」</strong>と言った\nんです</p>",
},
t,
)
}

func TestEastAsianLineBreaks(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ type Config struct {
InlineParsers util.PrioritizedSlice /*<InlineParser>*/
ParagraphTransformers util.PrioritizedSlice /*<ParagraphTransformer>*/
ASTTransformers util.PrioritizedSlice /*<ASTTransformer>*/
EscapedSpace bool
}

// NewConfig returns a new Config.
Expand Down Expand Up @@ -635,6 +636,7 @@ type parser struct {
closeBlockers []CloseBlocker
paragraphTransformers []ParagraphTransformer
astTransformers []ASTTransformer
escapedSpace bool
config *Config
initSync sync.Once
}
Expand Down Expand Up @@ -695,6 +697,18 @@ func WithASTTransformers(ps ...util.PrioritizedValue) Option {
return &withASTTransformers{ps}
}

type withEscapedSpace struct {
}

func (o *withEscapedSpace) SetParserOption(c *Config) {
c.EscapedSpace = true
}

// WithEscapedSpace is a functional option indicates that a '\' escaped half-space(0x20) should not trigger parsers.
func WithEscapedSpace() Option {
return &withEscapedSpace{}
}

type withOption struct {
name OptionName
value interface{}
Expand Down Expand Up @@ -846,6 +860,7 @@ func (p *parser) Parse(reader text.Reader, opts ...ParseOption) ast.Node {
for _, v := range p.config.ASTTransformers {
p.addASTTransformer(v, p.config.Options)
}
p.escapedSpace = p.config.EscapedSpace
p.config = nil
})
c := &ParseConfig{}
Expand Down Expand Up @@ -1165,7 +1180,7 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
}
isSpace := util.IsSpace(c)
isPunct := util.IsPunct(c)
if (isPunct && !escaped) || isSpace || i == 0 {
if (isPunct && !escaped) || isSpace && !(escaped && p.escapedSpace) || i == 0 {
parserChar := c
if isSpace || (i == 0 && !isPunct) {
parserChar = ' '
Expand Down

0 comments on commit ae42b91

Please sign in to comment.