Commit 4b85003a authored by Stan Hu's avatar Stan Hu

Merge branch '344903-enable-code-highlighting-for-jupyter-diffs' into 'master'

Resolve "Enable code highlighting for Jupyter Diffs"

See merge request gitlab-org/gitlab!73743
parents f503c6a9 3555ebce
......@@ -27,7 +27,7 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
Gitlab::Highlight.highlight(
blob.path,
transformed_blob_data,
language: language,
language: transformed_blob_language,
plain: plain
)
end
......@@ -120,6 +120,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
blob.language_from_gitattributes
end
def transformed_blob_language
@transformed_blob_language ||= blob.path.ends_with?('.ipynb') ? 'md' : language
end
def transformed_blob_data
@transformed_blob ||= if blob.path.ends_with?('.ipynb')
new_blob = IpynbDiff.transform(blob.data,
......
......@@ -51,6 +51,34 @@ RSpec.describe Gitlab::Diff::File do
project.commit(branch_name).diffs.diff_files.first
end
describe 'initialize' do
context 'when file is ipynb' do
let(:commit) { project.commit("f6b7a707") }
let(:diff) { commit.raw_diffs.first }
let(:diff_file) { described_class.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
context 'and :jupyter_clean_diffs is enabled' do
before do
stub_feature_flags(jupyter_clean_diffs: true)
end
it 'recreates the diff by transforming the files' do
expect(diff_file.diff.diff).not_to include('"| Fake')
end
end
context 'but :jupyter_clean_diffs is disabled' do
before do
stub_feature_flags(jupyter_clean_diffs: false)
end
it 'does not recreate the diff' do
expect(diff_file.diff.diff).to include('"| Fake')
end
end
end
end
describe '#diff_lines' do
let(:diff_lines) { diff_file.diff_lines }
......
......@@ -121,6 +121,43 @@ RSpec.describe BlobPresenter do
end
end
describe '#highlight_transformed' do
context 'when blob is ipynb' do
let(:blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') }
let(:git_blob) { blob.__getobj__ }
it 'uses md as the transformed language' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', anything, plain: nil, language: 'md')
presenter.highlight_transformed
end
it 'transforms the blob' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', include("%%"), plain: nil, language: 'md')
presenter.highlight_transformed
end
end
context 'when blob is other file type' do
let(:git_blob) { blob.__getobj__ }
before do
allow(git_blob)
.to receive(:data)
.and_return("line one\nline two\nline 3")
allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
end
it 'does not transform the file' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby')
presenter.highlight_transformed
end
end
end
describe '#raw_plain_data' do
let(:blob) { repository.blob_at('HEAD', file) }
......
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