diff options
-rw-r--r-- | plugins/markdown/markdown.php | 11 | ||||
-rw-r--r-- | tests/plugins/PluginMarkdownTest.php | 13 | ||||
-rw-r--r-- | tests/plugins/resources/markdown.html | 24 | ||||
-rw-r--r-- | tests/plugins/resources/markdown.md | 24 |
4 files changed, 67 insertions, 5 deletions
diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php index eb7bf23d..a764b6fa 100644 --- a/plugins/markdown/markdown.php +++ b/plugins/markdown/markdown.php | |||
@@ -155,8 +155,9 @@ function reverse_text2clickable($description) | |||
155 | $lineCount = 0; | 155 | $lineCount = 0; |
156 | 156 | ||
157 | foreach ($descriptionLines as $descriptionLine) { | 157 | foreach ($descriptionLines as $descriptionLine) { |
158 | // Detect line of code | 158 | // Detect line of code: starting with 4 spaces, |
159 | $codeLineOn = preg_match('/^ /', $descriptionLine) > 0; | 159 | // except lists which can start with +/*/- or `2.` after spaces. |
160 | $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0; | ||
160 | // Detect and toggle block of code | 161 | // Detect and toggle block of code |
161 | if (!$codeBlockOn) { | 162 | if (!$codeBlockOn) { |
162 | $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0; | 163 | $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0; |
@@ -173,10 +174,10 @@ function reverse_text2clickable($description) | |||
173 | $descriptionLine | 174 | $descriptionLine |
174 | ); | 175 | ); |
175 | 176 | ||
176 | // Reverse hashtag links if we're in a code block. | 177 | // Reverse all links in code blocks, only non hashtag elsewhere. |
177 | $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : ''; | 178 | $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?'; |
178 | $descriptionLine = preg_replace( | 179 | $descriptionLine = preg_replace( |
179 | '!<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>!m', | 180 | '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m', |
180 | '$1', | 181 | '$1', |
181 | $descriptionLine | 182 | $descriptionLine |
182 | ); | 183 | ); |
diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php index 3593a556..12bdda24 100644 --- a/tests/plugins/PluginMarkdownTest.php +++ b/tests/plugins/PluginMarkdownTest.php | |||
@@ -151,4 +151,17 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |||
151 | $data = hook_markdown_render_daily($data); | 151 | $data = hook_markdown_render_daily($data); |
152 | $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); | 152 | $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); |
153 | } | 153 | } |
154 | |||
155 | /** | ||
156 | * Test hashtag links processed with markdown. | ||
157 | */ | ||
158 | function testMarkdownHashtagLinks() | ||
159 | { | ||
160 | $md = file_get_contents('tests/plugins/resources/markdown.md'); | ||
161 | $md = format_description($md); | ||
162 | $html = file_get_contents('tests/plugins/resources/markdown.html'); | ||
163 | |||
164 | $data = process_markdown($md); | ||
165 | $this->assertEquals($html, $data); | ||
166 | } | ||
154 | } | 167 | } |
diff --git a/tests/plugins/resources/markdown.html b/tests/plugins/resources/markdown.html new file mode 100644 index 00000000..c0fbe7f4 --- /dev/null +++ b/tests/plugins/resources/markdown.html | |||
@@ -0,0 +1,24 @@ | |||
1 | <div class="markdown"><ul> | ||
2 | <li>test: | ||
3 | <ul> | ||
4 | <li><a href="http://link.tld">zero</a></li> | ||
5 | <li><a href="http://link.tld">two</a></li> | ||
6 | <li><a href="http://link.tld">three</a></li> | ||
7 | </ul></li> | ||
8 | </ul> | ||
9 | <ol> | ||
10 | <li><a href="http://link.tld">zero</a> | ||
11 | <ol> | ||
12 | <li><a href="http://link.tld">two</a></li> | ||
13 | <li><a href="http://link.tld">three</a></li> | ||
14 | <li><a href="http://link.tld">four</a></li> | ||
15 | <li>foo <a href="?addtag=foobar" title="Hashtag foobar">#foobar</a></li> | ||
16 | </ol></li> | ||
17 | </ol> | ||
18 | <p><a href="?addtag=foobar" title="Hashtag foobar">#foobar</a> foo <code>lol #foo</code> <a href="?addtag=bar" title="Hashtag bar">#bar</a></p> | ||
19 | <p>fsdfs <a href="http://link.tld">http://link.tld</a> <a href="?addtag=foobar" title="Hashtag foobar">#foobar</a> <code>http://link.tld</code></p> | ||
20 | <pre><code>http://link.tld #foobar | ||
21 | next #foo</code></pre> | ||
22 | <p>Block:</p> | ||
23 | <pre><code>lorem ipsum #foobar http://link.tld | ||
24 | #foobar http://link.tld</code></pre></div> \ No newline at end of file | ||
diff --git a/tests/plugins/resources/markdown.md b/tests/plugins/resources/markdown.md new file mode 100644 index 00000000..0b8be7c5 --- /dev/null +++ b/tests/plugins/resources/markdown.md | |||
@@ -0,0 +1,24 @@ | |||
1 | * test: | ||
2 | * [zero](http://link.tld) | ||
3 | + [two](http://link.tld) | ||
4 | - [three](http://link.tld) | ||
5 | |||
6 | 1. [zero](http://link.tld) | ||
7 | 2. [two](http://link.tld) | ||
8 | 3. [three](http://link.tld) | ||
9 | 4. [four](http://link.tld) | ||
10 | 5. foo #foobar | ||
11 | |||
12 | #foobar foo `lol #foo` #bar | ||
13 | |||
14 | fsdfs http://link.tld #foobar `http://link.tld` | ||
15 | |||
16 | http://link.tld #foobar | ||
17 | next #foo | ||
18 | |||
19 | Block: | ||
20 | |||
21 | ``` | ||
22 | lorem ipsum #foobar http://link.tld | ||
23 | #foobar http://link.tld | ||
24 | ``` \ No newline at end of file | ||