Extended autolinks

edit

CommonMark only makes a link of an address between angle brackets, such as <https://example.com>. Autolinks adds the extended autolinks of GitHub Flavored Markdown, so that the addresses people type are links: an address that starts with www., http:// or https://, and an email address. An address that starts with www. links to http://. An email address can start with mailto:, and a Jabber address with xmpp:, which can be followed by a resource after a /, as in xmpp:me@example.com/home: the scheme is part of the link, and the address is its own destination.

var options = new MarkdownParseOptions { Extensions = MarkdownExtensions.Autolinks };
var tree = MarkdownSyntaxTree.ParseText("See www.example.com, https://example.com/docs or contact@example.com.\n", options);

var links = tree.GetRoot().DescendantNodes().OfType<MarkdownAutolinkSyntax>().ToList();
Assert.Equal(["http://www.example.com", "https://example.com/docs", "mailto:contact@example.com"], links.Select(link => link.Destination));
Assert.Equal("<p>See <a href=\"http://www.example.com\">www.example.com</a>, <a href=\"https://example.com/docs\">https://example.com/docs</a> or <a href=\"mailto:contact@example.com\">contact@example.com</a>.</p>\n", MarkdownHtmlRenderer.ToHtml(tree));

An extended autolink is a MarkdownAutolinkSyntax that holds the tokens of its text: Destination gives the URL, and IsExtended tells it from an autolink between angle brackets. The trailing punctuation, such as the period that ends a sentence, is not part of the link.

Scheme autolinks recognize more schemes, and replace these autolinks when both are enabled.