Skip to content

Commit

Permalink
Merge pull request #36 from 8fold/go-deeper-with-content
Browse files Browse the repository at this point in the history
Go deeper with content
  • Loading branch information
joshbruce authored Dec 19, 2023
2 parents ccbe5b1 + 9efc9ae commit 6e8927b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 7 deletions.
38 changes: 34 additions & 4 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,50 @@ public function publicFile(string $filename, string $at = '/'): PublicFile
* @return string[]
*/
public function titles(string $at = ''): array
{
return array_values(
$this->linkStack($at)
);
}

/**
* @return array<string, string>
*/
public function breadcrumbs(
string $at = '',
int $offset = 0,
int|false $length = false
): array {
$sorted = array_reverse(
$this->linkStack($at)
);

if ($length === false) {
$length = null;

}

return array_slice($sorted, $offset, $length);
}

/**
* @return array<string, string>
*/
public function linkStack(string $at = ''): array
{
$pathParts = explode('/', $at);
$filtered = array_filter($pathParts);

$titles = [];
$stack = [];
while (count($filtered) > 0) {
$path = '/' . implode('/', $filtered) . '/';
$titles[] = $this->publicMeta(at: $path)->title();
$stack[$path] = $this->publicMeta(at: $path)->title();

array_pop($filtered);
}

$titles[] = $this->publicMeta(at: '/')->title();
$stack['/'] = $this->publicMeta(at: '/')->title();

return array_filter($titles);
return array_filter($stack);
}
}
6 changes: 3 additions & 3 deletions tests/SiteMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function can_skip_sitemap(): void

$expected = <<<xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.5</priority></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/l3-page/l4-page/</loc><lastmod>2023-01-01</lastmod><priority>0.25</priority></url><url><loc>http://ex.ample/l1-page/l2-page/l3-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.5</priority></url></urlset>
xml;

$result = (string) Sitemap::create(
Expand All @@ -51,7 +51,7 @@ public function can_change_default_priority(): void
{
$expected = <<<xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.75</priority></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/l3-page/l4-page/</loc><lastmod>2023-01-01</lastmod><priority>0.25</priority></url><url><loc>http://ex.ample/l1-page/l2-page/l3-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.75</priority></url></urlset>
xml;

$result = (string) Sitemap::create(
Expand All @@ -72,7 +72,7 @@ public function is_expected_xml(): void
{
$expected = <<<xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.5</priority></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://ex.ample/l1-page/l2-page/l3-page/l4-page/</loc><lastmod>2023-01-01</lastmod><priority>0.25</priority></url><url><loc>http://ex.ample/l1-page/l2-page/l3-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/l2-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/l1-page/</loc><lastmod>2023-01-01</lastmod><priority>1</priority></url><url><loc>http://ex.ample/</loc><priority>0.5</priority></url></urlset>
xml;

$result = (string) Sitemap::create(
Expand Down
95 changes: 95 additions & 0 deletions tests/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,101 @@ public function has_titles(): void
);
}

/**
* @test
*/
public function has_expected_link_stack(): void
{
$expected = [
'/l1-page/l2-page/' => 'L2 page',
'/l1-page/' => 'L1 page',
'/' => 'Root test content'
];

$result = $this->site()->linkStack('/l1-page/l2-page');

$this->assertSame(
$expected,
$result
);
}

/**
* @test
*/
public function can_build_breadcrumbs_l2(): void
{
$expected = [
'/' => 'Root test content',
'/l1-page/' => 'L1 page',
'/l1-page/l2-page/' => 'L2 page'
];

$result = $this->site()->breadcrumbs('/l1-page/l2-page');

$this->assertSame(
$expected,
$result
);
}

/**
* @test
*/
public function can_build_breadcrumbs_l4(): void
{
$expected = [
'/' => 'Root test content',
'/l1-page/' => 'L1 page',
'/l1-page/l2-page/' => 'L2 page',
'/l1-page/l2-page/l3-page/' => 'L3 page',
'/l1-page/l2-page/l3-page/l4-page/' => 'L4 page'
];

$result = $this->site()->breadcrumbs('/l1-page/l2-page/l3-page/l4-page');

$this->assertSame(
$expected,
$result
);
}

/**
* @test
*/
public function can_build_breadcrumbs_can_exclude_levels(): void
{
$expected = [
// '/' => 'Root test content',
'/l1-page/' => 'L1 page',
'/l1-page/l2-page/' => 'L2 page',
'/l1-page/l2-page/l3-page/' => 'L3 page',
'/l1-page/l2-page/l3-page/l4-page/' => 'L4 page'
];

$result = $this->site()->breadcrumbs('/l1-page/l2-page/l3-page/l4-page', 1);

$this->assertSame(
$expected,
$result
);

$expected = [
// '/' => 'Root test content',
// '/l1-page/' => 'L1 page',
'/l1-page/l2-page/' => 'L2 page',
'/l1-page/l2-page/l3-page/' => 'L3 page',
// '/l1-page/l2-page/l3-page/l4-page/' => 'L4 page'
];

$result = $this->site()->breadcrumbs('/l1-page/l2-page/l3-page/l4-page', 2, 2);

$this->assertSame(
$expected,
$result
);
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "L4 page",
"created": "20230101",
"priority": 0.25
}
5 changes: 5 additions & 0 deletions tests/test-content/public/l1-page/l2-page/l3-page/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "L3 page",
"created": "20230101",
"priority": 1.0
}

0 comments on commit 6e8927b

Please sign in to comment.