Skip to content

Commit

Permalink
Allow using a directory index page
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Mar 29, 2021
1 parent 374ac1e commit b54e978
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Controller/PageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ public function __construct(

public function __invoke(string $slug): Response
{
// redirect a suffixed page ("foo/bar.md" should be redirected to "foo/bar")
if (false !== strpos($slug, '.md')) {
$slug = preg_replace('/\.md$/', '', $slug);

return $this->redirectToRoute('mobizel_markdown_docs_page_show', ['slug' => $slug]);
}

// redirect a directory homepage ("foo/bar/index" should be redirected to "foo/bar")
if (false !== strpos($slug, '/index')) {
$slug = preg_replace('/\/index$/', '', $slug);

return $this->redirectToRoute('mobizel_markdown_docs_page_show', ['slug' => $slug]);
}

$page = $this->pageItemDataProvider->getPage($slug);

if (null === $page) {
Expand Down
19 changes: 18 additions & 1 deletion src/DataProvider/PageCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getRootPages(): iterable
->in($this->docsDir)
->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($this->docsDir))
->sort(PageSorter::sortByTitle());

$pages = [];
Expand All @@ -47,9 +48,10 @@ public function getRootPages(): iterable

/** @var string $slug */
$slug = preg_replace('/\.md$/', '', $pageInfo->getRelativePathName());
$slug = preg_replace('/\/index$/', '', $slug);

$pages[] = $this->createPage(
$slug,
(string) $slug,
$pageInfo->getTitle(),
$pageInfo->getContentWithoutTitle()
);
Expand All @@ -68,6 +70,7 @@ public function getChildrenPages(string $parentSlug): iterable
->in($this->docsDir.'/'.$parentSlug)
->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($this->docsDir.'/'.$parentSlug))
->sort(PageSorter::sortByTitle());
} catch (DirectoryNotFoundException $exception) {
return [];
Expand Down Expand Up @@ -127,6 +130,7 @@ private function getRootPagesMap(): array
->files()
->in($this->docsDir)
->depth(0)
->append($this->createDirectoryIndexFinder($this->docsDir))
->sort(PageSorter::sortByTitle())
;

Expand All @@ -137,6 +141,7 @@ private function getRootPagesMap(): array

/** @var string $slug */
$slug = preg_replace('/\.md$/', '', $pageInfo->getRelativePathName());
$slug = preg_replace('/\/index$/', '', $slug);

$pages[] = ['slug' => $slug, 'title' => $pageInfo->getTitle()];
}
Expand All @@ -153,7 +158,9 @@ private function getChildrenPagesMap(array $row, array $pages, int &$currentPosi
$finder
->files()
->in($this->docsDir.'/'.$parentSlug)
->notName('index.md')
->depth(0)
->append($this->createDirectoryIndexFinder($this->docsDir.'/'.$parentSlug))
->sort(PageSorter::sortByTitle())
;
} catch (DirectoryNotFoundException $exception) {
Expand All @@ -163,6 +170,7 @@ private function getChildrenPagesMap(array $row, array $pages, int &$currentPosi
foreach ($finder as $file) {
$pageInfo = new PageInfo($file->getPathname(), $file->getRelativePath(), $file->getRelativePathname());
$slug = $parentSlug.'/'.preg_replace('/\.md$/', '', $pageInfo->getRelativePathName());
$slug = preg_replace('/\/index$/', '', $slug);

$pageToAdd = ['slug' => $slug, 'title' => $pageInfo->getTitle()];

Expand All @@ -174,4 +182,13 @@ private function getChildrenPagesMap(array $row, array $pages, int &$currentPosi

return $pages;
}

private function createDirectoryIndexFinder(string $dir): Finder
{
return (new Finder())
->files()
->name('index.md')
->in($dir)
->depth(1);
}
}
4 changes: 4 additions & 0 deletions src/DataProvider/PageItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function getPage(string $slug): ?PageOutput
{
$templateAbsolutePath = $this->templateHandler->getTemplateAbsolutePath($slug);

if (!is_file($templateAbsolutePath)) {
$templateAbsolutePath = $this->templateHandler->getTemplateAbsolutePath($slug.'/index');
}

if (!is_file($templateAbsolutePath)) {
return null;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Controller/PageActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public function testRedirection()
$this->assertEquals('/bdd', $client->getResponse()->getTargetUrl());
}

public function testDirectoryIndexPageRedirection()
{
$client = static::createClient();

$client->request('GET', 'book/index');

$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertEquals('/book', $client->getResponse()->getTargetUrl());
}

public function testShowPage()
{
$client = static::createClient();
Expand All @@ -37,6 +47,26 @@ public function testShowPage()
$this->assertSelectorTextSame('html h1', 'Documentation');
}

public function testDirectoryIndexPage()
{
$client = static::createClient();

$client->request('GET', 'book');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSelectorTextSame('html h1', 'Book homepage');
}

public function testSubDirectoryIndexPage()
{
$client = static::createClient();

$client->request('GET', 'book/stephen-king');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSelectorTextSame('html h1', 'Stephen King Books');
}

public function testNotFoundPage()
{
$client = static::createClient();
Expand Down
1 change: 1 addition & 0 deletions tests/docs/book/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Book homepage
1 change: 1 addition & 0 deletions tests/docs/book/stephen-king/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Stephen King Books

0 comments on commit b54e978

Please sign in to comment.