Skip to content
Open
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
35 changes: 27 additions & 8 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ function setSafeMode($safeMode)
'steam:',
);

protected $useTargetBlankLinks = false;

function setTargetBlankLinks($useTargetBlankLinks)
{
$this->useTargetBlankLinks = (bool)$useTargetBlankLinks;

return $this;
}

#
# Lines
#
Expand Down Expand Up @@ -1154,6 +1163,16 @@ protected function inlineCode($Excerpt)
}
}

protected function updateLinkAttributes($attributes)
{
if ($this->useTargetBlankLinks)
{
$attributes['target'] = '_blank';
$attributes['rel'] = 'noopener noreferrer';
}
return $attributes;
}

protected function inlineEmailTag($Excerpt)
{
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
Expand All @@ -1170,9 +1189,9 @@ protected function inlineEmailTag($Excerpt)
'element' => array(
'name' => 'a',
'text' => $matches[1],
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);
}
Expand Down Expand Up @@ -1262,10 +1281,10 @@ protected function inlineLink($Excerpt)
'handler' => 'line',
'nonNestables' => array('Url', 'Link'),
'text' => null,
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => null,
'title' => null,
),
)),
);

$extent = 0;
Expand Down Expand Up @@ -1417,9 +1436,9 @@ protected function inlineUrl($Excerpt)
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);

Expand All @@ -1438,9 +1457,9 @@ protected function inlineUrlTag($Excerpt)
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'attributes' => $this->updateLinkAttributes(array(
'href' => $url,
),
)),
),
);
}
Expand Down