Commit c43da039 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'bw-enable-sourcepos-ee' into 'master'

Enable CommonMark source line position information (EE)

See merge request gitlab-org/gitlab-ee!9083
parents f8eda01f 1e5015b7
......@@ -18,6 +18,7 @@ describe Projects::MergeRequests::DraftsController do
before do
sign_in(user)
stub_licensed_features(batch_comments: true)
stub_commonmark_sourcepos_disabled
end
describe 'GET #index' do
......
......@@ -4,6 +4,10 @@ require 'spec_helper'
describe IssuablesHelper do
describe '#issuable_initial_data' do
before do
stub_commonmark_sourcepos_disabled
end
it 'returns the correct data for an epic' do
user = create(:user)
epic = create(:epic, author: user, description: 'epic text')
......
......@@ -32,8 +32,13 @@ module Banzai
:DEFAULT # default rendering system. Nothing special.
].freeze
def initialize
@renderer = Banzai::Renderer::CommonMark::HTML.new(options: RENDER_OPTIONS)
RENDER_OPTIONS_SOURCEPOS = RENDER_OPTIONS + [
:SOURCEPOS # enable embedding of source position information
].freeze
def initialize(context)
@context = context
@renderer = Banzai::Renderer::CommonMark::HTML.new(options: render_options)
end
def render(text)
......@@ -41,6 +46,12 @@ module Banzai
@renderer.render(doc)
end
private
def render_options
@context[:no_sourcepos] ? RENDER_OPTIONS : RENDER_OPTIONS_SOURCEPOS
end
end
end
end
......
......@@ -20,7 +20,7 @@ module Banzai
tables: true
}.freeze
def initialize
def initialize(context = nil)
html_renderer = Banzai::Renderer::Redcarpet::HTML.new
@renderer = ::Redcarpet::Markdown.new(html_renderer, OPTIONS)
end
......
......@@ -6,7 +6,7 @@ module Banzai
def initialize(text, context = nil, result = nil)
super(text, context, result)
@renderer = renderer(context[:markdown_engine]).new
@renderer = renderer(context[:markdown_engine]).new(context)
@text = @text.delete("\r")
end
......
......@@ -41,6 +41,9 @@ module Banzai
whitelist[:elements].push('abbr')
whitelist[:attributes]['abbr'] = %w(title)
# Allow the 'data-sourcepos' from CommonMark on all elements
whitelist[:attributes][:all].push('data-sourcepos')
# Disallow `name` attribute globally, allow on `a`
whitelist[:attributes][:all].delete('name')
whitelist[:attributes]['a'].push('name')
......
......@@ -73,7 +73,8 @@ module Banzai
html = Banzai::Filter::MarkdownFilter.call(transform_markdown(match), context)
# link is wrapped in a <p>, so strip that off
html.sub('<p>', '').chomp('</p>')
p_node = Nokogiri::HTML.fragment(html).at_css('p')
p_node ? p_node.children.to_html : html
end
def spaced_link_filter(text)
......
......@@ -6,7 +6,8 @@ module Banzai
def self.transform_context(context)
super(context).merge(
only_path: false,
xhtml: true
xhtml: true,
no_sourcepos: true
)
end
end
......
......@@ -14,6 +14,12 @@ module Banzai
Filter::ExternalLinkFilter
]
end
def self.transform_context(context)
super(context).merge(
no_sourcepos: true
)
end
end
end
end
......@@ -11,7 +11,8 @@ module Banzai
def self.transform_context(context)
super(context).merge(
only_path: false
only_path: false,
no_sourcepos: true
)
end
end
......
......@@ -29,6 +29,12 @@ module Banzai
Filter::CommitReferenceFilter
]
end
def self.transform_context(context)
super(context).merge(
no_sourcepos: true
)
end
end
end
end
......@@ -93,7 +93,7 @@ module Gitlab
end
def markdown(text)
Banzai.render(text, project: @source_parent, no_original_data: true)
Banzai.render(text, project: @source_parent, no_original_data: true, no_sourcepos: true)
end
end
end
......
......@@ -173,6 +173,7 @@ describe IssuablesHelper do
before do
allow(helper).to receive(:current_user).and_return(user)
allow(helper).to receive(:can?).and_return(true)
stub_commonmark_sourcepos_disabled
end
it 'returns the correct data for an issue' do
......
......@@ -30,21 +30,21 @@ describe Banzai::Filter::MarkdownFilter do
end
it 'adds language to lang attribute when specified' do
result = filter("```html\nsome code\n```")
result = filter("```html\nsome code\n```", no_sourcepos: true)
expect(result).to start_with("<pre><code lang=\"html\">")
expect(result).to start_with('<pre><code lang="html">')
end
it 'does not add language to lang attribute when not specified' do
result = filter("```\nsome code\n```")
result = filter("```\nsome code\n```", no_sourcepos: true)
expect(result).to start_with("<pre><code>")
expect(result).to start_with('<pre><code>')
end
it 'works with utf8 chars in language' do
result = filter("```日\nsome code\n```")
result = filter("```日\nsome code\n```", no_sourcepos: true)
expect(result).to start_with("<pre><code lang=\"\">")
expect(result).to start_with('<pre><code lang="日">')
end
end
......@@ -67,6 +67,38 @@ describe Banzai::Filter::MarkdownFilter do
end
end
describe 'source line position' do
context 'using CommonMark' do
before do
stub_const('Banzai::Filter::MarkdownFilter::DEFAULT_ENGINE', :common_mark)
end
it 'defaults to add data-sourcepos' do
result = filter('test')
expect(result).to eq '<p data-sourcepos="1:1-1:4">test</p>'
end
it 'disables data-sourcepos' do
result = filter('test', no_sourcepos: true)
expect(result).to eq '<p>test</p>'
end
end
context 'using Redcarpet' do
before do
stub_const('Banzai::Filter::MarkdownFilter::DEFAULT_ENGINE', :redcarpet)
end
it 'does not support data-sourcepos' do
result = filter('test')
expect(result).to eq '<p>test</p>'
end
end
end
describe 'footnotes in tables' do
it 'processes footnotes in table cells' do
text = <<-MD.strip_heredoc
......@@ -77,7 +109,7 @@ describe Banzai::Filter::MarkdownFilter do
[^1]: a footnote
MD
result = filter(text)
result = filter(text, no_sourcepos: true)
expect(result).to include('<td>foot <sup')
expect(result).to include('<section class="footnotes">')
......
......@@ -301,6 +301,13 @@ describe Banzai::Filter::SanitizationFilter do
expect(act.to_html).to eq exp
end
it 'allows the `data-sourcepos` attribute globally' do
exp = %q{<p data-sourcepos="1:1-1:10">foo/bar.md</p>}
act = filter(exp)
expect(act.to_html).to eq exp
end
describe 'footnotes' do
it 'allows correct footnote id property on links' do
exp = %q{<a href="#fn1" id="fnref1">foo/bar.md</a>}
......
......@@ -13,6 +13,10 @@ describe Banzai::Pipeline::DescriptionPipeline do
output
end
before do
stub_commonmark_sourcepos_disabled
end
it 'uses a limited whitelist' do
doc = parse('# Description')
......
......@@ -54,6 +54,8 @@ describe Banzai::Pipeline::FullPipeline do
end
it 'properly adds the necessary ids and classes' do
stub_commonmark_sourcepos_disabled
expect(html.lines.map(&:strip).join("\n")).to eq filtered_footnote
end
end
......
......@@ -67,13 +67,17 @@ describe CacheMarkdownField do
end
let(:markdown) { '`Foo`' }
let(:html) { '<p dir="auto"><code>Foo</code></p>' }
let(:html) { '<p dir="auto"><code>Foo</code></p>' }
let(:updated_markdown) { '`Bar`' }
let(:updated_html) { '<p dir="auto"><code>Bar</code></p>' }
let(:updated_html) { '<p dir="auto"><code>Bar</code></p>' }
let(:thing) { ThingWithMarkdownFields.new(foo: markdown, foo_html: html, cached_markdown_version: CacheMarkdownField::CACHE_COMMONMARK_VERSION) }
before do
stub_commonmark_sourcepos_disabled
end
describe '.attributes' do
it 'excludes cache attributes' do
expect(thing.attributes.keys.sort).to eq(%w[bar baz foo])
......
......@@ -159,6 +159,10 @@ describe CacheableAttributes do
describe 'edge cases' do
describe 'caching behavior', :use_clean_rails_memory_store_caching do
before do
stub_commonmark_sourcepos_disabled
end
it 'retrieves upload fields properly' do
ar_record = create(:appearance, :with_logo)
ar_record.cache!
......
require 'spec_helper'
describe Redactable do
before do
stub_commonmark_sourcepos_disabled
end
shared_examples 'model with redactable field' do
it 'redacts unsubscribe token' do
model[field] = 'some text /sent_notifications/00000000000000000000000000000000/unsubscribe more text'
......
......@@ -7,6 +7,8 @@ describe API::Markdown do
let(:user) {} # No-op. It gets overwritten in the contexts below.
before do
stub_commonmark_sourcepos_disabled
post api("/markdown", user), params: params
end
......
......@@ -10,6 +10,7 @@ describe GroupChildEntity do
before do
allow(request).to receive(:current_user).and_return(user)
stub_commonmark_sourcepos_disabled
end
shared_examples 'group child json' do
......
......@@ -64,6 +64,12 @@ module StubGitlabCalls
end
end
def stub_commonmark_sourcepos_disabled
allow_any_instance_of(Banzai::Filter::MarkdownEngines::CommonMark)
.to receive(:render_options)
.and_return(Banzai::Filter::MarkdownEngines::CommonMark::RENDER_OPTIONS)
end
private
def stub_container_registry_tag_manifest_content
......
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