Skip to content

Commit 4211db1

Browse files
committed
[BUGFIX] Fix toctrees with maxdepth, without titlesonly
1 parent 4494706 commit 4211db1

File tree

8 files changed

+288
-89
lines changed

8 files changed

+288
-89
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace phpDocumentor\Guides\Compiler\NodeTransformers\MenuNodeTransformers;
66

77
use phpDocumentor\Guides\Compiler\CompilerContext;
8+
use phpDocumentor\Guides\Nodes\Menu\ContentMenuNode;
89
use phpDocumentor\Guides\Nodes\Menu\MenuEntryNode;
910
use phpDocumentor\Guides\Nodes\Menu\MenuNode;
1011
use phpDocumentor\Guides\Nodes\Menu\SectionMenuEntryNode;
@@ -15,7 +16,7 @@
1516

1617
use const PHP_INT_MAX;
1718

18-
final class SectionMenuEntryNodeTransformer extends AbstractMenuEntryNodeTransformer
19+
final class ContentsMenuEntryNodeTransformer extends AbstractMenuEntryNodeTransformer
1920
{
2021
private const DEFAULT_MAX_LEVELS = PHP_INT_MAX;
2122

@@ -33,6 +34,10 @@ public function supports(Node $node): bool
3334
/** @return list<MenuEntryNode> */
3435
protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $node, CompilerContext $compilerContext): array
3536
{
37+
if (!$currentMenu instanceof ContentMenuNode) {
38+
return [$node];
39+
}
40+
3641
assert($node instanceof SectionMenuEntryNode);
3742
$depth = (int) $currentMenu->getOption('depth', self::DEFAULT_MAX_LEVELS - 1) + 1;
3843
$documentEntry = $compilerContext->getDocumentNode()->getDocumentEntry();

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $node, C
5959
$this->isInRootline($documentEntry, $compilerContext->getDocumentNode()->getDocumentEntry()),
6060
$this->isCurrent($documentEntry, $currentPath),
6161
);
62-
if (!$currentMenu->hasOption('titlesonly')) {
63-
$this->addSubSectionsToMenuEntries($documentEntry, $menuEntry, $maxDepth - 1);
62+
if (!$currentMenu->hasOption('titlesonly') && $maxDepth > 1) {
63+
$this->addSubSectionsToMenuEntries($documentEntry, $menuEntry, $maxDepth);
6464
}
6565

6666
if ($currentMenu instanceof TocNode) {

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/SubInternalMenuEntryNodeTransformer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $node, C
3838
assert($node instanceof InternalMenuEntryNode);
3939
$maxDepth = (int) $currentMenu->getOption('maxdepth', self::DEFAULT_MAX_LEVELS);
4040
$documentEntryOfMenuEntry = $compilerContext->getProjectNode()->getDocumentEntry($node->getUrl());
41-
$this->addSubEntries($compilerContext, $node, $documentEntryOfMenuEntry, $node->getLevel() + 1, $maxDepth);
41+
$this->addSubEntries($currentMenu, $compilerContext, $node, $documentEntryOfMenuEntry, $node->getLevel() + 1, $maxDepth);
4242

4343
return [$node];
4444
}
@@ -50,6 +50,7 @@ public function getPriority(): int
5050
}
5151

5252
private function addSubEntries(
53+
MenuNode $currentMenu,
5354
CompilerContext $compilerContext,
5455
InternalMenuEntryNode $sectionMenuEntry,
5556
DocumentEntryNode $documentEntry,
@@ -71,8 +72,13 @@ private function addSubEntries(
7172
self::isInRootline($subDocumentEntryNode, $compilerContext->getDocumentNode()->getDocumentEntry()),
7273
self::isCurrent($subDocumentEntryNode, $compilerContext->getDocumentNode()->getFilePath()),
7374
);
75+
76+
if (!$currentMenu->hasOption('titlesonly') && $maxDepth - $currentLevel + 1 > 1) {
77+
$this->addSubSectionsToMenuEntries($subDocumentEntryNode, $subMenuEntry, $maxDepth - $currentLevel + 2);
78+
}
79+
7480
$sectionMenuEntry->addMenuEntry($subMenuEntry);
75-
$this->addSubEntries($compilerContext, $subMenuEntry, $subDocumentEntryNode, $currentLevel + 1, $maxDepth);
81+
$this->addSubEntries($currentMenu, $compilerContext, $subMenuEntry, $subDocumentEntryNode, $currentLevel + 1, $maxDepth);
7682
}
7783
}
7884
}

tests/Integration/tests/toctree/toctree-maxdepth/expected/index.html

Lines changed: 235 additions & 83 deletions
Large diffs are not rendered by default.

tests/Integration/tests/toctree/toctree-maxdepth/input/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ Maxdepth 4
3232

3333
page1
3434
subpage2/page1
35+
36+
Maxdepth 5
37+
38+
.. toctree::
39+
:maxdepth: 5
40+
41+
page1
42+
subpage2/page1
43+
44+
45+
46+
No Maxdepth
47+
48+
.. toctree::
49+
50+
page1
51+
subpage2/page1

tests/Integration/tests/toctree/toctree-maxdepth/input/page1.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Page 1, Level 3
1515

1616
Paragraph.
1717

18+
Page 1, Level 4
19+
...............
20+
21+
Paragraph.
22+
23+
Page 1, Level 5
24+
###############
25+
26+
Paragraph.
27+
1828
Page 1, Level 2.2
1929
-----------------
2030

tests/Integration/tests/toctree/toctree-maxdepth/input/skip

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/Integration/tests/toctree/toctree-maxdepth/input/subpage1/page1.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ Subpage 1, Page 1, Level 3
1313

1414
Paragraph.
1515

16+
Subpage 1, Page 1, Level 4
17+
..........................
18+
19+
Paragraph.
20+
21+
Subpage 1, Page 1, Level 5
22+
##########################
23+
24+
Paragraph.
25+
1626
Subpage 1, Page 1, Level 2.2
1727
----------------------------
1828

0 commit comments

Comments
 (0)