Labs-Windows icon indicating copy to clipboard operation
Labs-Windows copied to clipboard

[MarkdownTextBlock] Support Extensions

Open nerocui opened this issue 1 year ago • 1 comments

Proposal: Extensions

The underlying parser Markdig supports extensions. Some of these come bundled with that package, such as Pipe Tables and Diagrams. Others can be added with extra code.

Simple properties for built-in extensions

<md:MarkdownTextBlock UseEmphasisExtras="True" UseAutoLinks="True" Text="..." />

We can ship with a few UseFoo properties that allow to configure the built-in extensions that end up both in the MarkdownPipeline and the Renderer. This makes it super easy to control which Markdown extensions get enabled without having to mess around with a MarkdownPipeline or any C# code.

Custom extensions

Declaration in XAML:

<md:MarkdownTextBlock Text="...">
  <md:MarkdownTextBlock.CustomElements>
    <foo:CustomElement />
  </md:MarkdownTextBlock.CustomElements>
</md:MarkdownTextBlock>

And C#, implementing a new interface we ship IElement:

class CustomElement : IElement<CustomObject>
{
  // Parsing
  public void AddToPipeline(MarkdownPipelineBuilder pipelineBuilder)
  {
    pipelineBuilder.Extensions.Add(...);
  }

  // Rendering
  public UWPObjectRenderer<CustomObject> GetObjectRenderer() => new CustomRenderer();
}

With this approach, we can allow users to add arbitrary extensions on top of the control in a very simple and declarative manner. A quick glance at the XAML tells you all the built-in and custom extensions currently being used.

Originally posted by @jcoc611-microsoft in https://github.com/CommunityToolkit/Labs-Windows/discussions/536#discussioncomment-11309494

nerocui avatar Nov 25 '24 22:11 nerocui