Skip to content

Commit

Permalink
Fixing a bug with "Header Links" from files in a sub-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Oct 28, 2021
1 parent 7a2accf commit d51c32b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/Renderers/SpanNodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ private function renderLink(SpanToken $spanToken, string $span): string
$metaEntry = $this->environment->getMetaEntry();

if ($metaEntry !== null && $metaEntry->hasTitle($link)) {
$url = $metaEntry->getUrl() . '#' . Environment::slugify($link);
// A strangely-complex way to simply get the current relative URL
// For example, if the current page is "reference/page", then
// this would return "page" so the final URL is href="page#some-anchor".
$currentRelativeUrl = $this->environment->relativeUrl('/' . $metaEntry->getUrl());
$url = $currentRelativeUrl . '#' . Environment::slugify($link);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Builder/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function testReferenceToTitleWith2CharactersLong(): void
$contents = $this->getFileContents($this->targetFile('subdir/test.html'));

self::assertStringContainsString(
'<a href="subdir/test.html#em">em</a>',
'<a href="test.html#em">em</a>',
$contents
);
}
Expand Down

0 comments on commit d51c32b

Please sign in to comment.