File: jekyll_commonmark_spec.rb

package info (click to toggle)
ruby-jekyll-commonmark 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: ruby: 271; sh: 9; makefile: 7
file content (145 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true

require "spec_helper"

describe(Jekyll::Converters::Markdown::CommonMark) do
  let(:options) { [] }
  let(:extensions) { [] }
  let(:config) do
    {
      "commonmark" => {
        "options"    => options,
        "extensions" => extensions,
      },
    }
  end
  let(:commonmark) { described_class.new(config) }
  let(:output) { commonmark.convert(content) }

  context "with default configuration" do
    it "produces the correct script tag" do
      actual = commonmark.convert("# Heading")
      expected = "<h1>Heading</h1>"
      expect(actual).to match(expected)
    end

    it "does not treat newlines as hardbreaks" do
      actual = commonmark.convert("a\nb")
      expected = "<p>a\nb</p>"
      expect(actual).to match(expected)
    end

    it "treats double linebreaks as a new paragraph" do
      actual = commonmark.convert("a\n\nb")
      expected = "<p>a</p>\n<p>b</p>"
      expect(actual).to match(expected)
    end

    it "escapes quotes" do
      actual = commonmark.convert('"SmartyPants"')
      expected = "<p>&quot;SmartyPants&quot;</p>"
      expect(actual).to match(expected)
    end

    it "does not link urls" do
      actual = commonmark.convert("https://example.com")
      expected = "https://example.com"
      expect(actual).to match(expected)
    end

    it "highlights fenced code-block" do
      content = <<~CODE
        ```yaml
        # Sample configuration
        title: CommonMark Test
        verbose: true
        atm_pin: 1234
        ```
      CODE

      output = <<~HTML
        <div class="language-yaml highlighter-rouge">
          <div class="highlight">
            <pre class="highlight">
              <code data-lang="yaml">
                <span class="c1"># Sample configuration</span>
                <span class="na">title</span><span class="pi">:</span>
                <span class="s">CommonMark Test</span>
                <span class="na">verbose</span><span class="pi">:</span>
                <span class="kc">true</span>
                <span class="na">atm_pin</span><span class="pi">:</span>
                <span class="m">1234</span>
              </code>
            </pre>
          </div>
        </div>
      HTML

      expect(commonmark.convert(content).gsub(%r!\s+!, "")).to match(output.gsub(%r!\s+!, ""))
    end
  end

  context "with SmartyPants enabled" do
    let(:options) { ["SMART"] }

    it "makes quotes curly" do
      actual = commonmark.convert('"SmartyPants"')
      expected = "<p>“SmartyPants”</p>"
      expect(actual).to match(expected)
    end
  end

  context "with hardbreaks enabled" do
    let(:options) { ["HARDBREAKS"] }

    it "treats newlines as hardbreaks" do
      actual = commonmark.convert("a\nb")
      expected = "<p>a<br />\nb</p>"
      expect(actual).to match(expected)
    end
  end

  context "with nobreaks enabled" do
    let(:options) { ["NOBREAKS"] }

    it "treats newlines as a single space" do
      actual = commonmark.convert("a\nb")
      expected = "<p>a b</p>"
      expect(actual).to match(expected)
    end
  end

  context "with autolink enabled" do
    let(:extensions) { ["autolink"] }

    it "links urls" do
      actual = commonmark.convert("https://example.com")
      expected = '<p><a href="https://example.com">https://example.com</a></p>'
      expect(actual).to match(expected)
    end
  end

  context "with invalid config" do
    let(:config) { "DEFAULT" }

    it "renders correct markup" do
      actual = commonmark.convert("# Heading\n\nhttps://example.com")
      expected = "<h1>Heading</h1>\n<p>https://example.com</p>"
      expect(actual).to match(expected)
    end
  end

  context "with invalid options and extensions" do
    let(:options)    { ["SOFTBREAKS"] }
    let(:extensions) { ["SOFTBREAKS"] }

    it "outputs warning messages but renders correct markup" do
      actual, output = capture_stdout { commonmark.convert("# Heading\n\nhttps://example.com") }
      expected = "<h1>Heading</h1>\n<p>https://example.com</p>"

      expect(output).to match("CommonMark: SOFTBREAKS is not a valid option")
      expect(output).to match("CommonMark: SOFTBREAKS is not a valid extension")
      expect(actual).to match(expected)
    end
  end
end