From 687cda367316edb9f84b3e425e76c9e9279e96be Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Oct 2024 18:15:54 +0200 Subject: Fix regex to detect URLs in cleanup_bbcode. This fixes the issue where the text after the URL would be included in the link if it was immediately followed by a newline. Example: https://example.com this is a test. Would become: #^[url=https://example.com_this]https://example.com_this[/url] is a test --- include/text.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/text.php b/include/text.php index 137622b7d..e69ce7d10 100644 --- a/include/text.php +++ b/include/text.php @@ -3755,12 +3755,9 @@ function cleanup_bbcode($body) { $body = preg_replace_callback('/\[img(.*?)\[\/(img)\]/ism','\red_escape_codeblock',$body); $body = preg_replace_callback('/\[zmg(.*?)\[\/(zmg)\]/ism','\red_escape_codeblock',$body); - $body = preg_replace_callback("/([^\]\='".'"'."\;\/\{]|^|\#\^)(https?\:\/\/[a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\\ -+\,\(\)]+)/ismu", '\nakedoembed', $body); - - $body = preg_replace_callback("/([^\]\='".'"'."\;\/\{]|^|\#\^)(https?\:\/\/[a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\\ -+\,\(\)]+)/ismu", '\red_zrl_callback', $body); + $body = preg_replace_callback("/([^\]\='".'"'."\;\/\{]|^|\#\^)(https?\:\/\/[a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\\+\,\(\)]+)/ismu", '\nakedoembed', $body); + $body = preg_replace_callback("/([^\]\='".'"'."\;\/\{]|^|\#\^)(https?\:\/\/[a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\\+\,\(\)]+)/ismu", '\red_zrl_callback', $body); $body = preg_replace_callback('/\[\$b64code(.*?)\[\/(code)\]/ism','\red_unescape_codeblock',$body); $body = preg_replace_callback('/\[\$b64summary(.*?)\[\/(summary)\]/ism','\red_unescape_codeblock',$body); -- cgit v1.2.3 From ecde4a3dede85bfa0fa32a91c51c335716c68c4c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Oct 2024 18:19:43 +0200 Subject: Change config used by Markdown tests to reflect actual use. This makes the configuration used align better with how it is being used in the mdpost addon. This also reveals some issues that are less than ideal for Markdown posts. The relevant test cases have been adjusted to pass with the new config, but have been commented. --- tests/unit/includes/BBCodeTest.php | 8 ++++++++ tests/unit/includes/MarkdownTest.php | 33 +++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index c6a60f35b..136fc6e0e 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -143,6 +143,14 @@ class BBCodeTest extends UnitTestCase { 'example url: https://example.com', 'example url: https://example.com' ], + 'naked url followed by newline' => [ + "https://www.example.com\nhave a great day.", + 'https://www.example.com
have a great day.', + ], + 'inline naked url' => [ + "This is a link https://example.com/some/path more info.", + 'This is a link https://example.com/some/path more info.', + ], 'naked url within code block is not converted to link' => [ "[code]\nhttp://example.com\n[/code]", "
http://example.com
" diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php index 217d12ca2..55dbb4445 100644 --- a/tests/unit/includes/MarkdownTest.php +++ b/tests/unit/includes/MarkdownTest.php @@ -36,7 +36,7 @@ class MarkdownTest extends UnitTestCase { * @dataProvider markdown_to_bbcode_provider */ public function test_markdown_to_bbcode(string $expected, string $src): void { - $this->assertEquals($expected, markdown_to_bb($src)); + $this->assertEquals($expected, markdown_to_bb($src, true, ['preserve_lf' => true])); } public static function markdown_to_bbcode_provider(): array { @@ -54,11 +54,14 @@ class MarkdownTest extends UnitTestCase { 'This is a test of **bold text**, *italic text* and ***bold and italic text***' ], 'multiline text' => [ - 'This text is text wrapped over multiple lines.', + // This is not as expected in markdown, but may be needed + // for compatibility with bbcode behaviour. + "This text is\ntext wrapped\nover multiple\nlines.", "This text is\ntext wrapped\nover multiple\nlines." ], 'text with hard linebreak' => [ - "Line one\nLine two", + // An extra line break is inserted here... + "Line one\n\nLine two", "Line one \nLine two" ], 'paragraphs' => [ @@ -78,29 +81,39 @@ class MarkdownTest extends UnitTestCase { '`some code`' ], 'inline code with wrapped text' => [ - '[code]some code unwrapped[/code]', + // Not sure if the newline should be preseved here? + "[code]some code\nunwrapped[/code]", "`some code\n unwrapped`" ], 'code block no language' => [ - "[code]some code\nover multiple lines[/code]", + "[code]some code\nover multiple lines\n[/code]", "```\nsome code\nover multiple lines\n```" ], 'code block no language indented' => [ - "[code]some code\n over multiple lines\n with indentation[/code]", + // For some reason one space char is eaten on indented lines. + "[code]some code\n over multiple lines\n with indentation\n[/code]", "```\nsome code\n over multiple lines\n with indentation\n```" ], 'code block with language' => [ - "[code=php]<?php\necho phpinfo();[/code]", + "[code=php]<?php\necho phpinfo();\n[/code]", "```php\n [ - "[code]an example url https://example.com[/code]", + "[code]an example url https://example.com\n[/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]", "[code]\nproxy_pass http://example.com;\n[/code]" - ] + ], + 'naked url followed by newline' => [ + "https://example.com\nhave a great day.", + "https://example.com\nhave a great day.", + ], + 'inline naked url' => [ + 'This is a link https://example.com/some/path more info.', + 'This is a link https://example.com/some/path more info.', + ], ]; } -- cgit v1.2.3 From d1648927b52b537314b470fdb9b5001ed6809c4f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Oct 2024 18:23:30 +0200 Subject: Fix some whitespace issues in zid.php. No functional change. --- include/zid.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/include/zid.php b/include/zid.php index 159a3b834..2b5d53916 100644 --- a/include/zid.php +++ b/include/zid.php @@ -261,25 +261,25 @@ function zidify_text($s) { */ function red_zrl_callback($matches) { - // Catch and exclude trailing punctuation - preg_match("/[.,;:!?)]*$/i", $matches[2], $pts); - $matches[2] = substr($matches[2], 0, strlen($matches[2])-strlen($pts[0])); + // Catch and exclude trailing punctuation + preg_match("/[.,;:!?)]*$/i", $matches[2], $pts); + $matches[2] = substr($matches[2], 0, strlen($matches[2])-strlen($pts[0])); - $zrl = is_matrix_url($matches[2]); + $zrl = is_matrix_url($matches[2]); - $t = strip_zids($matches[2]); - if($t !== $matches[2]) { - $zrl = true; - $matches[2] = $t; - } + $t = strip_zids($matches[2]); + if($t !== $matches[2]) { + $zrl = true; + $matches[2] = $t; + } - if($matches[1] === '#^') - $matches[1] = ''; + if($matches[1] === '#^') + $matches[1] = ''; - if($zrl) - return $matches[1] . '#^[zrl=' . $matches[2] . ']' . $matches[2] . '[/zrl]' . $pts[0]; + if($zrl) + return $matches[1] . '#^[zrl=' . $matches[2] . ']' . $matches[2] . '[/zrl]' . $pts[0]; - return $matches[1] . '#^[url=' . $matches[2] . ']' . $matches[2] . '[/url]' . $pts[0]; + return $matches[1] . '#^[url=' . $matches[2] . ']' . $matches[2] . '[/url]' . $pts[0]; } /** -- cgit v1.2.3