present: test Markdown & fix splitting of pre blocks

Consider this input:

	This is preformatted:

		line 1
		line 2

		line 3 after blank line

Before, this would split into two different <pre> sections
in Markdown mode. Now it matches legacy mode and doesn't.

Fixes golang/go#37972.

Change-Id: I6bf156c76ef9c63b6c3ffe6cce431d9589def867
Reviewed-on: https://go-review.googlesource.com/c/tools/+/224958
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/present/parse.go b/present/parse.go
index c4c7d94..8340559 100644
--- a/present/parse.go
+++ b/present/parse.go
@@ -470,13 +470,61 @@
 					return nil, err
 				}
 				e = t
+
+			case isMarkdown:
+				// Collect Markdown lines, including blank lines and indented text.
+				var block []string
+				endLine, endBlock := lines.line-1, -1 // end is last non-empty line
+				for ok {
+					trim := strings.TrimSpace(text)
+					if trim != "" {
+						// Command breaks text block.
+						// Section heading breaks text block in markdown.
+						if text[0] == '.' || text[0] == '#' || isSpeakerNote(text) {
+							break
+						}
+						if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
+							text = text[1:]
+						}
+						endLine, endBlock = lines.line, len(block)
+					}
+					block = append(block, text)
+					text, ok = lines.next()
+				}
+				block = block[:endBlock+1]
+				lines.line = endLine + 1
+				if len(block) == 0 {
+					break
+				}
+
+				// Replace all leading tabs with 4 spaces,
+				// which render better in code blocks.
+				// CommonMark defines that for parsing the structure of the file
+				// a tab is equivalent to 4 spaces, so this change won't
+				// affect the later parsing at all.
+				// An alternative would be to apply this to code blocks after parsing,
+				// at the same time that we update <a> targets, but that turns out
+				// to be quite difficult to modify in the AST.
+				for i, line := range block {
+					if len(line) > 0 && line[0] == '\t' {
+						short := strings.TrimLeft(line, "\t")
+						line = strings.Repeat("    ", len(line)-len(short)) + short
+						block[i] = line
+					}
+				}
+				html, err := renderMarkdown([]byte(strings.Join(block, "\n")))
+				if err != nil {
+					return nil, err
+				}
+				e = HTML{HTML: html}
+
 			default:
