Skip to content

Commit

Permalink
Merge pull request #1561 from hydephp/improve-markdown-to-plaintext-c…
Browse files Browse the repository at this point in the history
…onverter

Update Markdown to plaintext formatter to trim whitespace hydephp/develop@296556d
  • Loading branch information
github-actions committed Feb 13, 2024
1 parent 04b56f8 commit 1ecb306
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 1ecb306

Please sign in to comment.