mirror of
https://github.com/humhub/humhub.git
synced 2025-02-24 11:14:00 +01:00
Minor RichText widgets improvements
This commit is contained in:
parent
7464bb83fe
commit
1f25f95620
@ -26,6 +26,11 @@ class RichText extends JsWidget
|
|||||||
*/
|
*/
|
||||||
public $text = "";
|
public $text = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string original text before parsed
|
||||||
|
*/
|
||||||
|
public $originalText = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
@ -63,38 +68,14 @@ class RichText extends JsWidget
|
|||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
|
$this->originalText = $this->text;
|
||||||
|
|
||||||
if ($this->encode) {
|
if ($this->encode) {
|
||||||
$this->text = Html::encode($this->text);
|
$this->text = Html::encode($this->text);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->minimal && !$this->edit) {
|
if (!$this->minimal && !$this->edit) {
|
||||||
$maxOembedCount = 3; // Maximum OEmbeds
|
$this->translateOmbed();
|
||||||
$oembedCount = 0; // OEmbeds used
|
|
||||||
$that = $this;
|
|
||||||
|
|
||||||
$pattern= <<<REGEXP
|
|
||||||
/(?(R) # in case of recursion match parentheses
|
|
||||||
\(((?>[^\s()]+)|(?R))*\)
|
|
||||||
| # else match a link with title
|
|
||||||
(https?|ftp):\/\/(([^\s()]+)|(?R))+(?<![\.,:;\'"!\?\s])
|
|
||||||
)/x
|
|
||||||
REGEXP;
|
|
||||||
$this->text = preg_replace_callback($pattern, function ($match) use (&$oembedCount, &$maxOembedCount, &$that) {
|
|
||||||
|
|
||||||
// Try use oembed
|
|
||||||
if ($maxOembedCount > $oembedCount) {
|
|
||||||
$oembed = UrlOembed::GetOEmbed($match[0]);
|
|
||||||
if ($oembed) {
|
|
||||||
$oembedCount++;
|
|
||||||
return $oembed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$options = strpos($match[0], Yii::$app->settings->get('baseUrl')) === 0 ? [] : ['target' => '_blank', 'rel' => "noopener noreferrer"];
|
|
||||||
|
|
||||||
// The markdown parser will parse the links by itself
|
|
||||||
return ($this->markdown) ? $match[0] : Html::a($match[0], Html::decode($match[0]), $options);
|
|
||||||
}, $this->text);
|
|
||||||
|
|
||||||
// mark emails
|
// mark emails
|
||||||
$this->text = preg_replace_callback('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})/', function ($match) {
|
$this->text = preg_replace_callback('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})/', function ($match) {
|
||||||
@ -130,6 +111,40 @@ REGEXP;
|
|||||||
return trim($output);
|
return trim($output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates Ombed links
|
||||||
|
*/
|
||||||
|
protected function translateOmbed()
|
||||||
|
{
|
||||||
|
$maxOembedCount = 3; // Maximum OEmbeds
|
||||||
|
$oembedCount = 0; // OEmbeds used
|
||||||
|
$that = $this;
|
||||||
|
|
||||||
|
$pattern = <<<REGEXP
|
||||||
|
/(?(R) # in case of recursion match parentheses
|
||||||
|
\(((?>[^\s()]+)|(?R))*\)
|
||||||
|
| # else match a link with title
|
||||||
|
(https?|ftp):\/\/(([^\s()]+)|(?R))+(?<![\.,:;\'"!\?\s])
|
||||||
|
)/x
|
||||||
|
REGEXP;
|
||||||
|
$this->text = preg_replace_callback($pattern, function ($match) use (&$oembedCount, &$maxOembedCount, &$that) {
|
||||||
|
|
||||||
|
// Try use oembed
|
||||||
|
if ($maxOembedCount > $oembedCount) {
|
||||||
|
$oembed = UrlOembed::GetOEmbed($match[0]);
|
||||||
|
if ($oembed) {
|
||||||
|
$oembedCount++;
|
||||||
|
return $oembed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = strpos($match[0], Yii::$app->settings->get('baseUrl')) === 0 ? [] : ['target' => '_blank', 'rel' => "noopener noreferrer"];
|
||||||
|
|
||||||
|
// The markdown parser will parse the links by itself
|
||||||
|
return ($this->markdown) ? $match[0] : Html::a($match[0], Html::decode($match[0]), $options);
|
||||||
|
}, $this->text);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace emojis from text to img tag
|
* Replace emojis from text to img tag
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user