+				// Collect text lines.
 				var block []string
 				for ok && strings.TrimSpace(text) != "" {
 					// Command breaks text block.
 					// Section heading breaks text block in markdown.
-					if text[0] == '.' || isMarkdown && text[0] == '#' || isSpeakerNote(text) {
-						lines.back()
+					if text[0] == '.' || isSpeakerNote(text) {
 						break
 					}
 					if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
@@ -488,30 +536,7 @@
 				if len(block) == 0 {
 					break
 				}
-				if isMarkdown {
-					// Replace all leading tabs with 4 spaces,
-					// which render better in code blocks.
-					// CommonMark defines that for parsing the structure of the file
-					// a tab is equivalent to 4 spaces, so this change won't
-					// affect the later parsing at all.
-					// An alternative would be to apply this to code blocks after parsing,
-					// at the same time that we update <a> targets, but that turns out
-					// to be quite difficult to modify in the AST.
-					for i, line := range block {
-						if len(line) > 0 && line[0] == '\t' {
-							short := strings.TrimLeft(line, "\t")
-							line = strings.Repeat("    ", len(line)-len(short)) + short
-							block[i] = line
-						}
-					}
-					html, err := renderMarkdown([]byte(strings.Join(block, "\n")))
-					if err != nil {
-						return nil, err
-					}
-					e = HTML{HTML: html}
-				} else {
-					e = Text{Lines: block}
-				}
+				e = Text{Lines: block}
 			}
 			if e != nil {
 				section.Elem = append(section.Elem, e)
diff --git a/present/testdata/basic.md b/present/testdata/basic.md
new file mode 100644
index 0000000..ba69a25
--- /dev/null
+++ b/present/testdata/basic.md
@@ -0,0 +1,24 @@
+# Title
+Subtitle
+
+Name
+
+## Heading
+
+Text
+on two lines.
+
+More text.
+
+---
+<h1>Title</h1>
+<h2>Subtitle</h2>
+<author>
+<p>Name</p>
+</author>
+<section>
+<h2 id="TOC_1.">Heading</h2>
+<p>Text
+on two lines.</p>
+<p>More text.</p>
+</section>
diff --git a/present/testdata/code.md b/present/testdata/code.md
new file mode 100644
index 0000000..09d100f
--- /dev/null
+++ b/present/testdata/code.md
@@ -0,0 +1,39 @@
+# Code
+
+##
+
+Code:
+
+.code testdata/code.txt
+
+Snippet:
+
+.code testdata/code.txt /Snippet/
+
+Highlight:
+
+.code testdata/code.txt HL1
+
+---
+<h1>Code</h1>
+<section>
+<p>Code:</p>
+<div class="code">
+<pre><span num="1">code file</span>
+<span num="2">Snippet</span>
+<span num="3">important</span>
+</pre>
+</div>
+<p>Snippet:</p>
+<div class="code">
+<pre><span num="2">Snippet</span>
+</pre>
+</div>
+<p>Highlight:</p>
+<div class="code">
+<pre><span num="1">code file</span>
+<span num="2">Snippet</span>
+<span num="3"><b>important</b></span>
+</pre>
+</div>
+</section>
diff --git a/present/testdata/list.md b/present/testdata/list.md
new file mode 100644
index 0000000..3d69069
--- /dev/null
+++ b/present/testdata/list.md
@@ -0,0 +1,34 @@
+# List
+
+##
+
+- Item 1
+ on two lines.
+- Item 2.
+- Item 3.
+
+- Item 4 in list despite preceding blank line.
+- Item 5.
+
+---
+<h1>List</h1>
+<section>
+<ul>
+<li>
+<p>Item 1
+on two lines.</p>
+</li>
+<li>
+<p>Item 2.</p>
+</li>
+<li>
+<p>Item 3.</p>
+</li>
+<li>
+<p>Item 4 in list despite preceding blank line.</p>
+</li>
+<li>
+<p>Item 5.</p>
+</li>
+</ul>
+</section>
diff --git a/present/testdata/media.md b/present/testdata/media.md
new file mode 100644
index 0000000..94b7995
--- /dev/null
+++ b/present/testdata/media.md
@@ -0,0 +1,22 @@
+# Media
+
+##
+
+.image gopher.jpg _ 100
+.caption A gopher.
+
+.iframe https://golang.org/
+
+.link https://golang.org/ The Gopher's home.
+
+.html testdata/media.html
+
+---
+<h1>Media</h1>
+<section>
+<img src="gopher.jpg" width="100" alt="">
+<figcaption>A gopher.</figcaption>
+<iframe src="https://golang.org/"></iframe>
+<p class="link"><a href="https://golang.org/">The Gopher&#39;s home.</a></p>
+<!-- media.html -->
+</section>
diff --git a/present/testdata/pre.md b/present/testdata/pre.md
new file mode 100644
index 0000000..042ecc9
--- /dev/null
+++ b/present/testdata/pre.md
@@ -0,0 +1,39 @@
+# Pre
+
+##
+
+Pre with tab:
+
+	code block
+	on two lines
+
+Text with space:
+
+ now a text block
+ on two lines
+
+Pre with blank line:
+
+	before
+
+	after
+
+EOF
+
+---
+<h1>Pre</h1>
+<section>
+<p>Pre with tab:</p>
+<pre><code>code block
+on two lines
+</code></pre>
+<p>Text with space:</p>
+<p>now a text block
+on two lines</p>
+<p>Pre with blank line:</p>
+<pre><code>before
+
+after
+</code></pre>
+<p>EOF</p>
+</section>