diff --git a/Sources/BBCode/Tt.php b/Sources/BBCode/Tt.php index 32807ea825..b460ca7c82 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/Parser.php b/Sources/Parser.php index 7646afe9c9..f3d92569b7 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( + '/(?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: