Replace Diaspora mentions before the parsing to HTML in Text\Markdown
This commit is contained in:
parent
4df567ebce
commit
b39872578d
|
@ -53,6 +53,8 @@ class Markdown
|
||||||
return $url;
|
return $url;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$text = self::convertDiasporaMentionsToHtml($text);
|
||||||
|
|
||||||
$html = $MarkdownParser->transform($text);
|
$html = $MarkdownParser->transform($text);
|
||||||
|
|
||||||
DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack());
|
DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack());
|
||||||
|
@ -61,35 +63,42 @@ class Markdown
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback function to replace a Diaspora style mention in a mention for Friendica
|
* Replace Diaspora-style mentions in a text since they trip the Markdown parser autolinker.
|
||||||
*
|
*
|
||||||
* @param array $match Matching values for the callback
|
* @param string $text
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function convertDiasporaMentionsToHtml(string $text)
|
||||||
|
{
|
||||||
|
return preg_replace_callback(
|
||||||
|
'/([@!]){(?:([^}]+?); ?)?([^} ]+)}/',
|
||||||
|
/*
|
||||||
|
* Matching values for the callback
|
||||||
* [1] = mention type (@ or !)
|
* [1] = mention type (@ or !)
|
||||||
* [2] = name (optional)
|
* [2] = name (optional)
|
||||||
* [3] = address
|
* [3] = profile URL
|
||||||
* @return string Replaced mention
|
|
||||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
||||||
* @throws \ImagickException
|
|
||||||
*/
|
*/
|
||||||
private static function diasporaMention2BBCodeCallback($match)
|
function ($matches) {
|
||||||
{
|
if ($matches[3] == '') {
|
||||||
if ($match[3] == '') {
|
return '';
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = Contact::getDetailsByAddr($match[3]);
|
$data = Contact::getDetailsByAddr($matches[3]);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = $match[2];
|
$name = $matches[2];
|
||||||
|
|
||||||
if ($name == '') {
|
if ($name == '') {
|
||||||
$name = $data['name'];
|
$name = $data['name'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $match[1] . '[url=' . $data['url'] . ']' . $name . '[/url]';
|
return $matches[1] . '<a href="' . $data['url'] . '">' . $name . '</a>';
|
||||||
|
},
|
||||||
|
$text
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -110,9 +119,6 @@ class Markdown
|
||||||
|
|
||||||
$s = self::convert($s);
|
$s = self::convert($s);
|
||||||
|
|
||||||
$regexp = "/([@!])\{(?:([^\}]+?); ?)?([^\} ]+)\}/";
|
|
||||||
$s = preg_replace_callback($regexp, ['self', 'diasporaMention2BBCodeCallback'], $s);
|
|
||||||
|
|
||||||
$s = HTML::toBBCode($s);
|
$s = HTML::toBBCode($s);
|
||||||
|
|
||||||
// protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
|
// protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
|
||||||
|
|
Loading…
Reference in a new issue