Skip to content

Commit e050757

Browse files
Exclude current page from the pages list
1 parent 0352c64 commit e050757

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/Generator/HtmlPageGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ private function replaceTags(string $template, string $pageHtml, PageConfigDto $
4646
);
4747

4848
if (false !== stripos($html, '__PAGES_LIST__')) {
49-
$pagesList = $this->pagesListGenerator->generate();
49+
$pagesList = $this->pagesListGenerator->generate(
50+
currentPage: $page
51+
);
5052

5153
$html = str_replace(
5254
'__PAGES_LIST__',
@@ -57,7 +59,8 @@ private function replaceTags(string $template, string $pageHtml, PageConfigDto $
5759

5860
if (false !== stripos($html, '__PAGES_LIST_SHORT__')) {
5961
$pagesList = $this->pagesListGenerator->generate(
60-
$this->blogConfig->getShortPagesListItemsCount()
62+
currentPage: $page,
63+
limit: $this->blogConfig->getShortPagesListItemsCount()
6164
);
6265

6366
$html = str_replace(

src/Generator/ListGeneratorInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace MarkdownBlog\Generator;
44

5+
use MarkdownBlog\DTO\PageConfigDto;
6+
57
interface ListGeneratorInterface
68
{
7-
public function generateShort(int $limit): string;
9+
public function generateShort(PageConfigDto $currentPage, int $limit): string;
810

9-
public function generate(?int $limit = null): string;
11+
public function generate(PageConfigDto $currentPage, int $limit = null): string;
1012
}

src/Generator/PagesListGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public function __construct(
1313

1414
}
1515

16-
public function generateShort(int $limit): string
16+
public function generateShort(PageConfigDto $currentPage, int $limit): string
1717
{
18-
return $this->generate($limit);
18+
return $this->generate($currentPage, $limit);
1919
}
2020

21-
public function generate(?int $limit = null): string
21+
public function generate(PageConfigDto $currentPage, ?int $limit = null): string
2222
{
2323
$result = '<ul class="pages-list">';
2424
$result .= "\n";
@@ -30,6 +30,10 @@ public function generate(?int $limit = null): string
3030
break;
3131
}
3232

33+
if ($page->outputFile === $currentPage->outputFile) {
34+
continue;
35+
}
36+
3337
$result .= sprintf(
3438
'<li><a href="%s">%s</a></li>',
3539
$page->outputFile,

tests/Generator/PagesListGeneratorTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function testPagesListHtml(): void
3232
{
3333
$collection = new PagesConfigCollection();
3434

35+
$currentPage = new PageConfigDto(
36+
title: 'Current page',
37+
markdownFile: 'test.md',
38+
outputFile: 'current.html',
39+
templateFile: 'testx.html'
40+
);
41+
3542
$collection->add(
3643
config: new PageConfigDto(
3744
title: 'First page',
@@ -41,6 +48,8 @@ public function testPagesListHtml(): void
4148
)
4249
);
4350

51+
$collection->add($currentPage);
52+
4453
$collection->add(
4554
config: new PageConfigDto(
4655
title: 'Second page',
@@ -54,7 +63,9 @@ public function testPagesListHtml(): void
5463
->method('getPagesConfig')
5564
->willReturn($collection);
5665

57-
$actual = $this->pagesListGenerator->generate();
66+
$actual = $this->pagesListGenerator->generate(
67+
currentPage: $currentPage
68+
);
5869

5970
$expected = <<<HTML
6071
<ul class="pages-list">
@@ -70,6 +81,13 @@ public function testPagesListHtml(): void
7081

7182
public function testShortPagesListHtml(): void
7283
{
84+
$currentPage = new PageConfigDto(
85+
title: 'Current page',
86+
markdownFile: 'current.md',
87+
outputFile: 'current.html',
88+
templateFile: 'test.html'
89+
);
90+
7391
$collection = new PagesConfigCollection();
7492

7593
$collection->add(
@@ -81,6 +99,8 @@ public function testShortPagesListHtml(): void
8199
)
82100
);
83101

102+
$collection->add($currentPage);
103+
84104
$collection->add(
85105
config: new PageConfigDto(
86106
title: 'Second page',
@@ -103,7 +123,10 @@ public function testShortPagesListHtml(): void
103123
->method('getPagesConfig')
104124
->willReturn($collection);
105125

106-
$actual = $this->pagesListGenerator->generateShort(2);
126+
$actual = $this->pagesListGenerator->generateShort(
127+
currentPage: $currentPage,
128+
limit: 2
129+
);
107130

108131
$expected = <<<HTML
109132
<ul class="pages-list">

0 commit comments

Comments
 (0)