Skip to content

Commit 430392c

Browse files
authored
Merge pull request #761 from CommunityToolkit/niels9001/mdtb-improvements
MarkdownTextBlock improvements
2 parents d620e8b + 2c967b4 commit 430392c

20 files changed

+1224
-53
lines changed

components/MarkdownTextBlock/samples/MarkdownTextBlock.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ See all the markdown features and syntax supported by the control:
3434
Try typing markdown and see it rendered in real-time:
3535

3636
> [!Sample MarkdownTextBlockLiveEditorSample]
37+
38+
## Custom theme sample
39+
Try different styling options for all markdown elements with a custom theme:
40+
41+
> [!Sample MarkdownTextBlockCustomThemeSample]
42+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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. -->
2+
<local:MarkdownTextBlockCustomThemeSampleBase x:Class="MarkdownTextBlockExperiment.Samples.MarkdownTextBlockCustomThemeSample"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="using:MarkdownTextBlockExperiment.Samples"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
11+
<ScrollViewer>
12+
<controls:MarkdownTextBlock x:Name="MarkdownTextBlock"
13+
Margin="12"
14+
Config="{x:Bind MarkdownConfig, Mode=OneWay}"
15+
Text="{x:Bind MarkdownText, Mode=OneWay}" />
16+
</ScrollViewer>
17+
</local:MarkdownTextBlockCustomThemeSampleBase>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.WinUI.Controls;
6+
7+
namespace MarkdownTextBlockExperiment.Samples;
8+
9+
/// <summary>
10+
/// A sample demonstrating custom theming options for the MarkdownTextBlock control with live editing.
11+
/// </summary>
12+
[ToolkitSample(id: nameof(MarkdownTextBlockCustomThemeSample), "Custom Theme", description: "A sample showcasing custom theming options with live editing for headings, code blocks, quotes, tables, and more.")]
13+
public sealed partial class MarkdownTextBlockCustomThemeSample : MarkdownTextBlockCustomThemeSampleBase
14+
{
15+
public MarkdownConfig MarkdownConfig { get; private set; }
16+
17+
public string MarkdownText { get; } = @"
18+
# Custom Theme Demo
19+
20+
This sample demonstrates the **custom theming** capabilities of the `MarkdownTextBlock` control.
21+
22+
## Heading Styles
23+
24+
Each heading level has customizable foreground color, font size, weight, and margin.
25+
26+
### Heading 3
27+
#### Heading 4
28+
29+
## Inline Code
30+
31+
Here is some `inline code` with custom styling. Try adjusting the padding, corner radius, and colors in the editor panel!
32+
33+
Another example: `config.Themes.InlineCodePadding`
34+
35+
## Images
36+
37+
Images can be styled with max width, max height, and stretch options. Notice how the text flows naturally without any gaps above or below the image:
38+
39+
![Windows Terminal](https://devblogs.microsoft.com/commandline/wp-content/uploads/sites/33/2025/11/0.96-Social-media-image-V2-1024x536.webp)
40+
41+
The image above automatically scales to respect the max width setting while maintaining its aspect ratio. Text continues to flow seamlessly below it.
42+
43+
## Code Blocks
44+
45+
```csharp
46+
// Code blocks have custom styling too!
47+
public class CustomTheme
48+
{
49+
public string Name { get; set; }
50+
public Color PrimaryColor { get; set; }
51+
52+
public void ApplyTheme()
53+
{
54+
Console.WriteLine($""Applying theme: {Name}"");
55+
}
56+
}
57+
```
58+
59+
## Block Quotes
60+
61+
> This is a styled block quote with custom background,
62+
> border color, padding, and corner radius.
63+
>
64+
> Try changing the border width and corner radius!
65+
66+
## Links
67+
68+
Check out [this link](https://github.com/CommunityToolkit) - links have custom foreground colors.
69+
70+
## Tables
71+
72+
| Feature | Status | Notes |
73+
|---------|--------|-------|
74+
| Headings | ✅ | Per-level colors |
75+
| Inline Code | ✅ | Full styling |
76+
| Code Blocks | ✅ | Background, border, font |
77+
| Quotes | ✅ | Border, background, radius |
78+
79+
---
80+
81+
## Horizontal Rule
82+
83+
The line above is a horizontal rule with customizable thickness and margin.
84+
85+
## Lists
86+
87+
Try adjusting the **Bullet Spacing** and **Gutter Width** settings to see how list formatting changes!
88+
89+
- Top level list item
90+
- Nested item level 1
91+
- Nested item level 2
92+
- Nested item level 3
93+
- Another top level item
94+
- Adjust the theme settings in the options panel
95+
- The gutter width controls how much each level is indented
96+
- The bullet spacing controls space after the bullet character
97+
- Click **Apply Changes** to see updates
98+
- Use **Reset to Defaults** to start over
99+
100+
Numbered lists work too:
101+
102+
1. First item
103+
2. Second item
104+
1. Nested numbered item
105+
2. Another nested item
106+
1. Deep nesting works
107+
3. Third item
108+
";
109+
110+
public MarkdownTextBlockCustomThemeSample()
111+
{
112+
MarkdownConfig = new MarkdownConfig { Themes = CreateThemes() };
113+
this.InitializeComponent();
114+
}
115+
116+
public override void ApplyTheme()
117+
{
118+
MarkdownConfig = new MarkdownConfig { Themes = CreateThemes() };
119+
120+
// Force re-render by toggling text
121+
MarkdownTextBlock.Config = MarkdownConfig;
122+
var text = MarkdownTextBlock.Text;
123+
MarkdownTextBlock.Text = "";
124+
MarkdownTextBlock.Text = text;
125+
}
126+
}

0 commit comments

Comments
 (0)