diff --git a/src/Markdig.Tests/TestAutoLinks.cs b/src/Markdig.Tests/TestAutoLinks.cs new file mode 100644 index 00000000..dd5e455b --- /dev/null +++ b/src/Markdig.Tests/TestAutoLinks.cs @@ -0,0 +1,22 @@ +using Markdig.Extensions.AutoLinks; + +namespace Markdig.Tests; + +[TestFixture] +public class TestAutoLinks +{ + [Test] + [TestCase("https://localhost", "

https://localhost

")] + [TestCase("http://localhost", "

http://localhost

")] + [TestCase("https://l", "

https://l

")] + [TestCase("www.l", "

www.l

")] + public void TestLinksWithAllowDomainWithoutPeriod(string markdown, string expected) + { + var pipeline = new MarkdownPipelineBuilder() + .UseAutoLinks(new AutoLinkOptions { AllowDomainWithoutPeriod = true }) + .Build(); + var html = Markdown.ToHtml(markdown, pipeline); + + Assert.That(html, Is.EqualTo(expected).IgnoreWhiteSpace); + } +} diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs index 307b216b..189a6411 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs @@ -22,4 +22,9 @@ public class AutoLinkOptions /// Should a www link be prefixed with https:// instead of http:// (false by default) /// public bool UseHttpsForWWWLinks { get; set; } + + /// + /// Should a link host name with no period, e.g. https://localhost, be allowed (false by default) + /// + public bool AllowDomainWithoutPeriod { get; set; } } diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs index 515c9882..6ee6b9f0 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs @@ -164,7 +164,7 @@ public class AutoLinkParser : InlineParser } // Do not need to check if a telephone number is a valid domain - if (c != 't' && !LinkHelper.IsValidDomain(link, domainOffset)) + if (c != 't' && !LinkHelper.IsValidDomain(link, domainOffset, Options.AllowDomainWithoutPeriod)) { return false; } diff --git a/src/Markdig/Helpers/LinkHelper.cs b/src/Markdig/Helpers/LinkHelper.cs index ac47b294..a994b3f2 100644 --- a/src/Markdig/Helpers/LinkHelper.cs +++ b/src/Markdig/Helpers/LinkHelper.cs @@ -1006,7 +1006,7 @@ public static class LinkHelper return c == '\0' || c.IsSpaceOrTab() || c.IsControl() || (isAutoLink && c == '<'); // TODO: specs unclear. space is strict or relaxed? (includes tabs?) } - public static bool IsValidDomain(string link, int prefixLength) + public static bool IsValidDomain(string link, int prefixLength, bool allowDomainWithoutPeriod) { // https://github.github.com/gfm/#extended-www-autolink // A valid domain consists of alphanumeric characters, underscores (_), hyphens (-) and periods (.). @@ -1052,7 +1052,7 @@ public static class LinkHelper segmentHasCharacters = true; } - return segmentCount != 1 && // At least one dot was present + return (segmentCount != 1 || allowDomainWithoutPeriod) && // At least one dot was present segmentHasCharacters && // Last segment has valid characters segmentCount - lastUnderscoreSegment >= 2; // No underscores are present in the last two segments of the domain }