Skip to content

Commit 1bb3bec

Browse files
authored
Require space between hash/content in ATX heading (#1140)
While writing some Markdown documentation for Rails, I came across an interesting case where trying to link to an instance method at the start of a line would instead parse as an H1 heading: ```markdown #response_body= ``` Expected: ```html <a href=""><code>#response_body=</code></a> ``` Actual: ```html <h1>response_body=</h1> ``` According to the CommonMark spec: > At least one space or tab is required between the # characters and the > heading’s contents, unless the heading is empty. Note that many > implementations currently do not require the space. However, the space > was required by the original ATX implementation, and it helps prevent > things like the following from being parsed as headings: > > Example 64 So while some implementations do not follow this requirement, I believe RDoc should because it makes it easy to write text similar to Example 64 (which was used in the new test) and it also enables automatically linking to instance methods at the start of a line.
1 parent 27932d0 commit 1bb3bec

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ AtxInline = !@Newline !(@Sp /#*/ @Sp @Newline) Inline
530530
AtxStart = < /\#{1,6}/ >
531531
{ text.length }
532532

533-
AtxHeading = AtxStart:s @Sp AtxInline+:a (@Sp /#*/ @Sp)? @Newline
533+
AtxHeading = AtxStart:s @Spacechar+ AtxInline+:a (@Sp /#*/ @Sp)? @Newline
534534
{ RDoc::Markup::Heading.new(s, a.join) }
535535

536536
SetextHeading = SetextHeading1 | SetextHeading2

test/rdoc/test_rdoc_markdown.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,23 @@ def test_parse_escape
414414
end
415415

416416
def test_parse_heading_atx
417-
doc = parse "# heading\n"
417+
# CommonMark Example 62
418+
(1..6).each do |level|
419+
doc = parse "#{"#" * level} heading\n"
420+
421+
expected = @RM::Document.new(
422+
@RM::Heading.new(level, "heading"))
423+
424+
assert_equal expected, doc
425+
end
426+
427+
# CommonMark Example 64
428+
doc = parse "#5 bolt\n\n#hashtag\n"
418429

419430
expected = @RM::Document.new(
420-
@RM::Heading.new(1, "heading"))
431+
para("#5 bolt"),
432+
para("#hashtag"),
433+
)
421434

422435
assert_equal expected, doc
423436
end

0 commit comments

Comments
 (0)