From 86e953f49566a0f4bb2220a8e5f4013044f4a49f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 7 Feb 2024 22:34:13 +0100 Subject: Fix: Preserve hard linebreaks from markdown and html --- include/html2bbcode.php | 6 +++++- tests/unit/includes/MarkdownTest.php | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/html2bbcode.php b/include/html2bbcode.php index cbfbe10ba..71ea68dae 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -192,7 +192,8 @@ function html2bbcode($message) node2bbcode($doc, 'blockquote', array(), '[quote]', '[/quote]'); - node2bbcode($doc, 'br', array(), "\n", ''); + // Use a temporary tag to keep line breaks + node2bbcode($doc, 'br', array(), '[br]', ''); node2bbcode($doc, 'p', array('class'=>'MsoNormal'), "\n", ""); node2bbcode($doc, 'div', array('class'=>'MsoNormal'), "\r", ""); @@ -302,6 +303,9 @@ function html2bbcode($message) $message = str_replace(array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'), array('[b]', '[/b]', '[i]', '[/i]'), $message); + // Restore linebreaks from temp tag + $message = str_replace('[br] ', "\n", $message); + // Handling Yahoo style of mails // $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message); diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php index c51fbfe16..87cfc2899 100644 --- a/tests/unit/includes/MarkdownTest.php +++ b/tests/unit/includes/MarkdownTest.php @@ -57,6 +57,10 @@ class MarkdownTest extends UnitTestCase { 'This text is text wrapped over multiple lines.', "This text is\ntext wrapped\nover multiple\nlines." ], + 'text with hard linebreak' => [ + "Line one\nLine two", + "Line one \nLine two" + ], 'paragraphs' => [ "Paragraph one\n\nParagraph two", "Paragraph one\n\nParagraph two", @@ -73,6 +77,10 @@ class MarkdownTest extends UnitTestCase { '[code]some code[/code]', '`some code`' ], + 'inline code with wrapped text' => [ + '[code]some code unwrapped[/code]', + "`some code\n unwrapped`" + ], 'code block no language' => [ "[code]some code\nover multiple lines[/code]", "```\nsome code\nover multiple lines\n```" -- cgit v1.2.3