Commit ed0203ae authored by Imre Farkas's avatar Imre Farkas

Merge branch '25375-webide-markdown-broken-images' into 'master'

Fix issue with broken images in Web IDE markdown

See merge request gitlab-org/gitlab!31638
parents c408f973 e1646039
...@@ -44,6 +44,7 @@ export default { ...@@ -44,6 +44,7 @@ export default {
'isEditModeActive', 'isEditModeActive',
'isCommitModeActive', 'isCommitModeActive',
'isReviewModeActive', 'isReviewModeActive',
'currentBranch',
]), ]),
...mapGetters('fileTemplates', ['showFileTemplatesBar']), ...mapGetters('fileTemplates', ['showFileTemplatesBar']),
shouldHideEditor() { shouldHideEditor() {
...@@ -87,6 +88,9 @@ export default { ...@@ -87,6 +88,9 @@ export default {
theme: this.editorTheme, theme: this.editorTheme,
}; };
}, },
currentBranchCommit() {
return this.currentBranch?.commit.id;
},
}, },
watch: { watch: {
file(newVal, oldVal) { file(newVal, oldVal) {
...@@ -315,6 +319,7 @@ export default { ...@@ -315,6 +319,7 @@ export default {
:file-path="file.path" :file-path="file.path"
:file-size="file.size" :file-size="file.size"
:project-path="file.projectId" :project-path="file.projectId"
:commit-sha="currentBranchCommit"
:type="fileType" :type="fileType"
/> />
<diff-viewer <diff-viewer
......
...@@ -24,6 +24,11 @@ export default { ...@@ -24,6 +24,11 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
commitSha: {
type: String,
required: false,
default: '',
},
projectPath: { projectPath: {
type: String, type: String,
required: false, required: false,
...@@ -62,6 +67,7 @@ export default { ...@@ -62,6 +67,7 @@ export default {
:file-size="fileSize" :file-size="fileSize"
:project-path="projectPath" :project-path="projectPath"
:content="content" :content="content"
:commit-sha="commitSha"
/> />
</div> </div>
</template> </template>
...@@ -16,6 +16,11 @@ export default { ...@@ -16,6 +16,11 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
commitSha: {
type: String,
required: false,
default: '',
},
filePath: { filePath: {
type: String, type: String,
required: false, required: false,
...@@ -55,6 +60,9 @@ export default { ...@@ -55,6 +60,9 @@ export default {
text: this.content, text: this.content,
path: this.filePath, path: this.filePath,
}; };
if (this.commitSha) {
postBody.ref = this.commitSha;
}
const postOptions = { const postOptions = {
cancelToken: axiosSource.token, cancelToken: axiosSource.token,
}; };
......
...@@ -37,7 +37,7 @@ module PreviewMarkdown ...@@ -37,7 +37,7 @@ module PreviewMarkdown
when 'groups' then { group: group } when 'groups' then { group: group }
when 'projects' then projects_filter_params when 'projects' then projects_filter_params
else {} else {}
end.merge(requested_path: params[:path]) end.merge(requested_path: params[:path], ref: params[:ref])
end end
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
......
---
title: Fix issue with broken images in Web IDE markdown
merge_request: 31638
author:
type: fixed
...@@ -1020,6 +1020,32 @@ describe ProjectsController do ...@@ -1020,6 +1020,32 @@ describe ProjectsController do
expect(json_response['body']).to include(expanded_path) expect(json_response['body']).to include(expanded_path)
end end
end end
context 'when path and ref parameters are provided' do
let(:project_with_repo) { create(:project, :repository) }
let(:preview_markdown_params) do
{
namespace_id: project_with_repo.namespace,
id: project_with_repo,
text: "![](./logo-white.png)\n",
ref: 'other_branch',
path: 'files/images/README.md'
}
end
before do
project_with_repo.add_maintainer(user)
project_with_repo.repository.create_branch('other_branch')
end
it 'renders JSON body with image links expanded' do
expanded_path = "/#{project_with_repo.full_path}/-/raw/other_branch/files/images/logo-white.png"
post :preview_markdown, params: preview_markdown_params
expect(json_response['body']).to include(expanded_path)
end
end
end end
describe '#ensure_canonical_path' do describe '#ensure_canonical_path' do
......
...@@ -95,12 +95,13 @@ describe MarkupHelper do ...@@ -95,12 +95,13 @@ describe MarkupHelper do
context 'when text contains a relative link to an image in the repository' do context 'when text contains a relative link to an image in the repository' do
let(:image_file) { "logo-white.png" } let(:image_file) { "logo-white.png" }
let(:text_with_relative_path) { "![](./#{image_file})\n" } let(:text_with_relative_path) { "![](./#{image_file})\n" }
let(:generated_html) { helper.markdown(text_with_relative_path, requested_path: requested_path) } let(:generated_html) { helper.markdown(text_with_relative_path, requested_path: requested_path, ref: ref) }
subject { Nokogiri::HTML.parse(generated_html) } subject { Nokogiri::HTML.parse(generated_html) }
context 'when requested_path is provided in the context' do context 'when requested_path is provided, but ref isn\'t' do
let(:requested_path) { 'files/images/README.md' } let(:requested_path) { 'files/images/README.md' }
let(:ref) { nil }
it 'returns the correct HTML for the image' do it 'returns the correct HTML for the image' do
expanded_path = "/#{project.full_path}/-/raw/master/files/images/#{image_file}" expanded_path = "/#{project.full_path}/-/raw/master/files/images/#{image_file}"
...@@ -110,13 +111,43 @@ describe MarkupHelper do ...@@ -110,13 +111,43 @@ describe MarkupHelper do
end end
end end
context 'when requested_path parameter is not provided' do context 'when requested_path and ref parameters are both provided' do
let(:requested_path) { 'files/images/README.md' }
let(:ref) { 'other_branch' }
it 'returns the correct HTML for the image' do
project.repository.create_branch('other_branch')
expanded_path = "/#{project.full_path}/-/raw/#{ref}/files/images/#{image_file}"
expect(subject.css('a')[0].attr('href')).to eq(expanded_path)
expect(subject.css('img')[0].attr('data-src')).to eq(expanded_path)
end
end
context 'when ref is provided, but requested_path isn\'t' do
let(:ref) { 'other_branch' }
let(:requested_path) { nil }
it 'returns the correct HTML for the image' do
project.repository.create_branch('other_branch')
expanded_path = "/#{project.full_path}/-/blob/#{ref}/./#{image_file}"
expect(subject.css('a')[0].attr('href')).to eq(expanded_path)
expect(subject.css('img')[0].attr('data-src')).to eq(expanded_path)
end
end
context 'when neither requested_path, nor ref parameter is provided' do
let(:ref) { nil }
let(:requested_path) { nil } let(:requested_path) { nil }
it 'returns the link to the image path as a relative path' do it 'returns the correct HTML for the image' do
expanded_path = "/#{project.full_path}/-/blob/master/./#{image_file}" expanded_path = "/#{project.full_path}/-/blob/master/./#{image_file}"
expect(subject.css('a')[0].attr('href')).to eq(expanded_path) expect(subject.css('a')[0].attr('href')).to eq(expanded_path)
expect(subject.css('img')[0].attr('data-src')).to eq(expanded_path)
end end
end end
end end
......
...@@ -26,7 +26,23 @@ describe('RepoEditor', () => { ...@@ -26,7 +26,23 @@ describe('RepoEditor', () => {
f.active = true; f.active = true;
f.tempFile = true; f.tempFile = true;
vm.$store.state.openFiles.push(f); vm.$store.state.openFiles.push(f);
vm.$store.state.projects = {
'gitlab-org/gitlab': {
branches: {
master: {
name: 'master',
commit: {
id: 'abcdefgh',
},
},
},
},
};
vm.$store.state.currentProjectId = 'gitlab-org/gitlab';
vm.$store.state.currentBranchId = 'master';
Vue.set(vm.$store.state.entries, f.path, f); Vue.set(vm.$store.state.entries, f.path, f);
spyOn(vm, 'getFileData').and.returnValue(Promise.resolve()); spyOn(vm, 'getFileData').and.returnValue(Promise.resolve());
......
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