Skip to content

Commit

Permalink
Implementing suppport to nested content dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
erikaheidi committed Oct 7, 2024
1 parent a252835 commit 2ef2689
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ContentType

public int $index = 100;

public array $children = [];

/**
* @throws Exception\ContentNotFoundException
*/
Expand Down
14 changes: 10 additions & 4 deletions src/Provider/ContentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,17 @@ public function fetchFromTag(string $tag, int $start = 0, int $limit = 20): ?Con
/**
* @throws ContentNotFoundException
*/
public function getContentTypes(): array
public function getContentTypes(?string $path = null, string $parent = ''): array
{
$contentTypes = [];
$order = [];
foreach (glob($this->data_path . '/*', GLOB_ONLYDIR) as $route) {
$content = new ContentType(basename($route), $this->data_path);
$data_path = $path ?? $this->data_path;
foreach (glob($data_path . '/*', GLOB_ONLYDIR) as $route) {
$content = new ContentType($parent . basename($route), $data_path);
$contentTypes[$content->slug] = $content;
$order[$content->slug] = $content->index;

$content->children = $this->getContentTypes($route, $content->slug . '/');
}

asort($order, SORT_NUMERIC);
Expand All @@ -228,7 +231,10 @@ public function getContentTypes(): array
*/
public function getContentType(string $contentType): ContentType
{
return new ContentType($contentType, $this->data_path);
$content = new ContentType($contentType, $this->data_path);
$content->children = $this->getContentTypes($content->contentDir . '/' . $content->slug, $content->slug);

return $content;
}

public function fetchFrom(ContentType $contentType, int $start = 0, int $limit = 20, bool $parse_markdown = false, string $orderBy = 'desc'): ?ContentCollection
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
->and($content->body_markdown)->toBeString();
});

it('loads nested content and parses front matter', function () {
$content = $this->app->content->fetch('docs/en/test0');
expect($content->frontMatterGet('title'))->toBe('Testing Sub-Level En')
->and($content->body_markdown)->toBeString();
});

it('loads the full list of content when no limit is passed', function () {
$content = $this->app->content->fetchAll(0, 0);
expect($content)->toBeInstanceOf(ContentCollection::class)
Expand All @@ -50,6 +56,34 @@
->and(count($tags))->toBeGreaterThan(2);
});

it('loads the right number of content types', function () {
$types = $this->app->content->getContentTypes();
expect($types)->toHaveCount(3);
});

it('loads the right number of articles in a top-level ContentType', function () {
$type = $this->app->content->getContentType('docs');
expect($type)->toBeInstanceOf(ContentType::class)
->and($type->title)->toBe('Docs')
->and($this->app->content->fetchFrom($type))->toHaveCount(1);

});

it('loads nested Content Types', function () {
$type = $this->app->content->getContentType('docs');
expect($type)->toBeInstanceOf(ContentType::class)
->and($type->children)->toHaveCount(2);

});

it('fetches a nested content type and its posts', function () {
$type = $this->app->content->getContentType('docs/en');
expect($type)->toBeInstanceOf(ContentType::class)
->and($type->title)->toBe('English Docs')
->and($this->app->content->fetchFrom($type))->toHaveCount(2);

});

it('loads content types respecting index order', function () {
$types = $this->app->content->getContentTypes();
$ctype = $types[0];
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/docs/en/_index
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: English Docs
description: English Docs section
index: 100
---
9 changes: 9 additions & 0 deletions tests/resources/docs/en/test0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Testing Sub-Level En
description: Test Description
custom: My Custom info
tags: test, librarian
index: 1
---

## Testing
9 changes: 9 additions & 0 deletions tests/resources/docs/en/test1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Testing Sub-Level En 02
description: Test Description
custom: My Custom info
tags: test, librarian
index: 1
---

## Testing
9 changes: 9 additions & 0 deletions tests/resources/docs/pt/test0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Testando Sub-Level pt
description: Test Description
custom: My Custom info
tags: test, librarian
index: 1
---

## Testando
9 changes: 9 additions & 0 deletions tests/resources/docs/pt/test1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Testando Sub-Level ppt 02
description: Test Description
custom: My Custom info
tags: test, librarian
index: 1
---

## Testando

0 comments on commit 2ef2689

Please sign in to comment.