Skip to content

Commit ca29f2e

Browse files
committed
Add property to disable links
1 parent a425dfc commit ca29f2e

File tree

3 files changed

+147
-128
lines changed

3 files changed

+147
-128
lines changed

components/MarkdownTextBlock/src/MarkdownTextBlock.Properties.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public partial class MarkdownTextBlock
8080
typeof(bool),
8181
typeof(MarkdownTextBlock),
8282
new PropertyMetadata(false));
83+
84+
/// <summary>
85+
/// Identifies the <see cref="DisableLinksProperty"/> dependency property.
86+
/// </summary>
87+
private static readonly DependencyProperty DisableLinksProperty = DependencyProperty.Register(
88+
nameof(DisableLinksProperty),
89+
typeof(bool),
90+
typeof(MarkdownTextBlock),
91+
new PropertyMetadata(false));
8392

8493
/// <summary>
8594
/// Identifies the <see cref="UseSoftlineBreakAsHardlineBreak"/> dependency property.
@@ -167,6 +176,15 @@ public bool DisableHtml
167176
get => (bool)GetValue(DisableHtmlProperty);
168177
set => SetValue(DisableHtmlProperty, value);
169178
}
179+
180+
/// <summary>
181+
/// If true, Disables link parsing.
182+
/// </summary>
183+
public bool DisableLinks
184+
{
185+
get => (bool)GetValue(DisableLinksProperty);
186+
set => SetValue(DisableLinksProperty, value);
187+
}
170188

171189
/// <summary>
172190
/// If true, considers single newlines as hardline breaks.

components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ private void Build()
120120
_renderer.ObjectRenderers.Add(new EmphasisInlineRenderer());
121121
if (!DisableHtml) _renderer.ObjectRenderers.Add(new HtmlEntityInlineRenderer());
122122
_renderer.ObjectRenderers.Add(new LineBreakInlineRenderer());
123-
_renderer.ObjectRenderers.Add(new LinkInlineRenderer());
123+
if (!DisableLinks) _renderer.ObjectRenderers.Add(new LinkInlineRenderer());
124124
_renderer.ObjectRenderers.Add(new LiteralInlineRenderer());
125-
_renderer.ObjectRenderers.Add(new ContainerInlineRenderer());
125+
if (!DisableLinks) _renderer.ObjectRenderers.Add(new ContainerInlineRenderer());
126126

