From 4536e5793854808e0e71f5b2d41dcca9f9f87565 Mon Sep 17 00:00:00 2001 From: yuin Date: Mon, 25 Nov 2019 20:35:02 +0900 Subject: [PATCH] Fixes #35 --- extra_test.go | 25 +++++++++++++++++++++++++ parser/parser.go | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/extra_test.go b/extra_test.go index a9040e8..a794211 100644 --- a/extra_test.go +++ b/extra_test.go @@ -32,3 +32,28 @@ func TestEndsWithNonSpaceCharacters(t *testing.T) { t.Errorf("%s \n---------\n %s", source, b.String()) } } + +func TestWindowsNewLine(t *testing.T) { + markdown := New(WithRendererOptions( + html.WithXHTML(), + )) + source := []byte("a \r\nb\n") + var b bytes.Buffer + err := markdown.Convert(source, &b) + if err != nil { + t.Error(err.Error()) + } + if b.String() != "

a
\nb

\n" { + t.Errorf("%s\n---------\n%s", source, b.String()) + } + + source = []byte("a\\\r\nb\r\n") + var b2 bytes.Buffer + err = markdown.Convert(source, &b2) + if err != nil { + t.Error(err.Error()) + } + if b2.String() != "

a
\nb

\n" { + t.Errorf("\n%s\n---------\n%s", source, b2.String()) + } +} diff --git a/parser/parser.go b/parser/parser.go index 917da5b..1f1e370 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1154,7 +1154,14 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) if lineLength > 2 && line[lineLength-2] == '\\' && softLinebreak { // ends with \\n stop-- hardlineBreak = true + + } else if lineLength > 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && softLinebreak { // ends with \\r\n + stop -= 2 + hardlineBreak = true } else if lineLength > 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && softLinebreak { // ends with [space][space]\n + stop-- + hardlineBreak = true + } else if lineLength > 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && softLinebreak { // ends with [space][space]\r\n hardlineBreak = true } rest := diff.WithStop(stop)