aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2024-10-02 20:04:30 +0000
committerMario <mario@mariovavti.com>2024-10-02 20:04:30 +0000
commit0f3e01f3436077ab20bebbf748367ac7e19264c1 (patch)
tree36d0b5e3816f7c3667c5ac390982055dd93bd448
parent79220ede662241f45d95010ab0aef70423fda4eb (diff)
downloadvolse-hubzilla-0f3e01f3436077ab20bebbf748367ac7e19264c1.tar.gz
volse-hubzilla-0f3e01f3436077ab20bebbf748367ac7e19264c1.tar.bz2
volse-hubzilla-0f3e01f3436077ab20bebbf748367ac7e19264c1.zip
markdown: Don't link URLs in code blocks.
When passing a content throught the `markdown_to_bb` function to convert any markdown in the content, any recognized URLs in the content would be converted to BBCode links as a post processing step after the main conversion. After commit a1ccacb825edac6ae36e5db4f62ebfe7aeaebe9f this did no longer consider content within code blocks, and would thus convert them as to BBCode links. Example: The following content [code] example url: https://example.com [/code] Would be converted to [code] example url: [url=https://example.com]https://example.com[/url] [/code] Prior to commit a1ccacb825edac6ae36e5db4f62ebfe7aeaebe9f, code blocks would be protected, so this would not happen. This patch removes the post processing step for converting plain URLs to links completely from this routine. This functionality is in any case covered in the actual BBCode parser where it belongs. This will have some other side effects as well, such as images and links created using Markdown, will not be converted to [zmg] or [zrl] tags where that would be done automatically before. If you intend to use a [zrl] or [zmg] tag, you now need to do so explicitly. (cherry picked from commit 803cd74b4881a617a56be4fb5780d6d25fd5433f) Co-authored-by: Harald Eilertsen <haraldei@anduin.net>
-rw-r--r--include/markdown.php16
-rw-r--r--tests/unit/includes/BBCodeTest.php14
-rw-r--r--tests/unit/includes/MarkdownTest.php8
3 files changed, 21 insertions, 17 deletions
diff --git a/include/markdown.php b/include/markdown.php
index b2adcd0d5..90d671fe4 100644
--- a/include/markdown.php
+++ b/include/markdown.php
@@ -80,22 +80,6 @@ function markdown_to_bb($s, $use_zrl = false, $options = []) {
$s = html2bbcode($s);
- // $s = bb_code_protect($s);
-
- // Convert everything that looks like a link to a link
- if($use_zrl) {
- if (strpos($s,'[/img]') !== false) {
- $s = preg_replace_callback("/\[img\](.*?)\[\/img\]/ism", 'use_zrl_cb_img', $s);
- $s = preg_replace_callback("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", 'use_zrl_cb_img_x', $s);
- }
- $s = preg_replace_callback("/([^\]\=\{\/]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)([\,\.\:\;]\s|$)/ismu", 'use_zrl_cb_link',$s);
- }
- else {
- $s = preg_replace("/([^\]\=\{\/]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)([\,\.\:\;]\s|$)/ismu", '$1[url=$2$3]$2$3[/url]$4',$s);
- }
-
- // $s = bb_code_unprotect($s);
-
// remove duplicate adjacent code tags
$s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php
index 30ea00ba6..c6a60f35b 100644
--- a/tests/unit/includes/BBCodeTest.php
+++ b/tests/unit/includes/BBCodeTest.php
@@ -138,7 +138,15 @@ class BBCodeTest extends UnitTestCase {
'del tag' => [
'some [s]strike through[/s] text',
'some <del>strike through</del> text'
- ]
+ ],
+ 'naked url is converted to link' => [
+ 'example url: https://example.com',
+ 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>'
+ ],
+ 'naked url within code block is not converted to link' => [
+ "[code]\nhttp://example.com\n[/code]",
+ "<pre><code>http://example.com</code></pre>"
+ ],
];
}
@@ -223,6 +231,10 @@ class BBCodeTest extends UnitTestCase {
"<pre><code>some\n indented\ncode</code></pre>",
"[code]some\n indented\ncode[/code]"
],
+ 'code block with URL' => [
+ '<pre><code>\nproxy_pass http://example.com\n</code></pre>',
+ '[code]\nproxy_pass http://example.com\n[/code]'
+ ],
'paragraph with a mention and some text' => [
'<p><span class="h-card" translate="no"><a href="https://example.org/@profile" class="u-url mention">@<span>profile</span></a></span> some content</p>',
'[url=https://example.org/@profile]@profile[/url] some content'
diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php
index 310130bf1..217d12ca2 100644
--- a/tests/unit/includes/MarkdownTest.php
+++ b/tests/unit/includes/MarkdownTest.php
@@ -93,6 +93,14 @@ class MarkdownTest extends UnitTestCase {
"[code=php]&lt;?php\necho phpinfo();[/code]",
"```php\n<?php\necho phpinfo();\n```"
],
+ 'code block with URL' => [
+ "[code]an example url https://example.com[/code]",
+ "```\nan example url https://example.com\n```"
+ ],
+ 'bbcode code block with URL' => [
+ "[code] proxy_pass http://example.com; [/code]",
+ "[code]\nproxy_pass http://example.com;\n[/code]"
+ ]
];
}