Skip to content

Commit

Permalink
Merge pull request #603 from hydephp/update-markdown-to-plaintext-for…
Browse files Browse the repository at this point in the history
…matter-to-trim-whitespace

Update Markdown to plaintext formatter to trim whitespace
  • Loading branch information
caendesilva authored Feb 13, 2024
2 parents 779d32e + 1ecb306 commit 881e6cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Framework/Actions/ConvertsMarkdownToPlainText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
27 changes: 22 additions & 5 deletions tests/Feature/Actions/ConvertsMarkdownToPlainTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ public function testItRemovesCode()
public function testItRemovesCodeBlocks()
{
$markdown = <<<'MD'
<p>Hello World</p>
<p>Hello World</p>
MD;

$text = <<<'TXT'
Hello World
Hello World
TXT;

$this->assertSame($text, $this->convert($markdown));
Expand Down Expand Up @@ -367,9 +367,9 @@ public function testItRemovesHtml()
$text = <<<'TXT'
This word is bold. This word is italic.
&copy; My Company
&copy; My Company
Hello World
TXT;
Expand Down Expand Up @@ -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(''));
Expand Down

0 comments on commit 881e6cf

Please sign in to comment.