Generate parent directory link instead of using '..'

This commit is contained in:
Chris Kankiewicz
2020-01-19 00:37:31 -07:00
parent 8f80669825
commit 3eb4793706
5 changed files with 44 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ class TwigProvider
ViewFunctions\Config::class,
ViewFunctions\Icon::class,
ViewFunctions\Markdown::class,
ViewFunctions\ParentDir::class,
ViewFunctions\SizeForHumans::class,
];

View 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('/');
}
}

View File

@@ -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">

View File

@@ -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 %}

View 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'));
}
}