/// <summary> /// Renders a code element. /// </summary> protected override void RenderCode(CodeBlock element, IRenderContext context) { if (!(context is BlockCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockCollection = localContext.BlockCollection; var brush = localContext.Foreground; if (CodeForeground != null && !localContext.OverrideForeground) { brush = CodeForeground; } var paragraph = new Paragraph { FontFamily = CodeFontFamily ?? FontFamily, Foreground = brush, LineHeight = FontSize * 1.4, Background = CodeBackground, BorderBrush = CodeBorderBrush, BorderThickness = CodeBorderThickness, Padding = CodePadding, Margin = CodeMargin }; var hasCustomSyntax = CodeBlockResolver.ParseSyntax(paragraph.Inlines, element.Text, element.CodeLanguage); if (!hasCustomSyntax) { paragraph.Inlines.Add(new Run { Text = element.Text }); } blockCollection.Add(paragraph); }
/// <summary> /// Renders a code element. /// </summary> protected override void RenderCode(CodeBlock element, IRenderContext context) { if (!(context is UIElementCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockUIElementCollection = localContext.BlockUIElementCollection; var brush = localContext.Foreground; if (CodeForeground != null && !localContext.OverrideForeground) { brush = CodeForeground; } var textBlock = new RichTextBlock { FontFamily = CodeFontFamily ?? FontFamily, Foreground = brush, LineHeight = FontSize * 1.4, FlowDirection = FlowDirection }; textBlock.PointerWheelChanged += Preventative_PointerWheelChanged; var paragraph = new Paragraph(); textBlock.Blocks.Add(paragraph); // Allows external Syntax Highlighting var hasCustomSyntax = CodeBlockResolver.ParseSyntax(paragraph.Inlines, element.Text, element.CodeLanguage); if (!hasCustomSyntax) { paragraph.Inlines.Add(new Run { Text = element.Text }); } // Ensures that Code has Horizontal Scroll and doesn't wrap. var viewer = new ScrollViewer { Background = CodeBackground, BorderBrush = CodeBorderBrush, BorderThickness = CodeBorderThickness, Padding = CodePadding, Margin = CodeMargin, Content = textBlock }; if (!WrapCodeBlock) { viewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; viewer.HorizontalScrollMode = ScrollMode.Auto; viewer.VerticalScrollMode = ScrollMode.Disabled; viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; } // Add it to the blocks blockUIElementCollection.Add(viewer); }