Commit 632113e4 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'relative-markdown-links' into 'master'

Add support for relative links starting with ./ or / to RelativeLinkFilter

## What does this MR do?

- Allow explicit relative links in Markdown (starting with `./`).
- Allow absolute links (relative to the branch's root directory) in Markdown (starting with `/`).

## Why was this MR needed?

Those link types were not supported before.

## What are the relevant issue numbers?

fixes #19028

See merge request !5586
parents c009f620 40da543f
...@@ -4,6 +4,7 @@ v 8.11.0 (unreleased) ...@@ -4,6 +4,7 @@ v 8.11.0 (unreleased)
- Fix the title of the toggle dropdown button. !5515 (herminiotorres) - Fix the title of the toggle dropdown button. !5515 (herminiotorres)
- Improve diff performance by eliminating redundant checks for text blobs - Improve diff performance by eliminating redundant checks for text blobs
- Remove magic comments (`# encoding: UTF-8`) from Ruby files. !5456 (winniehell) - Remove magic comments (`# encoding: UTF-8`) from Ruby files. !5456 (winniehell)
- Add support for relative links starting with ./ or / to RelativeLinkFilter (winniehell)
- Fix CI status icon link underline (ClemMakesApps) - Fix CI status icon link underline (ClemMakesApps)
- Cache the commit author in RequestStore to avoid extra lookups in PostReceive - Cache the commit author in RequestStore to avoid extra lookups in PostReceive
- Fix of 'Commits being passed to custom hooks are already reachable when using the UI' - Fix of 'Commits being passed to custom hooks are already reachable when using the UI'
......
...@@ -87,10 +87,13 @@ module Banzai ...@@ -87,10 +87,13 @@ module Banzai
def build_relative_path(path, request_path) def build_relative_path(path, request_path)
return request_path if path.empty? return request_path if path.empty?
return path unless request_path return path unless request_path
return path[1..-1] if path.start_with?('/')
parts = request_path.split('/') parts = request_path.split('/')
parts.pop if uri_type(request_path) != :tree parts.pop if uri_type(request_path) != :tree
path.sub!(%r{^\./}, '')
while path.start_with?('../') while path.start_with?('../')
parts.pop parts.pop
path.sub!('../', '') path.sub!('../', '')
......
...@@ -78,12 +78,24 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do ...@@ -78,12 +78,24 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do
end end
context 'with a valid repository' do context 'with a valid repository' do
it 'rebuilds absolute URL for a file in the repo' do
doc = filter(link('/doc/api/README.md'))
expect(doc.at_css('a')['href']).
to eq "/#{project_path}/blob/#{ref}/doc/api/README.md"
end
it 'rebuilds relative URL for a file in the repo' do it 'rebuilds relative URL for a file in the repo' do
doc = filter(link('doc/api/README.md')) doc = filter(link('doc/api/README.md'))
expect(doc.at_css('a')['href']). expect(doc.at_css('a')['href']).
to eq "/#{project_path}/blob/#{ref}/doc/api/README.md" to eq "/#{project_path}/blob/#{ref}/doc/api/README.md"
end end
it 'rebuilds relative URL for a file in the repo with leading ./' do
doc = filter(link('./doc/api/README.md'))
expect(doc.at_css('a')['href']).
to eq "/#{project_path}/blob/#{ref}/doc/api/README.md"
end
it 'rebuilds relative URL for a file in the repo up one directory' do it 'rebuilds relative URL for a file in the repo up one directory' do
relative_link = link('../api/README.md') relative_link = link('../api/README.md')
doc = filter(relative_link, requested_path: 'doc/update/7.14-to-8.0.md') doc = filter(relative_link, requested_path: 'doc/update/7.14-to-8.0.md')
......
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