From 91a0767d5371d4e4d40c97d93261ec18d7cc5e1f Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Fri, 11 Mar 2016 10:22:18 -0400 Subject: [PATCH] BUGFIX: Fixed incorrectly formed page specific anchor links (fixes #104) --- code/DocumentationParser.php | 3 +++ javascript/DocumentationViewer.js | 2 +- tests/DocumentationParserTest.php | 10 ++++++++++ tests/docs/en/test.md | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/code/DocumentationParser.php b/code/DocumentationParser.php index c46dde3..fe3404b 100755 --- a/code/DocumentationParser.php +++ b/code/DocumentationParser.php @@ -482,6 +482,9 @@ class DocumentationParser $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)) { diff --git a/javascript/DocumentationViewer.js b/javascript/DocumentationViewer.js index afe31ff..acf8b3e 100755 --- a/javascript/DocumentationViewer.js +++ b/javascript/DocumentationViewer.js @@ -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 = ''; diff --git a/tests/DocumentationParserTest.php b/tests/DocumentationParserTest.php index 83e6e1e..781e9e1 100755 --- a/tests/DocumentationParserTest.php +++ b/tests/DocumentationParserTest.php @@ -197,6 +197,16 @@ HTML; $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 diff --git a/tests/docs/en/test.md b/tests/docs/en/test.md index 082763e..f925548 100755 --- a/tests/docs/en/test.md +++ b/tests/docs/en/test.md @@ -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)`