mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-30 17:20:12 +02:00
Generate parent directory link instead of using '..'
This commit is contained in:
@@ -19,6 +19,7 @@ class TwigProvider
|
||||
ViewFunctions\Config::class,
|
||||
ViewFunctions\Icon::class,
|
||||
ViewFunctions\Markdown::class,
|
||||
ViewFunctions\ParentDir::class,
|
||||
ViewFunctions\SizeForHumans::class,
|
||||
];
|
||||
|
||||
|
25
app/ViewFunctions/ParentDir.php
Normal file
25
app/ViewFunctions/ParentDir.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\ViewFunctions;
|
||||
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class ParentDir extends ViewFunction
|
||||
{
|
||||
/** @var string The function name */
|
||||
protected $name = 'parent_dir';
|
||||
|
||||
/**
|
||||
* Get the parent directory for a given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __invoke(string $path)
|
||||
{
|
||||
return Collection::make(
|
||||
explode('/', $path)
|
||||
)->filter()->slice(0, -1)->implode('/');
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
<a
|
||||
href="{{ parentDir ? '..' : file.getRelativePathname }}"
|
||||
href="/{{ parentDir ? parent_dir(path) : file.getPathname }}"
|
||||
class="flex flex-col items-center rounded-lg font-mono group hover:bg-gray-200 hover:shadow"
|
||||
>
|
||||
<div class="flex justify-between items-center p-4 w-full">
|
||||
|
@@ -20,9 +20,7 @@
|
||||
</div>
|
||||
|
||||
{% if not search and not is_root %}
|
||||
{{ include('components/file.twig', {
|
||||
parentDir: true
|
||||
}, with_context = false) }}
|
||||
{{ include('components/file.twig', { parentDir: true }) }}
|
||||
{% endif %}
|
||||
|
||||
{% for file in files %}
|
||||
|
16
tests/ViewFunctions/ParentTest.php
Normal file
16
tests/ViewFunctions/ParentTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\ViewFunctions\ParentDir;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ParentDirTest extends TestCase
|
||||
{
|
||||
public function test_it_can_get_the_parent_directory_from_a_path(): void
|
||||
{
|
||||
$parentDir = new ParentDir($this->container, $this->config);
|
||||
|
||||
$this->assertEquals('foo/bar', $parentDir('foo/bar/baz'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user