Skip to content

Commit 38b7737

Browse files
committed
Fix URL navigation from docs
1 parent c19b6da commit 38b7737

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

samples/MvvmSample.Core/ViewModels/SamplePageViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ private async Task LoadDocsAsync(string? name)
6868
using StreamReader reader = new(stream);
6969
string text = await reader.ReadToEndAsync();
7070

71-
// Fixups
72-
string trimmedText = Regex.Replace(text, @"[ \r\n]+?!\[[^]]+\]\([^)]+\)[ \r\n]+?", string.Empty); // Drop image links
73-
string updatedText = trimmedText.Replace("(/dotnet", "(https://docs.microsoft.com/dotnet"); // Update relative links
71+
// Drop image links
72+
string trimmedText = Regex.Replace(text, @"[ \r\n]+?!\[[^]]+\]\([^)]+\)[ \r\n]+?", string.Empty);
7473

7574
Texts = MarkdownHelper.GetParagraphs(trimmedText);
7675

samples/MvvmSampleUwp/Controls/DocumentationBlock.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using Microsoft.Toolkit.Uwp.UI.Controls;
7+
using Windows.System;
58
using Windows.UI.Xaml;
69
using Windows.UI.Xaml.Controls;
710

@@ -10,8 +13,39 @@ namespace MvvmSampleUwp.Controls;
1013
/// <summary>
1114
/// A simple control that acts as a container for a documentation block.
1215
/// </summary>
16+
[TemplatePart(Name = "PART_MarkdownTextBlock", Type = typeof(MarkdownTextBlock))]
1317
public sealed class DocumentationBlock : ContentControl
1418
{
19+
/// <summary>
20+
/// The <see cref="MarkdownTextBlock"/> instance in use.
21+
/// </summary>
22+
private MarkdownTextBlock markdownTextBlock;
23+
24+
/// <inheritdoc/>
25+
protected override void OnApplyTemplate()
26+
{
27+
base.OnApplyTemplate();
28+
29+
markdownTextBlock = (MarkdownTextBlock)GetTemplateChild("PART_MarkdownTextBlock");
30+
31+
markdownTextBlock.LinkClicked += MarkdownTextBlock_LinkClicked;
32+
}
33+
34+
/// <summary>
35+
/// Handles a clicked link in a markdown block.
36+
/// </summary>
37+
/// <param name="sender">The source <see cref="MarkdownTextBlock"/> control.</param>
38+
/// <param name="e">The input arguments.</param>
39+
private void MarkdownTextBlock_LinkClicked(object sender, LinkClickedEventArgs e)
40+
{
41+
if (Uri.TryCreate(e.Link, UriKind.Absolute, out Uri result) ||
42+
(e.Link.StartsWith("/dotnet") &&
43+
Uri.TryCreate($"https://docs.microsoft.com{e.Link}", UriKind.Absolute, out result)))
44+
{
45+
_ = Launcher.LaunchUriAsync(result);
46+
}
47+
}
48+
1549
/// <summary>
1650
/// Gets or sets the <see cref="string"/> representing the text to display.
1751
/// </summary>

samples/MvvmSampleUwp/Controls/DocumentationBlock.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
BorderThickness="1"
2222
CornerRadius="4">
2323
<controls:MarkdownTextBlock
24+
Name="PART_MarkdownTextBlock"
2425
Padding="12"
2526
Background="Transparent"
2627
BorderBrush="Transparent"

0 commit comments

Comments
 (0)