Skip to content

Commit

Permalink
Merge pull request #626 from phpDocumentor/fix/absolute_image_url
Browse files Browse the repository at this point in the history
Fix: allow absolute image urls
  • Loading branch information
linawolf authored Oct 21, 2023
2 parents 9cf400b + 42ce7ff commit 476d691
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<img
src="{{ asset(node.value) }}"
src="{%- if node.value is external_target -%} {{ node.value }} {%- else -%} {{ asset(node.value) }} {%- endif -%}"
{% if node.hasOption('width') %}width="{{ node.option('width') }}"{% endif%}
{% if node.hasOption('height') %}height="{{ node.option('height') }}"{% endif%}
{% if node.hasOption('align') %}align="{{ node.option('align') }}"{% endif%}
Expand Down
7 changes: 7 additions & 0 deletions packages/guides/src/Twig/AssetsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace phpDocumentor\Guides\Twig;

use League\Flysystem\Exception;
use League\Uri\Uri;
use League\Uri\UriInfo;
use LogicException;
use phpDocumentor\Guides\Meta\InternalTarget;
use phpDocumentor\Guides\Meta\Target;
Expand All @@ -26,6 +28,7 @@
use phpDocumentor\Guides\UrlGeneratorInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Stringable;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\TwigTest;
Expand Down Expand Up @@ -66,6 +69,10 @@ public function getTests(): array
/** @param mixed $value */
static fn (mixed $value): bool => $value instanceof Node,
),
new TwigTest(
'external_target',
static fn (string|Stringable $value): bool => UriInfo::isAbsolute(Uri::createFromString($value)),
),
];
}

Expand Down
4 changes: 4 additions & 0 deletions packages/guides/src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ final class UrlGenerator implements UrlGeneratorInterface
public function absoluteUrl(string $basePath, string $url): string
{
$uri = UriFactory::createUri($url);
if (UriInfo::isAbsolute($uri)) {
return $url;
}

if (UriInfo::isAbsolutePath($uri)) {
return $url;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Functional/tests/image-fullurl/image-fullurl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Test</p>
<img src="https://img.shields.io/github/contributors/ORG/REPO" />
3 changes: 3 additions & 0 deletions tests/Functional/tests/image-fullurl/image-fullurl.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test

.. image:: https://img.shields.io/github/contributors/ORG/REPO

0 comments on commit 476d691

Please sign in to comment.