Skip to content

Commit

Permalink
Merge pull request #202 from microsoft/zt/184-wcr-fixes
Browse files Browse the repository at this point in the history
#184: Fix relative WCR readme links
  • Loading branch information
nmetulev authored Feb 13, 2025
2 parents 9850f89 + ae27c90 commit fbc2036
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions AIDevGallery/Pages/ModelPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -21,6 +22,8 @@ namespace AIDevGallery.Pages;

internal sealed partial class ModelPage : Page
{
private const string DocsBaseUrl = "https://learn.microsoft.com/";
private const string WcrDocsRelativePath = "/windows/ai/apis/";
public ModelFamily? ModelFamily { get; set; }
private ModelType? modelFamilyType;
public bool IsNotApi => !modelFamilyType.HasValue || !ModelTypeHelpers.ApiDefinitionDetails.ContainsKey(modelFamilyType.Value);
Expand Down Expand Up @@ -203,10 +206,17 @@ private void CopyButton_Click(object sender, RoutedEventArgs e)

private void MarkdownTextBlock_LinkClicked(object sender, CommunityToolkit.WinUI.UI.Controls.LinkClickedEventArgs e)
{
ModelDetailsLinkClickedEvent.Log(e.Link);
string link = e.Link;

if(!IsNotApi && !IsValidUrl(link))
{
link = FixWcrReadmeLink(link);
}

ModelDetailsLinkClickedEvent.Log(link);
Process.Start(new ProcessStartInfo()
{
FileName = e.Link,
FileName = link,
UseShellExecute = true
});
}
Expand All @@ -219,4 +229,26 @@ private void SampleList_ItemInvoked(ItemsView sender, ItemsViewItemInvokedEventA
App.MainWindow.Navigate("Samples", new SampleNavigationArgs(sample, availableModel));
}
}

private bool IsValidUrl(string url)
{
Uri uri;
return Uri.TryCreate(url, UriKind.Absolute, out uri!) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
}

private string FixWcrReadmeLink(string link)
{
string fixedLink;

if(link.StartsWith('/'))
{
fixedLink = Path.Join(DocsBaseUrl, link);
}
else
{
fixedLink = Path.Join(DocsBaseUrl, WcrDocsRelativePath, link.Replace(".md", string.Empty));
}

return fixedLink;
}
}

0 comments on commit fbc2036

Please sign in to comment.