Skip to content

Commit

Permalink
Merge pull request #78 from shivabhusal/master
Browse files Browse the repository at this point in the history
Lets \n to be present in <pre> tag. solves #77
  • Loading branch information
xijo authored Oct 31, 2021
2 parents 2eab8dd + 63b5019 commit f2d7824
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/reverse_markdown/converters/pre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def treat(node, state)
case node.name
when 'code'
node.text
when 'text'
# Preserve '\n' in the middle of text/words, get rid of indentation spaces
ReverseMarkdown.cleaner.remove_leading_newlines(node.text.gsub("\n", '<br>').strip.gsub('<br>', "\n"))
when 'br'
"\n"
else
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/reverse_markdown/converters/pre_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
expect(converter.convert(node)).to include " puts foo\n"
end

it 'preserves new lines' do
it 'preserves new lines as <br>' do
node = node_for("<pre>one<br>two<br>three</pre>")
expect(converter.convert(node)).to include "\n\n one\n two\n three\n\n"
end

it 'preserves new lines as <br> and \n' do
node = node_for("<pre>one\ntwo\nthree<br>four</pre>")
expect(converter.convert(node)).to include "\n\n one\n two\n three\n four\n\n"
end

it 'handles code tags correctly' do
node = node_for("<pre><code>foobar</code></pre>")
Expand All @@ -41,6 +46,11 @@
expect(converter.convert(node)).to include "```\nfoo\nbar\n```"
end

it 'preserves new lines as <br> and \n' do
node = node_for("<pre>one\ntwo\nthree<br>four</pre>")
expect(converter.convert(node)).to include "```\none\ntwo\nthree\nfour\n```"
end

it 'handles code tags correctly' do
node = node_for("<pre><code>foobar</code></pre>")
expect(converter.convert(node)).to include "```\nfoobar\n```"
Expand Down

0 comments on commit f2d7824

Please sign in to comment.