Skip to content

Commit 6131f9a

Browse files
author
Juan Osorio
committed
MarkdownTextBlock: Allows passing an existing MarkdownDocument as the content, closes CommunityToolkit#596
1 parent 05709d0 commit 6131f9a

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Diff for: components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="MarkdownTextBlockExperiment.Samples.MarkdownTextBlockCustomSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -39,6 +39,6 @@
3939
Text="Built-in Sample" />
4040
<controls:MarkdownTextBlock Grid.Row="3"
4141
Config="{x:Bind MarkdownConfig, Mode=OneTime}"
42-
Text="{x:Bind Text, Mode=OneTime}" />
42+
MarkdownDocument="{x:Bind Doc, Mode=OneTime}" />
4343
</Grid>
4444
</Page>

Diff for: components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
6+
using Markdig;
7+
using Markdig.Syntax;
68

79
namespace MarkdownTextBlockExperiment.Samples;
810

@@ -14,7 +16,7 @@ public sealed partial class MarkdownTextBlockCustomSample : Page
1416
{
1517
private MarkdownConfig _config;
1618
private MarkdownConfig _liveConfig;
17-
private string _text;
19+
private MarkdownDocument _doc;
1820
private const string _markdown = @"
1921
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.
2022
@@ -594,18 +596,18 @@ public MarkdownConfig LiveMarkdownConfig
594596
set => _liveConfig = value;
595597
}
596598

597-
public string Text
599+
public MarkdownDocument Doc
598600
{
599-
get => _text;
600-
set => _text = value;
601+
get => _doc;
602+
set => _doc = value;
601603
}
602604

603605
public MarkdownTextBlockCustomSample()
604606
{
605607
this.InitializeComponent();
606608
_config = new MarkdownConfig();
607609
_liveConfig = new MarkdownConfig();
608-
_text = _markdown;
610+
_doc = Markdown.Parse(_markdown);
609611
MarkdownTextBox.Text = "# Hello World\n\n";
610612
}
611613
}

Diff for: components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers;
66
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
77
using Markdig;
8+
using Markdig.Syntax;
89

910
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
1011

@@ -30,6 +31,12 @@ public partial class MarkdownTextBlock : Control
3031
typeof(MarkdownTextBlock),
3132
new PropertyMetadata(null, OnTextChanged));
3233

34+
private static readonly DependencyProperty MarkdownDocumentProperty = DependencyProperty.Register(
35+
nameof(MarkdownDocument),
36+
typeof(MarkdownDocument),
37+
typeof(MarkdownTextBlock),
38+
new PropertyMetadata(null, OnMarkdownDocumentChanged));
39+
3340
public MarkdownConfig Config
3441
{
3542
get => (MarkdownConfig)GetValue(ConfigProperty);
@@ -42,6 +49,12 @@ public string Text
4249
set => SetValue(TextProperty, value);
4350
}
4451

52+
public MarkdownDocument? MarkdownDocument
53+
{
54+
get => (MarkdownDocument?)GetValue(MarkdownDocumentProperty);
55+
set => SetValue(MarkdownDocumentProperty, value);
56+
}
57+
4558
private static void OnConfigChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
4659
{
4760
if (d is MarkdownTextBlock self && e.NewValue != null)
@@ -51,6 +64,14 @@ private static void OnConfigChanged(DependencyObject d, DependencyPropertyChange
5164
}
5265

5366
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
67+
{
68+
if (d is MarkdownTextBlock self && e.NewValue != null)
69+
{
70+
self.MarkdownDocument = string.IsNullOrEmpty(self.Text) ? null : Markdown.Parse(self.Text, self._pipeline);
71+
}
72+
}
73+
74+
private static void OnMarkdownDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
5475
{
5576
if (d is MarkdownTextBlock self && e.NewValue != null)
5677
{
@@ -96,10 +117,9 @@ private void ApplyText(bool rerender)
96117
_renderer.ReloadDocument();
97118
}
98119

99-
if (!string.IsNullOrEmpty(Text))
120+
if (MarkdownDocument != null)
100121
{
101-
var markdown = Markdown.Parse(Text, _pipeline);
102-
_renderer.Render(markdown);
122+
_renderer.Render(MarkdownDocument);
103123
}
104124
}
105125
}

0 commit comments

Comments
 (0)