Skip to content

Commit 66c69de

Browse files
committed
[FEATURE] Support Hyperlinks to local pages in rst and md
local pages can be linked by source name, target name or pure name without ending. Links can be relative to the current file or absolute to the project root
1 parent c023f1a commit 66c69de

File tree

16 files changed

+202
-1
lines changed

16 files changed

+202
-1
lines changed

packages/guides-markdown/src/Markdown/MarkupLanguageParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Log\LoggerInterface;
1717
use RuntimeException;
1818

19+
use function ltrim;
1920
use function md5;
2021
use function sprintf;
2122
use function strtolower;
@@ -54,7 +55,7 @@ public function parse(ParserContext $parserContext, string $contents): DocumentN
5455

5556
private function parseDocument(NodeWalker $walker, string $hash): DocumentNode
5657
{
57-
$document = new DocumentNode($hash, $this->getParserContext()->getCurrentAbsolutePath());
58+
$document = new DocumentNode($hash, ltrim($this->getParserContext()->getCurrentAbsolutePath(), '/'));
5859
$this->document = $document;
5960

6061
while ($event = $walker->next()) {

packages/guides/resources/config/guides.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use phpDocumentor\Guides\ReferenceResolvers\ExternalReferenceResolver;
3131
use phpDocumentor\Guides\ReferenceResolvers\InterlinkReferenceResolver;
3232
use phpDocumentor\Guides\ReferenceResolvers\InternalReferenceResolver;
33+
use phpDocumentor\Guides\ReferenceResolvers\PageHyperlinkResolver;
3334
use phpDocumentor\Guides\ReferenceResolvers\ReferenceResolver;
3435
use phpDocumentor\Guides\ReferenceResolvers\ReferenceResolverPreRender;
3536
use phpDocumentor\Guides\ReferenceResolvers\SluggerAnchorReducer;
@@ -124,6 +125,8 @@
124125

125126
->set(AnchorHyperlinkResolver::class)
126127

128+
->set(PageHyperlinkResolver::class)
129+
127130
->set(AnchorReferenceResolver::class)
128131

129132
->set(InternalReferenceResolver::class)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\ReferenceResolvers;
6+
7+
use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
8+
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
9+
use phpDocumentor\Guides\RenderContext;
10+
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
11+
12+
use function rtrim;
13+
14+
/**
15+
* Resolves named and anonymous references to source files
16+
*
17+
* `Link Text <../page.rst>`_ or [Link Text](path/to/another/page.md)
18+
*/
19+
class PageHyperlinkResolver implements ReferenceResolver
20+
{
21+
// Named links and anchors take precedence
22+
public final const PRIORITY = -200;
23+
24+
public function __construct(
25+
private readonly UrlGeneratorInterface $urlGenerator,
26+
private readonly DocumentNameResolverInterface $documentNameResolver,
27+
) {
28+
}
29+
30+
public function resolve(LinkInlineNode $node, RenderContext $renderContext): bool
31+
{
32+
if (!$node instanceof HyperLinkNode) {
33+
return false;
34+
}
35+
36+
$canonicalDocumentName = $this->documentNameResolver->canonicalUrl($renderContext->getDirName(), $node->getTargetReference());
37+
$canonicalDocumentName = rtrim($canonicalDocumentName, '.' . $renderContext->getOutputFormat());
38+
$canonicalDocumentName = rtrim($canonicalDocumentName, '.rst');
39+
$canonicalDocumentName = rtrim($canonicalDocumentName, '.md');
40+
$document = $renderContext->getProjectNode()->findDocumentEntry($canonicalDocumentName);
41+
if ($document === null) {
42+
return false;
43+
}
44+
45+
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $document->getFile()));
46+
if ($node->getValue() === '') {
47+
$node->setValue($document->getTitle()->toString());
48+
}
49+
50+
return true;
51+
}
52+
53+
public static function getPriority(): int
54+
{
55+
return self::PRIORITY;
56+
}
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- content start -->
2+
<div class="section" id="some-file">
3+
<h1>Some file</h1>
4+
5+
6+
7+
<ul>
8+
<li><a href="/page1.html">link page 1</a></li>
9+
10+
<li><a href="/subpages/subpage1.html">link subpage 1</a></li>
11+
12+
</ul>
13+
14+
</div>
15+
16+
<!-- content end -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!-- content start -->
2+
<div class="section" id="some-file">
3+
<h1>Some file</h1>
4+
5+
<a id="page1"></a>
6+
7+
8+
<ul>
9+
<li><a href="/page1.html">link page 1</a></li>
10+
11+
<li><a href="/subpages/subpage1.html">link subpage 1</a></li>
12+
13+
<li><a href="/page1.html">link page 1</a></li>
14+
15+
<li><a href="/page1.html">link page 1</a></li>
16+
17+
<li><a href="/subpages/subpage1.html">link subpage 1</a></li>
18+
19+
<li><a href="/page1.html">link page 1</a></li>
20+
21+
<li><a href="/subpages/index.html#page1">link page 1</a></li>
22+
23+
<li><a href="/subpages/subpage1.html">link subpage 1</a></li>
24+
25+
<li><a href="/subpages/index.html#page1">link page 1</a></li>
26+
27+
</ul>
28+
29+
</div>
30+
31+
<!-- content end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Some file
2+
=========
3+
4+
* `link page 1 <page1.rst>`_
5+
6+
* `link subpage 1 <subpages/subpage1.rst>`_
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Page 1
2+
======
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _page1:
2+
3+
Some file
4+
=========
5+
6+
* `link page 1 </page1.rst>`_
7+
* `link subpage 1 <subpage1.rst>`_
8+
* `link page 1 <../page1.rst>`_
9+
10+
11+
* `link page 1 </page1.html>`_
12+
* `link subpage 1 <subpage1.html>`_
13+
* `link page 1 <../page1.html>`_
14+
15+
* `link page 1 </page1>`_
16+
* `link subpage 1 <subpage1>`_
17+
* `link page 1 <../page1>`_
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Subpage 1
2+
=========
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- content start -->
2+
<h1>Markdown with links</h1>
3+
4+
<p>This is a Markdown document with some basic formatting.</p>
5+
6+
7+
<ul>
8+
<li><p><a href="/page1.html">Page 1</a></p></li>
9+
10+
<li><p><a href="/page1.html">Page 1</a></p></li>
11+
12+
<li><p><a href="/subpages/subpage1.html">Subpage 1</a></p></li>
13+
14+
</ul>
15+
16+
<!-- content end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- content start -->
2+
<h1>Markdown with links</h1>
3+
4+
<p>This is a Markdown document with some basic formatting.</p>
5+
6+
7+
<ul>
8+
<li><p><a href="/subpages/subpage1.html">Subpage 1</a></p></li>
9+
10+
<li><p><a href="/page1.html">Page 1</a></p></li>
11+
12+
<li><p><a href="/page1.html">Page 1</a></p></li>
13+
14+
<li><p><a href="/page1.html">Page 1</a></p></li>
15+
16+
<li><p><a href="/page1.html">Page 1</a></p></li>
17+
18+
</ul>
19+
20+
<!-- content end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<guides xmlns="https://www.phpdoc.org/guides"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
5+
input-format="md"
6+
>
7+
<project title="Project Title" version="6.4"/>
8+
</guides>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Markdown with links
2+
3+
This is a Markdown document with some basic formatting.
4+
5+
* [Page 1](page1.md)
6+
* [Page 1](/page1.md)
7+
* [Subpage 1](subpages/subpage1.md)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Page 1
2+
3+
This is a Markdown document with some basic formatting.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Markdown with links
2+
3+
This is a Markdown document with some basic formatting.
4+
5+
* [Subpage 1](subpage1.md)
6+
* [Page 1](../page1.md)
7+
* [Page 1](/page1.md)
8+
* [Page 1](/page1.html)
9+
* [Page 1](/page1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Subpage 1
2+
3+
This is a Markdown document with some basic formatting.

0 commit comments

Comments
 (0)