Skip to content

Commit

Permalink
[TASK] Warn about duplicate link targets
Browse files Browse the repository at this point in the history
releases: main, 1.0
  • Loading branch information
linawolf committed Mar 17, 2024
1 parent 3896e4f commit 9daa2a3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\SectionNode;
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
use Psr\Log\LoggerInterface;
use SplStack;
use Webmozart\Assert\Assert;

use function sprintf;

/** @implements NodeTransformer<DocumentNode|AnchorNode|SectionNode> */
final class CollectLinkTargetsTransformer implements NodeTransformer
{
Expand All @@ -34,6 +37,7 @@ final class CollectLinkTargetsTransformer implements NodeTransformer

public function __construct(
private readonly AnchorNormalizer $anchorReducer,
private LoggerInterface|null $logger = null,
) {
/*
* TODO: remove stack here, as we should not have sub documents in this way, sub documents are
Expand Down Expand Up @@ -68,17 +72,43 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
$currentDocument = $this->documentStack->top();
Assert::notNull($currentDocument);
$anchor = $node->getId();
$compilerContext->getProjectNode()->addLinkTarget(
$anchor,
new InternalTarget(
$currentDocument->getFilePath(),
if ($compilerContext->getProjectNode()->hasInternalTarget($anchor, $node->getLinkType())) {
$this->logger?->warning(
sprintf(
'Duplicate anchor "%s" for link type "%s" in document "%s". The anchor is already used at "%s"',
$anchor,
$node->getLinkType(),
$compilerContext->getDocumentNode()->getFilePath(),
$compilerContext->getProjectNode()->getInternalTarget($anchor, $node->getLinkType())?->getDocumentPath(),
),
$compilerContext->getLoggerInformation(),
);
} else {
$compilerContext->getProjectNode()->addLinkTarget(
$anchor,
$node->getLinkText(),
$node->getLinkType(),
),
);
new InternalTarget(
$currentDocument->getFilePath(),
$anchor,
$node->getLinkText(),
$node->getLinkType(),
),
);
}
if ($node instanceof MultipleLinkTargetsNode) {
foreach ($node->getAdditionalIds() as $id) {
if ($compilerContext->getProjectNode()->hasInternalTarget($id, $node->getLinkType())) {
$this->logger?->warning(
sprintf(
'Duplicate anchor "%s" for link type "%s" in document "%s". The anchor is already used at "%s"',
$anchor,
$node->getLinkType(),
$compilerContext->getDocumentNode()->getFilePath(),
$compilerContext->getProjectNode()->getInternalTarget($anchor, $node->getLinkType())?->getDocumentPath(),
),
$compilerContext->getLoggerInformation(),
);
}

$compilerContext->getProjectNode()->addLinkTarget(
$id,
new InternalTarget(
Expand Down
5 changes: 5 additions & 0 deletions packages/guides/src/Nodes/ProjectNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public function addLinkTarget(string $anchorName, InternalTarget $target): void
$this->internalLinkTargets[$target->getLinkType()][$anchorName] = $target;
}

public function hasInternalTarget(string $anchorName, string $linkType = SectionNode::STD_LABEL): bool
{
return isset($this->internalLinkTargets[$linkType][$anchorName]);
}

public function getInternalTarget(string $anchorName, string $linkType = SectionNode::STD_LABEL): InternalTarget|null
{
return $this->internalLinkTargets[$linkType][$anchorName] ?? null;
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/tests/section-nesting/section-nesting.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ <h1>
<h1>
Level 1 Test 4
</h1>
<div class="section" id="level-2-test-3">
<div class="section" id="level-2-test-3b">
<h2>
Level 2 Test 3
Level 2 Test 3b
</h2>
<div class="section" id="level-3-test-1">
<h3>
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/tests/section-nesting/section-nesting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Level 1 Test 3
Level 1 Test 4
==============

Level 2 Test 3
--------------
Level 2 Test 3b
---------------

Level 3 Test 1
**************
Expand Down

0 comments on commit 9daa2a3

Please sign in to comment.