This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathHorizontalRuleBlock.cs
More file actions
79 lines (71 loc) · 2.87 KB
/
Copy pathHorizontalRuleBlock.cs
File metadata and controls
79 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Toolkit.Parsers.Core;
namespace Microsoft.Toolkit.Parsers.Markdown.Blocks
{
/// <summary>
/// Represents a horizontal line.
/// </summary>
public class HorizontalRuleBlock : MarkdownBlock
{
/// <summary>
/// Initializes a new instance of the <see cref="HorizontalRuleBlock"/> class.
/// </summary>
public HorizontalRuleBlock()
: base(MarkdownBlockType.HorizontalRule)
{
}
/// <summary>
/// Parses a horizontal rule.
/// </summary>
/// <param name="markdown"> The markdown text. </param>
/// <param name="start"> The location of the start of the line. </param>
/// <param name="end"> The location of the end of the line. </param>
/// <returns> A parsed horizontal rule block, or <c>null</c> if this is not a horizontal rule. </returns>
internal static HorizontalRuleBlock Parse(string markdown, int start, int end)
{
// A horizontal rule is a line with at least 3 stars, optionally separated by spaces
// OR a line with at least 3 dashes, optionally separated by spaces
// OR a line with at least 3 underscores, optionally separated by spaces.
char hrChar = '\0';
int hrCharCount = 0;
int pos = start;
while (pos < end)
{
char c = markdown[pos++];
if (c == '*' || c == '-' || c == '_')
{
// All of the non-whitespace characters on the line must match.
if (hrCharCount > 0 && c != hrChar)
{
return null;
}
hrChar = c;
hrCharCount++;
}
else if (c == '\n')
{
break;
}
else if (!ParseHelpers.IsMarkdownWhiteSpace(c))
{
return null;
}
}
HorizontalRuleBlock result = new HorizontalRuleBlock();
// substring to get the parsed horizontalrule from the full markdown string
result.OriginalMarkdown = markdown.Substring(start, end - start > 0 ? end - start : 0);
// Hopefully there were at least 3 stars/dashes/underscores.
return hrCharCount >= 3 ? result : null;
}
/// <summary>
/// Converts the object into it's textual representation.
/// </summary>
/// <returns> The textual representation of this object. </returns>
public override string ToString()
{
return "---";
}
}
}