Skip to content

Commit

Permalink
Merge pull request #758 from wouterj/section-anchors
Browse files Browse the repository at this point in the history
Move section anchors before the section title
  • Loading branch information
jaapio authored Dec 22, 2023
2 parents 33f8712 + 2f4502e commit b26277e
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div class="section{% if node.classes %} {{ node.classesString }}{% endif %}" id="{{ node.title.id }}">
{{ renderNode(node.title) }}
{% for childNode in node.children %}
{{ renderNode(childNode) }}
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{{ renderNode(node.title) }}

{% for childNode in node.children %}
{{ renderNode(childNode) }}
{% endfor %}
2 changes: 1 addition & 1 deletion packages/guides/src/Nodes/SectionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SectionNode extends CompoundNode implements LinkTargetNode

public function __construct(private readonly TitleNode $title)
{
parent::__construct();
parent::__construct([$title]);
}

public function getTitle(): TitleNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testAnchorNodeShouldBeMovedToNextSectionNodeWhenPositionedAboveS
$this->documentNodeTraverser->traverse($document, $context);

self::assertCount(1, $context->getDocumentNode()->getChildren());
self::assertCount(1, $section->getChildren());
self::assertCount(2, $section->getChildren());
self::assertSame($node, $section->getChildren()[0]);
}

Expand All @@ -74,7 +74,7 @@ public function testMultipleAnchorsShouldBeMovedToNextSectionNodeWhenPositionedA
$this->documentNodeTraverser->traverse($document, $context);

self::assertCount(1, $context->getDocumentNode()->getChildren());
self::assertCount(4, $section->getChildren());
self::assertCount(5, $section->getChildren());
self::assertEquals($node4, $section->getChildren()[0]);
self::assertEquals($node3, $section->getChildren()[1]);
self::assertEquals($node2, $section->getChildren()[2]);
Expand All @@ -84,8 +84,10 @@ public function testMultipleAnchorsShouldBeMovedToNextSectionNodeWhenPositionedA
public function testAnchorShouldNotBeMovedTwice(): void
{
$node1 = new AnchorNode('foo');
$section = new SectionNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('foo'), 1, 'id'));
$subSection = new SectionNode(new TitleNode(InlineCompoundNode::getPlainTextInlineNode('foo'), 1, 'id'));
$sectionTitle = new TitleNode(InlineCompoundNode::getPlainTextInlineNode('foo'), 1, 'id');
$section = new SectionNode($sectionTitle);
$subSectionTitle = new TitleNode(InlineCompoundNode::getPlainTextInlineNode('sub foo'), 2, 'sub-id');
$subSection = new SectionNode($subSectionTitle);
$section->addChildNode(new AnchorNode('bar'));
$section->addChildNode($subSection);

Expand All @@ -100,10 +102,10 @@ public function testAnchorShouldNotBeMovedTwice(): void
self::assertCount(1, $context->getDocumentNode()->getChildren());
$updatedSection = $context->getDocumentNode()->getChildren()[0];
self::assertInstanceOf(SectionNode::class, $updatedSection);
self::assertEquals([$node1, $subSection], $updatedSection->getChildren());
$updatedSubSection = $updatedSection->getChildren()[1];
self::assertEquals([$node1, $sectionTitle, $subSection], $updatedSection->getChildren());
$updatedSubSection = $updatedSection->getChildren()[2];
self::assertInstanceOf(SectionNode::class, $updatedSubSection);
self::assertEquals([new AnchorNode('bar')], $updatedSubSection->getChildren());
self::assertEquals([new AnchorNode('bar'), $subSectionTitle], $updatedSubSection->getChildren());
}

public function testNoMoveWhenAnchorIsOnlyChild(): void
Expand Down Expand Up @@ -143,8 +145,8 @@ public function testMoveAnchorsAtTheEndOfSectionToNextSection(): void
[$firstChild, $secondChild] = $context->getDocumentNode()->getChildren();
self::assertInstanceOf(SectionNode::class, $firstChild);
self::assertInstanceOf(SectionNode::class, $secondChild);
self::assertCount(0, $firstChild->getChildren());
self::assertCount(2, $secondChild->getChildren());
self::assertCount(1, $firstChild->getChildren());
self::assertCount(3, $secondChild->getChildren());
}

public function testMoveAnchorsAtTheEndOfSectionToNextParentNeighbourSection(): void
Expand All @@ -171,7 +173,7 @@ public function testMoveAnchorsAtTheEndOfSectionToNextParentNeighbourSection():
[$firstChild, $secondChild] = $context->getDocumentNode()->getChildren();
self::assertInstanceOf(SectionNode::class, $firstChild);
self::assertInstanceOf(SectionNode::class, $secondChild);
self::assertCount(1, $firstChild->getChildren());
self::assertCount(2, $secondChild->getChildren());
self::assertCount(2, $firstChild->getChildren());
self::assertCount(3, $secondChild->getChildren());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public function testRemoveChildFromChild(): void

