diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml
index 7173b399d..c71a51bcc 100644
--- a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml
+++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml
@@ -1,4 +1,4 @@
-
+
+ MarkdownDocument="{x:Bind Doc, Mode=OneTime}" />
diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs
index 01e3127dd..248d036e9 100644
--- a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs
+++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs
@@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
+using Markdig;
+using Markdig.Syntax;
namespace MarkdownTextBlockExperiment.Samples;
@@ -14,7 +16,7 @@ public sealed partial class MarkdownTextBlockCustomSample : Page
{
private MarkdownConfig _config;
private MarkdownConfig _liveConfig;
- private string _text;
+ private MarkdownDocument _doc;
private const string _markdown = @"
This control was originally written by [Nero Cui](https://github.com/nerocui) for [JitHub](https://github.com/JitHubApp/JitHubV2). The control is powered by the popular [Markdig](https://github.com/xoofx/markdig) markdown parsing library and *almost* supports the full markdown syntax, with a focus on super-efficient parsing and rendering.
@@ -594,10 +596,10 @@ public MarkdownConfig LiveMarkdownConfig
set => _liveConfig = value;
}
- public string Text
+ public MarkdownDocument Doc
{
- get => _text;
- set => _text = value;
+ get => _doc;
+ set => _doc = value;
}
public MarkdownTextBlockCustomSample()
@@ -605,7 +607,7 @@ public MarkdownTextBlockCustomSample()
this.InitializeComponent();
_config = new MarkdownConfig();
_liveConfig = new MarkdownConfig();
- _text = _markdown;
+ _doc = Markdown.Parse(_markdown);
MarkdownTextBox.Text = "# Hello World\n\n";
}
}
diff --git a/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs b/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
index efba3cb4b..a620d8baf 100644
--- a/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
+++ b/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
@@ -5,6 +5,7 @@
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers;
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
using Markdig;
+using Markdig.Syntax;
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
@@ -30,6 +31,12 @@ public partial class MarkdownTextBlock : Control
typeof(MarkdownTextBlock),
new PropertyMetadata(null, OnTextChanged));
+ private static readonly DependencyProperty MarkdownDocumentProperty = DependencyProperty.Register(
+ nameof(MarkdownDocument),
+ typeof(MarkdownDocument),
+ typeof(MarkdownTextBlock),
+ new PropertyMetadata(null, OnMarkdownDocumentChanged));
+
public MarkdownConfig Config
{
get => (MarkdownConfig)GetValue(ConfigProperty);
@@ -42,6 +49,12 @@ public string Text
set => SetValue(TextProperty, value);
}
+ public MarkdownDocument? MarkdownDocument
+ {
+ get => (MarkdownDocument?)GetValue(MarkdownDocumentProperty);
+ set => SetValue(MarkdownDocumentProperty, value);
+ }
+
private static void OnConfigChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
@@ -51,6 +64,14 @@ private static void OnConfigChanged(DependencyObject d, DependencyPropertyChange
}
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is MarkdownTextBlock self && e.NewValue != null)
+ {
+ self.MarkdownDocument = string.IsNullOrEmpty(self.Text) ? null : Markdown.Parse(self.Text, self._pipeline);
+ }
+ }
+
+ private static void OnMarkdownDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
{
@@ -96,10 +117,9 @@ private void ApplyText(bool rerender)
_renderer.ReloadDocument();
}
- if (!string.IsNullOrEmpty(Text))
+ if (MarkdownDocument != null)
{
- var markdown = Markdown.Parse(Text, _pipeline);
- _renderer.Render(markdown);
+ _renderer.Render(MarkdownDocument);
}
}
}