127127
// Extension renderers
128128
if (UsePipeTables) _renderer.ObjectRenderers.Add(new TableRenderer());
Lines changed: 127 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,127 @@
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 Markdig.Syntax;
6-
using RomanNumerals;
7-
using System.Globalization;
8-
9-
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
10-
11-
internal class MyList : IAddChild
12-
{
13-
private Paragraph _paragraph;
14-
private InlineUIContainer _container;
15-
private StackPanel _stackPanel;
16-
private ListBlock _listBlock;
17-
private BulletType _bulletType;
18-
private bool _isOrdered;
19-
private int _startIndex = 1;
20-
private int _index = 1;
21-
private const string _dot = "• ";
22-
23-
public TextElement TextElement
24-
{
25-
get => _paragraph;
26-
}
27-
28-
public MyList(ListBlock listBlock)
29-
{
30-
_paragraph = new Paragraph();
31-
_container = new InlineUIContainer();
32-
_stackPanel = new StackPanel();
33-
_listBlock = listBlock;
34-
35-
if (listBlock.IsOrdered)
36-
{
37-
_isOrdered = true;
38-
_bulletType = ToBulletType(listBlock.BulletType);
39-
40-
if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart))
41-
{
42-
_startIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo);
43-
_index = _startIndex;
44-
}
45-
}
46-
47-
_stackPanel.Orientation = Orientation.Vertical;
48-
_stackPanel.Margin = new Thickness(left: 0, top: 8, right: 0, bottom: 8);
49-
_container.Child = _stackPanel;
50-
_paragraph.Inlines.Add(_container);
51-
}
52-
53-
public void AddChild(IAddChild child)
54-
{
55-
var grid = new Grid();
56-
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Pixel) });
57-
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Pixel) });
58-
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
59-
string bullet;
60-
if (_isOrdered)
61-
{
62-
bullet = _bulletType switch
63-
{
64-
BulletType.Number => $"{_index}. ",
65-
BulletType.LowerAlpha => $"{_index.ToAlphabetical()}. ",
66-
BulletType.UpperAlpha => $"{_index.ToAlphabetical().ToUpper()}. ",
67-
BulletType.LowerRoman => $"{_index.ToRomanNumerals().ToLower()} ",
68-
BulletType.UpperRoman => $"{_index.ToRomanNumerals().ToUpper()} ",
69-
BulletType.Circle => _dot,
70-
_ => _dot
71-
};
72-
_index++;
73-
}
74-
else
75-
{
76-
bullet = _dot;
77-
}
78-
var textBlock = new TextBlock()
79-
{
80-
Text = bullet,
81-
};
82-
textBlock.SetValue(Grid.ColumnProperty, 0);
83-
textBlock.VerticalAlignment = VerticalAlignment.Top;
84-
textBlock.TextAlignment = TextAlignment.Right;
85-
grid.Children.Add(textBlock);
86-
var flowDoc = new MyFlowDocument();
87-
flowDoc.AddChild(child);
88-
89-
flowDoc.RichTextBlock.SetValue(Grid.ColumnProperty, 2);
90-
flowDoc.RichTextBlock.Padding = new Thickness(0);
91-
flowDoc.RichTextBlock.VerticalAlignment = VerticalAlignment.Top;
92-
grid.Children.Add(flowDoc.RichTextBlock);
93-
94-
_stackPanel.Children.Add(grid);
95-
}
96-
97-
private BulletType ToBulletType(char bullet)
98-
{
99-
// Gets or sets the type of the bullet (e.g: '1', 'a', 'A', 'i', 'I').
100-
switch (bullet)
101-
{
102-
case '1':
103-
return BulletType.Number;
104-
case 'a':
105-
return BulletType.LowerAlpha;
106-
case 'A':
107-
return BulletType.UpperAlpha;
108-
case 'i':
109-
return BulletType.LowerRoman;
110-
case 'I':
111-
return BulletType.UpperRoman;
112-
default:
113-
return BulletType.Circle;
114-
}
115-
}
116-
}
117-
118-
internal enum BulletType
119-
{
120-
Circle,
121-
Number,
122-
LowerAlpha,
123-
UpperAlpha,
124-
LowerRoman,
125-
UpperRoman
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 Markdig.Syntax;
6+
using RomanNumerals;
7+
using System.Globalization;
8+
9+
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
10+
11+
internal class MyList : IAddChild
12+
{
13+
private Paragraph _paragraph;
14+
private InlineUIContainer _container;
15+
private StackPanel _stackPanel;
16+
private ListBlock _listBlock;
17+
private BulletType _bulletType;
18+
private bool _isOrdered;
19+
private int _startIndex = 1;
20+
private int _index = 1;
21+
private const string _dot = "• ";
22+
23+
public TextElement TextElement
24+
{
25+
get => _paragraph;
26+
}
27+
28+
public MyList(ListBlock listBlock)
29+
{
30+
_paragraph = new Paragraph();
31+
_container = new InlineUIContainer();
32+
_stackPanel = new StackPanel();
33+
_listBlock = listBlock;
34+
35+
if (listBlock.IsOrdered)
36+
{
37+
_isOrdered = true;
38+
_bulletType = ToBulletType(listBlock.BulletType);
39+
40+
if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart))
41+
{
42+
_startIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo);
43+
_index = _startIndex;
44+
}
45+
}
46+
47+
_stackPanel.Orientation = Orientation.Vertical;
48+
_stackPanel.Margin = new Thickness(left: 0, top: 8, right: 0, bottom: 8);
49+
_container.Child = _stackPanel;
50+
_paragraph.Inlines.Add(_container);
51+
}
52+
53+
public void AddChild(IAddChild child)
54+
{
55+
var grid = new Grid();
56+
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Pixel) });
57+
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Pixel) });
58+
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
59+
string bullet;
60+
if (_isOrdered)
61+
{
62+
bullet = _bulletType switch
63+
{
64+
BulletType.Number => $"{_index}. ",
65+
BulletType.LowerAlpha => $"{_index.ToAlphabetical()}. ",
66+
BulletType.UpperAlpha => $"{_index.ToAlphabetical().ToUpper()}. ",
67+
BulletType.LowerRoman => $"{_index.ToRomanNumerals().ToLower()} ",
68+
BulletType.UpperRoman => $"{_index.ToRomanNumerals().ToUpper()} ",
69+
BulletType.Circle => _dot,
70+
_ => _dot
71+
};
72+
_index++;
73+
}
74+
else
75+
{
76+
bullet = _dot;
77+
}
78+
var textBlock = new TextBlock()
79+
{
80+
Text = bullet,
81+
};
82+
textBlock.SetValue(Grid.ColumnProperty, 0);
83+
textBlock.VerticalAlignment = VerticalAlignment.Top;
84+
textBlock.TextAlignment = TextAlignment.Right;
85+
AutomationProperties.SetAccessibilityView(textBlock, AccessibilityView.Raw);
86+
grid.Children.Add(textBlock);
87+
var flowDoc = new MyFlowDocument();
88+
flowDoc.AddChild(child);
89+
90+
flowDoc.RichTextBlock.SetValue(Grid.ColumnProperty, 2);
91+
flowDoc.RichTextBlock.Padding = new Thickness(0);
92+
flowDoc.RichTextBlock.VerticalAlignment = VerticalAlignment.Top;
93+
grid.Children.Add(flowDoc.RichTextBlock);
94+
95+
_stackPanel.Children.Add(grid);
96+
}
97+
98+
private BulletType ToBulletType(char bullet)
99+
{
100+
// Gets or sets the type of the bullet (e.g: '1', 'a', 'A', 'i', 'I').
101+
switch (bullet)
102+
{
103+
case '1':
104+
return BulletType.Number;
105+
case 'a':
106+
return BulletType.LowerAlpha;
107+
case 'A':
108+
return BulletType.UpperAlpha;
109+
case 'i':
110+
return BulletType.LowerRoman;
111+
case 'I':
112+
return BulletType.UpperRoman;
113+
default:
114+
return BulletType.Circle;
115+
}
116+
}
117+
}
118+
119+
internal enum BulletType
120+
{
121+
Circle,
122+
Number,
123+
LowerAlpha,
124+
UpperAlpha,
125+
LowerRoman,
126+
UpperRoman
127+
}

0 commit comments

Comments
 (0)