Commit 553c9f3c authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fix-broken-links-in-help' into 'master'

Fix broken links on help page

Closes #57554

See merge request gitlab-org/gitlab-ce!25120
parents b753fc6d 752887b9
...@@ -13,9 +13,10 @@ class HelpController < ApplicationController ...@@ -13,9 +13,10 @@ class HelpController < ApplicationController
# Remove YAML frontmatter so that it doesn't look weird # Remove YAML frontmatter so that it doesn't look weird
@help_index = File.read(Rails.root.join('doc', 'README.md')).sub(YAML_FRONT_MATTER_REGEXP, '') @help_index = File.read(Rails.root.join('doc', 'README.md')).sub(YAML_FRONT_MATTER_REGEXP, '')
# Prefix Markdown links with `help/` unless they are external links # Prefix Markdown links with `help/` unless they are external links.
# See http://rubular.com/r/X3baHTbPO2 # '//' not necessarily part of URL, e.g., mailto:mail@example.com
@help_index.gsub!(%r{(?<delim>\]\()(?!.+://)(?!/)(?<link>[^\)\(]+\))}) do # See https://rubular.com/r/DFHZl5w8d3bpzV
@help_index.gsub!(%r{(?<delim>\]\()(?!\w+:)(?!/)(?<link>[^\)\(]+\))}) do
"#{$~[:delim]}#{Gitlab.config.gitlab.relative_url_root}/help/#{$~[:link]}" "#{$~[:delim]}#{Gitlab.config.gitlab.relative_url_root}/help/#{$~[:link]}"
end end
end end
......
...@@ -37,6 +37,46 @@ describe HelpController do ...@@ -37,6 +37,46 @@ describe HelpController do
expect(assigns[:help_index]).to eq '[external](https://some.external.link)' expect(assigns[:help_index]).to eq '[external](https://some.external.link)'
end end
end end
context 'when relative url with external on same line' do
it 'prefix it with /help/' do
stub_readme("[API](api/README.md) [external](https://some.external.link)")
get :index
expect(assigns[:help_index]).to eq '[API](/help/api/README.md) [external](https://some.external.link)'
end
end
context 'when relative url with http:// in query' do
it 'prefix it with /help/' do
stub_readme("[API](api/README.md?go=https://example.com/)")
get :index
expect(assigns[:help_index]).to eq '[API](/help/api/README.md?go=https://example.com/)'
end
end
context 'when mailto URL' do
it 'do not change it' do
stub_readme("[report bug](mailto:bugs@example.com)")
get :index
expect(assigns[:help_index]).to eq '[report bug](mailto:bugs@example.com)'
end
end
context 'when protocol-relative link' do
it 'do not change it' do
stub_readme("[protocol-relative](//example.com)")
get :index
expect(assigns[:help_index]).to eq '[protocol-relative](//example.com)'
end
end
end end
describe 'GET #show' do describe 'GET #show' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment