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