diff --git a/config/markdown.php b/config/markdown.php index 7b76375..6feee43 100644 --- a/config/markdown.php +++ b/config/markdown.php @@ -187,8 +187,5 @@ 'max_nesting_level' => INF, 'link_attributes' => [], - 'link_renderer_view' => 'ark::external-link', - 'link_renderer_view_attributes' => [ - 'inline' => true, - ], + 'link_renderer_view_attributes' => [], ]; diff --git a/src/Extensions/Link/LinkRenderer.php b/src/Extensions/Link/LinkRenderer.php index 0dacc3b..7270588 100644 --- a/src/Extensions/Link/LinkRenderer.php +++ b/src/Extensions/Link/LinkRenderer.php @@ -47,29 +47,28 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen $attrs['rel'] = 'noopener nofollow noreferrer'; } - if ($this->isInternalLink($attrs['href'])) { - $attrs = array_merge(Arr::only($attrs, ['href', 'id', 'class', 'name', 'title']), config('markdown.link_attributes', [])); + $attrs = array_merge(Arr::only($attrs, ['href', 'id', 'class', 'name', 'title']), config('markdown.link_attributes', [])); - return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children())); - } + $content = $htmlRenderer->renderInlines($inline->children()); - $text = $attrs['title'] ?? $attrs['href']; + if (! $this->isInternalLink($attrs['href'])) { + $attrs['target'] = '_blank'; + $attrs['data-external'] = 'true'; - // If the child is not as URL we can use it as the text for the link - $children = trim($htmlRenderer->renderInlines($inline->children())); + $externalLinkIcon = view('ark::icon', array_merge( + config('markdown.link_renderer_view_attributes', []), + [ + 'attributes' => new ComponentAttributeBag([]), + 'name' => 'link', + 'class' => 'inline ml-1 -mt-1.5', + 'size' => 'sm', + ] + )); - if (! filter_var($children, FILTER_VALIDATE_URL)) { - $text = $children; + $content .= ' '.$externalLinkIcon->render(); } - return view(config('markdown.link_renderer_view', 'ark::external-link'), array_merge( - config('markdown.link_renderer_view_attributes', ['inline' => true]), - [ - 'attributes' => new ComponentAttributeBag([]), - 'text' => $text, - 'url' => $attrs['href'], - ] - ))->render(); + return new HtmlElement('a', $attrs, $content); } public function setConfiguration(ConfigurationInterface $configuration) diff --git a/tests/Extensions/Link/LinkRendererTest.php b/tests/Extensions/Link/LinkRendererTest.php index d54de99..164c26d 100644 --- a/tests/Extensions/Link/LinkRendererTest.php +++ b/tests/Extensions/Link/LinkRendererTest.php @@ -5,7 +5,6 @@ use League\CommonMark\HtmlRenderer; use League\CommonMark\Inline\Element\Link; use League\CommonMark\Inline\Element\Text; - use League\CommonMark\Inline\Renderer\TextRenderer; use League\CommonMark\Util\Configuration; use function Spatie\Snapshots\assertMatchesSnapshot; diff --git a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(ftpgoogle.com)__1.txt b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(ftpgoogle.com)__1.txt index e2b5287..79d50c8 100644 --- a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(ftpgoogle.com)__1.txt +++ b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(ftpgoogle.com)__1.txt @@ -1,8 +1,4 @@ - - Label - +Label + + + \ No newline at end of file diff --git a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(google.com)__1.txt b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(google.com)__1.txt index 5e4e648..8d2f9ec 100644 --- a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(google.com)__1.txt +++ b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(google.com)__1.txt @@ -1,8 +1,4 @@ - - Label - +Label + + + \ No newline at end of file diff --git a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(httpsgoogle.com)__1.txt b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(httpsgoogle.com)__1.txt index ef90ca6..2d0b14d 100644 --- a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(httpsgoogle.com)__1.txt +++ b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(httpsgoogle.com)__1.txt @@ -1,8 +1,4 @@ - - Label - +Label + + + \ No newline at end of file diff --git a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(unsupportedrelativeurl)__1.txt b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(unsupportedrelativeurl)__1.txt index 39fd992..f870b82 100644 --- a/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(unsupportedrelativeurl)__1.txt +++ b/tests/__snapshots__/LinkRendererTest__it_should_render_external_links_with_(unsupportedrelativeurl)__1.txt @@ -1,8 +1,4 @@ - - Label - +Label + + + \ No newline at end of file diff --git a/tests/views/external-link.blade.php b/tests/views/external-link.blade.php deleted file mode 100644 index ecadd45..0000000 --- a/tests/views/external-link.blade.php +++ /dev/null @@ -1,15 +0,0 @@ -@props([ - 'url', - 'text', - 'class' => 'link font-semibold', - 'inline' => false, - 'allowWrap' => false, - 'small' => false, -]) - {{ $text }} - diff --git a/tests/views/icon.blade.php b/tests/views/icon.blade.php new file mode 100644 index 0000000..c639b77 --- /dev/null +++ b/tests/views/icon.blade.php @@ -0,0 +1,34 @@ +@props([ + 'name', + 'size' => 'base', + 'style' => '', + 'class' => '' +]) +@php + $availableSizes = ['2xs', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', 'base']; + + if (in_array($size, $availableSizes) || empty($size)) { + $size = [ + '2xs' => 'w-2 h-2 ', + 'xs' => 'w-3 h-3 ', + 'sm' => 'w-4 h-4 ', + 'md' => 'w-6 h-6 ', + 'lg' => 'w-8 h-8 ', + 'xl' => 'w-12 h-12 ', + '2xl' => 'w-14 h-14 ', + 'base' => 'w-5 h-5 ', + ][$size ?? 'base']; + } + + if (!empty($style)) { + $style = [ + 'secondary' => 'text-theme-secondary-500 ', + 'success' => 'text-theme-success-500 ', + 'danger' => 'text-theme-danger-500 ', + ][$style]; + } +@endphp + + + +