Skip to content

Commit

Permalink
Changed to centralized protocol list
Browse files Browse the repository at this point in the history
  • Loading branch information
mtirionMSFT committed Dec 16, 2024
1 parent 7eb4818 commit 0e554d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
7 changes: 1 addition & 6 deletions src/DocAssembler/DocAssembler/FileService/FileInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public List<Hyperlink> GetLocalHyperlinks(string root, string filePath)
.Descendants<LinkInline>()
.Where(x => !x.UrlHasPointyBrackets &&
!string.IsNullOrEmpty(x.Url) &&
!x.Url.StartsWith("https://", StringComparison.InvariantCulture) &&
!x.Url.StartsWith("http://", StringComparison.InvariantCulture) &&
!x.Url.StartsWith("ftps://", StringComparison.InvariantCulture) &&
!x.Url.StartsWith("ftp://", StringComparison.InvariantCulture) &&
!x.Url.StartsWith("mailto:", StringComparison.InvariantCulture) &&
!x.Url.StartsWith("xref:", StringComparison.InvariantCulture))
!Hyperlink.Protocols.Any(p => x.Url.StartsWith(p.Key, StringComparison.OrdinalIgnoreCase)))
.Select(d => new Hyperlink(markdownFilePath, d.Line + 1, d.Column + 1, d.Url ?? string.Empty)
{
UrlSpanStart = d.UrlSpan.Start,
Expand Down
38 changes: 23 additions & 15 deletions src/DocAssembler/DocAssembler/FileService/Hyperlink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Copyright (c) DocFx Companion Tools. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
using System;
using System.Text;
using DocAssembler.FileService;

namespace DocAssembler.FileService;

Expand All @@ -11,6 +13,19 @@ namespace DocAssembler.FileService;
/// </summary>
public class Hyperlink
{
/// <summary>
/// Gets the protocol mappings to <see cref="HyperlinkType"/>s.
/// </summary>
public static readonly Dictionary<string, HyperlinkType> Protocols = new Dictionary<string, HyperlinkType>()
{
{ "https://", HyperlinkType.Webpage },
{ "http://", HyperlinkType.Webpage },
{ "ftps://", HyperlinkType.Ftp },
{ "ftp://", HyperlinkType.Ftp },
{ "mailto://", HyperlinkType.Mail },
{ "xref://", HyperlinkType.CrossReference },
};

private static readonly char[] _uriFragmentOrQueryString = new char[] { '#', '?' };
private static readonly char[] _additionalInvalidChars = @"\/?:*".ToArray();
private static readonly char[] _invalidPathChars = Path.GetInvalidPathChars().Concat(_additionalInvalidChars).ToArray();
Expand Down Expand Up @@ -41,23 +56,16 @@ public Hyperlink(string filePath, int line, int col, string url)
LinkType = HyperlinkType.Empty;
if (!string.IsNullOrWhiteSpace(url))
{
if (url.StartsWith("https://", StringComparison.InvariantCulture) || url.StartsWith("http://", StringComparison.InvariantCulture))
foreach (var protocol in Protocols)
{
LinkType = HyperlinkType.Webpage;
}
else if (url.StartsWith("ftps://", StringComparison.InvariantCulture) || url.StartsWith("ftp://", StringComparison.InvariantCulture))
{
LinkType = HyperlinkType.Ftp;
}
else if (url.StartsWith("mailto:", StringComparison.InvariantCulture))
{
LinkType = HyperlinkType.Mail;
}
else if (url.StartsWith("xref:", StringComparison.InvariantCulture))
{
LinkType = HyperlinkType.CrossReference;
if (url.StartsWith(protocol.Key, StringComparison.OrdinalIgnoreCase))
{
LinkType = protocol.Value;
break;
}
}
else

if (LinkType == HyperlinkType.Empty)
{
Url = UrlDecode(Url).NormalizePath();

Expand Down

0 comments on commit 0e554d4

Please sign in to comment.