Skip to content

Commit cf4a0d5

Browse files
committed
added support to custom index page
1 parent 64d0c79 commit cf4a0d5

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/StaticBuilder.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,27 @@ public function buildPaginatedIndex(): void
6161
$this->saveFile($pageOutputDir . '/index.html', $this->getListingPage($i, $pages, $contentList));
6262
}
6363

64-
$pageOne = $this->outputPath . '/page/1/index.html';
65-
if (is_file($pageOne)) {
66-
copy($pageOne, $this->outputPath . '/index.html');
64+
$indexPage = $this->getIndexPage();
65+
$this->saveFile($this->outputPath . '/index.html', $indexPage);
66+
}
67+
68+
public function getIndexPage(): string
69+
{
70+
$indexPage = null;
71+
72+
if ($this->siteConfig->site_index !== null) {
73+
$page = $this->contentProvider->fetch($this->siteConfig->site_index);
74+
$indexPage = $this->getSinglePage($page);
6775
}
76+
77+
if ($indexPage === null) {
78+
$pageOne = $this->outputPath . '/page/1/index.html';
79+
if (is_file($pageOne)) {
80+
$indexPage = file_get_contents($this->outputPath . '/page/1/index.html');
81+
}
82+
}
83+
84+
return $indexPage;
6885
}
6986

7087
public function buildPaginatedTagPage(string $tag): void

tests/Pest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@ function getDefaultAppConfig(): array
8686
];
8787
}
8888

89+
function getCustomIndexPageApp(): App
90+
{
91+
$config = getDefaultAppConfig();
92+
$config['site_index'] = 'posts/test0';
93+
94+
$app = new App($config);
95+
$app->addService('builder', new StaticBuilder());
96+
$app->addService('twig', new TwigServiceProvider());
97+
$app->addService('librarian', new LibrarianServiceProvider());
98+
$app->addService('content', new ContentServiceProvider());
99+
$app->librarian->boot();
100+
101+
return $app;
102+
}
103+

tests/Unit/StaticBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
->and(is_dir($builder->outputPath . '/posts/page'))->toBeTrue();
3535
});
3636

37+
test('StaticBuilder builds custom index page', function () {
38+
$app = getCustomIndexPageApp();
39+
$app->builder->buildPaginatedIndex();
40+
expect(is_file($app->builder->outputPath . '/index.html'))->toBeTrue();
41+
42+
$content = file_get_contents($app->builder->outputPath . '/index.html');
43+
expect($content)->toMatch("/template single/");
44+
});
45+
3746
test('StaticBuilder builds paginated tag pages', function () {
3847
/** @var StaticBuilder $builder */
3948
$builder = $this->app->builder;

0 commit comments

Comments
 (0)