Skip to content

Commit 89c2943

Browse files
authored
Merge pull request #998 from phpDocumentor/backport/1.x/pr-996
[1.x] [BUGFIX] Prefere anchors over titles in references
2 parents e8ae58c + 0ec7c3f commit 89c2943

File tree

28 files changed

+294
-46
lines changed

28 files changed

+294
-46
lines changed

packages/guides/resources/config/guides.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use phpDocumentor\Guides\ReferenceResolvers\ReferenceResolver;
4141
use phpDocumentor\Guides\ReferenceResolvers\ReferenceResolverPreRender;
4242
use phpDocumentor\Guides\ReferenceResolvers\SluggerAnchorNormalizer;
43+
use phpDocumentor\Guides\ReferenceResolvers\TitleReferenceResolver;
4344
use phpDocumentor\Guides\Renderer\HtmlRenderer;
4445
use phpDocumentor\Guides\Renderer\InMemoryRendererFactory;
4546
use phpDocumentor\Guides\Renderer\InterlinkObjectsRenderer;
@@ -146,6 +147,8 @@
146147

147148
->set(AnchorReferenceResolver::class)
148149

150+
->set(TitleReferenceResolver::class)
151+
149152
->set(InternalReferenceResolver::class)
150153

151154
->set(DocReferenceResolver::class)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function enterNode(Node $node, CompilerContextInterface $compilerContext)
8888
$currentDocument->getFilePath(),
8989
$anchorName,
9090
$node->getLinkText(),
91-
$node->getLinkType(),
91+
SectionNode::STD_TITLE,
9292
),
9393
);
9494

packages/guides/src/Nodes/SectionNode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
final class SectionNode extends CompoundNode implements LinkTargetNode
2020
{
2121
public const STD_LABEL = 'std:label';
22+
public const STD_TITLE = 'std:title';
2223

2324
public function __construct(private readonly TitleNode $title)
2425
{

packages/guides/src/ReferenceResolvers/AnchorHyperlinkResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
1717
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
18+
use phpDocumentor\Guides\Nodes\SectionNode;
1819
use phpDocumentor\Guides\RenderContext;
1920
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
2021

@@ -43,7 +44,10 @@ public function resolve(LinkInlineNode $node, RenderContext $renderContext, Mess
4344
$target = $renderContext->getProjectNode()->getInternalTarget($reducedAnchor);
4445

4546
if ($target === null) {
46-
return false;
47+
$target = $renderContext->getProjectNode()->getInternalTarget($reducedAnchor, SectionNode::STD_TITLE);
48+
if ($target === null) {
49+
return false;
50+
}
4751
}
4852

4953
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getAnchor()));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\ReferenceResolvers;
15+
16+
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
17+
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode;
18+
use phpDocumentor\Guides\Nodes\SectionNode;
19+
use phpDocumentor\Guides\RenderContext;
20+
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
21+
22+
/**
23+
* Resolves references with an anchor URL.
24+
*
25+
* A link is an anchor if it starts with a hashtag
26+
*/
27+
final class TitleReferenceResolver implements ReferenceResolver
28+
{
29+
public final const PRIORITY = -200;
30+
31+
public function __construct(
32+
private readonly AnchorNormalizer $anchorReducer,
33+
private readonly UrlGeneratorInterface $urlGenerator,
34+
) {
35+
}
36+
37+
public function resolve(LinkInlineNode $node, RenderContext $renderContext, Messages $messages): bool
38+
{
39+
if (!$node instanceof ReferenceNode || $node->getInterlinkDomain() !== '') {
40+
return false;
41+
}
42+
43+
$reducedAnchor = $this->anchorReducer->reduceAnchor($node->getTargetReference());
44+
$target = $renderContext->getProjectNode()->getInternalTarget($reducedAnchor, SectionNode::STD_TITLE);
45+
46+
if ($target === null) {
47+
return false;
48+
}
49+
50+
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getPrefix() . $target->getAnchor()));
51+
if ($node->getValue() === '') {
52+
$node->setValue($target->getTitle() ?? '');
53+
}
54+
55+
return true;
56+
}
57+
58+
public static function getPriority(): int
59+
{
60+
return self::PRIORITY;
61+
}
62+
}

