Skip to content

Commit 3bed168

Browse files
Update Sample App to properly open links found in docs now
Have docs in release mode look at live branch of last shipped docs.
1 parent 8fa1b43 commit 3bed168

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public static async void EnsureCacheLatest()
7171
}
7272

7373
private string _cachedDocumentation = string.Empty;
74-
private string _cachedPath = string.Empty;
7574

7675
internal static async Task<Sample> FindAsync(string category, string name)
7776
{
@@ -149,8 +148,21 @@ public string CodeUrl
149148

150149
public string XamlCode { get; private set; }
151150

151+
/// <summary>
152+
/// Gets or sets the path set in the samples.json pointing to the doc for the sample.
153+
/// </summary>
152154
public string DocumentationUrl { get; set; }
153155

156+
/// <summary>
157+
/// Gets or sets the absolute local doc path for cached file in app.
158+
/// </summary>
159+
public string LocalDocumentationFilePath { get; set; }
160+
161+
/// <summary>
162+
/// Gets or sets the base path segment to the current document location.
163+
/// </summary>
164+
public string RemoteDocumentationPath { get; set; }
165+
154166
public string Icon { get; set; }
155167

156168
public string BadgeUpdateVersionRequired { get; set; }
@@ -191,32 +203,29 @@ public async Task<string> GetCSharpSourceAsync()
191203
}
192204
}
193205

194-
#pragma warning disable SA1009 // Doesn't like ValueTuples.
195-
public async Task<(string contents, string path)> GetDocumentationAsync()
196-
#pragma warning restore SA1009 // Doesn't like ValueTuples.
206+
public async Task<string> GetDocumentationAsync()
197207
{
198208
if (!string.IsNullOrWhiteSpace(_cachedDocumentation))
199209
{
200-
return (_cachedDocumentation, _cachedPath);
210+
return _cachedDocumentation;
201211
}
202212

203213
var filepath = string.Empty;
204214
var filename = string.Empty;
205-
var localPath = string.Empty;
215+
LocalDocumentationFilePath = string.Empty;
206216

207217
var docRegex = new Regex("^" + _docsOnlineRoot + "(?<branch>.+?)/docs/(?<file>.+)");
208218
var docMatch = docRegex.Match(DocumentationUrl);
209219
if (docMatch.Success)
210220
{
211221
filepath = docMatch.Groups["file"].Value;
212-
filename = Path.GetFileName(filepath);
213-
localPath = $"ms-appx:///docs/{Path.GetDirectoryName(filepath)}/";
222+
filename = Path.GetFileName(RemoteDocumentationPath);
223+
RemoteDocumentationPath = Path.GetDirectoryName(filepath);
224+
LocalDocumentationFilePath = $"ms-appx:///docs/{RemoteDocumentationPath}/";
214225
}
215226

216227
#if !DEBUG // use the docs repo in release mode
217-
string modifiedDocumentationUrl = $"{_docsOnlineRoot}master/docs/{filepath}";
218-
219-
_cachedPath = modifiedDocumentationUrl.Replace(filename, string.Empty);
228+
string modifiedDocumentationUrl = $"{_docsOnlineRoot}live/docs/{filepath}";
220229

221230
// Read from Cache if available.
222231
try
@@ -264,15 +273,14 @@ public async Task<string> GetCSharpSourceAsync()
264273
{
265274
var result = await localDocsStream.ReadTextAsync(Encoding.UTF8);
266275
_cachedDocumentation = ProcessDocs(result);
267-
_cachedPath = localPath;
268276
}
269277
}
270278
catch (Exception)
271279
{
272280
}
273281
}
274282

275-
return (_cachedDocumentation, _cachedPath);
283+
return _cachedDocumentation;
276284
}
277285

278286
/// <summary>

Microsoft.Toolkit.Uwp.SampleApp/Pages/SampleController.xaml.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
273273

274274
if (CurrentSample.HasDocumentation)
275275
{
276-
#pragma warning disable SA1008 // Opening parenthesis must be spaced correctly
277-
var (contents, path) = await CurrentSample.GetDocumentationAsync();
278-
#pragma warning restore SA1008 // Opening parenthesis must be spaced correctly
279-
documentationPath = path;
276+
var contents = await CurrentSample.GetDocumentationAsync();
280277
if (!string.IsNullOrWhiteSpace(contents))
281278
{
282279
DocumentationTextBlock.Text = contents;
@@ -431,9 +428,15 @@ private async void DocumentationTextBlock_OnLinkClicked(object sender, LinkClick
431428
{
432429
TrackingManager.TrackEvent("Link", e.Link);
433430
var link = e.Link;
434-
if (e.Link.EndsWith(".md"))
431+
if (link.EndsWith(".md"))
435432
{
436-
link = string.Format("https://docs.microsoft.com/en-us/windows/communitytoolkit/{0}/{1}", CurrentSample.CategoryName.ToLower(), link.Replace(".md", string.Empty));
433+
// Link to one of our other documents, so we'll construct the proper link here
434+
link = string.Format("https://docs.microsoft.com/windows/communitytoolkit/{0}/{1}", CurrentSample.RemoteDocumentationPath, link.Replace(".md", string.Empty));
435+
}
436+
else if (link.StartsWith("/"))
437+
{
438+
// We don't root our links to other docs.microsoft.com pages anymore, so we'll add it here.
439+
link = string.Format("https://docs.microsoft.com{0}", link);
437440
}
438441

439442
if (Uri.TryCreate(link, UriKind.Absolute, out Uri result))
@@ -450,7 +453,7 @@ private async void DocumentationTextBlock_ImageResolving(object sender, ImageRes
450453
// Determine if the link is not absolute, meaning it is relative.
451454
if (!Uri.TryCreate(e.Url, UriKind.Absolute, out Uri url))
452455
{
453-
url = new Uri(documentationPath + e.Url);
456+
url = new Uri(CurrentSample.LocalDocumentationFilePath + e.Url);
454457
}
455458

456459
if (url.Scheme == "ms-appx")
@@ -698,7 +701,6 @@ public bool UseBackground
698701

699702
private PaneState _paneState;
700703
private bool _onlyDocumentation;
701-
private string documentationPath;
702704

703705
private ThemeListener _themeListener;
704706

0 commit comments

Comments
 (0)