Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit f392e15

Browse files
authored
refactor: use a regular links with special attributes for external links (#19)
1 parent 4b0cbaa commit f392e15

9 files changed

+67
-69
lines changed

config/markdown.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,5 @@
187187
'max_nesting_level' => INF,
188188

189189
'link_attributes' => [],
190-
'link_renderer_view' => 'ark::external-link',
191-
'link_renderer_view_attributes' => [
192-
'inline' => true,
193-
],
190+
'link_renderer_view_attributes' => [],
194191
];

src/Extensions/Link/LinkRenderer.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,28 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
4747
$attrs['rel'] = 'noopener nofollow noreferrer';
4848
}
4949

50-
if ($this->isInternalLink($attrs['href'])) {
51-
$attrs = array_merge(Arr::only($attrs, ['href', 'id', 'class', 'name', 'title']), config('markdown.link_attributes', []));
50+
$attrs = array_merge(Arr::only($attrs, ['href', 'id', 'class', 'name', 'title']), config('markdown.link_attributes', []));
5251

53-
return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
54-
}
52+
$content = $htmlRenderer->renderInlines($inline->children());
5553

56-
$text = $attrs['title'] ?? $attrs['href'];
54+
if (! $this->isInternalLink($attrs['href'])) {
55+
$attrs['target'] = '_blank';
56+
$attrs['data-external'] = 'true';
5757

58-
// If the child is not as URL we can use it as the text for the link
59-
$children = trim($htmlRenderer->renderInlines($inline->children()));
58+
$externalLinkIcon = view('ark::icon', array_merge(
59+
config('markdown.link_renderer_view_attributes', []),
60+
[
61+
'attributes' => new ComponentAttributeBag([]),
62+
'name' => 'link',
63+
'class' => 'inline ml-1 -mt-1.5',
64+
'size' => 'sm',
65+
]
66+
));
6067

61-
if (! filter_var($children, FILTER_VALIDATE_URL)) {
62-
$text = $children;
68+
$content .= ' '.$externalLinkIcon->render();
6369
}
6470

65-
return view(config('markdown.link_renderer_view', 'ark::external-link'), array_merge(
66-
config('markdown.link_renderer_view_attributes', ['inline' => true]),
67-
[
68-
'attributes' => new ComponentAttributeBag([]),
69-
'text' => $text,
70-
'url' => $attrs['href'],
71-
]
72-
))->render();
71+
return new HtmlElement('a', $attrs, $content);
7372
}
7473

7574
public function setConfiguration(ConfigurationInterface $configuration)

tests/Extensions/Link/LinkRendererTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use League\CommonMark\HtmlRenderer;
66
use League\CommonMark\Inline\Element\Link;
77
use League\CommonMark\Inline\Element\Text;
8-
98
use League\CommonMark\Inline\Renderer\TextRenderer;
109
use League\CommonMark\Util\Configuration;
1110
use function Spatie\Snapshots\assertMatchesSnapshot;
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<a
2-
href="ftp://google.com"
3-
class="link font-semibold inline space-x-1 whitespace-nowrap"
4-
target="_blank"
5-
rel="noopener nofollow noreferrer"
6-
>
7-
<span>Label</span>
8-
</a>
1+
<a href="ftp://google.com" title="Title" target="_blank" data-external="true">Label <svg wire:key="GfLVwv0b" class="fill-current w-4 h-4 inline ml-1 -mt-1.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
2+
<path d="M23.251 7.498V.748h-6.75m6.75 0l-15 15m3-10.5h-9a1.5 1.5 0 00-1.5 1.5v15a1.5 1.5 0 001.5 1.5h15a1.5 1.5 0 001.5-1.5v-9" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
3+
</svg>
4+
</a>
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<a
2-
href="//google.com"
3-
class="link font-semibold inline space-x-1 whitespace-nowrap"
4-
target="_blank"
5-
rel="noopener nofollow noreferrer"
6-
>
7-
<span>Label</span>
8-
</a>
1+
<a href="//google.com" title="Title" target="_blank" data-external="true">Label <svg wire:key="GfLVwv0b" class="fill-current w-4 h-4 inline ml-1 -mt-1.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
2+
<path d="M23.251 7.498V.748h-6.75m6.75 0l-15 15m3-10.5h-9a1.5 1.5 0 00-1.5 1.5v15a1.5 1.5 0 001.5 1.5h15a1.5 1.5 0 001.5-1.5v-9" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
3+
</svg>
4+
</a>
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<a
2-
href="https://google.com"
3-
class="link font-semibold inline space-x-1 whitespace-nowrap"
4-
target="_blank"
5-
rel="noopener nofollow noreferrer"
6-
>
7-
<span>Label</span>
8-
</a>
1+
<a href="https://google.com" title="Title" target="_blank" data-external="true">Label <svg wire:key="GfLVwv0b" class="fill-current w-4 h-4 inline ml-1 -mt-1.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
2+
<path d="M23.251 7.498V.748h-6.75m6.75 0l-15 15m3-10.5h-9a1.5 1.5 0 00-1.5 1.5v15a1.5 1.5 0 001.5 1.5h15a1.5 1.5 0 001.5-1.5v-9" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
3+
</svg>
4+
</a>
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<a
2-
href="unsupported/relative/url"
3-
class="link font-semibold inline space-x-1 whitespace-nowrap"
4-
target="_blank"
5-
rel="noopener nofollow noreferrer"
6-
>
7-
<span>Label</span>
8-
</a>
1+
<a href="unsupported/relative/url" title="Title" target="_blank" data-external="true">Label <svg wire:key="GfLVwv0b" class="fill-current w-4 h-4 inline ml-1 -mt-1.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
2+
<path d="M23.251 7.498V.748h-6.75m6.75 0l-15 15m3-10.5h-9a1.5 1.5 0 00-1.5 1.5v15a1.5 1.5 0 001.5 1.5h15a1.5 1.5 0 001.5-1.5v-9" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
3+
</svg>
4+
</a>

tests/views/external-link.blade.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/views/icon.blade.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@props([
2+
'name',
3+
'size' => 'base',
4+
'style' => '',
5+
'class' => ''
6+
])
7+
@php
8+
$availableSizes = ['2xs', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', 'base'];
9+
10+
if (in_array($size, $availableSizes) || empty($size)) {
11+
$size = [
12+
'2xs' => 'w-2 h-2 ',
13+
'xs' => 'w-3 h-3 ',
14+
'sm' => 'w-4 h-4 ',
15+
'md' => 'w-6 h-6 ',
16+
'lg' => 'w-8 h-8 ',
17+
'xl' => 'w-12 h-12 ',
18+
'2xl' => 'w-14 h-14 ',
19+
'base' => 'w-5 h-5 ',
20+
][$size ?? 'base'];
21+
}
22+
23+
if (!empty($style)) {
24+
$style = [
25+
'secondary' => 'text-theme-secondary-500 ',
26+
'success' => 'text-theme-success-500 ',
27+
'danger' => 'text-theme-danger-500 ',
28+
][$style];
29+
}
30+
@endphp
31+
32+
<svg wire:key="GfLVwv0b" class="fill-current w-4 h-4 inline ml-1 -mt-1.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
33+
<path d="M23.251 7.498V.748h-6.75m6.75 0l-15 15m3-10.5h-9a1.5 1.5 0 00-1.5 1.5v15a1.5 1.5 0 001.5 1.5h15a1.5 1.5 0 001.5-1.5v-9" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
34+
</svg>

0 commit comments

Comments
 (0)