From a0545f2b449fd73c745c7e50e431c29cc9f3cf7e Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Wed, 21 Oct 2026 16:04:20 -0600 Subject: [PATCH 1/2] Set the [tt] BBCode's type to BBCode::TYPE_UNPARSED_CONTENT Signed-off-by: Jon Stovell --- Sources/BBCode/Tt.php | 11 +++-------- Sources/Parsers/MarkdownParser.php | 9 ++++++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Sources/BBCode/Tt.php b/Sources/BBCode/Tt.php index 32807ea825c..b460ca7c821 100644 --- a/Sources/BBCode/Tt.php +++ b/Sources/BBCode/Tt.php @@ -32,20 +32,15 @@ class Tt extends BBCode /** * */ - public ?string $before = ''; + public ?string $type = BBCode::TYPE_UNPARSED_CONTENT; /** * */ - public ?string $after = ''; + public ?string $content = '$1'; /** * */ - public ?string $disabled_before = ''; - - /** - * - */ - public ?string $disabled_after = ''; + public ?string $disabled_content = '$1'; } diff --git a/Sources/Parsers/MarkdownParser.php b/Sources/Parsers/MarkdownParser.php index c22a5315d11..89cf1344f3b 100644 --- a/Sources/Parsers/MarkdownParser.php +++ b/Sources/Parsers/MarkdownParser.php @@ -3403,7 +3403,6 @@ protected function renderCodeBlock(array $element): void foreach (BBCodeParser::getCodes() as $code) { if ($code->tag === 'code' && $code->type === $bbc_type) { - list($before, $after) = preg_split('/\$1/', $code->content); break; } } @@ -3412,6 +3411,8 @@ protected function renderCodeBlock(array $element): void $code = $this->disableCode($code); } + list($before, $after) = preg_split('/\$1/', $code->content); + $this->rendered .= strtr( $before, [ @@ -4179,7 +4180,9 @@ protected function renderInlineCode(array $element): void $code = $this->disableCode($code); } - $this->rendered .= $code->before; + list($before, $after) = preg_split('/\$1/', $code->content); + + $this->rendered .= $before; break; default: @@ -4197,7 +4200,7 @@ protected function renderInlineCode(array $element): void break; case self::OUTPUT_HTML: - $this->rendered .= $code->after; + $this->rendered .= $after; break; default: From af16edf89fa7fc70a5512a84bd31fcfc803d7f8e Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Wed, 21 Oct 2026 16:58:50 -0600 Subject: [PATCH 2/2] Protects contents of Markdown code blocks and spans from other parsers Signed-off-by: Jon Stovell --- Sources/Parser.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Sources/Parser.php b/Sources/Parser.php index 7646afe9c9f..f3d92569b78 100644 --- a/Sources/Parser.php +++ b/Sources/Parser.php @@ -254,6 +254,18 @@ abstract class Parser */ private static array $results = []; + /** + * @var array + * + * Placeholders used to protect certain strings from processing + * by other parsers. + */ + private static array $placeholder_strings = [ + 'bbcode' => [], + 'smileys' => [], + 'markdown' => [], + ]; + /**************** * Public methods ****************/ @@ -994,6 +1006,11 @@ protected static function toHTML(string $string, int $input_types, array $option $input_types = $input_types | ($smileys ? self::INPUT_SMILEYS : 0); + // Protect the content of Markdown code blocks and spans. + if ($input_types & self::INPUT_MARKDOWN) { + $string = self::protectMarkdownCode($string); + } + // Parse the BBCode. if ($input_types & self::INPUT_BBC) { $string = BBcodeParser::load(!empty($options['for_print']))->parse($string, !empty($input_types & self::INPUT_SMILEYS), $options['cache_id'], $options['parse_tags']); @@ -1009,6 +1026,7 @@ protected static function toHTML(string $string, int $input_types, array $option // Parse the Markdown. if ($input_types & self::INPUT_MARKDOWN) { + $string = strtr($string, self::$placeholder_strings['markdown']); $string = MarkdownParser::load(self::OUTPUT_HTML)->parse($string, true, $options); } @@ -1338,4 +1356,36 @@ protected static function fixTag(string $message, string $tag, array $protocols, return $message; } + + /** + * Replaces the contents of Markdown code blocks and spans with placeholders + * to protect them from being parsed by BBCodeParser and/or SmileyParser. + * + * @param string $string A string that might contain Markdown code. + * @return string Updated version of the string. + */ + protected static function protectMarkdownCode(string $string): string + { + $string = preg_replace_callback( + '/(?