Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This serves two purposes:
### Added
- Updated the `HydeKernel` array representation to include the Hyde version in https://github.com/hydephp/develop/pull/1786
- Registered the `cache:clear` command to make it easier to clear the cache in https://github.com/hydephp/develop/pull/1881
- Added a new `Hyperlinks::isRemote()` helper method to check if a URL is remote in https://github.com/hydephp/develop/pull/1882

### Changed
- Updated the `Serializable` trait to provide a default automatic `toArray` method in https://github.com/hydephp/develop/pull/1791
Expand All @@ -20,12 +21,14 @@ This serves two purposes:
- Improved the accessibility of the heading permalinks feature in https://github.com/hydephp/develop/pull/1803
- The `torchlight:install` command is now hidden from the command list as it's already installed in https://github.com/hydephp/develop/pull/1879
- Updated the home page fallback link in the 404 template to lead to the site root in https://github.com/hydephp/develop/pull/1880 (fixes https://github.com/hydephp/develop/issues/1781)
- Normalized remote URL checks so that protocol relative URLs `//` are consistently considered to be remote in all places in https://github.com/hydephp/develop/pull/1882 (fixes https://github.com/hydephp/develop/issues/1788)
- Updated to HydeFront v3.4 in https://github.com/hydephp/develop/pull/1803
- Realtime Compiler: Virtual routes are now managed through the service container in https://github.com/hydephp/develop/pull/1858
- Realtime Compiler: Improved the dashboard layout in https://github.com/hydephp/develop/pull/1866

### Deprecated
- The `PostAuthor::getName()` method is now deprecated and will be removed in v2. (use `$author->name` instead) in https://github.com/hydephp/develop/pull/1794
- Deprecated the `FeaturedImage::isRemote()` method in favor of the new `Hyperlinks::isRemote()` method in https://github.com/hydephp/develop/pull/1882

### Removed
- for now removed features.
Expand Down
12 changes: 10 additions & 2 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function mediaLink(string $destination, bool $validate = false): string
*/
public function asset(string $name, bool $preferQualifiedUrl = false): string
{
if (str_starts_with($name, 'http')) {
if (static::isRemote($name)) {
return $name;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public function url(string $path = ''): string
{
$path = $this->formatLink(trim($path, '/'));

if (str_starts_with($path, 'http')) {
if (static::isRemote($path)) {
return $path;
}

Expand All @@ -173,4 +173,12 @@ public function route(string $key): ?Route
{
return $this->kernel->routes()->get($key);
}

/**
* Determine if the given URL is a remote link.
*/
public static function isRemote(string $url): bool
{
return str_starts_with($url, 'http') || str_starts_with($url, '//');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RuntimeException;
use Illuminate\Support\Str;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;

Expand Down Expand Up @@ -72,7 +73,7 @@ protected function makeSource(): string
throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file'));
}

if (FeaturedImage::isRemote($value)) {
if (Hyperlinks::isRemote($value)) {
return $value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Hyde\Facades\Config;
use Illuminate\Support\Str;
use Hyde\Support\BuildWarnings;
use JetBrains\PhpStorm\Deprecated;
use Illuminate\Support\Facades\Http;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;

Expand All @@ -19,7 +21,6 @@
use function filesize;
use function sprintf;
use function key;
use function str_starts_with;

/**
* Object representation of a blog post's featured image.
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(
protected readonly ?string $licenseUrl = null,
protected readonly ?string $copyrightText = null
) {
$this->type = self::isRemote($source) ? self::TYPE_REMOTE : self::TYPE_LOCAL;
$this->type = Hyperlinks::isRemote($source) ? self::TYPE_REMOTE : self::TYPE_LOCAL;
$this->source = $this->setSource($source);
}

Expand Down Expand Up @@ -241,8 +242,14 @@ protected function getContentLengthForRemoteImage(): int
return 0;
}

/**
* @codeCoverageIgnore Deprecated method.
*
* @deprecated This method will be removed in v2.0. Please use `Hyperlinks::isRemote` instead.
*/
#[Deprecated(reason: 'Replaced by the \Hyde\Foundation\Kernel\Hyperlinks::isRemote method', replacement: '\Hyde\Foundation\Kernel\Hyperlinks::isRemote(%parametersList%)', since: '1.8.0')]
public static function isRemote(string $source): bool
{
return str_starts_with($source, 'http') || str_starts_with($source, '//');
return Hyperlinks::isRemote($source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Hyde\Facades\Meta;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\MarkdownPost;
use Hyde\Foundation\Kernel\Hyperlinks;

use function str_starts_with;
use function substr_count;
use function str_repeat;

Expand Down Expand Up @@ -77,7 +77,7 @@ protected function resolveImageLink(string $image): string
{
// Since this is run before the page is rendered, we don't have the currentPage property.
// So we need to run some of the same calculations here to resolve the image path link.
return str_starts_with($image, 'http') ? $image
return Hyperlinks::isRemote($image) ? $image
: str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image;
}
}
30 changes: 30 additions & 0 deletions packages/framework/tests/Feature/Foundation/HyperlinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,34 @@ public function testRouteHelperWithInvalidRoute()
{
$this->assertNull($this->class->route('foo'));
}

public function testIsRemoteWithHttpUrl()
{
$this->assertTrue(Hyperlinks::isRemote('http://example.com'));
}

public function testIsRemoteWithHttpsUrl()
{
$this->assertTrue(Hyperlinks::isRemote('https://example.com'));
}

public function testIsRemoteWithProtocolRelativeUrl()
{
$this->assertTrue(Hyperlinks::isRemote('//example.com'));
}

public function testIsRemoteWithRelativeUrl()
{
$this->assertFalse(Hyperlinks::isRemote('/path/to/resource'));
}

public function testIsRemoteWithAbsoluteLocalPath()
{
$this->assertFalse(Hyperlinks::isRemote('/var/www/html/index.php'));
}

public function testIsRemoteWithEmptyString()
{
$this->assertFalse(Hyperlinks::isRemote(''));
}
}