]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fix hashtags with markdown escape enabled 1212/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 14 Aug 2018 10:26:51 +0000 (12:26 +0200)
committerArthurHoaro <arthur@hoa.ro>
Tue, 14 Aug 2018 10:26:51 +0000 (12:26 +0200)
They're now transformed to markdown syntax links before processing them through Parsedown.

Fixes #1210

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

index 821bb12511bf15b98aba0dab6b757c0fc15079a5..21972814c046182f3e818a83787ff92111e96d19 100644 (file)
@@ -215,6 +215,15 @@ function reverse_text2clickable($description)
             $descriptionLine
         );
 
+        // Make hashtag links markdown ready, otherwise the links will be ignored with escape set to true
+        if (!$codeBlockOn && !$codeLineOn) {
+            $descriptionLine = preg_replace(
+                '#<a href="([^ ]*)"'. $hashtagTitle .'>([^<]+)</a>#m',
+                '[$2]($1)',
+                $descriptionLine
+            );
+        }
+
         $descriptionOut .= $descriptionLine;
         if ($lineCount++ < count($descriptionLines) - 1) {
             $descriptionOut .= PHP_EOL;
index b31e817fe6093c85e7a5dbb57813a1d6f2ec0ad0..319a94ba835ccfaeadfc917e487d35cb7a584165 100644 (file)
@@ -106,6 +106,18 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($text, $reversedText);
     }
 
+    /**
+     * Test reverse_text2clickable().
+     */
+    public function testReverseText2clickableHashtags()
+    {
+        $text = file_get_contents('tests/plugins/resources/hashtags.raw');
+        $md = file_get_contents('tests/plugins/resources/hashtags.md');
+        $clickableText = hashtag_autolink($text);
+        $reversedText = reverse_text2clickable($clickableText);
+        $this->assertEquals($md, $reversedText);
+    }
+
     /**
      * Test reverse_nl2br().
      */
@@ -246,7 +258,7 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase
             $this->conf->get('security.markdown_escape', true),
             $this->conf->get('security.allowed_protocols')
         );
-        $this->assertEquals($html, $data);
+        $this->assertEquals($html, $data . PHP_EOL);
     }
 
     /**
diff --git a/tests/plugins/resources/hashtags.md b/tests/plugins/resources/hashtags.md
new file mode 100644 (file)
index 0000000..46326de
--- /dev/null
@@ -0,0 +1,10 @@
+[#lol](?addtag=lol)
+
+    #test
+
+`#test2`
+
+```
+bla #bli blo
+#bla
+```
diff --git a/tests/plugins/resources/hashtags.raw b/tests/plugins/resources/hashtags.raw
new file mode 100644 (file)
index 0000000..9d2dc98
--- /dev/null
@@ -0,0 +1,10 @@
+#lol
+
+    #test
+
+`#test2`
+
+```
+bla #bli blo
+#bla
+```
index f1df4e7ee96fa1aba001e0b6ee067b8958d1c61e..c3460bf7880c58c4184bc862e115029c7364b574 100644 (file)
 <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 &lt;a href=&quot;?addtag=foobar&quot; title=&quot;Hashtag foobar&quot;&gt;#foobar&lt;/a&gt;</li>
+<li>foo <a href="?addtag=foobar">#foobar</a></li>
 </ol></li>
 </ol>
-<p>&lt;a href=&quot;?addtag=foobar&quot; title=&quot;Hashtag foobar&quot;&gt;#foobar&lt;/a&gt; foo <code>lol #foo</code> &lt;a href=&quot;?addtag=bar&quot; title=&quot;Hashtag bar&quot;&gt;#bar&lt;/a&gt;</p>
-<p>fsdfs <a href="http://link.tld">http://link.tld</a> &lt;a href=&quot;?addtag=foobar&quot; title=&quot;Hashtag foobar&quot;&gt;#foobar&lt;/a&gt; <code>http://link.tld</code></p>
+<p><a href="?addtag=foobar">#foobar</a> foo <code>lol #foo</code> <a href="?addtag=bar">#bar</a></p>
+<p>fsdfs <a href="http://link.tld">http://link.tld</a> <a href="?addtag=foobar">#foobar</a> <code>http://link.tld</code></p>
 <pre><code>http://link.tld #foobar
 next #foo</code></pre>
 <p>Block:</p>
@@ -30,4 +30,4 @@ next #foo</code></pre>
 <a href="ftp://test.tld/path/?query=value#hash">link</a><br />
 <a href="magnet:test.tld/path/?query=value#hash">link</a><br />
 <a href="http://alert(&#039;xss&#039;)">link</a><br />
-<a href="http://test.tld/path/?query=value#hash">link</a></p></div>
\ No newline at end of file
+<a href="http://test.tld/path/?query=value#hash">link</a></p></div>
index b8ebd9340870e338a8a125d1522f8fe96970cc15..9350a8c7dd9f4c7011d85f64dc6abba92eca3401 100644 (file)
@@ -31,4 +31,4 @@ lorem ipsum #foobar http://link.tld
 [link](ftp://test.tld/path/?query=value#hash)
 [link](magnet:test.tld/path/?query=value#hash)
 [link](javascript:alert('xss'))
-[link](other://test.tld/path/?query=value#hash)
\ No newline at end of file
+[link](other://test.tld/path/?query=value#hash)