Commit c2502887 authored by Francisco Javier López's avatar Francisco Javier López Committed by a_luna

First try rendering the whole view content

parent e80ae90b
# frozen_string_literal: true
class SnippetBlobPresenter < BlobPresenter
include Gitlab::Routing
def rich_data
return if blob.binary?
return unless blob.rich_viewer
if markup?
blob.rendered_markup
else
highlight(plain: false)
end
render_rich_partial
end
def plain_data
return if blob.binary?
highlight(plain: !markup?)
highlight(plain: false)
end
def raw_path
......@@ -27,10 +26,6 @@ class SnippetBlobPresenter < BlobPresenter
private
def markup?
blob.rich_viewer&.partial_name == 'markup'
end
def snippet
blob.container
end
......@@ -38,4 +33,24 @@ class SnippetBlobPresenter < BlobPresenter
def language
nil
end
def render_rich_partial
renderer.render("projects/blob/viewers/#{blob.rich_viewer.partial_name}",
viewer: blob.rich_viewer,
blob: blob,
blow_raw_path: raw_path)
end
def renderer
ActionView::Base.new(build_lookup_context, { snippet: snippet }, ActionController::Base.new).tap do |renderer|
renderer.extend ApplicationController._helpers
renderer.class_eval do
include Rails.application.routes.url_helpers
end
end
end
def build_lookup_context
ActionView::Base.build_lookup_context(ActionController::Base.view_paths)
end
end
......@@ -4,36 +4,67 @@ require 'spec_helper'
describe SnippetBlobPresenter do
describe '#rich_data' do
let(:snippet) { build(:personal_snippet) }
let_it_be(:snippet) { create(:personal_snippet) }
before do
allow_next_instance_of(ActionController::Base) do |instance|
allow(instance).to receive(:current_user).and_return(nil)
end
end
subject { described_class.new(snippet.blob).rich_data }
it 'returns nil when the snippet blob is binary' do
allow(snippet.blob).to receive(:binary?).and_return(true)
context 'with PersonalSnippet' do
let(:raw_url) { "http://127.0.0.1:3000/snippets/#{snippet.id}/raw" }
let(:snippet) { build(:personal_snippet) }
expect(subject).to be_nil
end
it 'returns nil when the snippet blob is binary' do
allow(snippet.blob).to receive(:binary?).and_return(true)
it 'returns markdown content when snippet file is markup' do
snippet.file_name = 'test.md'
snippet.content = '*foo*'
expect(subject).to be_nil
end
expect(subject).to eq '<p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p>'
end
context 'with markdown format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.md', content: '*foo*') }
it 'returns syntax highlighted content' do
snippet.file_name = 'test.rb'
snippet.content = 'class Foo;end'
it 'returns rich markdown content' do
expect(subject).to eq '<div class="file-content md md-file"><p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p></div>'
end
end
expect(subject)
.to eq '<span id="LC1" class="line" lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span></span>'
end
context 'with notebook format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.ipynb') }
it 'returns plain text highlighted content' do
snippet.file_name = 'test'
snippet.content = 'foo'
it 'returns rich notebook content' do
expect(subject.strip).to eq '<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'+ snippet.id.to_s + '/raw" id="js-notebook-viewer"></div>'
end
end
expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>'
context 'with openapi format' do
let(:snippet) { create(:personal_snippet, file_name: 'openapi.yml') }
it 'returns rich openapi content' do
expect(subject).to eq '<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'+ snippet.id.to_s + '/raw" id="js-openapi-viewer"></div>'
end
end
context 'with svg format' do
let(:snippet) { create(:personal_snippet, file_name: 'test.svg') }
it 'returns rich svg content' do
snippet.file_name = 'test.svg'
expect(subject).to eq '<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'+ snippet.id.to_s + '/raw" id="js-openapi-viewer"></div>'
end
end
context 'with other format' do
let(:snippet) { create(:personal_snippet, file_name: 'test') }
it 'does not return no rich content' do
expect(subject).to be_nil
end
end
end
end
......@@ -55,19 +86,19 @@ describe SnippetBlobPresenter do
expect(subject).to eq '<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
end
it 'returns plain syntax content' do
it 'returns highlighted syntax content' do
snippet.file_name = 'test.rb'
snippet.content = 'class Foo;end'
expect(subject)
.to eq '<span id="LC1" class="line" lang="">class Foo;end</span>'
.to eq '<span id="LC1" class="line" lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span></span>'
end
it 'returns plain text highlighted content' do
snippet.file_name = 'test'
snippet.content = 'foo'
expect(subject).to eq '<span id="LC1" class="line" lang="">foo</span>'
expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>'
end
end
......
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