Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sample App] Markdown Renderer Polyfill #151

Closed
2 tasks
michael-hawker opened this issue Jun 9, 2022 · 8 comments · Fixed by #308
Closed
2 tasks

[Sample App] Markdown Renderer Polyfill #151

michael-hawker opened this issue Jun 9, 2022 · 8 comments · Fixed by #308
Assignees
Labels
documentation 📃 Improvements or additions to documentation enhancement Improvement to an existing feature help wanted Extra attention is needed sample app 🖼 Uno Issues related to Uno Platform WASM Bugs related to working with WASM/Codespaces

Comments

@michael-hawker
Copy link
Member

Split out from CommunityToolkit/Tooling-Windows-Submodule#72, 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:

var doctext = await GetDocumentationFileContents(Metadata);
var matches = MarkdownRegexSampleTag.Matches(doctext);
DocsAndSamples.Clear();
if (matches.Count == 0)
{
DocsAndSamples.Add(doctext);
}
else
{
int index = 0;
foreach (Match match in matches)
{
DocsAndSamples.Add(doctext.Substring(index, match.Index - index - 1));
DocsAndSamples.Add(ToolkitSampleRegistry.Listing[match.Groups["sampleid"].Value]);
index = match.Index + match.Length;
}
// Put rest of text at end
DocsAndSamples.Add(doctext.Substring(index));

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

<Page.Resources>
<DataTemplate x:Key="DocumentTemplate"
x:DataType="x:String">
<TextBlock win:IsTextSelectionEnabled="True"
Text="{Binding}" />
</DataTemplate>
<DataTemplate x:Key="SampleTemplate"
x:DataType="metadata:ToolkitSampleMetadata">
<!-- TODO: Display Header for name of sample? -->
<renderer:ToolkitSampleRenderer Metadata="{Binding}" />
</DataTemplate>
<local:DocOrSampleTemplateSelector x:Key="DocOrSampleTemplateSelector"
Document="{StaticResource DocumentTemplate}"
Sample="{StaticResource SampleTemplate}" />
</Page.Resources>

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: CommunityToolkit/MVVM-Samples@911b355~

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 michael-hawker added documentation 📃 Improvements or additions to documentation enhancement Improvement to an existing feature Uno Issues related to Uno Platform WASM Bugs related to working with WASM/Codespaces sample app 🖼 labels Jun 9, 2022
@michael-hawker michael-hawker added this to the Initial Release milestone Jun 9, 2022
@niels9001
Copy link
Collaborator

niels9001 commented Jun 9, 2022

  • 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!

@michael-hawker michael-hawker self-assigned this Jun 29, 2022
@michael-hawker
Copy link
Member Author

michael-hawker commented Jun 30, 2022

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 #99

@michael-hawker
Copy link
Member Author

Filed #182 to resolve underlying dependency issue needed to proceed here.

@michael-hawker
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

michael-hawker commented Nov 9, 2022

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
Copy link
Member Author

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 added a commit that referenced this issue Nov 9, 2022


Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
michael-hawker added a commit that referenced this issue Nov 10, 2022


Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
michael-hawker added a commit that referenced this issue Nov 10, 2022


Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
michael-hawker added a commit that referenced this issue Nov 18, 2022


Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
michael-hawker added a commit that referenced this issue Dec 1, 2022


Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
Martin1994 pushed a commit to Martin1994/Labs-Windows that referenced this issue Sep 2, 2023
…ommunityToolkit#151

Works pretty well, seems to be a timing issue with layout and the image in the Rive sample I think? Slowing down in the debugger causes it not to occur, so hard to understand.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📃 Improvements or additions to documentation enhancement Improvement to an existing feature help wanted Extra attention is needed sample app 🖼 Uno Issues related to Uno Platform WASM Bugs related to working with WASM/Codespaces
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants