Project

General

Profile

Defect #25299 » fix-markdown-section.patch

Kiichi Ozaki, 2017-03-10 07:50

View differences:

lib/redmine/wiki_formatting/markdown/formatter.rb
90 90
          i = 0
91 91
          l = 1
92 92
          inside_pre = false
93
          @text.split(/(^(?:.+\r?\n\r?(?:\=+|\-+)|#+.+|~~~.*)\s*$)/).each do |part|
93
          @text.split(/(^(?:.+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|
94 94
            level = nil
95
            if part =~ /\A~{3,}(\S+)?\s*$/
96
              if $1
97
                if !inside_pre
98
                  inside_pre = true
99
                end
100
              else
101
                inside_pre = !inside_pre
95
            if part =~ /\A(~{3,}|`{3,})(\S+)?\s*$/
96
              if !inside_pre
97
                inside_pre = true
98
              elsif !$2
99
                inside_pre = false
102 100
              end
103 101
            elsif inside_pre
104 102
              # nop
test/unit/lib/redmine/wiki_formatting/markdown_formatter_test.rb
80 80
    assert_equal '<p>This is a <a href="/issues">link</a></p>', @formatter.new(text).to_html.strip
81 81
  end
82 82

  
83
  STR_WITH_PRE = [
84
  # 0
85
"# Title
86

  
87
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.",
88
  # 1
89
"## Heading 2
90

  
91
~~~ruby
92
  def foo
93
  end
94
~~~
95

  
96
Morbi facilisis accumsan orci non pharetra.
97

  
98
```
99
Pre Content:
100

  
101
## Inside pre
102

  
103
<tag> inside pre block
104

  
105
Morbi facilisis accumsan orci non pharetra.
106
```",
107
  # 2
108
"### Heading 3
109

  
110
Nulla nunc nisi, egestas in ornare vel, posuere ac libero."]
111

  
112
  def test_get_section_should_ignore_pre_content
113
    text = STR_WITH_PRE.join("\n\n")
114

  
115
    assert_section_with_hash STR_WITH_PRE[1..2].join("\n\n"), text, 2
116
    assert_section_with_hash STR_WITH_PRE[2], text, 3
117
  end
118

  
119
  def test_update_section_should_not_escape_pre_content_outside_section
120
    text = STR_WITH_PRE.join("\n\n")
121
    replacement = "New text"
122

  
123
    assert_equal [STR_WITH_PRE[0..1], "New text"].flatten.join("\n\n"),
124
      @formatter.new(text).update_section(3, replacement)
125
  end
126

  
127
  private
128

  
129
  def assert_section_with_hash(expected, text, index)
130
    result = @formatter.new(text).get_section(index)
131

  
132
    assert_kind_of Array, result
133
    assert_equal 2, result.size
134
    assert_equal expected, result.first, "section content did not match"
135
    assert_equal Digest::MD5.hexdigest(expected), result.last, "section hash did not match"
136
  end
137

  
83 138
  end
84 139
end
(2-2/2)