]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fix an issue with links not being reversed in code blocks 674/head
authorArthurHoaro <arthur@hoa.ro>
Fri, 21 Oct 2016 10:38:38 +0000 (12:38 +0200)
committerArthurHoaro <arthur@hoa.ro>
Sat, 22 Oct 2016 09:13:48 +0000 (11:13 +0200)
Fixes #672

+ Markdown to HTML unit test

plugins/markdown/markdown.php
tests/plugins/PluginMarkdownTest.php
tests/plugins/resources/markdown.html [new file with mode: 0644]
tests/plugins/resources/markdown.md [new file with mode: 0644]

index eb7bf23d12a530f976ed7e7b5484644fb8b85619..a764b6fa88520711ec6b46183894040055e6fe3a 100644 (file)
@@ -155,8 +155,9 @@ function reverse_text2clickable($description)
     $lineCount = 0;
 
     foreach ($descriptionLines as $descriptionLine) {
-        // Detect line of code
-        $codeLineOn = preg_match('/^    /', $descriptionLine) > 0;
+        // Detect line of code: starting with 4 spaces,
+        // except lists which can start with +/*/- or `2.` after spaces.
+        $codeLineOn = preg_match('/^    +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
         // Detect and toggle block of code
         if (!$codeBlockOn) {
             $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
@@ -173,10 +174,10 @@ function reverse_text2clickable($description)
             $descriptionLine
         );
 
-        // Reverse hashtag links if we're in a code block.
-        $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : '';
+        // Reverse all links in code blocks, only non hashtag elsewhere.
+        $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
         $descriptionLine = preg_replace(
-            '!<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>!m',
+            '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
             '$1',
             $descriptionLine
         );
index 3593a5563ede8da2525352b09347035528c6d6da..12bdda24231b47a9cad0454694d030d7b4884362 100644 (file)
@@ -151,4 +151,17 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase
         $data = hook_markdown_render_daily($data);
         $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
     }
+
+    /**
+     * Test hashtag links processed with markdown.
+     */
+    function testMarkdownHashtagLinks()
+    {
+        $md = file_get_contents('tests/plugins/resources/markdown.md');
+        $md = format_description($md);
+        $html = file_get_contents('tests/plugins/resources/markdown.html');
+
+        $data = process_markdown($md);
+        $this->assertEquals($html, $data);
+    }
 }
diff --git a/tests/plugins/resources/markdown.html b/tests/plugins/resources/markdown.html
new file mode 100644 (file)
index 0000000..c0fbe7f
--- /dev/null
@@ -0,0 +1,24 @@
+<div class="markdown"><ul>
+<li>test:
+<ul>
+<li><a href="http://link.tld">zero</a></li>
+<li><a href="http://link.tld">two</a></li>
+<li><a href="http://link.tld">three</a></li>
+</ul></li>
+</ul>
+<ol>
+<li><a href="http://link.tld">zero</a>
+<ol>
+<li><a href="http://link.tld">two</a></li>
+<li><a href="http://link.tld">three</a></li>
+<li><a href="http://link.tld">four</a></li>
+<li>foo <a href="?addtag=foobar" title="Hashtag foobar">#foobar</a></li>
+</ol></li>
+</ol>
+<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>
+<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>
+<pre><code>http://link.tld #foobar
+next #foo</code></pre>
+<p>Block:</p>
+<pre><code>lorem ipsum #foobar http://link.tld
+#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 (file)
index 0000000..0b8be7c
--- /dev/null
@@ -0,0 +1,24 @@
+* test:
+    * [zero](http://link.tld)
+    + [two](http://link.tld)
+    - [three](http://link.tld)
+
+1. [zero](http://link.tld)
+  2. [two](http://link.tld)
+   3. [three](http://link.tld)
+    4. [four](http://link.tld)
+    5. foo #foobar
+
+#foobar foo `lol #foo` #bar
+
+fsdfs http://link.tld #foobar `http://link.tld`
+
+    http://link.tld #foobar
+    next #foo
+    
+Block:
+
+```
+lorem ipsum #foobar http://link.tld
+#foobar http://link.tld
+```
\ No newline at end of file