CommunityToolkit / WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building .NET apps with UWP and the Windows App SDK / WinUI 3 for Windows 10 and Windows 11. The toolkit is part of the .NET Foundation.
https://docs.microsoft.com/windows/communitytoolkit/
Other
5.88k stars 1.37k forks source link

MarkdownTextBlock doesn't support backticks in Link Text #2802

Open michael-hawker opened 5 years ago

michael-hawker commented 5 years ago

I'm submitting a...

Current behavior

Trying to render the following valid markdown doesn't work:

[`Windows.UI.Composition` APIs](https://docs.microsoft.com/windows/uwp/composition/visual-layer)

Expected behavior

[`Windows.UI.Composition` APIs](https://docs.microsoft.com/windows/uwp/composition/visual-layer)

Windows.UI.Composition APIs

Minimal reproduction of the problem with instructions

1) Open Sample App 2) Open MarkDownTextBlock sample 3) Add the following markdown to the textbox at the top:

[`Windows.UI.Composition` APIs](https://docs.microsoft.com/windows/uwp/composition/visual-layer)

Exception thrown in last line of RenderCodeRun trying to add inline to collection - System.ArgumentException: 'Value does not fall within the expected range.'

Environment

Nuget Package(s): 5.0.0

Package Version(s): 5.0.0

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [x] October 2018 Update (17763)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [x] October 2018 Update (17763)
- [ ] Insider Build (xxxxx)

Device form factor:
- [x] Desktop
- [ ] Mobile
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [x] 2017 (version: )
- [ ] 2017 Preview (version: )
Sergio0694 commented 5 years ago

As mentioned in #2806 to @michael-hawker for the related issue, I can confirm that this too can be fixed (at least as a quick workaround) by replacing this line: https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/a722f171cac7959d8fb39d732c5951018a2e8c07/Microsoft.Toolkit.Uwp.UI.Controls/MarkdownTextBlock/Render/MarkdownRenderer.Inlines.cs#L581

With this code:

try
{
    localContext.InlineCollection.Add(inlineUIContainer);
}
catch (ArgumentException)
{
    // Fallback span
    Run run = new Run
    {
        Text = text.Text,
        FontFamily = InlineCodeFontFamily ?? FontFamily,
        Foreground = InlineCodeForeground ?? Foreground
    };

    // Additional formatting
    if (localContext.WithinItalics) run.FontStyle = FontStyle.Italic;
    if (localContext.WithinBold) run.FontWeight = FontWeights.Bold;

    // Add the fallback block
    localContext.InlineCollection.Add(run);
}

This will still render the code with the usual code font/foreground, and will just lack the additional background color (as it's just a Span). Still though, the visual difference is negligible, and at least this will work instead of just crashing and halting the whole rendering process.

windowstoolkitbot commented 5 years ago

This issue seems inactive. It will automatically be closed in 14 days if there is no activity.

Jay-o-Way commented 1 year ago

following valid markdown

Micrsoft does advise against formatting text in a link, though...