Terry Thibodeau's Blog

Welcome to Terry Thibodeau's Blog Sign in | Join | Help

Visual Studio HTML link creation macro

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>
Published Wednesday, December 19, 2007 4:54 PM by Terry Thibodeau

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Terry Thibodeau said:

Note: Before someone chimes in from the peanut gallery (Justice~! ;)) and tells me that you can just go into design mode and hit a space or enter at the end of the URL, I have a special circumstance that doesn't allow me to do this.

I don't know if its a bug in VS 2008's design view, but I can't see the text in the ContentPlaceHolder in design view.

I'm using nested Master Pages and have a ContentPlaceHolder inside another ContentPlaceHolder (to allow me to override the container, or just the content).

December 19, 2007 5:37 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems