Skip to content

Commit e10e781

Browse files
linawolfjaapio
authored andcommitted
[BUGFIX] Prevent Duplicates in toctree and menu
A page that has already been explicitely added to a toctree does not get added by the glob directive once more. As having multiple occurences of the same page within one toctree or menu does not make sense in general, I applied array_unique to the menu_items of one level of the toctree or menu.
1 parent 77054c9 commit e10e781

File tree

10 files changed

+95
-1
lines changed

10 files changed

+95
-1
lines changed

packages/guides/src/Compiler/NodeTransformers/MenuNodeAddEntryTransformer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Log\LoggerInterface;
1717

1818
use function array_pop;
19+
use function array_unique;
1920
use function assert;
2021
use function explode;
2122
use function implode;
@@ -85,6 +86,8 @@ public function leaveNode(Node $node, CompilerContext $compilerContext): Node|nu
8586
}
8687
}
8788

89+
$menuEntries = array_unique($menuEntries);
90+
8891
$node = $node->withMenuEntries($menuEntries);
8992

9093
return $node;

packages/guides/src/Nodes/Menu/MenuEntryNode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public function addSection(MenuEntryNode $section): void
7272
{
7373
$this->sections[] = $section;
7474
}
75+
76+
public function __toString(): string
77+
{
78+
return $this->url . '#' . $this->anchor;
79+
}
7580
}

packages/guides/tests/unit/Compiler/NodeTransformers/MenuNodeAddSubDocumentsTransformerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ public static function tocTreeEntryProvider(): array
7979
'testRelativeTocUrlInSubdir' => [
8080
'current' => 'doc1/index',
8181
'paths' => ['index', 'doc1', 'doc2','doc1/index' , 'doc1/subdoc1', 'doc1/subdoc2', 'doc1/subdoc3', 'doc3/index'],
82-
'tocFiles' => ['subdoc1', 'subdoc1'],
82+
'tocFiles' => ['subdoc1', 'subdoc2'],
8383
'expectedCount' => 2,
8484
],
85+
'testMultipleGetRemoved' => [
86+
'current' => 'doc1/index',
87+
'paths' => ['index', 'doc1', 'doc2','doc1/index' , 'doc1/subdoc1', 'doc1/subdoc2', 'doc1/subdoc3', 'doc3/index'],
88+
'tocFiles' => ['subdoc1', 'subdoc1'],
89+
'expectedCount' => 1,
90+
],
8591
'testTocTreeAbsoluteGlobDoesNotAddIndex' => [
8692
'current' => 'index',
8793
'paths' => ['index', 'doc1', 'doc2', 'doc1/subdoc', 'doc3/doc1'],
@@ -103,6 +109,13 @@ public static function tocTreeEntryProvider(): array
103109
'expectedCount' => 2,
104110
'glob' => true,
105111
],
112+
'testRelativeGlobDoesNotAddDuplicates' => [
113+
'current' => 'index',
114+
'paths' => ['index', 'doc1', 'doc2', 'doc1/subdoc', 'doc3/doc1'],
115+
'tocFiles' => ['doc1','*'],
116+
'expectedCount' => 2,
117+
'glob' => true,
118+
],
106119
'testRelativeGlobInSubdirDoesNotAddIndex' => [
107120
'current' => 'doc1/index',
108121
'paths' => ['index', 'doc1', 'doc2','doc1/index' , 'doc1/subdoc1', 'doc1/subdoc2', 'doc1/subdoc3', 'doc3/index'],
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>index</title>
5+
6+
</head>
7+
<body>
8+
<div class="section" id="index">
9+
<h1>index</h1>
10+
11+
<div class="toc">
12+
<ul class="phpdocumentor-list">
13+
<li class="toc-item"><a href="/overview.html#overview">Overview</a></li>
14+
15+
<li class="toc-item"><a href="/apple.html#apple">Apple</a></li>
16+
17+
<li class="toc-item"><a href="/banana.html#banana">Banana</a></li>
18+
19+
<li class="toc-item"><a href="/cherry.html#cherry">Cherry</a></li>
20+
21+
</ul>
22+
</div>
23+
24+
</div>
25+
26+
</body>
27+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Overview</title>
5+
6+
</head>
7+
<body>
8+
<div class="section" id="overview">
9+
<h1>Overview</h1>
10+
11+
<p>Lorem Ipsum Dolor</p>
12+
<div class="menu"><ul class="phpdocumentor-list"><li class="toc-item"><a href="/overview.html#overview">Overview</a></li><li class="toc-item"><a href="/apple.html#apple">Apple</a></li><li class="toc-item"><a href="/banana.html#banana">Banana</a></li><li class="toc-item"><a href="/cherry.html#cherry">Cherry</a></li></ul></div>
13+
</div>
14+
15+
</body>
16+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Apple
2+
=====
3+
4+
Lorem Ipsum Dolor
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Banana
2+
======
3+
4+
Lorem Ipsum Dolor
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Cherry
2+
======
3+
4+
Lorem Ipsum Dolor
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
index
2+
=====
3+
4+
.. toctree::
5+
:titlesonly:
6+
:glob:
7+
8+
overview
9+
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Overview
2+
========
3+
4+
Lorem Ipsum Dolor
5+
6+
.. menu::
7+
8+
overview
9+
*

0 commit comments

Comments
 (0)