Skip to content

Commit

Permalink
[BUGFIX] Make links to special objects prefixable
Browse files Browse the repository at this point in the history
If we have both an anchor like '.. _demo:' and
a confval of the same name we would get duplicate id's and the links to those two elements could not be distinguished. Sphinx solves this by prefixing all confval links with "confval-".

Therefore, I introduce the possibility of prefixing links to distinguished linkable objects.

related phpDocumentor/guides#924

releases: main, 1.0
(cherry picked from commit 3ef4ee08b2049e485d6fb30beb85ee1cbbeb883d)
  • Loading branch information
linawolf committed Mar 23, 2024
1 parent 5688947 commit 96200cd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use phpDocumentor\Guides\Nodes\LinkTargetNode;
use phpDocumentor\Guides\Nodes\MultipleLinkTargetsNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\PrefixedLinkTargetNode;
use phpDocumentor\Guides\Nodes\SectionNode;
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -97,13 +98,19 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
$currentDocument = $this->documentStack->top();
Assert::notNull($currentDocument);
$anchor = $this->anchorReducer->reduceAnchor($node->getId());
$prefix = '';
if ($node instanceof PrefixedLinkTargetNode) {
$prefix = $node->getPrefix();
}

$this->addLinkTargetToProject(
$compilerContext,
new InternalTarget(
$currentDocument->getFilePath(),
$anchor,
$node->getLinkText(),
$node->getLinkType(),
$prefix,
),
);
if ($node instanceof MultipleLinkTargetsNode) {
Expand Down
6 changes: 6 additions & 0 deletions src/Meta/InternalTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
protected string $anchorName,
private readonly string|null $title = null,
private readonly string $linkType = SectionNode::STD_LABEL,
private readonly string $prefix = '',
) {
}

Expand Down Expand Up @@ -63,4 +64,9 @@ public function getLinkType(): string
{
return $this->linkType;
}

public function getPrefix(): string
{
return $this->prefix;
}
}
8 changes: 7 additions & 1 deletion src/Nodes/Inline/ReferenceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(
string $value = '',
private readonly string $interlinkDomain = '',
private readonly string $linkType = SectionNode::STD_LABEL,
private readonly string $prefix = '',
) {
parent::__construct(self::TYPE, $targetReference, $value);
}
Expand All @@ -62,6 +63,11 @@ public function getDebugInformation(): array

public function getInterlinkGroup(): string
{
return 'std:label';
return $this->linkType;
}

public function getPrefix(): string
{
return $this->prefix;
}
}
19 changes: 19 additions & 0 deletions src/Nodes/PrefixedLinkTargetNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Nodes;

interface PrefixedLinkTargetNode extends LinkTargetNode
{
public function getPrefix(): string;
}
2 changes: 1 addition & 1 deletion src/ReferenceResolvers/AnchorReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function resolve(LinkInlineNode $node, RenderContext $renderContext, Mess
return false;
}

$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getAnchor()));
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getPrefix() . $target->getAnchor()));
if ($node->getValue() === '') {
$node->setValue($target->getTitle() ?? '');
}
Expand Down

0 comments on commit 96200cd

Please sign in to comment.