$treeNode->getChildren()[0]->removeChild($nodeToRemove);

self::assertCount(0, $treeNode->getChildren()[0]->getChildren());
self::assertCount(1, $treeNode->getChildren()[0]->getChildren());
self::assertInstanceOf(CompoundNode::class, $treeNode->getChildren()[0]->getNode());
self::assertCount(0, $treeNode->getChildren()[0]->getNode()->getChildren());
self::assertCount(1, $treeNode->getChildren()[0]->getNode()->getChildren());
self::assertInstanceOf(CompoundNode::class, $treeNode->getNode());
self::assertSame($treeNode->getNode()->getChildren()[0], $treeNode->getChildren()[0]->getNode());
self::assertSame($treeNode->getNode(), $treeNode->getRoot()->getNode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<h1>Overview</h1>

<div class="section" id="overview-1">
<a id="rst-overview"></a>
<h2>Overview 1</h2>

<a id="rst-overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-2">
<a id="sphinx-overview"></a>
<h2>Overview 2</h2>

<a id="sphinx-overview"></a>
<p>Sphinx Overview content</p>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- content start -->
<div class="section" id="overview">
<a id="rst-overview"></a>
<h1>Overview</h1>

<a id="rst-overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-1">
<a id="sphinx-overview"></a>
<h1>Overview</h1>

<a id="sphinx-overview"></a>
<p>Sphinx Overview content</p>
<div class="section" id="somewhere-else">
<h2>Somewhere else</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- content start -->
<div class="section" id="overview">
<a id="rst-overview"></a>
<h1>Overview</h1>

<a id="rst-overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-1">
<a id="sphinx-overview"></a>
<h1>Overview</h1>

<a id="sphinx-overview"></a>
<p>Sphinx Overview content</p>
<div class="section" id="somewhere-else">
<h2>Somewhere else</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- content start -->
<div class="section" id="overview">
<a id="rst-overview"></a>
<h1>Overview</h1>

<a id="rst-overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-1">
<a id="sphinx-overview"></a>
<h1>Overview</h1>

<a id="sphinx-overview"></a>
<p>Sphinx Overview content</p>
<div class="section" id="somewhere-else">
<h2>Somewhere else</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- content start -->
<div class="section" id="some-file">
<a id="page1"></a>
<h1>Some file</h1>

<a id="page1"></a>


<ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- content start -->
<div class="section" id="overview">
<a id="RST Overview"></a>
<h1>Overview</h1>

<a id="RST Overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-1">
<a id="Other Overview"></a>
<h1>Overview</h1>

<a id="Other Overview"></a>
<p>Other Overview content</p>
<p>This is a link to the RST Overview: alternative</p>
<p>This is a link to the Other Overview: </p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- content start -->
<div class="section" id="overview">
<a id="RST Overview"></a>
<h1>Overview</h1>

<a id="RST Overview"></a>
<p>RST Overview content</p>
</div>

<div class="section" id="overview-1">
<a id="Other Overview"></a>
<h1>Overview</h1>

<a id="Other Overview"></a>
<p>Other Overview content</p>
<p>This is a link to the RST Overview: <a href="/index.html#RST Overview">alternative</a></p>
<p>This is a link to the Other Overview: <a href="/index.html#Other Overview">Overview</a></p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- content start -->
<div class="section" id="subpage-1-level-2-1">
<a id="level-2-1-subpage1"></a>
<h1>Subpage 1, Level 2-1</h1>

<a id="level-2-1-subpage1"></a>
<p>Lorem Ipsum Dolor.</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- content start -->
<div class="section" id="subpage-2-level-1-2">
<a id="level-1-2-subpage2"></a>
<h1>Subpage 2, Level 1-2</h1>

<a id="level-1-2-subpage2"></a>
<p>Lorem Ipsum Dolor.</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- content start -->
<div class="section" id="subpage-1-level-2-1">
<a id="level-2-1-subpage1"></a>
<h1>Subpage 1, Level 2-1</h1>

<a id="level-2-1-subpage1"></a>
<p>Lorem Ipsum Dolor.</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- content start -->
<div class="section" id="subpage-2-level-1-2">
<a id="level-1-2-subpage2"></a>
<h1>Subpage 2, Level 1-2</h1>

<a id="level-1-2-subpage2"></a>
<p>Lorem Ipsum Dolor.</p>


Expand Down

0 comments on commit b26277e

Please sign in to comment.