Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not sure if this would be welcome as its kind of an extension... #867

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ The MIT License (MIT)

Copyright (c) 2013-2018 Emanuil Rusev, erusev.com

Modified by The DigitalSorceress. www.digitalsorceress.com (added external link
target optinos) 2024 under the same license and in the same spirit as the
original.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
Expand Down
62 changes: 62 additions & 0 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ protected function textElements($text)
# Setters
#

/**
* ## When enabled all parsed hyperlinks will have their target attribute set to _blank
* - This will cause all links to open in a new winwow / tab
* - If you only want links to external sites to have the target _blank then set this false
* and instead, use setExtLinksInNewWindow to true instead
* ### NOTE that this property set to true overrides setExtLinksInNewWindow
* @param bool $allLinksInNewWindow
* @return void
*/
function setAllinksInNewWindow($allLinksInNewWindow)
{
$this->allLinksInNewWindow = $allLinksInNewWindow;
}

protected $allLinksInNewWindow;

/**
* ## When enabled all external hyperlinks will have their target attribute set to _blank
* - This will cause links to external sites to open in a new winwow / tab
* - It uses the presense of http/https as the determining factor as to whether a link is "external"
* - This is a bit of a cheat but it works well for my purposes
* - If you only want links to all sites to have the target _blank then set this false
* and instead, use setAllinksInNewWindow to true
* ### NOTE that SetAllLinksInNewWindow true will override this behavior
*
* @param bool $extLinksInNewWindow
* @return void
*/
function setExtLinksInNewWindow($extLinksInNewWindow)
{
$this->extLinksInNewWindow = $extLinksInNewWindow;
}

protected $extLinksInNewWindow;

function setBreaksEnabled($breaksEnabled)
{
$this->breaksEnabled = $breaksEnabled;
Expand Down Expand Up @@ -1398,6 +1433,7 @@ protected function inlineLink($Excerpt)
'attributes' => array(
'href' => null,
'title' => null,
'target' => null,
),
);

Expand Down Expand Up @@ -1427,6 +1463,12 @@ protected function inlineLink($Excerpt)
$Element['attributes']['title'] = substr($matches[2], 1, - 1);
}

if ($this->allLinksInNewWindow || ($this->extLinksInNewWindow && str_starts_with($matches[1], 'http'))) {
$Element['attributes']['target'] = '_blank';
} else {
$Element['attributes']['target'] = '_self';
}

$extent += strlen($matches[0]);
}
else
Expand All @@ -1452,6 +1494,12 @@ protected function inlineLink($Excerpt)

$Element['attributes']['href'] = $Definition['url'];
$Element['attributes']['title'] = $Definition['title'];

if ($this->allLinksInNewWindow || ($this->extLinksInNewWindow && str_starts_with($Definition['url]'], 'http'))) {
$Element['attributes']['target'] = '_blank';
} else {
$Element['attributes']['target'] = '_self';
}
}

return array(
Expand Down Expand Up @@ -1541,6 +1589,12 @@ protected function inlineUrl($Excerpt)
) {
$url = $matches[0][0];

if ($this->allLinksInNewWindow || ($this->extLinksInNewWindow && str_starts_with($url, 'http'))) {
$target = '_blank';
} else {
$target = '_self';
}

$Inline = array(
'extent' => strlen($matches[0][0]),
'position' => $matches[0][1],
Expand All @@ -1549,6 +1603,7 @@ protected function inlineUrl($Excerpt)
'text' => $url,
'attributes' => array(
'href' => $url,
'target' => $target,
),
),
);
Expand All @@ -1563,13 +1618,20 @@ protected function inlineUrlTag($Excerpt)
{
$url = $matches[1];

if ($this->allLinksInNewWindow || ($this->extLinksInNewWindow && str_starts_with($url, 'http'))) {
$target = '_blank';
} else {
$target = '_self';
}

return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'href' => $url,
'target' => $target,
),
),
);
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ $Parsedown->setMarkupEscaped(true);

Beware that this still allows users to insert unsafe scripting vectors, ex: `[xss](javascript:alert%281%29)`.

## Link Target Features
Fork by The Digital Sorceress to add options to let one control how the target attribute is set in links

The original parsedown did not set any link target attribute at all. This leads to the default that all links are opened in the same window. For users whow want alternate behavior, two new configuration options were added:

### setExtLinksInNewWindow

With this feature enabled:

``` php
$parser->setExtLinksInNewWindow(true);
```

Links that start with http (so http and https) will be considered "external" and thus will use the _blank target attribute - meaning that they will open in a new window/tab instead of in the current window/tab

When set to false, (and assuming setAllLinksInNewWindow is not set below) all links will open in _self

When links are parsed that do not start with http/https (so relative or absolute links, they will not be targeted to _blank with this setting on (or off)

### setAllLinksInNewWindow
Functions the same as setExtLinksInNewWindow above except that ALL links whether htey start wth http or not will be forced to target=_blank and thus open in a new tab/ window.

``` php
$parser->setAllLinksInNewWindow(true);
```

This setting set to true will override the setExtLinks and force all - so if both are on then this takes prioroty.

## Questions

**How does Parsedown work?**
Expand Down