Skip to content

Commit 42ce7ff

Browse files
committed
Fix: allow absolute image urls
To be able link to external urls we need to check at multiple places how to handle the url. fixes #621
1 parent 9cf400b commit 42ce7ff

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

packages/guides/resources/template/html/body/image.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<img
2-
src="{{ asset(node.value) }}"
2+
src="{%- if node.value is external_target -%} {{ node.value }} {%- else -%} {{ asset(node.value) }} {%- endif -%}"
33
{% if node.hasOption('width') %}width="{{ node.option('width') }}"{% endif%}
44
{% if node.hasOption('height') %}height="{{ node.option('height') }}"{% endif%}
55
{% if node.hasOption('align') %}align="{{ node.option('align') }}"{% endif%}

packages/guides/src/Twig/AssetsExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace phpDocumentor\Guides\Twig;
1515

1616
use League\Flysystem\Exception;
17+
use League\Uri\Uri;
18+
use League\Uri\UriInfo;
1719
use LogicException;
1820
use phpDocumentor\Guides\Meta\InternalTarget;
1921
use phpDocumentor\Guides\Meta\Target;
@@ -26,6 +28,7 @@
2628
use phpDocumentor\Guides\UrlGeneratorInterface;
2729
use Psr\Log\LoggerInterface;
2830
use RuntimeException;
31+
use Stringable;
2932
use Twig\Extension\AbstractExtension;
3033
use Twig\TwigFunction;
3134
use Twig\TwigTest;
@@ -66,6 +69,10 @@ public function getTests(): array
6669
/** @param mixed $value */
6770
static fn (mixed $value): bool => $value instanceof Node,
6871
),
72+
new TwigTest(
73+
'external_target',
74+
static fn (string|Stringable $value): bool => UriInfo::isAbsolute(Uri::createFromString($value)),
75+
),
6976
];
7077
}
7178

packages/guides/src/UrlGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ final class UrlGenerator implements UrlGeneratorInterface
3333
public function absoluteUrl(string $basePath, string $url): string
3434
{
3535
$uri = UriFactory::createUri($url);
36+
if (UriInfo::isAbsolute($uri)) {
37+
return $url;
38+
}
39+
3640
if (UriInfo::isAbsolutePath($uri)) {
3741
return $url;
3842
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Test</p>
2+
<img src="https://img.shields.io/github/contributors/ORG/REPO" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Test
2+
3+
.. image:: https://img.shields.io/github/contributors/ORG/REPO

0 commit comments

Comments
 (0)