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

")] [TestCase("https://localhost:5000", "

https://localhost:5000

")] [TestCase("www.l:5000", "

www.l:5000

")] 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); } // https://github.com/xoofx/markdig/issues/668 // A heading's implicit reference must not resolve inside another still-open // link bracket, which would break the outer link. [Test] public void TestAutoIdentifierHeadingLinkDoesNotHijackNestedLinkLabel() { var markdown = "# Testing Markdown\n\n" + "### Header\n\n" + "[Testing [Header]](https://www.bing.com)\n\n" + "[Testing [test]](https://www.google.com)\n"; var expected = "

Testing Markdown

\n" + "

Header

\n" + "

Testing [Header]

\n" + "

Testing [test]

"; var pipeline = new MarkdownPipelineBuilder() .UseAutoLinks() .UseAutoIdentifiers() .Build(); var html = Markdown.ToHtml(markdown, pipeline); Assert.That(html, Is.EqualTo(expected).IgnoreWhiteSpace); } [TestCase("[Header]", "Header")] [TestCase("[Header][]", "Header")] [TestCase("[label][Header]", "label")] [TestCase("[Testing [Header]](/target)", "Testing [Header]")] [TestCase("[Testing [Header][]](/target)", "Testing [Header][]")] [TestCase("[Testing [label][Header]](/target)", "Testing [label][Header]")] [TestCase("![Testing [Header]](/image.png)", "\"Testing")] public void TestAutoIdentifierReferenceResolution(string markdown, string expected) { foreach (var trackTrivia in new[] { false, true }) { var builder = new MarkdownPipelineBuilder().UseAutoLinks().UseAutoIdentifiers(); if (trackTrivia) { builder.EnableTrackTrivia(); } var html = Markdown.ToHtml("# Header\n\n" + markdown, builder.Build()); Assert.That(html, Is.EqualTo("

Header

\n

" + expected + "

").IgnoreWhiteSpace, $"TrackTrivia: {trackTrivia}"); } } [Test] public void TestExplicitReferenceStillResolvesInsideOpenLink() { var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().UseAutoIdentifiers().Build(); var html = Markdown.ToHtml("[Header]: /explicit\n\n# Header\n\n[Testing [Header]](/target)", pipeline); Assert.That(html, Is.EqualTo("

Header

\n

[Testing Header](/target)

").IgnoreWhiteSpace); } }