|
| 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 T3Docs\Typo3DocsTheme\Compiler\NodeTransformers; |
| 15 | + |
| 16 | +use phpDocumentor\Guides\Compiler\CompilerContextInterface; |
| 17 | +use phpDocumentor\Guides\Compiler\NodeTransformer; |
| 18 | +use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode; |
| 19 | +use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; |
| 20 | +use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; |
| 21 | +use phpDocumentor\Guides\Nodes\Node; |
| 22 | +use T3Docs\Typo3DocsTheme\Settings\Typo3DocsThemeSettings; |
| 23 | + |
| 24 | +use function assert; |
| 25 | + |
| 26 | +/** @implements NodeTransformer<CrossReferenceNode> */ |
| 27 | +final class RemoveInterlinkSelfReferencesFromCrossReferenceNodeTransformer implements NodeTransformer |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private readonly Typo3DocsThemeSettings $themeSettings, |
| 31 | + ) {} |
| 32 | + |
| 33 | + public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node |
| 34 | + { |
| 35 | + return $node; |
| 36 | + } |
| 37 | + |
| 38 | + public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null |
| 39 | + { |
| 40 | + assert($node instanceof CrossReferenceNode); |
| 41 | + if (!$this->themeSettings->hasSettings('interlink_shortcode')) { |
| 42 | + return $node; |
| 43 | + } |
| 44 | + $interlink = $this->themeSettings->getSettings('interlink_shortcode'); |
| 45 | + if ($interlink === '' || $node->getInterlinkDomain() !== $interlink) { |
| 46 | + return $node; |
| 47 | + } |
| 48 | + // Remove interlink references to the own current document |
| 49 | + if ($node instanceof ReferenceNode) { |
| 50 | + $newRef = new ReferenceNode( |
| 51 | + $node->getTargetReference(), |
| 52 | + $node->getValue(), |
| 53 | + '', |
| 54 | + $node->getLinkType(), |
| 55 | + $node->getPrefix() |
| 56 | + ); |
| 57 | + return $newRef; |
| 58 | + } |
| 59 | + if ($node instanceof DocReferenceNode) { |
| 60 | + $newDocRef = new DocReferenceNode( |
| 61 | + $node->getTargetReference(), |
| 62 | + $node->getValue(), |
| 63 | + ); |
| 64 | + return $newDocRef; |
| 65 | + } |
| 66 | + return $node; |
| 67 | + } |
| 68 | + |
| 69 | + public function supports(Node $node): bool |
| 70 | + { |
| 71 | + return $node instanceof CrossReferenceNode; |
| 72 | + } |
| 73 | + |
| 74 | + public function getPriority(): int |
| 75 | + { |
| 76 | + return 1000; |
| 77 | + } |
| 78 | +} |
0 commit comments