Skip to content
5 changes: 4 additions & 1 deletion Microsoft.Toolkit.Parsers/Markdown/Blocks/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ Or the code block starts and ends with ```
return new CodeBlock()
{
Text = code.ToString().Trim('\r', '\n'),
CodeLanguage = !string.IsNullOrWhiteSpace(codeLanguage) ? codeLanguage.Trim() : null
CodeLanguage = !string.IsNullOrWhiteSpace(codeLanguage) ? codeLanguage.Trim() : null,

// substring to get the parsed codeblock from the full markdown string
OriginalMarkdown = markdown.Substring(start, actualEnd - start > 0 ? actualEnd - start : 0)
};
}

Expand Down
6 changes: 6 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal static HeaderBlock ParseHashPrefixedHeader(string markdown, int start,
end--;
}

// substring to get the parsed header from the full markdown string
result.OriginalMarkdown = markdown.Substring(start, end - start > 0 ? end - start : 0);

// Parse the inline content.
result.Inlines = Common.ParseInlineChildren(markdown, pos, end);
return result;
Expand Down Expand Up @@ -144,6 +147,9 @@ internal static HeaderBlock ParseUnderlineStyleHeader(string markdown, int first
var result = new HeaderBlock();
result.HeaderLevel = underlineChar == '=' ? 1 : 2;

// substring to get the parsed header from the full markdown string
result.OriginalMarkdown = markdown.Substring(firstLineStart, secondLineEnd - firstLineStart > 0 ? secondLineEnd - firstLineStart : 0);

// Parse the inline content.
result.Inlines = Common.ParseInlineChildren(markdown, firstLineStart, firstLineEnd);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ internal static HorizontalRuleBlock Parse(string markdown, int start, int end)
}
}

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 ? new HorizontalRuleBlock() : null;
return hrCharCount >= 3 ? result : null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ internal static LinkReferenceBlock Parse(string markdown, int start, int end)
result.Id = id;
result.Url = url;
result.Tooltip = tooltip;

// substring to get the parsed link from the full markdown string
result.OriginalMarkdown = markdown.Substring(start, end - start > 0 ? end - start : 0);

return result;
}

Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ internal static ListBlock Parse(string markdown, int start, int maxEnd, int quot
}

var result = russianDolls[0].List;

// substring to get the parsed list from the full markdown string
result.OriginalMarkdown = markdown.Substring(start, actualEnd - start > 0 ? actualEnd - start : 0);
ReplaceStringBuilders(result);
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/Blocks/ParagraphBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ internal static ParagraphBlock Parse(string markdown)
{
var result = new ParagraphBlock();
result.Inlines = Common.ParseInlineChildren(markdown, 0, markdown.Length);

// no substring needed here, parsed markdown equals orignal markdown
result.OriginalMarkdown = markdown;
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/Blocks/QuoteBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ internal static QuoteBlock Parse(string markdown, int startOfLine, int maxEnd, i
// Recursively call into the markdown block parser.
result.Blocks = MarkdownDocument.Parse(markdown, startOfLine, maxEnd, quoteDepth: quoteDepth + 1, actualEnd: out actualEnd);

// substring to get the parsed quote from the full markdown string
result.OriginalMarkdown = markdown.Substring(startOfLine, actualEnd - startOfLine > 0 ? actualEnd - startOfLine : 0);

return result;
}
}
Expand Down
13 changes: 12 additions & 1 deletion Microsoft.Toolkit.Parsers/Markdown/Blocks/TableBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ internal static TableBlock Parse(string markdown, int start, int endOfFirstLine,
// interior vertical bars as there are interior vertical bars on the first line.
actualEnd = start;

// copy the original start index for the original markdown extraction
int origStart = start;

// First thing to do is to check if there is a vertical bar on the line.
var barSections = markdown.Substring(start, endOfFirstLine - start).Split('|');

Expand Down Expand Up @@ -160,7 +163,15 @@ internal static TableBlock Parse(string markdown, int start, int endOfFirstLine,
}

actualEnd = start;
return new TableBlock { ColumnDefinitions = columnDefinitions, Rows = rows };

TableBlock result = new TableBlock();
result.ColumnDefinitions = columnDefinitions;
result.Rows = rows;

// substring to get the parsed table from the full markdown string
result.OriginalMarkdown = markdown.Substring(origStart, actualEnd - origStart > 0 ? actualEnd - origStart : 0);

return result;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/Blocks/YamlHeaderBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ internal static YamlHeaderBlock Parse(string markdown, int start, int end, out i
return null;
}

// substring to get the parsed yaml from the full markdown string
result.OriginalMarkdown = markdown.Substring(start, realEndIndex - start > 0 ? realEndIndex - start : 0);

return result;
}

Expand Down
5 changes: 5 additions & 0 deletions Microsoft.Toolkit.Parsers/Markdown/MarkdownBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public abstract class MarkdownBlock : MarkdownElement
/// </summary>
public MarkdownBlockType Type { get; set; }

/// <summary>
/// Gets the original Markdown the element was parsed from.
/// </summary>
public string OriginalMarkdown { get; internal set; }

/// <summary>
/// Initializes a new instance of the <see cref="MarkdownBlock"/> class.
/// </summary>
Expand Down