Skip to content

Commit d4e46a3

Browse files
author
github-actions
committed
Merge pull request #291 from hydephp/internal-codebase-improvements
Internal codebase improvements hydephp/develop@598c15b
1 parent 3032621 commit d4e46a3

File tree

10 files changed

+82
-59
lines changed

10 files changed

+82
-59
lines changed

resources/views/components/post/image.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<figure aria-label="Cover image" itemprop="image" itemscope itemtype="http://schema.org/ImageObject" role="doc-cover">
2-
<img src="{{ $page->image->getLink($currentPage) }}" alt="{{ $page->image->description ?? '' }}" title="{{ $page->image->title ?? '' }}" itemprop="image" class="mb-0">
2+
<img src="{{ $page->image->getLink() }}" alt="{{ $page->image->description ?? '' }}" title="{{ $page->image->title ?? '' }}" itemprop="image" class="mb-0">
33
<figcaption aria-label="Image caption" itemprop="caption">
44
{!! $page->image->getFluentAttribution() !!}
55
</figcaption>

resources/views/layouts/docs.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class="p-4 overflow-y-auto border-y border-gray-300 dark:border-[#1b2533] h-[cal
7171
</nav>
7272
<footer id="sidebar-footer" class="h-16 absolute p-4 w-full bottom-0 left-0 text-center leading-8">
7373
<p>
74-
<a href="{{ Hyde::relativeLink('index.html', $currentPage) }}">Back to home page</a>
74+
<a href="{{ Hyde::relativeLink('index.html') }}">Back to home page</a>
7575
</p>
7676
</footer>
7777
</aside>

src/Hyde.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
* @method static string path(string $path = '')
2525
* @method static \Hyde\Framework\Helpers\Features features()
2626
* @method static bool hasFeature(string $feature)
27-
* @method static string relativeLink(string $destination, null|string $current = null)
27+
* @method static string relativeLink(string $destination)
2828
* @method static string getMarkdownPostPath(string $path = '')
2929
* @method static bool|int copy(string $from, string $to, bool $force = false)
3030
* @method static string getModelSourcePath(string $model, string $path = '')
31-
* @method static string image(string $name, string $current = null)
31+
* @method static string image(string $name)
3232
* @method static void macro(string $name, callable|object $macro)
3333
* @method static \Hyde\Framework\Contracts\RouteContract|null currentRoute()
3434
* @method static string currentPage()

src/HydeKernel.php

+14-26
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(?string $basePath = null)
3737
$this->setBasePath($basePath ?? getcwd());
3838
}
3939

40-
public static function getInstance(): static
40+
public static function getInstance(): HydeKernelContract
4141
{
4242
return app(HydeKernelContract::class);
4343
}
@@ -101,12 +101,12 @@ public function makeTitle(string $slug): string
101101
public function path(string $path = ''): string
102102
{
103103
if (empty($path)) {
104-
return static::getBasePath();
104+
return $this->getBasePath();
105105
}
106106

107107
$path = unslash($path);
108108

109-
return static::getBasePath().DIRECTORY_SEPARATOR.$path;
109+
return $this->getBasePath().DIRECTORY_SEPARATOR.$path;
110110
}
111111

112112
/**
@@ -146,30 +146,23 @@ public function pageLink(string $destination): string
146146
/**
147147
* Inject the proper number of `../` before the links in Blade templates.
148148
*
149-
* Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View.
150-
*
151149
* @param string $destination relative to output directory on compiled site
152-
* @param string|null $current the current URI path relative to the site root
153150
* @return string
154151
*
155152
* @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest
156153
*/
157-
public function relativeLink(string $destination, ?string $current = null): string
154+
public function relativeLink(string $destination): string
158155
{
159156
if (str_starts_with($destination, '../')) {
160157
return $destination;
161158
}
162159

163-
if ($current === null) {
164-
$current = static::currentPage();
165-
}
166-
167-
$nestCount = substr_count($current, '/');
160+
$nestCount = substr_count($this->currentPage(), '/');
168161
$route = '';
169162
if ($nestCount > 0) {
170163
$route .= str_repeat('../', $nestCount);
171164
}
172-
$route .= static::pageLink($destination);
165+
$route .= $this->pageLink($destination);
173166

174167
return str_replace('//', '/', $route);
175168
}
@@ -192,28 +185,23 @@ public function currentRoute(): ?RouteContract
192185