tests/Integration/tests/anchor/anchor-to-page/expected/objects.inv.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@
2020
]
2121
},
2222
"std:label": {
23-
"page": [
23+
"rst-overview": [
2424
"-",
2525
"-",
26-
"index.html#page",
27-
"Page"
26+
"rst-overview.html#rst-overview",
27+
"Overview"
2828
],
29-
"overview": [
29+
"sphinx-overview": [
3030
"-",
3131
"-",
32-
"sphinx-overview.html#overview",
32+
"sphinx-overview.html#sphinx-overview",
3333
"Overview"
34-
],
35-
"rst-overview": [
34+
]
35+
},
36+
"std:title": {
37+
"page": [
3638
"-",
3739
"-",
38-
"rst-overview.html#rst-overview",
39-
"Overview"
40+
"index.html#page",
41+
"Page"
4042
],
41-
"sphinx-overview": [
43+
"overview": [
4244
"-",
4345
"-",
44-
"sphinx-overview.html#sphinx-overview",
46+
"sphinx-overview.html#overview",
4547
"Overview"
4648
]
4749
}

tests/Integration/tests/confval/confval-name/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Confval directive"
1414
]
1515
},
16-
"std:label": {
16+
"std:label": [],
17+
"std:title": {
1718
"confval-directive": [
1819
"-",
1920
"-",

tests/Integration/tests/confval/confval-noindex/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Confval directive"
1414
]
1515
},
16-
"std:label": {
16+
"std:label": [],
17+
"std:title": {
1718
"confval-directive": [
1819
"-",
1920
"-",

tests/Integration/tests/confval/confval-warning/expected/objects.inv.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
]
1515
},
1616
"std:label": {
17-
"confval-directive": [
17+
"demo": [
1818
"-",
1919
"-",
20-
"index.html#confval-directive",
20+
"another.html#demo",
2121
"Confval directive"
22-
],
23-
"demo": [
22+
]
23+
},
24+
"std:title": {
25+
"confval-directive": [
2426
"-",
2527
"-",
26-
"another.html#demo",
28+
"index.html#confval-directive",
2729
"Confval directive"
2830
]
2931
},

tests/Integration/tests/interlink/inventory_json/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"Subpage 2"
3838
]
3939
},
40-
"std:label": {
40+
"std:label": [],
41+
"std:title": {
4142
"document-title": [
4243
"-",
4344
"-",

tests/Integration/tests/interlink/inventory_ref_json/expected/objects.inv.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,36 @@
3838
]
3939
},
4040
"std:label": {
41-
"document-title": [
42-
"My Project",
43-
"3.1.4",
44-
"index.html#document-title",
45-
"Document Title"
46-
],
4741
"my-index": [
4842
"My Project",
4943
"3.1.4",
5044
"index.html#my_index",
5145
"Document Title"
5246
],
53-
"page-1": [
47+
"my-cool-page": [
5448
"My Project",
5549
"3.1.4",
56-
"page1.html#page-1",
50+
"page1.html#my_cool_page",
5751
"Page 1"
5852
],
59-
"my-cool-page": [
53+
"another-cool-page": [
6054
"My Project",
6155
"3.1.4",
62-
"page1.html#my_cool_page",
56+
"subfolder\/subpage1.html#another_cool_page",
57+
"Subpage 1"
58+
]
59+
},
60+
"std:title": {
61+
"document-title": [
62+
"My Project",
63+
"3.1.4",
64+
"index.html#document-title",
65+
"Document Title"
66+
],
67+
"page-1": [
68+
"My Project",
69+
"3.1.4",
70+
"page1.html#page-1",
6371
"Page 1"
6472
],
6573
"page-2": [
@@ -80,12 +88,6 @@
8088
"subfolder\/subpage1.html#subpage-1",
8189
"Subpage 1"
8290
],
83-
"another-cool-page": [
84-
"My Project",
85-
"3.1.4",
86-
"subfolder\/subpage1.html#another_cool_page",
87-
"Subpage 1"
88-
],
8991
"subpage-2": [
9092
"My Project",
9193
"3.1.4",

tests/Integration/tests/meta/config-file/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"title": [
1213
"Project Title",
1314
"6.4",

tests/Integration/tests/meta/meta-data/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Document Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"document-title": [
1213
"My Project",
1314
"3.1.4",

tests/Integration/tests/meta/project-meta-global/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Document Title"
1414
]
1515
},
16-
"std:label": {
16+
"std:label": [],
17+
"std:title": {
1718
"a-page": [
1819
"My Project",
1920
"3.1.4",

tests/Integration/tests/meta/project-meta/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Document Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"document-title": [
1213
"My Project",
1314
"3.1.4",

tests/Integration/tests/meta/project-title-override-warning/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Document Title"
1414
]
1515
},
16-
"std:label": {
16+
"std:label": [],
17+
"std:title": {
1718
"a-page": [
1819
"My Project",
1920
"3.1.4",

tests/Integration/tests/meta/project-title/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Document Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"document-title": [
1213
"My Project",
1314
"-",

tests/Integration/tests/meta/settings/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Document Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"document-title": [
1213
"My Project",
1314
"3.1.4",

tests/Integration/tests/numeral-rst/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"Some Title"
1414
]
1515
},
16-
"std:label": {
16+
"std:label": [],
17+
"std:title": {
1718
"page-not-found-404": [
1819
"-",
1920
"-",

tests/Integration/tests/option/expected/objects.inv.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Some Title"
88
]
99
},
10-
"std:label": {
10+
"std:label": [],
11+
"std:title": {
1112
"some-title": [
1213
"-",
1314
"-",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- content start -->
2+
<div class="section" id="another-page">
3+
<h1>Another Page</h1>
4+
5+
<p>Lorem Ipsum</p>
6+
7+
<div class="section" id="flexforms">
8+
<h2>FlexForms</h2>
9+
10+
<p><a href="index.html#flexforms">FlexForms</a></p>
11+
12+
</div>
13+
</div>
14+
<!-- content end -->

0 commit comments

Comments
 (0)