Merge pull request #6554 from Ixiter/develop-bbcode-tags

Fixed some bugs with weird tagging in [code] blocks and tagging of multiple ##
This commit is contained in:
Hypolite Petovan 2019-01-29 17:11:54 -05:00 committed by GitHub
commit 2bd62bfdea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View file

@ -1178,7 +1178,7 @@ class BBCode extends BaseObject
// Extracting multi-line code blocks before the whitespace processing
$codeblocks = [];
$text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#is",
$text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#ism",
function ($matches) use (&$codeblocks) {
$return = $matches[0];
if (strpos($matches[2], "\n") !== false) {

View file

@ -2387,6 +2387,17 @@ class Item extends BaseObject
public static function setHashtags(&$item)
{
// What happens in [code], stays in [code]!
// escape the # and the [
// hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3
$item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism",
function ($match) {
// we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks
$find = ['#', '['];
$replace = [chr(27).'sharp', chr(27).'leftsquarebracket'];
return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]");
}, $item["body"]);
$tags = BBCode::getTags($item["body"]);
// No hashtags?
@ -2430,12 +2441,11 @@ class Item extends BaseObject
"#$2", $item["body"]);
foreach ($tags as $tag) {
if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=')) {
if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=') || $tag[1] == '#') {
continue;
}
$basetag = str_replace('_',' ',substr($tag,1));
$newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
$item["body"] = str_replace($tag, $newtag, $item["body"]);
@ -2450,6 +2460,16 @@ class Item extends BaseObject
// Convert back the masked hashtags
$item["body"] = str_replace("#", "#", $item["body"]);
// Remember! What happens in [code], stays in [code]
// roleback the # and [
$item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism",
function ($match) {
// we truly unESCape all sharp and leftsquarebracket
$find = [chr(27).'sharp', chr(27).'leftsquarebracket'];
$replace = ['#', '['];
return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]");
}, $item["body"]);
}
public static function getGuidById($id)