From 45a04f93747a128588268395071f00d0af70acd7 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Thu, 20 Dec 2018 20:22:27 -0600 Subject: Enable CommonMark source line position information This adds 'data-sourcepos' to tags, indicating which line of markdown it came from. Sets the stage for intelligently manipulating specific lines of markdown. --- spec/helpers/issuables_helper_spec.rb | 2 +- spec/lib/banzai/filter/markdown_filter_spec.rb | 40 +++++++++++++++++++--- spec/lib/banzai/filter/sanitization_filter_spec.rb | 7 ++++ .../banzai/pipeline/description_pipeline_spec.rb | 2 +- spec/models/concerns/cache_markdown_field_spec.rb | 17 ++++++--- spec/models/concerns/cacheable_attributes_spec.rb | 2 +- spec/models/concerns/redactable_spec.rb | 2 +- spec/requests/api/markdown_spec.rb | 2 +- spec/serializers/group_child_entity_spec.rb | 2 +- spec/support/helpers/markdown_feature.rb | 2 ++ 10 files changed, 63 insertions(+), 15 deletions(-) (limited to 'spec') diff --git a/spec/helpers/issuables_helper_spec.rb b/spec/helpers/issuables_helper_spec.rb index 81231cca085..963d3029830 100644 --- a/spec/helpers/issuables_helper_spec.rb +++ b/spec/helpers/issuables_helper_spec.rb @@ -193,7 +193,7 @@ describe IssuablesHelper do projectNamespace: @project.namespace.path, initialTitleHtml: issue.title, initialTitleText: issue.title, - initialDescriptionHtml: '

issue text

', + initialDescriptionHtml: '

issue text

