Skip to content

Commit 97470bd

Browse files
authored
Merge pull request #859 from JamesNK/jamesnk/autolinks-domain-no-period
Add AutoLinkOptions.AllowDomainWithoutPeriod
2 parents fb3fe8b + 90c73b7 commit 97470bd

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/Markdig.Tests/TestAutoLinks.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Markdig.Extensions.AutoLinks;
2+
3+
namespace Markdig.Tests;
4+
5+
[TestFixture]
6+
public class TestAutoLinks
7+
{
8+
[Test]
9+
[TestCase("https://localhost", "<p><a href=\"https://localhost\">https://localhost</a></p>")]
10+
[TestCase("http://localhost", "<p><a href=\"http://localhost\">http://localhost</a></p>")]
11+
[TestCase("https://l", "<p><a href=\"https://l\">https://l</a></p>")]
12+
[TestCase("www.l", "<p><a href=\"http://www.l\">www.l</a></p>")]
13+
[TestCase("https://localhost:5000", "<p><a href=\"https://localhost:5000\">https://localhost:5000</a></p>")]
14+
[TestCase("www.l:5000", "<p><a href=\"http://www.l:5000\">www.l:5000</a></p>")]
15+
public void TestLinksWithAllowDomainWithoutPeriod(string markdown, string expected)
16+
{
17+
var pipeline = new MarkdownPipelineBuilder()
18+
.UseAutoLinks(new AutoLinkOptions { AllowDomainWithoutPeriod = true })
19+
.Build();
20+
var html = Markdown.ToHtml(markdown, pipeline);
21+
22+
Assert.That(html, Is.EqualTo(expected).IgnoreWhiteSpace);
23+
}
24+
}

src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public AutoLinkOptions()
2222
/// Should a www link be prefixed with https:// instead of http:// (false by default)
2323
/// </summary>
2424
public bool UseHttpsForWWWLinks { get; set; }
25+
26+
/// <summary>
27+
/// Should auto-linking allow a domain with no period, e.g. https://localhost (false by default)
28+
/// </summary>
29+
public bool AllowDomainWithoutPeriod { get; set; }
2530
}

src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
164164
}
165165

166166
// Do not need to check if a telephone number is a valid domain
167-
if (c != 't' && !LinkHelper.IsValidDomain(link, domainOffset))
167+
if (c != 't' && !LinkHelper.IsValidDomain(link, domainOffset, Options.AllowDomainWithoutPeriod))
168168
{
169169
return false;
170170
}

src/Markdig/Helpers/LinkHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ private static bool IsEndOfUri(char c, bool isAutoLink)
10061006
return c == '\0' || c.IsSpaceOrTab() || c.IsControl() || (isAutoLink && c == '<'); // TODO: specs unclear. space is strict or relaxed? (includes tabs?)
10071007
}
10081008

1009-
public static bool IsValidDomain(string link, int prefixLength)
1009+
public static bool IsValidDomain(string link, int prefixLength, bool allowDomainWithoutPeriod = false)
10101010
{
10111011
// https://github.github.com/gfm/#extended-www-autolink
10121012
// A valid domain consists of alphanumeric characters, underscores (_), hyphens (-) and periods (.).
@@ -1052,7 +1052,7 @@ public static bool IsValidDomain(string link, int prefixLength)
10521052
segmentHasCharacters = true;
10531053
}
10541054

1055-
return segmentCount != 1 && // At least one dot was present
1055+
return (segmentCount != 1 || allowDomainWithoutPeriod) && // At least one dot was present
10561056
segmentHasCharacters && // Last segment has valid characters
10571057
segmentCount - lastUnderscoreSegment >= 2; // No underscores are present in the last two segments of the domain
10581058
}

0 commit comments

Comments
 (0)