diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/MarkdownTextBlock.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/MarkdownTextBlock.xaml index 11a0deff313..8ba7a9041e9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/MarkdownTextBlock.xaml +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/MarkdownTextBlock.xaml @@ -44,15 +44,16 @@ + + + - - - + diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs index 95f0c3e9978..e83ea316c03 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs @@ -539,47 +539,70 @@ protected override void RenderCodeRun(CodeInline element, IRenderContext context throw new RenderContextIncorrectException(); } - var text = CreateTextBlock(localContext); - text.Text = CollapseWhitespace(context, element.Text); - text.FontFamily = InlineCodeFontFamily ?? FontFamily; - text.Foreground = InlineCodeForeground ?? Foreground; + var text = CollapseWhitespace(context, element.Text); - if (localContext.WithinItalics) + // Avoid a crash if the current inline is inside an hyperline. + // This happens when using inline code blocks like [`SomeCode`](https://www.foo.bar). + if (localContext.Parent is Hyperlink) { - text.FontStyle = FontStyle.Italic; - } + // Fallback span + Run run = new Run + { + Text = text, + FontFamily = InlineCodeFontFamily ?? FontFamily, + Foreground = InlineCodeForeground ?? Foreground + }; - if (localContext.WithinBold) - { - text.FontWeight = FontWeights.Bold; - } + // Additional formatting + if (localContext.WithinItalics) + { + run.FontStyle = FontStyle.Italic; + } - var borderthickness = InlineCodeBorderThickness; - var padding = InlineCodePadding; + if (localContext.WithinBold) + { + run.FontWeight = FontWeights.Bold; + } - var border = new Border + // Add the fallback block + localContext.InlineCollection.Add(run); + } + else { - BorderThickness = borderthickness, - BorderBrush = InlineCodeBorderBrush, - Background = InlineCodeBackground, - Child = text, - Padding = padding, - Margin = InlineCodeMargin - }; + var textBlock = CreateTextBlock(localContext); + textBlock.Text = text; + textBlock.FontFamily = InlineCodeFontFamily ?? FontFamily; + textBlock.Foreground = InlineCodeForeground ?? Foreground; - // Aligns content in InlineUI, see https://social.msdn.microsoft.com/Forums/silverlight/en-US/48b5e91e-efc5-4768-8eaf-f897849fcf0b/richtextbox-inlineuicontainer-vertical-alignment-issue?forum=silverlightarchieve - border.RenderTransform = new TranslateTransform - { - Y = 4 - }; + if (localContext.WithinItalics) + { + textBlock.FontStyle = FontStyle.Italic; + } - var inlineUIContainer = new InlineUIContainer - { - Child = border, - }; + if (localContext.WithinBold) + { + textBlock.FontWeight = FontWeights.Bold; + } - // Add it to the current inlines - localContext.InlineCollection.Add(inlineUIContainer); + var inlineUIContainer = new InlineUIContainer + { + Child = new Border + { + BorderThickness = InlineCodeBorderThickness, + BorderBrush = InlineCodeBorderBrush, + Background = InlineCodeBackground, + Child = textBlock, + Padding = InlineCodePadding, + Margin = InlineCodeMargin, + + // Aligns content in InlineUI, see https://social.msdn.microsoft.com/Forums/silverlight/en-US/48b5e91e-efc5-4768-8eaf-f897849fcf0b/richtextbox-inlineuicontainer-vertical-alignment-issue?forum=silverlightarchieve + RenderTransform = new TranslateTransform { Y = 4 } + } + }; + + // Add it to the current inlines + localContext.InlineCollection.Add(inlineUIContainer); + } } } } \ No newline at end of file