Skip to content

Commit 88e1d08

Browse files
authored
fix: update some preg_replace calls in MarkdownRemover and add tests (#57)
1 parent 6eca2fb commit 88e1d08

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/TextProcessor/MarkdownRemover.php

+8-12
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,15 @@ public function process(TextInterface $text): TextInterface
4545
// Remove blockquotes
4646
$output = \PhpSpellcheck\preg_replace('/^\s{0,3}>\s?/', '', $output);
4747
// Remove reference-style links?
48+
4849
$output = \PhpSpellcheck\preg_replace('/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/', '', $output);
49-
/**
50-
* Remove atx-style headers.
51-
*
52-
*@TODO find a way to merge the two regex below
53-
* remove ## Heading ##
54-
*/
55-
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*)(\s+#{1,6})$/m', '$1', $output);
56-
// remove ## Heading
57-
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*)$/m', '$1', $output);
58-
// Remove emphasis (repeat the line to remove double emphasis)
59-
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
60-
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
50+
// Remove ## Heading
51+
$output = \PhpSpellcheck\preg_replace('/^#{1,6}\s+(.*?)(?:\s+#{1,6})?$/m', '$1', $output);
52+
// Remove all layers of emphasis
53+
while (\PhpSpellcheck\preg_match('/([\*_]{1,3})(\S.*?\S{0,1})\1/', $output)) {
54+
$output = \PhpSpellcheck\preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $output);
55+
}
56+
6157
// Remove list items
6258
$output = \PhpSpellcheck\preg_replace('/^([^\S\r\n]*)\*\s/m', '$1', $output);
6359
// Remove code blocks

tests/TextProcessor/MarkdownRemoverTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ public function testShouldRemoveDoubleEmphasis(): void
113113
$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
114114
}
115115

116+
public function testShouldRemoveTripleEmphasis(): void
117+
{
118+
$string = 'This text is ***really important***.';
119+
$expected = 'This text is really important.';
120+
121+
$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
122+
}
123+
124+
public function testShouldRemoveLongerEmphasis(): void
125+
{
126+
$string = 'This text is ******really important******.';
127+
$expected = 'This text is really important.';
128+
129+
$this->assertSame($expected, (new MarkdownRemover())->process(t($string))->getContent());
130+
}
131+
116132
public function testShouldRemoveHorizontalRules(): void
117133
{
118134
$string = "Some text on a line\n\n---\n\nA line below";

0 commit comments

Comments
 (0)