]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - plugins/markdown/markdown.php
Fix an issue with links not being reversed in code blocks
[github/shaarli/Shaarli.git] / plugins / markdown / markdown.php
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
         );