summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/markdown_filter_spec.rb
blob: cf49249756a2b7b07a1a39ce979a9ffa172ef093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'spec_helper'

describe Banzai::Filter::MarkdownFilter do
  include FilterSpecHelper

  describe 'markdown engine from context' do
    it 'defaults to CommonMark' do
      expect_any_instance_of(Banzai::Filter::MarkdownEngines::CommonMark).to receive(:render).and_return('test')

      filter('test')
    end

    it 'uses Redcarpet' do
      expect_any_instance_of(Banzai::Filter::MarkdownEngines::Redcarpet).to receive(:render).and_return('test')

      filter('test', { markdown_engine: :redcarpet })
    end

    it 'uses CommonMark' do
      expect_any_instance_of(Banzai::Filter::MarkdownEngines::CommonMark).to receive(:render).and_return('test')

      filter('test', { markdown_engine: :common_mark })
    end
  end

  describe 'code block' do
    context 'using CommonMark' do
      before do
        stub_const('Banzai::Filter::MarkdownFilter::DEFAULT_ENGINE', :common_mark)
      end

      it 'adds language to lang attribute when specified' do
        result = filter("```html\nsome code\n```")

        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```")

        expect(result).to start_with("<pre><code>")
      end

      it 'works with utf8 chars in language' do
        result = filter("```日\nsome code\n```")

        expect(result).to start_with("<pre><code lang=\"日\">")
      end
    end

    context 'using Redcarpet' do
      before do
        stub_const('Banzai::Filter::MarkdownFilter::DEFAULT_ENGINE', :redcarpet)
      end

      it 'adds language to lang attribute when specified' do
        result = filter("```html\nsome code\n```")

        expect(result).to start_with("\n<pre><code lang=\"html\">")
      end

      it 'does not add language to lang attribute when not specified' do
        result = filter("```\nsome code\n```")

        expect(result).to start_with("\n<pre><code>")
      end
    end
  end

  describe 'footnotes in tables' do
    it 'processes footnotes in table cells' do
      text = <<-MD.strip_heredoc
      | Column1   |
      | --------- |
      | foot [^1] |

      [^1]: a footnote
      MD

      result = filter(text)

      expect(result).to include('<td>foot <sup')
      expect(result).to include('<section class="footnotes">')
    end
  end
end