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
  • Loading branch information
caendesilva committed Feb 13, 2024
2 parents 19b461c + 56e3010 commit 296556d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Expand Up @@ -28,6 +28,7 @@ This serves two purposes:
### Changed
- Renamed local template variable `$document` to `$article` to better match the usage in https://github.com/hydephp/develop/pull/1506
- Updated semantic documentation article component to support existing variables in https://github.com/hydephp/develop/pull/1506
- Updated the Markdown to plain text converter to trim whitespace in https://github.com/hydephp/develop/pull/1561
- HydeFront: Changed `<code>` styling to display as inline instead of inline-block in https://github.com/hydephp/develop/pull/1525
- Realtime Compiler: Add strict type declarations in https://github.com/hydephp/develop/pull/1555
- Internal: Renamed snake case test methods to camel case in https://github.com/hydephp/develop/pull/1556
Expand Down
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);
}
}
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 296556d

Please sign in to comment.