File: setext_headers_test.exs

package info (click to toggle)
elixir-earmark-parser 1.4.44-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: makefile: 9
file content (132 lines) | stat: -rw-r--r-- 3,668 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
defmodule Acceptance.Ast.SetextHeadersTest do
  use Support.AcceptanceTestCase
  use ExUnit.Case, async: true
  import Support.Helpers, only: [as_ast: 1, parse_html: 1]

  describe "Base cases" do
    test "Level one" do
      markdown = "foo\n==="
      html = "<h1>foo</h1>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "Level two" do
      markdown = "foo\n---"
      html = "<h2>foo</h2>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "narrow escape" do
      markdown = "Foo\\\n----\n"
      html = "<h2>Foo\\</h2>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "Combinations" do
    test "levels one and two" do
      markdown = "Foo *bar*\n=========\n\nFoo *bar*\n---------\n"
      html = "<h1>Foo <em>bar</em></h1>\n<h2>Foo <em>bar</em></h2>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "and levels two and one" do
      markdown = "Foo\n-------------------------\n\nFoo\n=\n"
      html = "<h2>Foo</h2>\n<h1>Foo</h1>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  # There is no consensus on this one, I prefer to not define the behavior of this unless
  # there is a real use case
  # c.f. http://johnmacfarlane.net/babelmark2/?text=%60Foo%0A----%0A%60%0A%0A%3Ca+title%3D%22a+lot%0A---%0Aof+dashes%22%2F%3E%0A
  #    html = "<h2>`Foo</h2>\n<p>`</p>\n<h2>&lt;a title=&quot;a lot</h2>\n<p>of dashes&quot;/&gt;</p>\n"
  #    markdown = "`Foo\n----\n`\n\n<a title=\"a lot\n---\nof dashes\"/>\n"
  #
  describe "Setext headers with some context" do
    test "h1 after an unordered list" do
      markdown = "* foo\n\nbar\n==="
      html = "<ul><li>foo</li></ul><h1>bar</h1>"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "h2 after an unordered list" do
      markdown = "* foo\n\nbar\n---"
      html = "<ul><li>foo</li></ul><h2>bar</h2>"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "h1 after an ordered list and pending text" do
      markdown = "1. foo\n\nbar\n===\ntext"
      html = "<ol><li>foo</li></ol><h1>bar</h1><p>text</p>"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "h2 between two lists" do
      markdown = "* foo\n\nbar\n---\n\n1. baz"
      html = "<ul><li>foo</li></ul><h2>bar</h2><ol><li>baz</li></ol>"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "h2 between two lists more blank lines" do
      markdown = """
      1. foo


      bar
      ---


      * baz
      """

      ast = [ol("foo"), tag("h2", "bar"), ul("baz")]
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "after a table" do
    test "h2 after a table" do
      markdown = "|a|b|\n|d|e|\nbar\n---"

      html =
        "<table><tbody><tr>\n<td style=\"text-align: left;\">a</td><td style=\"text-align: left;\">b</td>\n</tr>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td>\n</tr>\n</tbody>\n</table>\n<h2>bar</h2>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end
end

# SPDX-License-Identifier: Apache-2.0