diff --git a/src/Framework/Actions/ConvertsMarkdownToPlainText.php b/src/Framework/Actions/ConvertsMarkdownToPlainText.php index c5638e53..d0a99ba1 100644 --- a/src/Framework/Actions/ConvertsMarkdownToPlainText.php +++ b/src/Framework/Actions/ConvertsMarkdownToPlainText.php @@ -93,6 +93,7 @@ protected function applyStringTransformations(string $markdown): string foreach ($lines as $line => $contents) { $contents = $this->removeTables($contents); $contents = $this->removeBlockquotes($contents); + $contents = $this->trimWhitespace($contents); $lines[$line] = $contents; } @@ -127,4 +128,16 @@ protected function removeBlockquotes(string $contents): string return $contents; } + + protected function trimWhitespace(string $contents): string + { + // If it is a list, don't trim the whitespace + $firstCharacter = substr(trim($contents), 0, 1); + + if ($firstCharacter === '-' || $firstCharacter === '*' || $firstCharacter === '+' || is_numeric($firstCharacter)) { + return $contents; + } + + return trim($contents); + } } diff --git a/tests/Feature/Actions/ConvertsMarkdownToPlainTextTest.php b/tests/Feature/Actions/ConvertsMarkdownToPlainTextTest.php index c8c2f220..8e7ba8e6 100644 --- a/tests/Feature/Actions/ConvertsMarkdownToPlainTextTest.php +++ b/tests/Feature/Actions/ConvertsMarkdownToPlainTextTest.php @@ -259,11 +259,11 @@ public function testItRemovesCode() public function testItRemovesCodeBlocks() { $markdown = <<<'MD' -

Hello World

+

Hello World

MD; $text = <<<'TXT' - Hello World + Hello World TXT; $this->assertSame($text, $this->convert($markdown)); @@ -367,9 +367,9 @@ public function testItRemovesHtml() $text = <<<'TXT' This word is bold. This word is italic. - - © My Company - + + © My Company + Hello World TXT; @@ -487,6 +487,23 @@ public function testItRemovesTables() $this->assertSame($text, $this->convert($markdown)); } + public function testItTrimsIndentation() + { + $markdown = <<<'MD' + foo + bar + baz + MD; + + $text = <<<'TXT' + foo + bar + baz + TXT; + + $this->assertSame($text, $this->convert($markdown)); + } + public function testWithEmptyString() { $this->assertSame('', $this->convert(''));