Skip to content

Commit a294b85

Browse files
Merge pull request #3407 from Sergio0694/bugfix/inline-code-url
Fixed rendering of markdown links with inline code
2 parents a47305c + d4b6f88 commit a294b85

File tree

2 files changed

+59
-35
lines changed

2 files changed

+59
-35
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/MarkdownTextBlock.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@
4444
<Setter Property="CodeBackground" Value="{ThemeResource MarkdownBackgroundBrush}" />
4545
<Setter Property="CodeBorderBrush" Value="{ThemeResource MarkdownBorderBrush}" />
4646
<Setter Property="CodeBorderThickness" Value="0" />
47+
<Setter Property="CodeFontFamily" Value="Consolas" />
48+
<Setter Property="CodeMargin" Value="0, 7, 0, 7" />
49+
<Setter Property="CodePadding" Value="10, 6, 10, 6" />
4750
<Setter Property="InlineCodeBorderThickness" Value="0" />
4851
<Setter Property="InlineCodePadding" Value="4, 2, 4, 2" />
4952
<Setter Property="InlineCodeMargin" Value="2,0,2,-4"/>
5053
<Setter Property="InlineCodeBackground" Value="{ThemeResource MarkdownInlineCodeBackgroundBrush}" />
5154
<Setter Property="InlineCodeBorderBrush" Value="{ThemeResource MarkdownBorderBrush}" />
5255
<Setter Property="InlineCodeForeground" Value="{ThemeResource MarkdownInlineCodeForegroundBrush}" />
53-
<Setter Property="CodeFontFamily" Value="Consolas" />
54-
<Setter Property="CodeMargin" Value="0, 7, 0, 7" />
55-
<Setter Property="CodePadding" Value="10, 6, 10, 6" />
56+
<Setter Property="InlineCodeFontFamily" Value="Consolas" />
5657
<Setter Property="EmojiFontFamily" Value="Segoe UI Emoji" />
5758
<Setter Property="Header1FontWeight" Value="Bold" />
5859
<Setter Property="Header1FontSize" Value="20" />

Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -539,47 +539,70 @@ protected override void RenderCodeRun(CodeInline element, IRenderContext context
539539
throw new RenderContextIncorrectException();
540540
}
541541

542-
var text = CreateTextBlock(localContext);
543-
text.Text = CollapseWhitespace(context, element.Text);
544-
text.FontFamily = InlineCodeFontFamily ?? FontFamily;
545-
text.Foreground = InlineCodeForeground ?? Foreground;
542+
var text = CollapseWhitespace(context, element.Text);
546543

547-
if (localContext.WithinItalics)
544+
// Avoid a crash if the current inline is inside an hyperline.
545+
// This happens when using inline code blocks like [`SomeCode`](https://www.foo.bar).
546+
if (localContext.Parent is Hyperlink)
548547
{
549-
text.FontStyle = FontStyle.Italic;
550-
}
548+
// Fallback span
549+
Run run = new Run
550+
{
551+
Text = text,
552+
FontFamily = InlineCodeFontFamily ?? FontFamily,
553+
Foreground = InlineCodeForeground ?? Foreground
554+
};
551555

552-
if (localContext.WithinBold)
553-
{
554-
text.FontWeight = FontWeights.Bold;
555-
}
556+
// Additional formatting
557+
if (localContext.WithinItalics)
558+
{
559+
run.FontStyle = FontStyle.Italic;
560+
}
556561

557-
var borderthickness = InlineCodeBorderThickness;
558-
var padding = InlineCodePadding;
562+
if (localContext.WithinBold)
563+
{
564+
run.FontWeight = FontWeights.Bold;
565+
}
559566

560-
var border = new Border
567+
// Add the fallback block
568+
localContext.InlineCollection.Add(run);
569+
}
570+
else
561571
{
562-
BorderThickness = borderthickness,
563-
BorderBrush = InlineCodeBorderBrush,
564-
Background = InlineCodeBackground,
565-
Child = text,
566-
Padding = padding,
567-
Margin = InlineCodeMargin
568-
};
572+
var textBlock = CreateTextBlock(localContext);
573+
textBlock.Text = text;
574+
textBlock.FontFamily = InlineCodeFontFamily ?? FontFamily;
575+
textBlock.Foreground = InlineCodeForeground ?? Foreground;
569576

570-
// 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
571-
border.RenderTransform = new TranslateTransform
572-
{
573-
Y = 4
574-
};
577+
if (localContext.WithinItalics)
578+
{
579+
textBlock.FontStyle = FontStyle.Italic;
580+
}
575581

576-
var inlineUIContainer = new InlineUIContainer
577-
{
578-
Child = border,
579-
};
582+
if (localContext.WithinBold)
583+
{
584+
textBlock.FontWeight = FontWeights.Bold;
585+
}
580586

581-
// Add it to the current inlines
582-
localContext.InlineCollection.Add(inlineUIContainer);
587+
var inlineUIContainer = new InlineUIContainer
588+
{
589+
Child = new Border
590+
{
591+
BorderThickness = InlineCodeBorderThickness,
592+
BorderBrush = InlineCodeBorderBrush,
593+
Background = InlineCodeBackground,
594+
Child = textBlock,
595+
Padding = InlineCodePadding,
596+
Margin = InlineCodeMargin,
597+
598+
// 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
599+
RenderTransform = new TranslateTransform { Y = 4 }
600+
}
601+
};
602+
603+
// Add it to the current inlines
604+
localContext.InlineCollection.Add(inlineUIContainer);
605+
}
583606
}
584607
}
585608
}

0 commit comments

Comments
 (0)