', initialDescriptionText: 'issue text', initialTaskStatus: '0 of 0 tasks completed' } diff --git a/spec/lib/banzai/filter/markdown_filter_spec.rb b/spec/lib/banzai/filter/markdown_filter_spec.rb index cf49249756a..62f68b5f0cb 100644 --- a/spec/lib/banzai/filter/markdown_filter_spec.rb +++ b/spec/lib/banzai/filter/markdown_filter_spec.rb @@ -32,19 +32,19 @@ describe Banzai::Filter::MarkdownFilter do it 'adds language to lang attribute when specified' do result = filter("```html\nsome code\n```") - expect(result).to start_with("
")
+        expect(result).to start_with('
')
       end
 
       it 'does not add language to lang attribute when not specified' do
         result = filter("```\nsome code\n```")
 
-        expect(result).to start_with("
")
+        expect(result).to start_with('
')
       end
 
       it 'works with utf8 chars in language' do
         result = filter("```日\nsome code\n```")
 
-        expect(result).to start_with("
")
+        expect(result).to start_with('
')
       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 '

test

' + end + + it 'disables data-sourcepos' do + result = filter('test', { no_sourcepos: true }) + + expect(result).to eq '

test

' + 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 '

test

' + end + end + end + describe 'footnotes in tables' do it 'processes footnotes in table cells' do text = <<-MD.strip_heredoc @@ -79,7 +111,7 @@ describe Banzai::Filter::MarkdownFilter do result = filter(text) - expect(result).to include('foot foot ') end end diff --git a/spec/lib/banzai/filter/sanitization_filter_spec.rb b/spec/lib/banzai/filter/sanitization_filter_spec.rb index 836af18e0b6..f2a5d7b2c9f 100644 --- a/spec/lib/banzai/filter/sanitization_filter_spec.rb +++ b/spec/lib/banzai/filter/sanitization_filter_spec.rb @@ -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{

foo/bar.md

} + 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{foo/bar.md} diff --git a/spec/lib/banzai/pipeline/description_pipeline_spec.rb b/spec/lib/banzai/pipeline/description_pipeline_spec.rb index 8cce1b96698..1f9d53c6345 100644 --- a/spec/lib/banzai/pipeline/description_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/description_pipeline_spec.rb @@ -8,7 +8,7 @@ describe Banzai::Pipeline::DescriptionPipeline do output = described_class.to_html(html, project: spy) - output.gsub!(%r{\A

(.*)

(.*)\z}, '\1\2') if unwrap + output.gsub!(%r{\A

(.*)

(.*)\z}, '\1\2') if unwrap output end diff --git a/spec/models/concerns/cache_markdown_field_spec.rb b/spec/models/concerns/cache_markdown_field_spec.rb index f8d50e89d40..da5f184af06 100644 --- a/spec/models/concerns/cache_markdown_field_spec.rb +++ b/spec/models/concerns/cache_markdown_field_spec.rb @@ -67,10 +67,11 @@ describe CacheMarkdownField do end let(:markdown) { '`Foo`' } - let(:html) { '

Foo

' } + let(:html) { '

Foo

' } let(:updated_markdown) { '`Bar`' } - let(:updated_html) { '

Bar

' } + let(:updated_html) { '

Bar

' } + let(:updated_redcarpet_html) { '

Bar

' } let(:thing) { ThingWithMarkdownFields.new(foo: markdown, foo_html: html, cached_markdown_version: CacheMarkdownField::CACHE_COMMONMARK_VERSION) } @@ -95,13 +96,16 @@ describe CacheMarkdownField do context 'a changed markdown field' do shared_examples 'with cache version' do |cache_version| let(:thing) { ThingWithMarkdownFields.new(foo: markdown, foo_html: html, cached_markdown_version: cache_version) } + let(:updated_version_html) do + cache_version == CacheMarkdownField::CACHE_REDCARPET_VERSION ? updated_redcarpet_html : updated_html + end before do thing.foo = updated_markdown thing.save end - it { expect(thing.foo_html).to eq(updated_html) } + it { expect(thing.foo_html).to eq(updated_version_html) } it { expect(thing.cached_markdown_version).to eq(cache_version) } end @@ -268,6 +272,9 @@ describe CacheMarkdownField do describe '#refresh_markdown_cache!' do shared_examples 'with cache version' do |cache_version| let(:thing) { ThingWithMarkdownFields.new(foo: markdown, foo_html: html, cached_markdown_version: cache_version) } + let(:updated_version_html) do + cache_version == CacheMarkdownField::CACHE_REDCARPET_VERSION ? updated_redcarpet_html : updated_html + end before do thing.foo = updated_markdown @@ -276,7 +283,7 @@ describe CacheMarkdownField do it 'fills all html fields' do thing.refresh_markdown_cache! - expect(thing.foo_html).to eq(updated_html) + expect(thing.foo_html).to eq(updated_version_html) expect(thing.foo_html_changed?).to be_truthy expect(thing.baz_html_changed?).to be_truthy end @@ -291,7 +298,7 @@ describe CacheMarkdownField do it 'saves the changes using #update_columns' do expect(thing).to receive(:persisted?).and_return(true) expect(thing).to receive(:update_columns) - .with("foo_html" => updated_html, "baz_html" => "", "cached_markdown_version" => cache_version) + .with("foo_html" => updated_version_html, "baz_html" => "", "cached_markdown_version" => cache_version) thing.refresh_markdown_cache! end diff --git a/spec/models/concerns/cacheable_attributes_spec.rb b/spec/models/concerns/cacheable_attributes_spec.rb index 689e7d3058f..9bc381694d6 100644 --- a/spec/models/concerns/cacheable_attributes_spec.rb +++ b/spec/models/concerns/cacheable_attributes_spec.rb @@ -177,7 +177,7 @@ describe CacheableAttributes do cache_record = Appearance.current expect(cache_record.description).to eq('**Hello**') - expect(cache_record.description_html).to eq('

Hello

') + expect(cache_record.description_html).to eq('

Hello

') end end end diff --git a/spec/models/concerns/redactable_spec.rb b/spec/models/concerns/redactable_spec.rb index 7d320edd492..fe17970d75f 100644 --- a/spec/models/concerns/redactable_spec.rb +++ b/spec/models/concerns/redactable_spec.rb @@ -35,7 +35,7 @@ describe Redactable do expected = 'some text /sent_notifications/REDACTED/unsubscribe more text' expect(model[field]).to eq expected - expect(model["#{field}_html"]).to eq "

#{expected}

" + expect(model["#{field}_html"]).to eq "

#{expected}

" end end diff --git a/spec/requests/api/markdown_spec.rb b/spec/requests/api/markdown_spec.rb index e82ef002d32..386679d9dfd 100644 --- a/spec/requests/api/markdown_spec.rb +++ b/spec/requests/api/markdown_spec.rb @@ -15,7 +15,7 @@ describe API::Markdown do expect(response).to have_http_status(201) expect(response.headers["Content-Type"]).to eq("application/json") expect(json_response).to be_a(Hash) - expect(json_response["html"]).to eq("

#{text}

") + expect(json_response["html"]).to eq("

#{text}

") end end diff --git a/spec/serializers/group_child_entity_spec.rb b/spec/serializers/group_child_entity_spec.rb index dbc40bddc30..7f2ddc9546a 100644 --- a/spec/serializers/group_child_entity_spec.rb +++ b/spec/serializers/group_child_entity_spec.rb @@ -102,7 +102,7 @@ describe GroupChildEntity do let(:description) { ':smile:' } it 'has the correct markdown_description' do - expect(json[:markdown_description]).to eq('

😄

') + expect(json[:markdown_description]).to eq('

😄

') end end diff --git a/spec/support/helpers/markdown_feature.rb b/spec/support/helpers/markdown_feature.rb index 96401379cf0..e60e5ee6783 100644 --- a/spec/support/helpers/markdown_feature.rb +++ b/spec/support/helpers/markdown_feature.rb @@ -10,6 +10,8 @@ class MarkdownFeature include FactoryBot::Syntax::Methods + SOURCEPOS_REGEX = 'data-sourcepos="\d*:\d*-\d*:\d*"'.freeze + attr_reader :fixture_path def initialize(fixture_path = Rails.root.join('spec/fixtures/markdown.md.erb')) -- cgit v1.2.1