Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #106 from webbuilders-group/anchor-links-fix
Browse files Browse the repository at this point in the history
BUGFIX: Fixed incorrectly formed page specific anchor links (fixes #104)
  • Loading branch information
dhensby committed Mar 11, 2016
2 parents 5d28a93 + 91a0767 commit 05741b1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions code/DocumentationParser.php
Expand Up @@ -482,6 +482,9 @@ public static function rewrite_relative_links($md, $page)
$fileBaseLink,
$url
);
} else if (preg_match('/^#/', $url)) {
// for relative links begining with a hash use the current page link
$relativeUrl = Controller::join_links($baselink, $page->getRelativeLink(), $url);
} else {
// Rewrite public URL
if (preg_match('/^\//', $url)) {
Expand Down
2 changes: 1 addition & 1 deletion javascript/DocumentationViewer.js
Expand Up @@ -120,7 +120,7 @@
*
* Automatically adds anchor links to headings that have IDs
*/
var url = window.location.href;
var url = window.location.href.replace(/#[a-zA-Z0-9\-\_]*/g, '');

$("#content h1[id], #content h2[id], #content h3[id], #content h4[id], #content h5[id], #content h6[id]").each(function() {
var link = '<a class="heading-anchor-link" title="Link to this section" href="'+ url + '#' + $(this).attr('id') + '">&para;</a>';
Expand Down
10 changes: 10 additions & 0 deletions tests/DocumentationParserTest.php
Expand Up @@ -197,6 +197,16 @@ public function testRelativeLinks()
$result
);

$this->assertContains(
'[link: with anchor](dev/docs/en/documentationparsertest/2.4/test/#anchor)',
$result
);

$this->assertContains(
'[link: relative anchor](dev/docs/en/documentationparsertest/2.4/test/#relative-anchor)',
$result
);

$result = DocumentationParser::rewrite_relative_links(
$this->subPage->getMarkdown(),
$this->subPage
Expand Down
1 change: 1 addition & 0 deletions tests/docs/en/test.md
Expand Up @@ -8,6 +8,7 @@ test
[link: subfolder page](subfolder/subpage)
[link: with anchor](/test#anchor)
[link: http](http://silverstripe.org)
[link: relative anchor](#relative-anchor)

`[Title](api:DataObject)`
`[Title](api:DataObject::$defaults)`
Expand Down

0 comments on commit 05741b1

Please sign in to comment.