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

🧪 [Experiment] MarkdownTextBlock

Open nerocui opened this issue 1 year ago • 4 comments

Approved from Discussion

https://github.com/CommunityToolkit/Labs-Windows/discussions/536

Problem Statement

Let's use this issue to track all improvements we are making to MardownTextBlock

Overview

This experiment adds the following components:

  • MarkdownTextBlock a markdown control that uses the popular markdig parser for syntax parsing

Using

You can try it out via the NuGet Packages here:

  • UWP: https://dev.azure.com/dotnet/CommunityToolkit/_artifacts/feed/CommunityToolkit-Labs/NuGet/CommunityToolkit.Labs.Uwp.Controls.MarkdownTextBlock/overview/0.1.241113-build.1784
  • WinUI 3: https://dev.azure.com/dotnet/CommunityToolkit/_artifacts/feed/CommunityToolkit-Labs/NuGet/CommunityToolkit.Labs.WinUI.Controls.MarkdownTextBlock/overview/0.1.241113-build.1784 Read more about Preview Packages here.

Additional info

No response

Implementation Requirements

  • [ ] Working Prototype
  • [x] Feature Complete
  • [x] Documentation
  • [x] Samples
  • [x] Unit Tests
  • [x] Community Feedback / Usage Testimonies

Tested Platforms

  • [x] UWP
  • [x] WinAppSDK / WinUI 3
  • [ ] Web Assembly (WASM)
  • [ ] Android
  • [ ] iOS
  • [ ] MacOS
  • [ ] Linux / GTK

Technical Review

  • [ ] Accessibility Audit
  • [ ] API/Naming Review
  • [ ] Code Quality/Style
  • [ ] Dependency Review
  • [ ] Design/Style Review
  • [ ] Final Approval

Community Help?

Yes

nerocui avatar Nov 25 '24 20:11 nerocui

If you don't set the MarkdownConfig should it be set to MarkdownConfig.Default by default? Right now, if you don't set MarkdownConfig it doesn't render. If that's the expected behavior, then you can ignore this.

rstewa avatar Nov 29 '24 01:11 rstewa

If you don't set the MarkdownConfig should it be set to MarkdownConfig.Default by default? Right now, if you don't set MarkdownConfig it doesn't render. If that's the expected behavior, then you can ignore this.

Yeah we'll address that in #611. Essentially no property should be "required" to just render some text.

nerocui avatar Nov 29 '24 01:11 nerocui

MarkdownTextBlock throws an exception with a particular Markdown example:

``` Will it ~blend~ crash? ```

Full Stack:

System.NullReferenceException: Object reference not set to an instance of an object.
   at CommunityToolkit.WinUI.Controls.TextElements.MyCodeBlock..ctor(CodeBlock codeBlock, MarkdownConfig config)
   at CommunityToolkit.WinUI.Controls.Renderers.ObjectRenderers.CodeBlockRenderer.Write(WinUIRenderer renderer, CodeBlock obj)
   at Markdig.Renderers.MarkdownObjectRenderer`2.Write(RendererBase renderer, MarkdownObject obj)
   at Markdig.Renderers.RendererBase.Write(MarkdownObject obj)
   at Markdig.Renderers.RendererBase.WriteChildren(ContainerBlock containerBlock)
   at Markdig.Renderers.RendererBase.Write(MarkdownObject obj)
   at CommunityToolkit.WinUI.Controls.Renderers.WinUIRenderer.Render(MarkdownObject markdownObject)
   at CommunityToolkit.WinUI.Controls.MarkdownTextBlock.ApplyText(Boolean rerender)
   at CommunityToolkit.WinUI.Controls.MarkdownTextBlock.OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at ABI.Microsoft.UI.Xaml.PropertyChangedCallback.Do_Abi_Invoke(IntPtr thisPtr, IntPtr d, IntPtr e)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IDependencyObjectMethods.SetValue(IObjectReference _obj, DependencyProperty dp, Object value)
   at CommunityToolkit.WinUI.Controls.MarkdownTextBlock.set_Text(String value)
   at Microsoft.CmdPal.UI.Pages.ShellPage.XamlBindingSetters.Set_CommunityToolkit_WinUI_Controls_MarkdownTextBlock_Text(MarkdownTextBlock obj, String value, String targetNullValue) in X:\source\jp-PowerToys\src\modules\cmdpal\Microsoft.CmdPal.UI\obj\x64\Debug\Pages\ShellPage.g.cs:line 103
   at Microsoft.CmdPal.UI.Pages.ShellPage.ShellPage_obj1_Bindings.Update_ViewModel_Details_Body(String obj, Int32 phase) in X:\source\jp-PowerToys\src\modules\cmdpal\Microsoft.CmdPal.UI\obj\x64\Debug\Pages\ShellPage.g.cs:line 3024
   at Microsoft.CmdPal.UI.Pages.ShellPage.ShellPage_obj1_Bindings.Update_ViewModel_Details(DetailsViewModel obj, Int32 phase) in X:\source\jp-PowerToys\src\modules\cmdpal\Microsoft.CmdPal.UI\obj\x64\Debug\Pages\ShellPage.g.cs:line 2966
   at Microsoft.CmdPal.UI.Pages.ShellPage.ShellPage_obj1_Bindings.ShellPage_obj1_BindingsTracking.PropertyChanged_ViewModel(Object sender, PropertyChangedEventArgs e) in X:\source\jp-PowerToys\src\modules\cmdpal\Microsoft.CmdPal.UI\obj\x64\Debug\Pages\ShellPage.g.cs:line 3237
   at Microsoft.CmdPal.Core.ViewModels.ShellViewModel.set_Details(DetailsViewModel value) in X:\source\jp-PowerToys\src\modules\cmdpal\Core\Microsoft.CmdPal.Core.ViewModels\obj\g\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\Microsoft.CmdPal.Core.ViewModels.ShellViewModel.g.cs:line 46
   at Microsoft.CmdPal.UI.Pages.ShellPage.<>c__DisplayClass24_0.<Receive>b__1() in X:\source\jp-PowerToys\src\modules\cmdpal\Microsoft.CmdPal.UI\Pages\ShellPage.xaml.cs:line 284
   at CommunityToolkit.WinUI.DispatcherQueueTimerExtensions.Timer_Tick(Object sender, Object e)
   at WinRT._EventSource_global__Windows_Foundation_TypedEventHandler_global__Microsoft_UI_Dispatching_DispatcherQueueTimer__object_.EventState.<GetEventInvoke>b__1_0(DispatcherQueueTimer sender, Object args)
   at WinRT.GenericTypeInstantiations.Windows_Foundation_TypedEventHandler_2_Microsoft_UI_Dispatching_DispatcherQueueTimer__object.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr args)

Originally reported in PowerToys: https://github.com/microsoft/PowerToys/issues/42142#issue-3475142631

samrueby avatar Oct 02 '25 13:10 samrueby

Details to the issue above: ^

codeBlock.Lines.Lines in MyCodeBlock.cs:78 is null.

https://github.com/CommunityToolkit/Labs-Windows/blob/c19046bdacd550a7607690c2a79b86ababb5a48a/components/MarkdownTextBlock/src/TextElements/MyCodeBlock.cs?plain=1#L78-L87

CommunityToolkit.Labs.WinUI.Controls.MarkdownTextBlock, version 0.1.250910-build.2249 reproducible on main c19046b

Easily reproducible in Labs sample app, by entering MD code block quotes ``` into the MarkdownTextBlock sample editor.

PR #742 seems to hotfix the issue

jiripolasek avatar Oct 02 '25 15:10 jiripolasek