support for auto linking urls with file protocol (#1402)
This commit is contained in:
@@ -16,6 +16,58 @@ class ParsedownExtension extends \Parsedown
|
|||||||
{
|
{
|
||||||
private $ids = [];
|
private $ids = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwritten to add support for file:///
|
||||||
|
*/
|
||||||
|
protected $safeLinksWhitelist = [
|
||||||
|
'file:///',
|
||||||
|
'http://',
|
||||||
|
'https://',
|
||||||
|
'ftp://',
|
||||||
|
'ftps://',
|
||||||
|
'mailto:',
|
||||||
|
'data:image/png;base64,',
|
||||||
|
'data:image/gif;base64,',
|
||||||
|
'data:image/jpeg;base64,',
|
||||||
|
'irc:',
|
||||||
|
'ircs:',
|
||||||
|
'git:',
|
||||||
|
'ssh:',
|
||||||
|
'news:',
|
||||||
|
'steam:',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwritten:
|
||||||
|
* - added support for file:///
|
||||||
|
* - open links in new windows
|
||||||
|
*/
|
||||||
|
protected function inlineUrl($Excerpt)
|
||||||
|
{
|
||||||
|
if ($this->urlsLinked !== true or !isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/\b(https?:[\/]{2}|file:[\/]{3})[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE)) {
|
||||||
|
$url = $matches[0][0];
|
||||||
|
|
||||||
|
$Inline = [
|
||||||
|
'extent' => strlen($matches[0][0]),
|
||||||
|
'position' => $matches[0][1],
|
||||||
|
'element' => [
|
||||||
|
'name' => 'a',
|
||||||
|
'text' => $url,
|
||||||
|
'attributes' => [
|
||||||
|
'href' => $url,
|
||||||
|
'target' => '_blank'
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return $Inline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function blockHeader($line)
|
protected function blockHeader($line)
|
||||||
{
|
{
|
||||||
$block = parent::blockHeader($line);
|
$block = parent::blockHeader($line);
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ class MarkdownTest extends TestCase
|
|||||||
<h1 id="test">test</h1>
|
<h1 id="test">test</h1>
|
||||||
<p>asdfasdfa</p>
|
<p>asdfasdfa</p>
|
||||||
<pre><code>ssdfsdf</code></pre>
|
<pre><code>ssdfsdf</code></pre>
|
||||||
|
<p><a href="http://example.com/foo-bar.html" target="_blank">http://example.com/foo-bar.html</a><br />
|
||||||
|
<a href="file:///home/kimai/images/beautiful-flower.png" target="_blank">file:///home/kimai/images/beautiful-flower.png</a></p>
|
||||||
<p>sdfsdf <a href="#test-1">asdfasdf</a> asdfasdf</p>
|
<p>sdfsdf <a href="#test-1">asdfasdf</a> asdfasdf</p>
|
||||||
<h1 id="test-1">test</h1>
|
<h1 id="test-1">test</h1>
|
||||||
<p>aasdfasdf<br />
|
<p>aasdfasdf<br />
|
||||||
@@ -50,6 +52,9 @@ asdfasdfa
|
|||||||
|
|
||||||
ssdfsdf
|
ssdfsdf
|
||||||
|
|
||||||
|
http://example.com/foo-bar.html
|
||||||
|
file:///home/kimai/images/beautiful-flower.png
|
||||||
|
|
||||||
sdfsdf [asdfasdf](#test-1) asdfasdf
|
sdfsdf [asdfasdf](#test-1) asdfasdf
|
||||||
|
|
||||||
# test
|
# test
|
||||||
|
|||||||
Reference in New Issue
Block a user