Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ private void Build()

// Default block renderers
_renderer.ObjectRenderers.Add(new CodeBlockRenderer());
_renderer.ObjectRenderers.Add(new ListRenderer());
_renderer.ObjectRenderers.Add(new ListRenderer());
_renderer.ObjectRenderers.Add(new ListItemRenderer());
_renderer.ObjectRenderers.Add(new HeadingRenderer());
_renderer.ObjectRenderers.Add(new ParagraphRenderer());
_renderer.ObjectRenderers.Add(new QuoteBlockRenderer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

using Markdig.Syntax;

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers.ObjectRenderers;

internal class ListItemRenderer : UWPObjectRenderer<ListItemBlock>
{
protected override void Write(WinUIRenderer renderer, ListItemBlock listItem)
{
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
if (listItem == null) throw new ArgumentNullException(nameof(listItem));

renderer.WriteChildren(listItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,73 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Markdig.Syntax;
using System.Globalization;
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
using Markdig.Syntax;
using RomanNumerals;

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers.ObjectRenderers;

internal class ListRenderer : UWPObjectRenderer<ListBlock>
{
public const string UnorderedListDot = "• ";

protected override void Write(WinUIRenderer renderer, ListBlock listBlock)
{
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
if (listBlock == null) throw new ArgumentNullException(nameof(listBlock));

var list = new MyList(listBlock);
int index = 1;
bool isOrdered = false;
BulletType bulletType = BulletType.Circle;
if (listBlock.IsOrdered)
{
isOrdered = true;
bulletType = ToOrderedBulletType(listBlock.BulletType);

renderer.Push(list);
if (listBlock.OrderedStart != null && listBlock.DefaultOrderedStart != listBlock.OrderedStart)
{
int.TryParse(listBlock.OrderedStart, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out index);
}
}

foreach (var item in listBlock)
foreach (var listItem in listBlock)
{
var listItemBlock = (ListItemBlock)item;
var listItem = new MyBlockContainer(listItemBlock);
renderer.Push(listItem);
renderer.WriteChildren(listItemBlock);
renderer.Pop();
renderer.PushListBullet(GetBulletString(isOrdered, bulletType, index));
renderer.Write(listItem);
renderer.PopListBullet();
index++;
}
}

internal static BulletType ToOrderedBulletType(char bullet)
{
return bullet switch
{
'1' => BulletType.Number,
'a' => BulletType.LowerAlpha,
'A' => BulletType.UpperAlpha,
'i' => BulletType.LowerRoman,
'I' => BulletType.UpperRoman,
_ => BulletType.Number,
};
}

renderer.Pop();
private static string GetBulletString(bool isOrdered, BulletType bulletType, int index)
{
if (isOrdered)
{
return bulletType switch
{
BulletType.Number => $"{index}. ",
BulletType.LowerAlpha => $"{index.ToAlphabetical()}. ",
BulletType.UpperAlpha => $"{index.ToAlphabetical().ToUpper(CultureInfo.CurrentCulture)}. ",
BulletType.LowerRoman => $"{index.ToRomanNumerals().ToLower(CultureInfo.CurrentCulture)} ",
BulletType.UpperRoman => $"{index.ToRomanNumerals().ToUpper(CultureInfo.CurrentCulture)} ",
BulletType.Circle => UnorderedListDot,
_ => $"{index}. "
};
}
else
{
return UnorderedListDot;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override void Write(WinUIRenderer renderer, ParagraphBlock obj)
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
if (obj == null) throw new ArgumentNullException(nameof(obj));

var paragraph = new MyParagraph(obj);
var paragraph = new MyParagraph(obj, renderer);
// set style
renderer.Push(paragraph);
renderer.WriteLeafInline(obj);
Expand Down
25 changes: 25 additions & 0 deletions components/MarkdownTextBlock/src/Renderers/WinUIRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class WinUIRenderer : RendererBase
private readonly Stack<IAddChild> _stack = new Stack<IAddChild>();
private char[] _buffer;
private MarkdownConfig _config = MarkdownConfig.Default;
private readonly Stack<string> _listBullets = new();

public MyFlowDocument FlowDocument { get; private set; }
public MarkdownConfig Config
{
Expand Down Expand Up @@ -134,6 +136,29 @@ public void WriteText(string? text, int offset, int length)
}
}

public void PushListBullet(string bullet)
{
_listBullets.Push(bullet);
}

public string PeekListBullet()
{
return _listBullets.Count > 0 ? _listBullets.Peek() : string.Empty;
}

public int GetListBulletCount()
{
return _listBullets.Count;
}

public void PopListBullet()
{
if (_listBullets.Count > 0)
{
_listBullets.Pop();
}
}

private static void AddInline(IAddChild parent, IAddChild inline)
{
parent.AddChild(inline);
Expand Down
15 changes: 15 additions & 0 deletions components/MarkdownTextBlock/src/TextElements/BulletType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;

internal enum BulletType
{
Circle,
Number,
LowerAlpha,
UpperAlpha,
LowerRoman,
UpperRoman
}
128 changes: 0 additions & 128 deletions components/MarkdownTextBlock/src/TextElements/MyList.cs

This file was deleted.

21 changes: 20 additions & 1 deletion components/MarkdownTextBlock/src/TextElements/MyParagraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers;
using Markdig.Syntax;

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;

internal class MyParagraph : IAddChild
{
private readonly WinUIRenderer _renderer;
private ParagraphBlock _paragraphBlock;
private Paragraph _paragraph;

Expand All @@ -16,10 +18,27 @@ public TextElement TextElement
get => _paragraph;
}

public MyParagraph(ParagraphBlock paragraphBlock)
public MyParagraph(ParagraphBlock paragraphBlock, WinUIRenderer renderer)
{
_paragraphBlock = paragraphBlock;
_paragraph = new Paragraph();
_renderer = renderer;

// Lists are plain Paragraph_s, one per item.
// This is so that you can select across list items.
Thickness margin = new Thickness(0, 8, 0, 8); // renderer.Config.Themes.BlockMargin;
int bulletCount = renderer.GetListBulletCount();
margin.Left += 30 * bulletCount;
_paragraph.Margin = margin;

if (bulletCount != 0)
{
string bullet = renderer.PeekListBullet();
Run bulletRun = new Run { Text = bullet + "\t" };

_paragraph.Inlines.Add(bulletRun);
_paragraph.TextIndent = -30;
}
}

public void AddChild(IAddChild child)
Expand Down
Loading