diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/CodeBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/CodeBlock.cs index 007162e0596..97c5bdcfde6 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/CodeBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/CodeBlock.cs @@ -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) }; } diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs index 1ea31cc2e61..31bb9a51536 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/HeaderBlock.cs @@ -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; @@ -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; diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/HorizontalRuleBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/HorizontalRuleBlock.cs index 12a3b487afa..2703f572716 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/HorizontalRuleBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/HorizontalRuleBlock.cs @@ -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; } /// diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/LinkReferenceBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/LinkReferenceBlock.cs index 1313650b2cd..c7ac55f5131 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/LinkReferenceBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/LinkReferenceBlock.cs @@ -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; } diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs index ba44b0e6664..c8583d75e3b 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs @@ -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; } diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/ParagraphBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/ParagraphBlock.cs index 9e871124a3b..8268af07934 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/ParagraphBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/ParagraphBlock.cs @@ -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; } diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/QuoteBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/QuoteBlock.cs index 8b5d712c0ab..c34aea6392d 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/QuoteBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/QuoteBlock.cs @@ -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; } } diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/TableBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/TableBlock.cs index 8224f45766b..e4fad91f0c1 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/TableBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/TableBlock.cs @@ -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('|'); @@ -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; } /// diff --git a/Microsoft.Toolkit.Parsers/Markdown/Blocks/YamlHeaderBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/Blocks/YamlHeaderBlock.cs index 1cfd33c1a0c..70049ee69df 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/Blocks/YamlHeaderBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/Blocks/YamlHeaderBlock.cs @@ -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; } diff --git a/Microsoft.Toolkit.Parsers/Markdown/MarkdownBlock.cs b/Microsoft.Toolkit.Parsers/Markdown/MarkdownBlock.cs index eb316dd4d9b..c4899ec5348 100644 --- a/Microsoft.Toolkit.Parsers/Markdown/MarkdownBlock.cs +++ b/Microsoft.Toolkit.Parsers/Markdown/MarkdownBlock.cs @@ -14,6 +14,11 @@ public abstract class MarkdownBlock : MarkdownElement /// public MarkdownBlockType Type { get; set; } + /// + /// Gets the original Markdown the element was parsed from. + /// + public string OriginalMarkdown { get; internal set; } + /// /// Initializes a new instance of the class. ///