Skip to content

Commit

Permalink
Merge pull request #483 from pinkary-project/fix/hashtags-and-code-bl…
Browse files Browse the repository at this point in the history
…ocks

feat(hashtags) Do not alter #... found within code/pre blocks
  • Loading branch information
nunomaduro authored Aug 4, 2024
2 parents 3240d84 + 277a603 commit b1596bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
public function parse(string $content): string
{
return (string) preg_replace_callback(
'/(<a\s+[^>]*>.*?<\/a>)|(?<!&)#([a-z0-9_]+)/i',
'/(<(a|code|pre)\s+[^>]*>.*?<\/\2>)|(?<!&)#([a-z0-9_]+)/is',
fn (array $matches): string => $matches[1] !== ''
? $matches[1]
: '<span class="text-blue-500">#'.$matches[2].'</span>',
: '<span class="text-blue-500">#'.$matches[3].'</span>',
$content
);
}
Expand Down
16 changes: 14 additions & 2 deletions tests/Unit/Services/ContentProvidersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,19 @@
'parsed' => 'Existing <a href="/route#segment">link with #segment</a> and a <span class="text-blue-500">#hashtag</span>.',
],
[
'content' => 'It&#039ll work with html escapes.',
'parsed' => 'It&#039ll work with html escapes.',
'content' => 'It&#039;ll work with html escapes.',
'parsed' => 'It&#039;ll work with html escapes.',
],
[
'content' => 'It works with <pre style="position: relative;"><code class="p-4 rounded-lg hljs php text-xs group/code" style="background-color: #23262E">
multiline $codeBlocks = <span class="hljs-keyword">true</span>;
#comment
</code></pre>
#hashtag',
'parsed' => 'It works with <pre style="position: relative;"><code class="p-4 rounded-lg hljs php text-xs group/code" style="background-color: #23262E">
multiline $codeBlocks = <span class="hljs-keyword">true</span>;
#comment
</code></pre>
<span class="text-blue-500">#hashtag</span>',
],
]);

0 comments on commit b1596bd

Please sign in to comment.