Skip to content

Commit

Permalink
fix: update some preg_replace calls in MarkdownRemover and add tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chr15k authored Jan 11, 2025
1 parent 6eca2fb commit 88e1d08
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/TextProcessor/MarkdownRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@ public function process(TextInterface $text): TextInterface
// Remove blockquotes
$output = \PhpSpellcheck\preg_replace('/^\s{0,3}>\s?/', '', $output);
// Remove reference-style links?
$output = \PhpSpellcheck\preg_replace('/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/', '', $output);
/**
* Remove atx-style headers.
*
*@TODO find a way to merge the two regex below
* remove ## Heading ##
*/
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*)(\s+#{1,6})$/m', '$1', $output);
// remove ## Heading
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*)$/m', '$1', $output);
// Remove emphasis (repeat the line to remove double emphasis)
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
// Remove ## Heading
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*?)(?:\s+#{1,6})?$/m', '$1', $output);
// Remove all layers of emphasis
while (\PhpSpellcheck\preg_match('/([\*_]{1,3})(\S.*?\S{0,1})\1/', $output)) {
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
}

// Remove list items
$output = \PhpSpellcheck\preg_replace('/^([^\S\r\n]*)\*\s/m', '$1', $output);
// Remove code blocks
Expand Down
16 changes: 16 additions & 0 deletions tests/TextProcessor/MarkdownRemoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public function testShouldRemoveDoubleEmphasis(): void
$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
}

public function testShouldRemoveTripleEmphasis(): void
{
$string = 'This text is ***really important***.';
$expected = 'This text is really important.';

$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
}

public function testShouldRemoveLongerEmphasis(): void
{
$string = 'This text is ******really important******.';
$expected = 'This text is really important.';

$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
}

public function testShouldRemoveHorizontalRules(): void
{
$string = "Some text on a line\n\n---\n\nA line below";
Expand Down

0 comments on commit 88e1d08

Please sign in to comment.