1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use PhpSpellcheck\Text; |
6
|
|
|
use PhpSpellcheck\TextProcessor\MarkdownRemover; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class MarkdownRemoverTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testShouldStripOutRemainingMarkdown(): void |
12
|
|
|
{ |
13
|
|
|
$string = '*Javascript* developers are the _best_.'; |
14
|
|
|
$expected = 'Javascript developers are the best.'; |
15
|
|
|
|
16
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testShouldLeaveNonMatchingMarkdownMarkdown(): void |
20
|
|
|
{ |
21
|
|
|
$string = '*Javascript* developers* are the _best_.'; |
22
|
|
|
$expected = 'Javascript developers* are the best.'; |
23
|
|
|
|
24
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testShouldLeaveNonMatchingMarkdownButStripEmptyAnchors(): void |
28
|
|
|
{ |
29
|
|
|
$string = '*Javascript* [developers]()* are the _best_.'; |
30
|
|
|
$expected = 'Javascript developers* are the best.'; |
31
|
|
|
|
32
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testShouldStripHtml(): void |
36
|
|
|
{ |
37
|
|
|
$string = '<p>Hello World</p>'; |
38
|
|
|
$expected = 'Hello World'; |
39
|
|
|
|
40
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testShouldStripAnchors(): void |
44
|
|
|
{ |
45
|
|
|
$string = '*Javascript* [developers](https://engineering.condenast.io/)* are the _best_.'; |
46
|
|
|
$expected = 'Javascript developers* are the best.'; |
47
|
|
|
|
48
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testShouldStripImgTags(): void |
52
|
|
|
{ |
53
|
|
|
$string = '*Javascript* developers are the _best_.'; |
54
|
|
|
$expected = 'Javascript developers are the best.'; |
55
|
|
|
|
56
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testShouldUseTheAltTextOfAnImageIfItIsProvided(): void |
60
|
|
|
{ |
61
|
|
|
$string = ''; |
62
|
|
|
$expected = 'This is the alt-text'; |
63
|
|
|
|
64
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testShouldStripCodeTags(): void |
68
|
|
|
{ |
69
|
|
|
$string = 'In `Getting Started` we set up `something` foo.'; |
70
|
|
|
$expected = 'In Getting Started we set up something foo.'; |
71
|
|
|
|
72
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testShouldLeaveHashtagsInHeadings(): void |
76
|
|
|
{ |
77
|
|
|
$string = '## This #heading contains #hashtags'; |
78
|
|
|
$expected = 'This #heading contains #hashtags'; |
79
|
|
|
|
80
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testShouldRemoveHeadingsTrailingHashtags(): void |
84
|
|
|
{ |
85
|
|
|
$string = '## This #heading contains #hashtags ##'; |
86
|
|
|
$expected = 'This #heading contains #hashtags'; |
87
|
|
|
|
88
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testShouldRemoveHeadingsHashtags(): void |
92
|
|
|
{ |
93
|
|
|
$string = '## This #heading contains #hashtags'; |
94
|
|
|
$expected = 'This #heading contains #hashtags'; |
95
|
|
|
|
96
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testShouldRemoveEmphasis(): void |
100
|
|
|
{ |
101
|
|
|
$string = 'I italicized an *I* and it _made_ me *sad*.'; |
102
|
|
|
$expected = 'I italicized an I and it made me sad.'; |
103
|
|
|
|
104
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testShouldRemoveDoubleEmphasis(): void |
108
|
|
|
{ |
109
|
|
|
$string = '**this sentence has __double styling__**'; |
110
|
|
|
$expected = 'this sentence has double styling'; |
111
|
|
|
|
112
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testShouldRemoveHorizontalRules(): void |
116
|
|
|
{ |
117
|
|
|
$string = "Some text on a line\n\n---\n\nA line below"; |
118
|
|
|
$expected = "Some text on a line\n\n\n\n\nA line below"; |
119
|
|
|
|
120
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testShouldRemoveHorizontalRulesAndReplaceByAtLeastABreakLine(): void |
124
|
|
|
{ |
125
|
|
|
$string = "Some text on a line\n---\nA line below"; |
126
|
|
|
$expected = "Some text on a line\n\n\nA line below"; |
127
|
|
|
|
128
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testShouldRemoveHorizontalRulesWithSpaceSeparatedAsterisks(): void |
132
|
|
|
{ |
133
|
|
|
$string = "Some text on a line\n\n* * *\n\nA line below"; |
134
|
|
|
$expected = "Some text on a line\n\n\n\n\nA line below"; |
135
|
|
|
|
136
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testShouldRemoveBlockquotes(): void |
140
|
|
|
{ |
141
|
|
|
$string = '>I am a blockquote'; |
142
|
|
|
$expected = 'I am a blockquote'; |
143
|
|
|
|
144
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testShouldRemoveBlockquotesWithSpaces(): void |
148
|
|
|
{ |
149
|
|
|
$string = '> I am a blockquote'; |
150
|
|
|
$expected = 'I am a blockquote'; |
151
|
|
|
|
152
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testShouldRemoveIndentedBlockquotes(): void |
156
|
|
|
{ |
157
|
|
|
$string = ' > I am a blockquote'; |
158
|
|
|
$expected = 'I am a blockquote'; |
159
|
|
|
|
160
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
161
|
|
|
|
162
|
|
|
$string = ' > I am a blockquote'; |
163
|
|
|
|
164
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testShouldNotRemoveGreaterThanSigns(): void |
168
|
|
|
{ |
169
|
|
|
$tests = [ |
170
|
|
|
['string' => '100 > 0', 'expected' => '100 > 0'], |
171
|
|
|
['string' => '100 >= 0', 'expected' => '100 >= 0'], |
172
|
|
|
['string' => '100>0', 'expected' => '100>0'], |
173
|
|
|
['string' => '> 100 > 0', 'expected' => '100 > 0'], |
174
|
|
|
['string' => '1 < 100', 'expected' => '1 < 100'], |
175
|
|
|
['string' => '1 <= 100', 'expected' => '1 <= 100'], |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
foreach ($tests as $test) { |
179
|
|
|
$this->assertSame( |
180
|
|
|
$test['expected'], |
181
|
|
|
(new MarkdownRemover())->process(Text::utf8($test['string']))->getContent() |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testShouldStripUnorderedListLeaders(): void |
187
|
|
|
{ |
188
|
|
|
$string = "Some text on a line\n\n* A list Item\n* Another list item"; |
189
|
|
|
$expected = "Some text on a line\n\nA list Item\nAnother list item"; |
190
|
|
|
|
191
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testShouldStripOrderedListLeaders(): void |
195
|
|
|
{ |
196
|
|
|
$string = "Some text on a line\n\n* A list Item\n* Another list item"; |
197
|
|
|
$expected = "Some text on a line\n\nA list Item\nAnother list item"; |
198
|
|
|
|
199
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testShouldStripOrderedListLeadersKeepingIndentation(): void |
203
|
|
|
{ |
204
|
|
|
$string = "Some text on a line\n\n* A list Item\n * Another list item"; |
205
|
|
|
$expected = "Some text on a line\n\n A list Item\n Another list item"; |
206
|
|
|
|
207
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testShouldStripCodeBlocks(): void |
211
|
|
|
{ |
212
|
|
|
$string = <<<CODE |
213
|
|
|
```js |
214
|
|
|
test |
215
|
|
|
``` |
216
|
|
|
CODE; |
217
|
|
|
|
218
|
|
|
$expected = <<<CODE |
219
|
|
|
|
220
|
|
|
test |
221
|
|
|
|
222
|
|
|
CODE; |
223
|
|
|
|
224
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testShouldHandleParagraphsWithMarkdown(): void |
228
|
|
|
{ |
229
|
|
|
$string = "\n## This is a heading ##\n\nThis is a paragraph with [a link](http://www.disney.com/).\n\n### This is another heading\n\nIn `Getting Started` we set up `something` foo.\n\n * Some list\n * With items\n * Even indented"; |
230
|
|
|
$expected = "\nThis is a heading\n\nThis is a paragraph with a link.\n\nThis is another heading\n\nIn Getting Started we set up something foo.\n\n Some list\n With items\n Even indented"; |
231
|
|
|
|
232
|
|
|
$this->assertSame($expected, (new MarkdownRemover())->process(Text::utf8($string))->getContent()); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|