From 79c4c237fa65da931042b09a9a2957647abbc542 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Wed, 22 Jan 2020 17:25:11 +0100 Subject: [PATCH] support for auto linking urls with file protocol (#1402) --- src/Utils/ParsedownExtension.php | 52 ++++++++++++++++++++++++++++++++ tests/Utils/MarkdownTest.php | 5 +++ 2 files changed, 57 insertions(+) diff --git a/src/Utils/ParsedownExtension.php b/src/Utils/ParsedownExtension.php index cbf4a4da..6ecc8d5a 100644 --- a/src/Utils/ParsedownExtension.php +++ b/src/Utils/ParsedownExtension.php @@ -16,6 +16,58 @@ class ParsedownExtension extends \Parsedown { 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) { $block = parent::blockHeader($line); diff --git a/tests/Utils/MarkdownTest.php b/tests/Utils/MarkdownTest.php index 3e17ec98..56476dbb 100644 --- a/tests/Utils/MarkdownTest.php +++ b/tests/Utils/MarkdownTest.php @@ -32,6 +32,8 @@ class MarkdownTest extends TestCase

test

asdfasdfa

ssdfsdf
+

http://example.com/foo-bar.html
+file:///home/kimai/images/beautiful-flower.png

sdfsdf asdfasdf asdfasdf

test

aasdfasdf
@@ -50,6 +52,9 @@ asdfasdfa ssdfsdf +http://example.com/foo-bar.html +file:///home/kimai/images/beautiful-flower.png + sdfsdf [asdfasdf](#test-1) asdfasdf # test