diff --git a/block.go b/block.go index 17bf17c..ba84c67 100644 --- a/block.go +++ b/block.go @@ -1093,14 +1093,23 @@ func blockParagraph(out *bytes.Buffer, rndr *render, data []byte) int { // render the paragraph renderParagraph(out, rndr, data[:prev]) + // ignore leading and trailing whitespace + eol := i - 1 + for prev < eol && (data[prev] == ' ' || data[prev] == '\t') { + prev++ + } + for eol > prev && (data[eol-1] == ' ' || data[eol-1] == '\t') { + eol-- + } + // render the header - // this ugly, convoluted closure avoids forcing variables onto the heap + // this ugly double closure avoids forcing variables onto the heap work := func(o *bytes.Buffer, r *render, d []byte) func() bool { return func() bool { parseInline(o, r, d) return true } - }(out, rndr, data[prev:i-1]) + }(out, rndr, data[prev:eol]) rndr.mk.Header(out, work, level, rndr.mk.Opaque) // find the end of the underline diff --git a/block_test.go b/block_test.go index 57e865d..834a7e6 100644 --- a/block_test.go +++ b/block_test.go @@ -91,9 +91,6 @@ func TestPrefixHeaderNoExtensions(t *testing.T) { "* List\n * Nested list\n # Nested header\n", "\n", - - "* List\n * Sublist\n Not a header\n ------\n", - "\n", } doTestsBlock(t, tests, 0) } @@ -154,9 +151,53 @@ func TestPrefixHeaderSpaceExtension(t *testing.T) { "* List\n * Nested list\n # Nested header\n", "\n", - - "* List\n * Sublist\n Not a header\n ------\n", - "\n", } doTestsBlock(t, tests, EXTENSION_SPACE_HEADERS) } + +func TestUnderlineHeaders(t *testing.T) { + var tests = []string{ + "Header 1\n========\n", + "

Header 1

\n", + + "Header 2\n--------\n", + "

Header 2

\n", + + "A\n=\n", + "

A

\n", + + "B\n-\n", + "

B

\n", + + "Paragraph\nHeader\n=\n", + "

Paragraph

\n\n

Header

\n", + + "Header\n===\nParagraph\n", + "

Header

\n\n

Paragraph

\n", + + "Header\n===\nAnother header\n---\n", + "

Header

\n\n

Another header

\n", + + " Header\n======\n", + "

Header

\n", + + " Code\n========\n", + "
Code\n
\n\n

========

\n", + + "Header with *inline*\n=====\n", + "

Header with inline

\n", + + "* List\n * Sublist\n Not a header\n ------\n", + "\n", + + "Paragraph\n\n\n\n\nHeader\n===\n", + "

Paragraph

\n\n

Header

\n", + + "Trailing space \n==== \n\n", + "

Trailing space

\n", + + "Trailing spaces\n==== \n\n", + "

Trailing spaces

\n", + } + doTestsBlock(t, tests, 0) +}