diff --git a/Wiki/MarkdownRenderer.cs b/Wiki/MarkdownRenderer.cs index 06814fb..6b20aa0 100644 --- a/Wiki/MarkdownRenderer.cs +++ b/Wiki/MarkdownRenderer.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Media; @@ -12,118 +13,128 @@ public static class MarkdownRenderer { public static Control Render(string markdown) { - var panel = new StackPanel { Spacing = 6 }; - if (string.IsNullOrWhiteSpace(markdown)) return panel; - - var doc = Markdown.Parse(markdown); - - foreach (var block in doc) + try { - switch (block) + var panel = new StackPanel { Spacing = 6 }; + if (string.IsNullOrWhiteSpace(markdown)) return panel; + + var doc = Markdown.Parse(markdown); + + foreach (var block in doc) { - case HeadingBlock hb: + switch (block) { - var text = GetInlineText(hb.Inline); - var tb = new TextBlock + case HeadingBlock hb: { - Text = text, - FontWeight = FontWeight.Bold, - Margin = new Avalonia.Thickness(0, hb.Level == 1 ? 6 : 2, 0, 2) - }; - tb.FontSize = hb.Level switch { 1 => 22, 2 => 18, 3 => 16, _ => 14 }; - panel.Children.Add(tb); - break; - } - - case ParagraphBlock pb: - { - var text = GetInlineText(pb.Inline); - var tb = new TextBlock { Text = text, TextWrapping = Avalonia.Media.TextWrapping.Wrap }; - panel.Children.Add(tb); - break; - } - - case FencedCodeBlock cb: - { - var sb = new StringBuilder(); - foreach (var line in cb.Lines.Lines) - { - sb.Append(line.ToString()); - } - var codeBox = new TextBox - { - Text = sb.ToString(), - FontFamily = "Consolas, monospace", - IsReadOnly = true, - AcceptsReturn = true - }; - panel.Children.Add(codeBox); - break; - } - - case ListBlock lb: - { - var sp = new StackPanel { Spacing = 2 }; - var number = 1; - foreach (var item in lb) - { - if (item is ListItemBlock lib) + var text = GetInlineText(hb.Inline); + var tb = new TextBlock { - var itemText = new StringBuilder(); - foreach (var sub in lib) - { - if (sub is ParagraphBlock pp) - itemText.Append(GetInlineText(pp.Inline)); - } - var tb = new TextBlock { Text = (lb.IsOrdered ? (number++ + ". ") : "• ") + itemText.ToString() }; - sp.Children.Add(tb); - } + Text = text, + FontWeight = FontWeight.Bold, + Margin = new Avalonia.Thickness(0, hb.Level == 1 ? 6 : 2, 0, 2) + }; + tb.FontSize = hb.Level switch { 1 => 22, 2 => 18, 3 => 16, _ => 14 }; + panel.Children.Add(tb); + break; + } + + case ParagraphBlock pb: + { + var text = GetInlineText(pb.Inline); + var tb = new TextBlock { Text = text, TextWrapping = Avalonia.Media.TextWrapping.Wrap }; + panel.Children.Add(tb); + break; + } + + case FencedCodeBlock cb: + { + var sb = new StringBuilder(); + foreach (var line in cb.Lines.Lines) + { + sb.Append(line.ToString()); + } + var codeBox = new TextBox + { + Text = sb.ToString(), + FontFamily = "Consolas, monospace", + IsReadOnly = true, + AcceptsReturn = true + }; + panel.Children.Add(codeBox); + break; + } + + case ListBlock lb: + { + var sp = new StackPanel { Spacing = 2 }; + var number = 1; + foreach (var item in lb) + { + if (item is ListItemBlock lib) + { + var itemText = new StringBuilder(); + foreach (var sub in lib) + { + if (sub is ParagraphBlock pp) + itemText.Append(GetInlineText(pp.Inline)); + } + var tb = new TextBlock { Text = (lb.IsOrdered ? (number++ + ". ") : "• ") + itemText.ToString() }; + sp.Children.Add(tb); + } + } + panel.Children.Add(sp); + break; + } + + default: + { + // fallback: raw text + panel.Children.Add(new TextBlock { Text = block.ToString() }); + break; } - panel.Children.Add(sp); - break; - } - - default: - { - // fallback: raw text - panel.Children.Add(new TextBlock { Text = block.ToString() }); - break; } } - } + + return panel; + } catch (Exception ex) { Logger.Log($"Error while : {ex.Message}",Logger.LogType.Error);} - return panel; + return new Panel(); } private static string GetInlineText(ContainerInline? container) { - if (container == null) return string.Empty; - var sb = new StringBuilder(); - foreach (var inline in container) + try { - switch (inline) + if (container == null) return string.Empty; + var sb = new StringBuilder(); + foreach (var inline in container) { - case LiteralInline li: - sb.Append(li.Content.ToString()); - break; - case EmphasisInline ei: - sb.Append(GetInlineText(ei)); - break; - case CodeInline ci: - sb.Append(ci.Content); - break; - case LinkInline li: - sb.Append(GetInlineText(li)); - break; - case LineBreakInline: - sb.Append("\n"); - break; - default: - sb.Append(inline.ToString()); - break; + switch (inline) + { + case LiteralInline li: + sb.Append(li.Content.ToString()); + break; + case EmphasisInline ei: + sb.Append(GetInlineText(ei)); + break; + case CodeInline ci: + sb.Append(ci.Content); + break; + case LinkInline li: + sb.Append(GetInlineText(li)); + break; + case LineBreakInline: + sb.Append("\n"); + break; + default: + sb.Append(inline.ToString()); + break; + } } - } - return sb.ToString(); + return sb.ToString(); + } catch (Exception ex) { Logger.Log($"Error while : {ex.Message}",Logger.LogType.Error);} + + return null; } }