From 803cd74b4881a617a56be4fb5780d6d25fd5433f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 30 Sep 2024 13:31:42 +0200 Subject: 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. --- tests/unit/includes/BBCodeTest.php | 14 +++++++++++++- tests/unit/includes/MarkdownTest.php | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'tests/unit/includes') 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 strike through text' - ] + ], + 'naked url is converted to link' => [ + 'example url: https://example.com', + 'example url: https://example.com' + ], + 'naked url within code block is not converted to link' => [ + "[code]\nhttp://example.com\n[/code]", + "
http://example.com
" + ], ]; } @@ -223,6 +231,10 @@ class BBCodeTest extends UnitTestCase { "
some\n    indented\ncode
", "[code]some\n indented\ncode[/code]" ], + 'code block with URL' => [ + '
\nproxy_pass http://example.com\n
', + '[code]\nproxy_pass http://example.com\n[/code]' + ], 'paragraph with a mention and some text' => [ '

@profile some content

', '[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]<?php\necho phpinfo();[/code]", "```php\n [ + "[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]" + ] ]; } -- cgit v1.2.3