Auto linking in markdown or text using AutoLink

Tue, 24 Dec 2019 13:34 UTC by garethbrown

For autolinking in text data in .NET / C#, I found a library called AutoLink:

https://github.com/mganss/AutoLink

AutoLink assumes you are working with plaintext as described on the GitHub README.md.

If you are working with HTML, you can hook AutoLink into the post processing step of HTML sanitizer. The following code demonstrates how you might do this (given that the example seems to be out of date as the HtmlSanitizer library no longer appears to have a PostProcessTag event).

// Note: Not production code - requires testing

var autolink = new AutoLink();

sanitizer.PostProcessNode += (s, e) =>
{
    var node = e.Node;

    if (node.NodeType == NodeType.Text)
    {
        string autoLinkedTextContent = autolink.Link(node.NodeValue);

        if (autoLinkedTextContent != node.NodeValue)
        {
            node.NodeValue = autoLinkedTextContent;
        }
    }
};

html = sanitizer.Sanitize(html);

In the end I was able to avoid using AutoLink as the excellent MarkedNet does AutoLinking by default.

entry_editor_demo.png


AppSoftware programming blog
UI block loader
One moment please ...