Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Markdown to plaintext formatter to trim whitespace #1561

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
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
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);
}
}
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
Loading