Skip to content

Commit cdcade0

Browse files
Fix issue in Markdown Parser with Two-Line Header 2 parsing
Issue was originally introduced when YAML front-matter support was added in e94daf5
1 parent 9625aab commit cdcade0

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Microsoft.Toolkit.Parsers.Markdown.Blocks
1111
{
1212
/// <summary>
1313
/// Represents a heading.
14+
/// <seealso href="https://spec.commonmark.org/0.29/#atx-headings">Single-Line Header CommonMark Spec</seealso>
15+
/// <seealso href="https://spec.commonmark.org/0.29/#setext-headings">Two-Line Header CommonMark Spec</seealso>
1416
/// </summary>
1517
public class HeaderBlock : MarkdownBlock
1618
{

Microsoft.Toolkit.Parsers/Markdown/MarkdownDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ internal static List<MarkdownBlock> Parse(string markdown, int start, int end, i
233233
realStartOfLine = startOfLine;
234234
endOfLine = startOfLine + 3;
235235
startOfNextLine = Common.FindNextSingleNewLine(markdown, startOfLine, end, out startOfNextLine);
236-
}
237236

238-
paragraphText.Clear();
237+
paragraphText.Clear();
238+
}
239239
}
240240

241241
if (newBlockElement == null && nonSpaceChar == '#' && nonSpacePos == startOfLine)

UnitTests/Markdown/Parse/HeaderTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using Microsoft.Toolkit.Parsers.Markdown.Blocks;
88
using Microsoft.Toolkit.Parsers.Markdown.Inlines;
9+
using Microsoft.Toolkit.Parsers.Markdown;
910

1011
namespace UnitTests.Markdown.Parse
1112
{
@@ -56,6 +57,13 @@ public void Header_2()
5657
[TestCategory("Parse - block")]
5758
public void Header_2_Alt()
5859
{
60+
/***
61+
* Note: This is a tricky scenario because Header 2's
62+
* can be easily confused with thematic breaks, see specs:
63+
* https://spec.commonmark.org/0.29/#setext-headings
64+
* https://spec.commonmark.org/0.29/#thematic-breaks
65+
***/
66+
5967
// Note: trailing spaces on the second line are okay.
6068
AssertEqual(CollapseWhitespace(@"
6169
Header 2
@@ -64,6 +72,25 @@ Header 2
6472
new TextRunInline { Text = "Header 2" }));
6573
}
6674

75+
[TestMethod]
76+
[TestCategory("Parse - block")]
77+
public void Header_2_Alt_NotHorizontalRule()
78+
{
79+
/***
80+
* Note: This is a tricky scenario because Header 2's
81+
* can be easily confused with thematic breaks, see specs:
82+
* https://spec.commonmark.org/0.29/#setext-headings
83+
* https://spec.commonmark.org/0.29/#thematic-breaks
84+
***/
85+
86+
// Note: trailing spaces on the second line are okay.
87+
AssertEqual(CollapseWhitespace(@"
88+
Header 2
89+
--- "),
90+
new HeaderBlock { HeaderLevel = 2 }.AddChildren(
91+
new TextRunInline { Text = "Header 2" }));
92+
}
93+
6794
[TestMethod]
6895
[TestCategory("Parse - block")]
6996
public void Header_3()

0 commit comments

Comments
 (0)