diff --git a/app/src/ViewFunctions/Url.php b/app/src/ViewFunctions/Url.php index 4a5883d..e415f6a 100644 --- a/app/src/ViewFunctions/Url.php +++ b/app/src/ViewFunctions/Url.php @@ -18,6 +18,10 @@ class Url extends ViewFunction { $path = preg_replace('/^[.\/]+/', '', $path); + if (is_file($path)) { + return $path; + } + return empty($path) ? '' : sprintf('?dir=%s', ltrim($path, './')); } } diff --git a/tests/ViewFunctions/UrlTest.php b/tests/ViewFunctions/UrlTest.php index da5898b..d0255fe 100644 --- a/tests/ViewFunctions/UrlTest.php +++ b/tests/ViewFunctions/UrlTest.php @@ -7,7 +7,7 @@ use Tests\TestCase; class UrlTest extends TestCase { - public function test_it_can_return_a_url(): void + public function test_it_can_return_a_directory_url(): void { $url = new Url($this->container, $this->config); @@ -16,7 +16,7 @@ class UrlTest extends TestCase $this->assertEquals('?dir=some/file.test', $url('some/file.test')); } - public function test_it_can_return_a_url_in_a_subdirectory(): void + public function test_it_can_return_a_directory_url_in_a_subdirectory(): void { $_SERVER['SCRIPT_NAME'] = '/some/dir/index.php'; @@ -26,4 +26,13 @@ class UrlTest extends TestCase $this->assertEquals('?dir=some/path', $url('some/path')); $this->assertEquals('?dir=some/file.test', $url('some/file.test')); } + + public function test_it_can_return_a_file_url(): void + { + chdir($this->filePath('.')); + + $url = new Url($this->container, $this->config); + $this->assertEquals('README.md', $url('README.md')); + $this->assertEquals('subdir/alpha.scss', $url('subdir/alpha.scss')); + } }