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
12 changes: 10 additions & 2 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Str;
use TightenCo\Jigsaw\IterableObject;

class Collection extends BaseCollection
Expand All @@ -29,12 +30,19 @@ public function loadItems(BaseCollection $items)
->map($this->getMap())
->filter($this->getFilter())
->keyBy(function ($item) {
return $item->getFilename();
return self::itemKey($item->getRelativePath(), $item->getFilename());
});

return $this->updateItems($this->addAdjacentItems($sortedItems));
}

public static function itemKey($relativePath, $filename)
{
return $relativePath && ! Str::startsWith($relativePath, '_')
? $relativePath . '/' . $filename
: $filename;
}

public function updateItems(BaseCollection $items)
{
$this->items = $this->getArrayableItems($items);
Expand All @@ -46,7 +54,7 @@ private function addAdjacentItems(BaseCollection $items)
{
$count = $items->count();
$adjacentItems = $items->map(function ($item) {
return $item->getFilename();
return self::itemKey($item->getRelativePath(), $item->getFilename());
});
$previousItems = $adjacentItems->prepend(null)->take($count);
$nextItems = $adjacentItems->push(null)->take(-$count);
Expand Down
7 changes: 7 additions & 0 deletions src/File/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function topLevelDirectory()
return count($parts) == 1 ? '' : $parts[0];
}

public function getRelativePathBelowTopLevel()
{
$relativePath = str_replace('\\', '/', $this->file->getRelativePath());

return ltrim((string) Str::after($relativePath, $this->topLevelDirectory()), '/');
}

public function getFilenameWithoutExtension()
{
return $this->getBasename('.' . $this->getFullExtension());
Expand Down
6 changes: 5 additions & 1 deletion src/Handlers/CollectionItemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use TightenCo\Jigsaw\Builders\PlainMarkdownBuilder;
use TightenCo\Jigsaw\Collection\Collection as JigsawCollection;
use TightenCo\Jigsaw\File\OutputFile;
use TightenCo\Jigsaw\Parsers\FrontMatterParser;

Expand Down Expand Up @@ -56,7 +57,10 @@ public function handle($file, $pageData)
return $handler->shouldHandle($file);
});
$collectionName = $this->getCollectionName($file);
$pageData->setPageVariableToCollectionItem($collectionName, $file->getFilenameWithoutExtension());
$pageData->setPageVariableToCollectionItem(
$collectionName,
JigsawCollection::itemKey($file->getRelativePathBelowTopLevel(), $file->getFilenameWithoutExtension()),
);

if ($pageData->page === null) {
return null;
Expand Down
26 changes: 26 additions & 0 deletions tests/CollectionItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ public function collection_item_view_path_for_flat_items_equals_filename()
);
}

#[Test]
public function collection_items_with_same_basename_in_different_directories_do_not_collide()
{
$config = collect(['collections' => [
'collection' => [
'path' => 'collection/{relativePath}/{filename}',
],
]]);
$files = $this->setupSource([
'_layouts' => [
'item.blade.php' => '@section(\'content\') @endsection',
],
'_collection' => [
'page.blade.php' => "@extends('_layouts.item')\nROOT",
'nested' => [
'page.blade.php' => "@extends('_layouts.item')\nNESTED",
],
],
]);

$this->buildSite($files, $config);

$this->assertEquals('ROOT', trim($files->getChild('build/collection/page.html')->getContent()));
$this->assertEquals('NESTED', trim($files->getChild('build/collection/nested/page.html')->getContent()));
}

#[Test]
public function collection_item_page_metadata_contains_extension()
{
Expand Down
Loading