From c5f64b57ce15509da665056b5a41b51dfaa3cad6 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Tue, 12 Jun 2018 15:07:37 -0700 Subject: [PATCH] Add support for CR and CRLF line endings fixes #5 --- src/unparsed-blocks/parse.php | 2 ++ tests/EdgeCaseTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/unparsed-blocks/parse.php b/src/unparsed-blocks/parse.php index 508dc6f..9707051 100644 --- a/src/unparsed-blocks/parse.php +++ b/src/unparsed-blocks/parse.php @@ -17,6 +17,8 @@ function parse( string $markdown, ): Document { $lines = $markdown + |> Str\replace($$, "\r\n", "\n") + |> Str\replace($$, "\r", "\n") |> Str\split($$, "\n") |> (C\lastx($$) === '' ? Vec\slice($$, 0, C\count($$) - 1) : $$) |> Vec\map($$, $line ==> tuple(0, $line)) diff --git a/tests/EdgeCaseTest.php b/tests/EdgeCaseTest.php index aa31d98..c490b88 100644 --- a/tests/EdgeCaseTest.php +++ b/tests/EdgeCaseTest.php @@ -34,6 +34,21 @@ public function getManualExamples( '*foo __bar *baz bim__ bam*', "

foo bar *baz bim bam

\n", ), + // CR + tuple( + "CR Foo\x0d\x0d> Bar\x0d\x0dBaz", + "

CR Foo

\n
\n

Bar

\n
\n

Baz

\n", + ), + // LF + tuple( + "LF Foo\x0a\x0a> Bar\x0a\x0aBaz", + "

LF Foo

\n
\n

Bar

\n
\n

Baz

\n", + ), + // CRLF + tuple( + "CRLF Foo\x0d\x0a\x0d\x0a> Bar\x0d\x0a\x0d\x0aBaz", + "

CRLF Foo

\n
\n

Bar

\n
\n

Baz

\n", + ), ]; }