From b99282804d682251928f932812068513f4061abb Mon Sep 17 00:00:00 2001
From: Fu Xu <fuxu@fuxu.name>
Date: Thu, 10 Nov 2016 13:57:46 +0800
Subject: [PATCH] fix error links in help page

---
 app/controllers/help_controller.rb            |  4 +-
 changelogs/unreleased/fix-help-page-links.yml |  4 ++
 spec/controllers/help_controller_spec.rb      | 38 +++++++++++++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/unreleased/fix-help-page-links.yml

diff --git a/app/controllers/help_controller.rb b/app/controllers/help_controller.rb
index 4eca278599f..4b3c71874be 100644
--- a/app/controllers/help_controller.rb
+++ b/app/controllers/help_controller.rb
@@ -7,8 +7,8 @@ class HelpController < ApplicationController
     @help_index = File.read(Rails.root.join('doc', 'README.md'))
 
     # Prefix Markdown links with `help/` unless they already have been
-    # See http://rubular.com/r/nwwhzH6Z8X
-    @help_index.gsub!(/(\]\()(?!help\/)([^\)\(]+)(\))/, '\1help/\2\3')
+    # See http://rubular.com/r/ie2MlpdUMq
+    @help_index.gsub!(/(\]\()(\/?help\/)?([^\)\(]+\))/, '\1/help/\3')
   end
 
   def show
diff --git a/changelogs/unreleased/fix-help-page-links.yml b/changelogs/unreleased/fix-help-page-links.yml
new file mode 100644
index 00000000000..9e5f41c553f
--- /dev/null
+++ b/changelogs/unreleased/fix-help-page-links.yml
@@ -0,0 +1,4 @@
+---
+title: Fix error links in help index page
+merge_request: 7396
+author: Fu Xu
diff --git a/spec/controllers/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb
index 33c75e7584f..6fc6ea95e13 100644
--- a/spec/controllers/help_controller_spec.rb
+++ b/spec/controllers/help_controller_spec.rb
@@ -7,6 +7,40 @@ describe HelpController do
     sign_in(user)
   end
 
+  describe 'GET #index' do
+    context 'when url prefixed without /help/' do
+      it 'has correct url prefix' do
+        stub_readme("[API](api/README.md)")
+        get :index
+        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
+      end
+    end
+
+    context 'when url prefixed with help/' do
+      it 'will be an absolute path' do
+        stub_readme("[API](help/api/README.md)")
+        get :index
+        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
+      end
+    end
+
+    context 'when url prefixed with help' do
+      it 'will be an absolute path' do
+        stub_readme("[API](helpful_hints/README.md)")
+        get :index
+        expect(assigns[:help_index]).to eq '[API](/help/helpful_hints/README.md)'
+      end
+    end
+
+    context 'when url prefixed with /help/' do
+      it 'will not be changed' do
+        stub_readme("[API](/help/api/README.md)")
+        get :index
+        expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
+      end
+    end
+  end
+
   describe 'GET #show' do
     context 'for Markdown formats' do
       context 'when requested file exists' do
@@ -72,4 +106,8 @@ describe HelpController do
       end
     end
   end
+
+  def stub_readme(content)
+    allow(File).to receive(:read).and_return(content)
+  end
 end
-- 
GitLab