diff --git a/app/assets/javascripts/copy_as_gfm.js.es6 b/app/assets/javascripts/copy_as_gfm.js.es6 index 63291853548..8e7f4c54213 100644 --- a/app/assets/javascripts/copy_as_gfm.js.es6 +++ b/app/assets/javascripts/copy_as_gfm.js.es6 @@ -3,9 +3,11 @@ (() => { const gfmRules = { - // The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML. + // The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert + // GitLab Flavored Markdown (GFM) to HTML. // These handlers consequently convert that same HTML to GFM to be copied to the clipboard. - // Every filter in lib/banzai/pipeline/gfm_pipeline.rb that generates HTML from GFM should have a handler here, in reverse order. + // Every filter in lib/banzai/pipeline/gfm_pipeline.rb that generates HTML + // from GFM should have a handler here, in reverse order. // The GFM-to-HTML-to-GFM cycle is tested in spec/features/copy_as_gfm_spec.rb. InlineDiffFilter: { 'span.idiff.addition'(el, text) { @@ -113,7 +115,7 @@ return backticks + spaceOrNoSpace + text + spaceOrNoSpace + backticks; }, 'blockquote'(el, text) { - return text.trim().split('\n').map((s) => (`> ${s}`).trim()).join('\n'); + return text.trim().split('\n').map((s) => `> ${s}`.trim()).join('\n'); }, 'img'(el, text) { return `![${el.getAttribute('alt')}](${el.getAttribute('src')})`; diff --git a/lib/banzai/pipeline/gfm_pipeline.rb b/lib/banzai/pipeline/gfm_pipeline.rb index 7b652aa79ec..ac95a79009b 100644 --- a/lib/banzai/pipeline/gfm_pipeline.rb +++ b/lib/banzai/pipeline/gfm_pipeline.rb @@ -2,8 +2,10 @@ module Banzai module Pipeline class GfmPipeline < BasePipeline # These filters convert GitLab Flavored Markdown (GFM) to HTML. - # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 consequently convert that same HTML to GFM to be copied to the clipboard. - # Every filter that generates HTML from GFM should have a handler in app/assets/javascripts/copy_as_gfm.js.es6, in reverse order. + # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 + # consequently convert that same HTML to GFM to be copied to the clipboard. + # Every filter that generates HTML from GFM should have a handler in + # app/assets/javascripts/copy_as_gfm.js.es6, in reverse order. # The GFM-to-HTML-to-GFM cycle is tested in spec/features/copy_as_gfm_spec.rb. def self.filters @filters ||= FilterArray[ diff --git a/spec/features/copy_as_gfm_spec.rb b/spec/features/copy_as_gfm_spec.rb index dd22f92c5c8..cedddadb05c 100644 --- a/spec/features/copy_as_gfm_spec.rb +++ b/spec/features/copy_as_gfm_spec.rb @@ -16,7 +16,7 @@ describe 'Copy as GFM', feature: true, js: true do # The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML. # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 consequently convert that same HTML to GFM. # To make sure these filters and handlers are properly aligned, this spec tests the GFM-to-HTML-to-GFM cycle - # by verifying (`html_to_gfm(gfm_to_html(gfm)) == gfm`) for a number of examples of GFM for every filter. + # by verifying (`html_to_gfm(gfm_to_html(gfm)) == gfm`) for a number of examples of GFM for every filter, using the `verify` helper. it 'supports nesting' do verify '> 1. [x] **[$`2 + 2`$ {-=-}{+=+} 2^2 ~~:thumbsup:~~](http://google.com)**' @@ -107,34 +107,159 @@ describe 'Copy as GFM', feature: true, js: true do verify '![Video](https://example.com/video.mp4)' end - it 'supports MathFilter' do + context 'MathFilter' do + it 'supports math as converted from GFM to HTML' do + verify( + '$`c = \pm\sqrt{a^2 + b^2}`$', + + # math block + <<-GFM.strip_heredoc + ```math + c = \pm\sqrt{a^2 + b^2} + ``` + GFM + ) + end + + it 'supports math as transformed from HTML to KaTeX' do + gfm = '$`c = \pm\sqrt{a^2 + b^2}`$' + + html = <<-HTML.strip_heredoc + + + + + + c + = + ± + + + + a + 2 + + + + + b + 2 + + + + + c = \\pm\\sqrt{a^2 + b^2} + + + + + + HTML + + output_gfm = html_to_gfm(html) + expect(output_gfm.strip).to eq(gfm.strip) + end + end + + + + it 'supports SyntaxHighlightFilter' do verify( - '$`c = \pm\sqrt{a^2 + b^2}`$', - # math block + <<-GFM.strip_heredoc, + ``` + Plain text + ``` + GFM + <<-GFM.strip_heredoc - ```math - c = \pm\sqrt{a^2 + b^2} + ```ruby + def foo + bar + end ``` GFM ) end - it 'supports SyntaxHighlightFilter' do - verify <<-GFM.strip_heredoc - ```ruby - def foo - bar - end - ``` - GFM - end - it 'supports MarkdownFilter' do verify( '`code`', '`` code with ` ticks ``', '> Quote', + # multiline quote <<-GFM.strip_heredoc, > Multiline