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

[Sample App] Markdown Renderer Polyfill

Open michael-hawker opened this issue 2 years ago • 5 comments

Split out from #93, Follow-on from PR #85

Problem Statement

Our current documentation is shown in raw Markdown instead of nice documentation.

We can use the Toolkit's MarkdownTextblock to resolve this, except on WASM. So we need a wrapper to make this work everywhere (in the sample app, not something we want to ship as a control).

Technical Background

In #85, I wrote code to split out the markdown snippets and inject the displayed samples in the ToolkitDocumentationRenderer:

https://github.com/CommunityToolkit/Labs-Windows/blob/8f2380383bd82d3620ad65e4e085b15dab92a855/common/CommunityToolkit.Labs.Shared/Renderers/ToolkitDocumentationRenderer.xaml.cs#L121-L141

As well as a Template Selector to pick the right template to use:

https://github.com/CommunityToolkit/Labs-Windows/blob/8f2380383bd82d3620ad65e4e085b15dab92a855/common/CommunityToolkit.Labs.Shared/Renderers/ToolkitDocumentationRenderer.xaml#L13-L29

So, all we should have to do is update the DocumentTemplate to leverage this new control that understands how to display the Markdown snippet.

Solution

Fortunately, @nickrandolph started a markdown based solution using marked.js for WASM that we can use for the individual blurbs from the MVVM Toolkit Sample App, see here: https://github.com/CommunityToolkit/MVVM-Samples/pull/38/commits/911b3551d6034dccd721af57ebbd502a4130b5aa~

And he's given us permission to leverage that code from that PR which didn't get merged:

image

I'm not sure if his version used WebView/marked even on native platform, so that may be a difference, but the general approach and code here should be handy to reference.

We'll probably want to update the Marked version referenced from the original PR there (and not sure if they included minified version).

We'll also need to a Third Party Notice file like we have in the toolkit since we'll be including the source directly as it's JavaScript.

Open Questions

  • [ ] Do we worry about supporting DocFX or the new GitHub flavored Note/Warning sections at the moment? We should eventually, but we did this with a specific custom renderer in the toolkit and not sure of marked.js support (though they also have custom rendering).
    • [ ] Was thinking would be useful for 'preview/experimental' banner at top of experiments, but maybe we inject that on top of everything manually or in the side-bar of the app? @niels9001 thoughts?

michael-hawker avatar Jun 09 '22 16:06 michael-hawker

  • [ ] Was thinking would be useful for 'preview/experimental' banner at top of experiments, but maybe we inject that on top of everything manually or in the side-bar of the app? @niels9001 thoughts?

Yeah, I guess we could use a InfoBar for this that shows that its an experimental control with maybe a link to some readme that explains what that means? I'll take it along in the mock-up!

niels9001 avatar Jun 09 '22 17:06 niels9001

Hmm... I'm having trouble even just trying to add a split reference to Microsoft.Toolkit.Uwp/CommunityToolkit.WinUI.UI.Controls.Markdown package in Labs.Heads.props, figured I could pull in the Labs.Uno.props file helpers as well and do something like:

  <!-- Project Head/Sample App dependencies -->
  <ItemGroup Condition="'$(IsUwp)' == 'True' AND '$(IsUno)' == 'False'">
    <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls.Markdown" Version="7.1.2"/>
  </ItemGroup>
  <ItemGroup Condition="'$(IsWinAppSdk)' == 'True' AND '$(IsUno)' == 'False'">
    <PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Markdown" Version="7.1.2"/>
  </ItemGroup>

(See branch llama/markdown)

But it's not restoring properly... hmm. May need some help here @Arlodotexe. In the meantime, I'm going to try and clean-up the MSB4011 warnings with an idea I had for CommunityToolkit/Labs-Windows#99

michael-hawker avatar Jun 30 '22 22:06 michael-hawker

Filed CommunityToolkit/Labs-Windows#182 to resolve underlying dependency issue needed to proceed here.

michael-hawker avatar Jul 05 '22 16:07 michael-hawker

I have the initial work by @nickrandolph ported over, tried to update Marked.js while I was at it (and added a third-party notice). However, couldn't get it to work on web assembly. It appears like it's loading the marked library, but then can't find it when it goes to execute parsing of the markdown blurb.

This work is in the llama/markdown-wasm branch if anyone wants to investigate this more. I was having a hard time debugging this under the browser.

michael-hawker avatar Jul 20 '22 05:07 michael-hawker

Was thinking the other day, we could just try using Markdig directly as a .NET dependency side. Means we wouldn't have to include js source in the repo and try injecting that into web page contents. Would just be able to grab Markdown and grab HTML output from Markdig, then either write to WASM page content or inject in WebView on other non-windows platforms. 🤔

This would also mean that if we ever get to re-writing the MarkdownTextblock to use Markdig, we could leverage this as a stop-gap for Uno platforms until they have RichTextBlock support. (unless we also change our rendering approach).

(This would also give us the opportunity to evaluate how we plug-in to Markdig and rendering for custom Markdown things and display that before we rework the Toolkit control... I think, maybe?)

michael-hawker avatar Jul 22 '22 20:07 michael-hawker

Could maybe do a similar approach with code preview to use ColorCode in order to spit out HTML to inject in the page for the code samples.

michael-hawker avatar Nov 03 '22 16:11 michael-hawker

Good news is that Markdig is working in WASM. Uno JS interop makes it pretty easy. Though need to match fonts. Also, issues with layout... so needs some more work. Probably has to do with height of control changing after initial layout pass?

michael-hawker avatar Nov 09 '22 06:11 michael-hawker

image

Have an initial version which works decently, though not sure how to properly color the links parsed by MarkDig, assume there's a CSS style we'd have to apply or something? I'm not sure how these things interact with Uno and how the WinUI styles work there... @Arlodotexe any thoughts?

Though, if this is a good enough approach, should be simple enough to have another RichTextBlock wrapper for ColorCode which uses that on Windows and Html on WASM for the C#/XAML examples. Don't have time to do that now though.

michael-hawker avatar Nov 09 '22 08:11 michael-hawker