writing a windows live writer plugin

Recently, I had the need to write a plugin for windows live writer so that I could highlight some code and click a tool item to get it formatted ready for publishing to my blog.
Surely this was going to be a relatively involved exercise, right?image

It was very simple - in fact it was so easy I thought I'd illustrate it here in a small post.

The first thing I did was to create a new C# class library and import the code formatting classes - I'm using the CSharpFormat by Manoli. Here's what the final version of the project looks like:

I'm referencing the WindowsLive.Writer.Api.dll assembly that resides at
"C:\Program Files\Windows Live\Writer\" and I have only one file called CSharpContentSource which is illustrated below.

This class is decorated with two attributes - the first, "WriterPlugin", defines a guid identifier for the plugin and the second, "InsertableContentSource", addes the appropriate behaviour for inserting content into live writer. Note that the string in the constructor is the menu text that will appear in live writer.
The class inherits from the ContentSource class and there is one method overriden  - CreateContent.

When this method is called, the content parameter contains the currently highlighted text - as it is a ref parameter we can change what will appear in the live writer application when our method completes.

using Manoli.Utils.CSharpFormat;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace LiveWriterCodeFormatting.LiveWriter
    [WriterPlugin("FE7AEF3C-7645-4fe0-9086-D1DB40C0D007", "CSharpFormatter")]
    [InsertableContentSource("CSharp formatting")]
    public class CSharpContentSource : ContentSource
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
            var format = new CSharpFormat();

                var newContent = format.FormatCode(content);
                content = "<div class=\"codeBlock\">" + newContent + "</div>";

            return DialogResult.OK;

In the method CreateContent I am simply taking the current content and formatting it through the format classes. I'm then adding my own blog requirements for css formatting and setting the content.
Simple stuff.

Lastly, if I introduce a post build event in project properties so that it copies the built assembly to the windows live writer plugins folder, each time I build it will auto install. The post build event is simply:
XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live\Writer\Plugins\"

When I'm writing a post I can now use the Insert tool bar button to select my plugin option: