Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Sources/BBCode/Tt.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@ class Tt extends BBCode
/**
*
*/
public ?string $before = '<code class="bbc_tt">';
public ?string $type = BBCode::TYPE_UNPARSED_CONTENT;

/**
*
*/
public ?string $after = '</code>';
public ?string $content = '<code class="bbc_tt">$1</code>';

/**
*
*/
public ?string $disabled_before = '';

/**
*
*/
public ?string $disabled_after = '';
public ?string $disabled_content = '$1';
}
50 changes: 50 additions & 0 deletions Sources/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************/
Expand Down Expand Up @@ -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']);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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(
'/(?<!`)`([^`]*)`(?!`)/',
function ($matches) {
self::$placeholder_strings['markdown'][md5($matches[1])] = $matches[1];

return '`' . md5($matches[1]) . '`';
},
$string,
);

$string = preg_replace_callback(
'/(?<!`)(`{3,})([^\n]*)(\X*?)(`{3,})/',
function ($matches) {
self::$placeholder_strings['markdown'][md5($matches[2])] = $matches[2];

return $matches[1] . md5($matches[2]) . $matches[3];
},
$string,
);

return $string;
}
}
9 changes: 6 additions & 3 deletions Sources/Parsers/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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,
[
Expand Down Expand Up @@ -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:
Expand All @@ -4197,7 +4200,7 @@ protected function renderInlineCode(array $element): void
break;

case self::OUTPUT_HTML:
$this->rendered .= $code->after;
$this->rendered .= $after;
break;

default:
Expand Down
Loading