Skip to content

Commit 38b351f

Browse files
authored
[BUGFIX] Prevent interlink from referencing itself (#760)
If the interlink domain used in a reference is the same like the interlink_shortcode of the current document use internal references instead. Resolves #672
1 parent 68e965f commit 38b351f

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

packages/typo3-docs-theme/resources/config/typo3-docs-theme.php

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1919
use T3Docs\Typo3DocsTheme\Api\Typo3ApiService;
2020
use T3Docs\Typo3DocsTheme\Compiler\NodeTransformers\ConfvalMenuNodeTransformer;
21+
use T3Docs\Typo3DocsTheme\Compiler\NodeTransformers\RemoveInterlinkSelfReferencesFromCrossReferenceNodeTransformer;
2122
use T3Docs\Typo3DocsTheme\Directives\ConfvalMenuDirective;
2223
use T3Docs\Typo3DocsTheme\Directives\DirectoryTreeDirective;
2324
use T3Docs\Typo3DocsTheme\Directives\GroupTabDirective;
@@ -80,6 +81,8 @@
8081
->tag('phpdoc.guides.directive')
8182
->set(ConfvalMenuNodeTransformer::class)
8283
->tag('phpdoc.guides.compiler.nodeTransformers')
84+
->set(RemoveInterlinkSelfReferencesFromCrossReferenceNodeTransformer::class)
85+
->tag('phpdoc.guides.compiler.nodeTransformers')
8386
->set(TwigExtension::class)
8487
->set(TwigExtension::class)
8588
->tag('twig.extension')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- content start -->
2+
<section class="section" id="document-title" data-rst-anchor="start">
3+
<a id="start"></a>
4+
<h1>Document Title<a class="headerlink" href="#document-title" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"><i class="fa-solid fa-paragraph"></i></a></h1>
5+
6+
<p>See <a href="#">Document Title</a> or <a href="#start">Document Title</a>.</p>
7+
8+
</section>
9+
<!-- content end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<guides
3+
xmlns="https://www.phpdoc.org/guides"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://www.phpdoc.org/guides vendor/phpdocumentor/guides-cli/resources/schema/guides.xsd"
6+
>
7+
<extension class="\T3Docs\Typo3DocsTheme\DependencyInjection\Typo3DocsThemeExtension"
8+
typo3-core-preferred="main"
9+
interlink-shortcode="changelog"
10+
/>
11+
<project title="Core"
12+
release="main"
13+
version="main"
14+
copyright="since 1997 by the TYPO3 contributors"
15+
/>
16+
</guides>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
.. _start:
3+
4+
==============
5+
Document Title
6+
==============
7+
8+
See :doc:`changelog:index` or :ref:`changelog:start`.

0 commit comments

Comments
 (0)