diff --git a/packages/guides/src/Compiler/Passes/AutomaticMenuPass.php b/packages/guides/src/Compiler/Passes/AutomaticMenuPass.php
index 8dee004fd..077578628 100644
--- a/packages/guides/src/Compiler/Passes/AutomaticMenuPass.php
+++ b/packages/guides/src/Compiler/Passes/AutomaticMenuPass.php
@@ -17,11 +17,20 @@
use phpDocumentor\Guides\Compiler\CompilerPass;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Settings\SettingsManager;
+use Psr\Log\LoggerInterface;
+
+use function array_pop;
+use function count;
+use function explode;
+use function implode;
+use function in_array;
+use function sprintf;
final class AutomaticMenuPass implements CompilerPass
{
public function __construct(
private readonly SettingsManager $settingsManager,
+ private readonly LoggerInterface|null $logger = null,
) {
}
@@ -43,6 +52,7 @@ public function run(array $documents, CompilerContextInterface $compilerContext)
$projectNode = $compilerContext->getProjectNode();
$rootDocumentEntry = $projectNode->getRootDocumentEntry();
+ $indexNames = explode(',', $this->settingsManager->getProjectSettings()->getIndexName());
foreach ($documents as $documentNode) {
if ($documentNode->isOrphan()) {
// Do not add orphans to the automatic menu
@@ -53,9 +63,48 @@ public function run(array $documents, CompilerContextInterface $compilerContext)
continue;
}
- $documentEntry = $projectNode->getDocumentEntry($documentNode->getFilePath());
- $documentEntry->setParent($rootDocumentEntry);
- $rootDocumentEntry->addChild($documentEntry);
+ $filePath = $documentNode->getFilePath();
+ $pathParts = explode('/', $filePath);
+ $documentEntry = $projectNode->getDocumentEntry($filePath);
+ if (count($pathParts) === 1 || count($pathParts) === 2 && in_array($pathParts[1], $indexNames, true)) {
+ $documentEntry->setParent($rootDocumentEntry);
+ $rootDocumentEntry->addChild($documentEntry);
+ continue;
+ }
+
+ $fileName = array_pop($pathParts);
+ $path = implode('/', $pathParts);
+ if (in_array($fileName, $indexNames, true)) {
+ array_pop($pathParts);
+ $path = implode('/', $pathParts);
+ }
+
+ $parentFound = false;
+ foreach ($indexNames as $indexName) {
+ $indexFile = $path . '/' . $indexName;
+ $parentEntry = $projectNode->findDocumentEntry($indexFile);
+ if ($parentEntry === null) {
+ continue;
+ }
+
+ $documentEntry->setParent($parentEntry);
+ $parentEntry->addChild($documentEntry);
+ $parentFound = true;
+ break;
+ }
+
+ if ($parentFound) {
+ continue;
+ }
+
+ $parentEntry = $projectNode->findDocumentEntry($path);
+ if ($parentEntry === null) {
+ $this->logger?->warning(sprintf('No parent found for file "%s/%s" attaching it to the document root instead. ', $path, $fileName));
+ continue;
+ }
+
+ $documentEntry->setParent($parentEntry);
+ $parentEntry->addChild($documentEntry);
}
return $documents;
diff --git a/packages/guides/src/Compiler/Passes/GlobalMenuPass.php b/packages/guides/src/Compiler/Passes/GlobalMenuPass.php
index 13ec50799..bdbabff9a 100644
--- a/packages/guides/src/Compiler/Passes/GlobalMenuPass.php
+++ b/packages/guides/src/Compiler/Passes/GlobalMenuPass.php
@@ -90,14 +90,23 @@ public function run(array $documents, CompilerContextInterface $compilerContext)
private function getNavMenuNodeFromDocumentEntries(CompilerContextInterface $compilerContext): NavMenuNode
{
- $menuEntries = [];
$rootDocumentEntry = $compilerContext->getProjectNode()->getRootDocumentEntry();
+ $menuEntries = $this->getMenuEntriesFromDocumentEntries($rootDocumentEntry);
+
+ return new NavMenuNode($menuEntries);
+ }
+
+ /** @return InternalMenuEntryNode[] */
+ public function getMenuEntriesFromDocumentEntries(DocumentEntryNode $rootDocumentEntry): array
+ {
+ $menuEntries = [];
foreach ($rootDocumentEntry->getChildren() as $documentEntryNode) {
- $newMenuEntry = new InternalMenuEntryNode($documentEntryNode->getFile(), $documentEntryNode->getTitle(), [], false, 1);
+ $children = $this->getMenuEntriesFromDocumentEntries($documentEntryNode);
+ $newMenuEntry = new InternalMenuEntryNode($documentEntryNode->getFile(), $documentEntryNode->getTitle(), $children, false, 1);
$menuEntries[] = $newMenuEntry;
}
- return new NavMenuNode($menuEntries);
+ return $menuEntries;
}
private function getNavMenuNodefromTocNode(CompilerContextInterface $compilerContext, TocNode $tocNode, string|null $menuType = null): NavMenuNode
diff --git a/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/expected/index.html b/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/expected/index.html
index 407d880ed..2a663cebf 100644
--- a/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/expected/index.html
+++ b/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/expected/index.html
@@ -52,11 +52,11 @@
Another Page
-
+
Yet Another Page
-
+
And Yet Another Page
@@ -88,33 +88,6 @@ Document Title
Lorem Ipsum Dolor.
-
-
-
diff --git a/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/input/skip b/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/input/skip
deleted file mode 100644
index 95484cd4d..000000000
--- a/tests/Integration/tests-full/automatic_menu/automatic-multilevel-menu/input/skip
+++ /dev/null
@@ -1 +0,0 @@
-Nested autocreated menus are not yet supported
\ No newline at end of file
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/index.html b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/index.html
new file mode 100644
index 000000000..f8ccc2b85
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/index.html
@@ -0,0 +1,167 @@
+
+
+
+ Dir 2 Title
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/somePage.html b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/somePage.html
new file mode 100644
index 000000000..3c8571544
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/dir2/somePage.html
@@ -0,0 +1,168 @@
+
+
+
+ Some Page in dir 2
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/index.html b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/index.html
new file mode 100644
index 000000000..0fe7f7d47
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/expected/index.html
@@ -0,0 +1,166 @@
+
+
+
+ Document Title
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Document Title
+
+
+
+
+
Document Title
+
+
Lorem Ipsum Dolor.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/anotherPage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/anotherPage.md
new file mode 100644
index 000000000..02e0321a5
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/anotherPage.md
@@ -0,0 +1,3 @@
+# Another Page
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir1/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir1/index.md
new file mode 100644
index 000000000..862aaea1a
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir1/index.md
@@ -0,0 +1,3 @@
+# Dir 1 Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/index.md
new file mode 100644
index 000000000..2f91b9fee
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/index.md
@@ -0,0 +1,3 @@
+# Dir 2 Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/somePage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/somePage.md
new file mode 100644
index 000000000..082675e84
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/somePage.md
@@ -0,0 +1,4 @@
+
+# Some Page in dir 2
+
+Lorem Ipsum `Dolor`.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir1/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir1/index.md
new file mode 100644
index 000000000..52cc0f8a4
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir1/index.md
@@ -0,0 +1,3 @@
+# Subdir 1 Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/index.md
new file mode 100644
index 000000000..2d7808f9e
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/index.md
@@ -0,0 +1,3 @@
+# Subdir 2 Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/somePage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/somePage.md
new file mode 100644
index 000000000..082675e84
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/somePage.md
@@ -0,0 +1,4 @@
+
+# Some Page in dir 2
+
+Lorem Ipsum `Dolor`.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/subsubdir1/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/subsubdir1/index.md
new file mode 100644
index 000000000..4e15974c0
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/subdir2/subsubdir1/index.md
@@ -0,0 +1,3 @@
+# Subsubdir 1 Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/yetAnotherPage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/yetAnotherPage.md
new file mode 100644
index 000000000..fd7c625a3
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/dir2/yetAnotherPage.md
@@ -0,0 +1,3 @@
+# Yet Another Page
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/guides.xml b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/guides.xml
new file mode 100644
index 000000000..4759d605e
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/guides.xml
@@ -0,0 +1,11 @@
+
+
+
+
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/index.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/index.md
new file mode 100644
index 000000000..9f6ae85ae
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/index.md
@@ -0,0 +1,3 @@
+# Document Title
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/somePage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/somePage.md
new file mode 100644
index 000000000..bc2473dd3
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/somePage.md
@@ -0,0 +1,4 @@
+
+# Some Page
+
+Lorem Ipsum `Dolor`.
diff --git a/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/yetAnotherPage.md b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/yetAnotherPage.md
new file mode 100644
index 000000000..fd7c625a3
--- /dev/null
+++ b/tests/Integration/tests-full/markdown-full/multi-nested-menu-md/input/yetAnotherPage.md
@@ -0,0 +1,3 @@
+# Yet Another Page
+
+Lorem Ipsum Dolor.
diff --git a/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/index.html b/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/index.html
index 30dfc3d5e..298cbe2a9 100644
--- a/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/index.html
+++ b/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/index.html
@@ -33,7 +33,30 @@
Dir 2 Title
-
+
+
+
Some Page
@@ -64,15 +87,15 @@
Dir 2 Title
-
+ class="nav-link current active" aria-current="page">Dir 2 Title
+
+
Some Page
diff --git a/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/somePage.html b/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/somePage.html
index 25b2abeda..0ec8ffd67 100644
--- a/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/somePage.html
+++ b/tests/Integration/tests-full/markdown-full/nested-menu-md/expected/dir2/somePage.html
@@ -30,14 +30,33 @@
Dir 1 Title
-
+
Dir 2 Title
-
-
- Some Page in dir 2
-
-
+
+
+
Some Page
@@ -68,13 +87,14 @@
Dir 2 Title
-
+ class="nav-link active">Dir 2 Title
+
+
Dir 2 Title
-
-
+
+
+
Some Page
@@ -71,12 +87,15 @@
Dir 2 Title
-
-
+ class="nav-link">Dir 2 Title
+
+
+
Some Page
diff --git a/tests/Integration/tests-full/markdown-full/nested-menu-md/input/skip b/tests/Integration/tests-full/markdown-full/nested-menu-md/input/skip
deleted file mode 100644
index 95484cd4d..000000000
--- a/tests/Integration/tests-full/markdown-full/nested-menu-md/input/skip
+++ /dev/null
@@ -1 +0,0 @@
-Nested autocreated menus are not yet supported
\ No newline at end of file