From 8b264c38fe73bbbf724cd12e4233fe831248f526 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 May 2023 08:17:23 +0000 Subject: [PATCH 1/2] Unify the heading levels --- src/Content/Text/BBCode.php | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index c59755445..89bd80202 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1403,12 +1403,32 @@ class BBCode } // Check for headers - $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '

$1

', $text); - $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '

$1

', $text); - $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '

$1

', $text); - $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '

$1

', $text); - $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '

$1

', $text); - $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '

$1

', $text); + + if ($simple_html == self::INTERNAL) { + //Ensure to always start with

if possible + $heading_count = 0; + for ($level = 6; $level > 0; $level--) { + if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) { + $heading_count++; + } + } + if ($heading_count > 0) { + $heading = min($heading_count + 3, 6); + for ($level = 6; $level > 0; $level--) { + if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) { + $text = preg_replace("(\[h$level\](.*?)\[\/h$level\])ism", "

$1

", $text); + $heading--; + } + } + } + } else { + $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '

$1

', $text); + $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '

$1

', $text); + $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '

$1

', $text); + $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '

$1

', $text); + $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '

$1

', $text); + $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '

$1

', $text); + } // Check for paragraph $text = preg_replace("(\[p\](.*?)\[\/p\])ism", '

$1

', $text); From a198108222d26cfa4a899393fb48c8a91a0c1f12 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 May 2023 09:01:52 +0000 Subject: [PATCH 2/2] Fix tests --- tests/src/Content/Text/BBCodeTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/src/Content/Text/BBCodeTest.php b/tests/src/Content/Text/BBCodeTest.php index 74d34f5a5..889b12caa 100644 --- a/tests/src/Content/Text/BBCodeTest.php +++ b/tests/src/Content/Text/BBCodeTest.php @@ -259,12 +259,12 @@ Karl Marx - Die ursprüngliche Akkumulation 'text' => '[emoji=https://fedi.underscore.world/emoji/custom/custom/heart_nb.png]:heart_nb:[/emoji]', ], 'task-12900-multiple-paragraphs' => [ - 'expectedHTML' => '

Header

This is a paragraph
with a line feed.

Second Chapter

', - 'text' => "[h1]Header[/h1][ul][*]One[*]Two[/ul]\n\nThis is a paragraph\nwith a line feed.\n\nSecond Chapter", + 'expectedHTML' => '

Header

This is a paragraph
with a line feed.

Second Chapter

', + 'text' => "[h4]Header[/h4][ul][*]One[*]Two[/ul]\n\nThis is a paragraph\nwith a line feed.\n\nSecond Chapter", ], 'task-12900-header-with-paragraphs' => [ - 'expectedHTML' => '

Header

Some Chapter

', - 'text' => '[h1]Header[/h1]Some Chapter', + 'expectedHTML' => '

Header

Some Chapter

', + 'text' => '[h4]Header[/h4]Some Chapter', ], 'bug-12842-ul-newlines' => [ 'expectedHTML' => '

This is:

',