193186
/**
194187
* Gets a relative web link to the given image stored in the _site/media folder.
195-
* Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View.
196188
*/
197-
public function image(string $name, string $current = null): string
189+
public function image(string $name): string
198190
{
199-
if ($current === null) {
200-
$current = static::currentPage();
201-
}
202-
203191
if (str_starts_with($name, 'http')) {
204192
return $name;
205193
}
206194

207-
return static::relativeLink('media/'.basename($name), $current);
195+
return $this->relativeLink('media/'.basename($name));
208196
}
209197

210198
/**
211199
* Return a qualified URI path, if SITE_URL is set in .env, else return false.
212200
*
213-
* @param string|null $path optional relative path suffix. Omit to return base url.
201+
* @param string $path optional relative path suffix. Omit to return base url.
214202
* @return string|false
215203
*/
216-
public function uriPath(?string $path = ''): string|false
204+
public function uriPath(string $path = ''): string|false
217205
{
218206
if (config('site.url', false)) {
219207
return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? '');
@@ -265,22 +253,22 @@ public function getModelSourcePath(string $model, string $path = ''): string
265253

266254
public function getBladePagePath(string $path = ''): string
267255
{
268-
return static::getModelSourcePath(BladePage::class, $path);
256+
return $this->getModelSourcePath(BladePage::class, $path);
269257
}
270258

271259
public function getMarkdownPagePath(string $path = ''): string
272260
{
273-
return static::getModelSourcePath(MarkdownPage::class, $path);
261+
return $this->getModelSourcePath(MarkdownPage::class, $path);
274262
}
275263

276264
public function getMarkdownPostPath(string $path = ''): string
277265
{
278-
return static::getModelSourcePath(MarkdownPost::class, $path);
266+
return $this->getModelSourcePath(MarkdownPost::class, $path);
279267
}
280268

281269
public function getDocumentationPagePath(string $path = ''): string
282270
{
283-
return static::getModelSourcePath(DocumentationPage::class, $path);
271+
return $this->getModelSourcePath(DocumentationPage::class, $path);
284272
}
285273

286274
/**

src/Models/Image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public function getSource(): ?string
101101
return $this->uri ?? $this->path ?? null;
102102
}
103103

104-
public function getLink(?string $currentPage = ''): string
104+
public function getLink(): string
105105
{
106-
return Hyde::image($this->getSource() ?? '', $currentPage);
106+
return Hyde::image($this->getSource() ?? '');
107107
}
108108

109109
public function getContentLength(): int

src/Views/Components/LinkComponent.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Hyde\Framework\Views\Components;
44

55
use Hyde\Framework\Hyde;
6-
use Illuminate\Support\Facades\View;
76
use Illuminate\View\Component;
87

98
class LinkComponent extends Component
@@ -12,7 +11,7 @@ class LinkComponent extends Component
1211

1312
public function __construct(string $href)
1413
{
15-
$this->href = Hyde::relativeLink($href, View::shared('currentPage') ?? '');
14+
$this->href = Hyde::relativeLink($href);
1615
}
1716

1817
public function render(): \Illuminate\Contracts\View\View

tests/Feature/ImageModelTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public function test_get_link_resolves_local_paths_when_on_nested_page()
168168
'path' => 'image.jpg',
169169
]);
170170

171-
$this->assertEquals('../media/image.jpg', $image->getLink('foo/bar'));
171+
$this->mockCurrentPage('foo/bar');
172+
$this->assertEquals('../media/image.jpg', $image->getLink());
172173
}
173174
}

tests/Feature/RouteTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function test_get_link_returns_correct_path_for_nested_current_page()
153153
{
154154
$route = new Route(new MarkdownPage(slug: 'foo'));
155155
view()->share('currentPage', 'foo/bar');
156-
$this->assertEquals(Hyde::relativeLink($route->getOutputFilePath(), 'foo/bar'), $route->getLink());
156+
$this->assertEquals(Hyde::relativeLink($route->getOutputFilePath()), $route->getLink());
157157
$this->assertEquals('../foo.html', $route->getLink());
158158
}
159159

tests/Unit/FileHelperRelativeLinkTest.php

+56-22
Original file line numberDiff line numberDiff line change
@@ -19,87 +19,121 @@ public function test_helper_returns_string_as_is_if_current_is_not_set()
1919

2020
public function test_helper_injects_proper_number_of_doubles_slash()
2121
{
22-
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html', 'foo/bar.html'));
22+
$this->mockCurrentPage('foo/bar.html');
23+
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html'));
2324
}
2425

2526
public function test_helper_injects_proper_number_of_doubles_slash_for_deeply_nested_paths()
2627
{
27-
$this->assertEquals('../../../foo.html', Hyde::relativeLink('foo.html', 'foo/bar/baz/qux.html'));
28+
$this->mockCurrentPage('foo/bar/baz/qux.html');
29+
$this->assertEquals('../../../foo.html', Hyde::relativeLink('foo.html'));
2830
}
2931

3032
public function test_helper_handles_destination_without_file_extension()
3133
{
32-
$this->assertEquals('../foo', Hyde::relativeLink('foo', 'foo/bar.html'));
34+
$this->mockCurrentPage('foo/bar.html');
35+
$this->assertEquals('../foo', Hyde::relativeLink('foo'));
3336
}
3437

3538
public function test_helper_handles_current_without_file_extension()
3639
{
37-
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html', 'foo/bar'));
40+
$this->mockCurrentPage('foo/bar');
41+
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html'));
3842
}
3943

4044
public function test_helper_handles_case_without_any_file_extensions()
4145
{
42-
$this->assertEquals('../foo', Hyde::relativeLink('foo', 'foo/bar'));
46+
$this->mockCurrentPage('foo/bar');
47+
$this->assertEquals('../foo', Hyde::relativeLink('foo'));
4348
}
4449

4550
public function test_helper_handles_case_with_mixed_file_extensions()
4651
{
52+
$this->mockCurrentPage('foo/bar.md');
4753
$this->assertEquals('../foo.md', Hyde::relativeLink('foo.md', 'foo/bar.md'));
54+
$this->mockCurrentPage('foo/bar.txt');
4855
$this->assertEquals('../foo.txt', Hyde::relativeLink('foo.txt', 'foo/bar.txt'));
4956
}
5057

5158
public function test_helper_handles_different_file_extensions()
5259
{
53-
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png', 'foo/bar'));
54-
$this->assertEquals('../foo.css', Hyde::relativeLink('foo.css', 'foo/bar'));
55-
$this->assertEquals('../foo.js', Hyde::relativeLink('foo.js', 'foo/bar'));
60+
$this->mockCurrentPage('foo/bar');
61+
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png'));
62+
$this->assertEquals('../foo.css', Hyde::relativeLink('foo.css'));
63+
$this->assertEquals('../foo.js', Hyde::relativeLink('foo.js'));
5664
}
5765

5866
public function test_helper_returns_pretty_url_if_enabled_and_destination_is_a_html_file()
5967
{
6068
config(['site.pretty_urls' => true]);
61-
$this->assertEquals('../foo', Hyde::relativeLink('foo.html', 'foo/bar.html'));
69+
$this->mockCurrentPage('foo/bar.html');
70+
$this->assertEquals('../foo', Hyde::relativeLink('foo.html'));
6271
}
6372

6473
public function test_helper_method_does_not_require_current_path_to_be_html_to_use_pretty_urls()
6574
{
6675
config(['site.pretty_urls' => true]);
67-
$this->assertEquals('../foo', Hyde::relativeLink('foo.html', 'foo/bar'));
76+
$this->mockCurrentPage('foo/bar');
77+
$this->assertEquals('../foo', Hyde::relativeLink('foo.html'));
6878
}
6979

7080
public function test_helper_returns_does_not_return_pretty_url_if_when_enabled_but_and_destination_is_not_a_html_file()
7181
{
7282
config(['site.pretty_urls' => true]);
73-
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png', 'foo/bar.html'));
83+
$this->mockCurrentPage('foo/bar.html');
84+
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png'));
7485
}
7586

7687
public function test_helper_rewrites_index_when_using_pretty_urls()
7788
{
7889
config(['site.pretty_urls' => true]);
79-
$this->assertEquals('/', Hyde::relativeLink('index.html', 'foo.html'));
80-
$this->assertEquals('../', Hyde::relativeLink('index.html', 'foo/bar.html'));
81-
$this->assertEquals('../../', Hyde::relativeLink('index.html', 'foo/bar/baz.html'));
90+
$this->mockCurrentPage('foo.html');
91+
$this->assertEquals('/', Hyde::relativeLink('index.html'));
92+
$this->mockCurrentPage('foo/bar.html');
93+
$this->assertEquals('../', Hyde::relativeLink('index.html'));
94+
$this->mockCurrentPage('foo/bar/baz.html');
95+
$this->assertEquals('../../', Hyde::relativeLink('index.html'));
8296
}
8397

8498
public function test_helper_does_not_rewrite_index_when_not_using_pretty_urls()
8599
{
86100
config(['site.pretty_urls' => false]);
87-
$this->assertEquals('index.html', Hyde::relativeLink('index.html', 'foo.html'));
88-
$this->assertEquals('../index.html', Hyde::relativeLink('index.html', 'foo/bar.html'));
89-
$this->assertEquals('../../index.html', Hyde::relativeLink('index.html', 'foo/bar/baz.html'));
101+
$this->mockCurrentPage('foo.html');
102+
$this->assertEquals('index.html', Hyde::relativeLink('index.html'));
103+
$this->mockCurrentPage('foo/bar.html');
104+
$this->assertEquals('../index.html', Hyde::relativeLink('index.html'));
105+
$this->mockCurrentPage('foo/bar/baz.html');
106+
$this->assertEquals('../../index.html', Hyde::relativeLink('index.html'));
90107
}
91108

92109
public function test_helper_rewrites_documentation_page_index_when_using_pretty_urls()
93110
{
94111
config(['site.pretty_urls' => true]);
95-
$this->assertEquals('docs/', Hyde::relativeLink('docs/index.html', 'foo.html'));
96-
$this->assertEquals('docs/', Hyde::relativeLink('docs/index.html', 'docs.html'));
97-
$this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html', 'foo/bar.html'));
98-
$this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html', 'docs/foo.html'));
112+
$this->mockCurrentPage('foo.html');
113+
$this->assertEquals('docs/', Hyde::relativeLink('docs/index.html'));
114+
$this->mockCurrentPage('docs.html');
115+
$this->assertEquals('docs/', Hyde::relativeLink('docs/index.html'));
116+
$this->mockCurrentPage('foo/bar.html');
117+
$this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html'));
118+
$this->mockCurrentPage('docs/foo.html');
119+
$this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html'));
120+
}
121+
122+
public function test_helper_does_not_rewrite_documentation_page_index_when_not_using_pretty_urls()
123+
{
124+
config(['site.pretty_urls' => false]);
125+
$this->mockCurrentPage('foo.html');
126+
$this->assertEquals('docs/index.html', Hyde::relativeLink('docs/index.html'));
127+
$this->mockCurrentPage('docs.html');
128+
$this->assertEquals('docs/index.html', Hyde::relativeLink('docs/index.html'));
129+
$this->mockCurrentPage('foo/bar.html');
130+
$this->assertEquals('../docs/index.html', Hyde::relativeLink('docs/index.html'));
131+
$this->mockCurrentPage('docs/foo.html');
132+
$this->assertEquals('../docs/index.html', Hyde::relativeLink('docs/index.html'));
99133
}
100134

101135
public function test_helper_does_not_rewrite_already_processed_links()
102136
{
103-
$this->assertEquals('../foo', Hyde::relativeLink('../foo', 'foo/bar.html'));
137+
$this->assertEquals('../foo', Hyde::relativeLink('../foo'));
104138
}
105139
}

tests/Unit/FileHelpersImageTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function test_image_helper_resolves_paths_for_nested_pages()
3434
];
3535

3636
foreach ($tests as $input => $expected) {
37-
$this->assertEquals(Hyde::image($input, 'foo/bar'), $expected);
37+
$this->mockCurrentPage('foo/bar');
38+
$this->assertEquals(Hyde::image($input), $expected);
3839
}
3940
}
4041
}

0 commit comments

Comments
 (0)