Today, I was editing some HTML in an aspx content page that I had pasted in from a Word doc. The document had quite a few URLs in it that needed "linkifying" (add "<a href", etc).
As I find myself doing this kind of editing quite a bit, I decided to write a quick macro to accomplish the task. There are many improvements that could be made to automatically search out URLs, set the target, etc. but, this is a first iteration. Feel free to share your improvements. ;)
Sub Linkify()
Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
Dim selectedText As String = selection.Text.Trim()
If selectedText = String.Empty Then
MsgBox("Nothing selected", MsgBoxStyle.Exclamation)
Exit Sub
End If
selection.Delete()
selection.Insert(String.Format("<a href=""{0}"" target=""_blank"">{0}</a>", selectedText))
selection.LineDown()
selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
End Sub
In Visual Studio, select the URL you wish to "linkify"
http://www.connicus.com
and run the macro. Voila!
<a href="http://www.connicus.com" target="_blank">http://www.connicus.com</a>