Skip to content

Commit 1959fd1

Browse files
committed
Preserve newlines in Markdown paragraph HTML output
The accept_paragraph method in ToHtml had a gsub that collapsed newlines into spaces (or removed them for CJK text). This only affected Markdown content since the RDoc markup parser already converts newlines to spaces at parse time (parser.rb:221). Remove the gsub so that Markdown soft breaks are preserved as newlines in the HTML output, matching GFM behavior. For example, `> foo\nbar` now produces `<p>foo\nbar</p>` instead of `<p>foo bar</p>`.
1 parent ce49b8b commit 1959fd1

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

lib/rdoc/markup/to_html.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ def accept_block_quote(block_quote)
313313
def accept_paragraph(paragraph)
314314
@res << "\n<p>"
315315
text = paragraph.text @hard_break
316-
text = text.gsub(/(#{SPACE_SEPARATED_LETTER_CLASS})?\K\r?\n(?=(?(1)(#{SPACE_SEPARATED_LETTER_CLASS})?))/o) {
317-
defined?($2) && ' '
318-
}
319316
@res << to_html(text)
320317
@res << "</p>\n"
321318
end

test/rdoc/markup/to_html_test.rb

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def accept_paragraph_br
236236
end
237237

238238
def accept_paragraph_break
239-
assert_equal "\n<p>hello<br> world</p>\n", @to.res.join
239+
assert_equal "\n<p>hello<br>\nworld</p>\n", @to.res.join
240240
end
241241

242242
def accept_paragraph_i
@@ -411,43 +411,17 @@ def test_accept_heading_dedup_resets_on_start_accepting
411411
end
412412

413413
def test_accept_paragraph_newline
414-
hellos = ["hello", "\u{393 3b5 3b9 3ac} \u{3c3 3bf 3c5}"]
415-
worlds = ["world", "\u{3ba 3cc 3c3 3bc 3bf 3c2}"]
416-
ohayo, sekai = %W"\u{304a 306f 3088 3046} \u{4e16 754c}"
417-
418-
hellos.product(worlds) do |hello, world|
419-
@to.start_accepting
420-
@to.accept_paragraph para("#{hello}\n", "#{world}\n")
421-
assert_equal "\n<p>#{hello} #{world}</p>\n", @to.res.join
422-
end
423-
424-
hellos.each do |hello|
425-
@to.start_accepting
426-
@to.accept_paragraph para("#{hello}\n", "#{sekai}\n")
427-
assert_equal "\n<p>#{hello}#{sekai}</p>\n", @to.res.join
428-
end
429-
430-
worlds.each do |world|
431-
@to.start_accepting
432-
@to.accept_paragraph para("#{ohayo}\n", "#{world}\n")
433-
assert_equal "\n<p>#{ohayo}#{world}</p>\n", @to.res.join
434-
end
435-
414+
# Newlines in paragraph parts are preserved as-is in HTML output.
415+
# The RDoc markup parser already converts newlines to spaces at parse
416+
# time (in parser.rb), so this only affects Markdown content where
417+
# newlines represent soft breaks and should be preserved per GFM spec.
436418
@to.start_accepting
437-
@to.accept_paragraph para("#{ohayo}\n", "#{sekai}\n")
438-
assert_equal "\n<p>#{ohayo}#{sekai}</p>\n", @to.res.join
419+
@to.accept_paragraph para("hello\n", "world\n")
420+
assert_equal "\n<p>hello\nworld\n</p>\n", @to.res.join
439421

440422
@to.start_accepting
441423
@to.accept_paragraph para("+hello+\n", "world\n")
442-
assert_equal "\n<p><code>hello</code> world</p>\n", @to.res.join
443-
444-
@to.start_accepting
445-
@to.accept_paragraph para("hello\n", "+world+\n")
446-
assert_equal "\n<p>hello <code>world</code></p>\n", @to.res.join
447-
448-
@to.start_accepting
449-
@to.accept_paragraph para("+hello+\n", "+world+\n")
450-
assert_equal "\n<p><code>hello</code> <code>world</code></p>\n", @to.res.join
424+
assert_equal "\n<p><code>hello</code>\nworld\n</p>\n", @to.res.join
451425
end
452426

453427
def test_accept_heading_output_decoration

test/rdoc/rdoc_markdown_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def test_parse_block_quote_continue
7575
assert_equal expected, doc
7676
end
7777

78+
def test_parse_block_quote_continue_html
79+
doc = parse <<-BLOCK_QUOTE
80+
> this is
81+
a block quote
82+
BLOCK_QUOTE
83+
84+
html = doc.accept(RDoc::Markup::ToHtml.new)
85+
86+
assert_include html, "<p>this is\na block quote</p>"
87+
end
88+
7889
def test_parse_block_quote_list
7990
doc = parse <<-BLOCK_QUOTE
8091
> text

0 commit comments

Comments
 (0)