Skip to content

Commit

Permalink
Merge pull request #124 from weaverryan/fix-title-links
Browse files Browse the repository at this point in the history
Fixing a bug with title links and ticks
  • Loading branch information
weaverryan authored Oct 26, 2020
2 parents 1873475 + b3c8108 commit 68419cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/Nodes/DocumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Exception;
use function array_unshift;
use function count;
use function explode;
use function implode;
use function is_string;
use function sprintf;
use function strlen;

class DocumentNode extends Node
{
Expand Down Expand Up @@ -139,7 +142,7 @@ public function getTitles() : array
}

$level = $node->getLevel();
$text = $node->getValue()->getValue();
$text = $this->getTextFromNode($node);
$redirection = $node->getTarget();
$value = $redirection !== '' ? [$text, $redirection] : $text;

Expand Down Expand Up @@ -241,4 +244,37 @@ private function postRenderValidate() : void
));
}
}

/**
* Fetches the actual "title" text from a TitleNode
*
* When SpanNode (parent class of TitleNode) sets it
* value property, it uses the SpanProcessor. For
* some reason, when that class sees literals, it
* replaces those with a 40 character sha. See
* SpanProcessor::replaceLiterals().
*
* I'm sure there is a good reason for this. But, when
* it comes to getting the "titles" for a Document,
* this results in some titles containing these 40 characters
* sha's - e.g. "All about abc123abc123abc123abc123...".
*
* This corrects that by looking back into the Node to
* get the original text.
*/
private function getTextFromNode(TitleNode $node) : ?string
{
$text = $node->getValue()->getValue();

$words = explode(' ', $text);
foreach ($words as $key => $word) {
if (strlen($word) !== 40 || ! isset($node->getValue()->getTokens()[$word])) {
continue;
}

$words[$key] = $node->getValue()->getTokens()[$word]->get('text');
}

return implode(' ', $words);
}
}
5 changes: 5 additions & 0 deletions tests/Builder/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public function testTitleLinks() : void
'<p>see <a href="magic-link.html#title-with-ampersand">title with ampersand &amp;</a></p>',
$contents
);

self::assertContains(
'<p>see <a href="magic-link.html#a-title-with-ticks">A title with ticks</a></p>',
$contents
);
}

public function testHeadings() : void
Expand Down
5 changes: 5 additions & 0 deletions tests/Builder/input/another.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ test
title with ampersand &
----------------------

A ``title with ticks``
----------------------

This same title named test exists in other documents. Make sure the link below goes to the one
in this doc and not the one in the other docs.

Expand All @@ -27,3 +30,5 @@ see `Another page`_
see `test`_

see `title with ampersand &`_

see `A title with ticks`_

0 comments on commit 68419cb

Please sign in to comment.