1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Add option to open links in new tab

This commit is contained in:
Stephen Sigwart 2020-07-30 10:13:31 -04:00
parent 1610e4747c
commit 12f15fff5b

View File

@ -119,6 +119,15 @@ class Parsedown
'steam:',
);
protected $useTargetBlankLinks = false;
function setTargetBlankLinks($useTargetBlankLinks)
{
$this->useTargetBlankLinks = (bool)$useTargetBlankLinks;
return $this;
}
#
# Lines
#
@ -1274,6 +1283,16 @@ class Parsedown
}
}
protected function updateLinkAttributes($attributes)
{
if ($this->useTargetBlankLinks)
{
$attributes['target'] = '_blank';
$attributes['rel'] = 'noopener noreferrer';
}
return $attributes;
}
protected function inlineEmailTag($Excerpt)
{
$hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?';
@ -1296,9 +1315,9 @@ class Parsedown
'element' => array(
'name' => 'a',
'text' => $matches[1],
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);
}
@ -1395,10 +1414,10 @@ class Parsedown
'destination' => 'elements',
),
'nonNestables' => array('Url', 'Link'),
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => null,
'title' => null,
),
)),
);
$extent = 0;
@ -1547,9 +1566,9 @@ class Parsedown
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);
@ -1568,9 +1587,9 @@ class Parsedown
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);
}