Skip to content

Commit

Permalink
Fix newlines not rendering in markdown blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 5, 2024
1 parent 289b9de commit 0aaeb9d
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions Bloxstrap/UI/Elements/Controls/MarkdownTextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace Bloxstrap.UI.Elements.Controls
[Localizability(LocalizationCategory.Text)]
class MarkdownTextBlock : TextBlock
{
private static MarkdownPipeline _markdownPipeline;
private static readonly MarkdownPipeline _markdownPipeline = new MarkdownPipelineBuilder()
.UseEmphasisExtras(Markdig.Extensions.EmphasisExtras.EmphasisExtraOptions.Marked) // enable '==' support
.UseSoftlineBreakAsHardlineBreak()
.Build();

public static readonly DependencyProperty MarkdownTextProperty =
DependencyProperty.Register(nameof(MarkdownText), typeof(string), typeof(MarkdownTextBlock),
Expand Down Expand Up @@ -70,9 +73,7 @@ public string MarkdownText
var textInline = linkInline.FirstChild;

if (string.IsNullOrEmpty(url))
{
return GetWpfInlineFromMarkdownInline(textInline);
}

var childInline = GetWpfInlineFromMarkdownInline(textInline);

Expand All @@ -82,14 +83,19 @@ public string MarkdownText
CommandParameter = url
};
}
else if (inline is LineBreakInline)
{
return new LineBreak();
}

return null;
}

private void AddMarkdownInline(Markdig.Syntax.Inlines.Inline? inline)
{
var wpfInline = GetWpfInlineFromMarkdownInline(inline);
if (wpfInline != null)

if (wpfInline is not null)
Inlines.Add(wpfInline);
}

Expand All @@ -101,26 +107,28 @@ private static void OnTextMarkdownChanged(DependencyObject dependencyObject, Dep
if (dependencyPropertyChangedEventArgs.NewValue is not string rawDocument)
return;

MarkdownDocument document = Markdown.Parse(rawDocument, _markdownPipeline);
var document = Markdown.Parse(rawDocument, _markdownPipeline);

markdownTextBlock.Inlines.Clear();

if (document.FirstOrDefault() is not ParagraphBlock paragraphBlock || paragraphBlock.Inline == null)
return;
var lastBlock = document.Last();

for (int i = 0; i < paragraphBlock.Inline.Count(); i++)
// matt was evidently very tired on the night he was first writing this
// https://github.com/pizzaboxer/bloxstrap/blob/289b9dec77cf35b2cc6504019bc9c7701626be1f/Bloxstrap/UI/Elements/Controls/MarkdownTextBlock.cs#L111
foreach (var block in document)
{
var inline = paragraphBlock.Inline.ElementAt(i);
if (block is not ParagraphBlock paragraphBlock || paragraphBlock.Inline is null)
continue;

markdownTextBlock.AddMarkdownInline(inline);
}
}
foreach (var inline in paragraphBlock.Inline)
markdownTextBlock.AddMarkdownInline(inline);

static MarkdownTextBlock()
{
_markdownPipeline = new MarkdownPipelineBuilder()
.UseEmphasisExtras(Markdig.Extensions.EmphasisExtras.EmphasisExtraOptions.Marked) // enable '==' support
.Build();
if (block != lastBlock)
{
markdownTextBlock.AddMarkdownInline(new LineBreakInline());
markdownTextBlock.AddMarkdownInline(new LineBreakInline());
}
}
}
}
}

0 comments on commit 0aaeb9d

Please sign in to comment.