This is a quick entry, mostly for me. I've been using the very good Markdown editor, Markdown Monster, and wanted to automate creating blog posts. I have a manual process that I thought I could somewhat automate.

MM has an addin that lets me write C# scripting and control a lot of the editor.

Through both the documentation plus trial-and-error, I was able to do the following:

  1. Prompt for a post title.
  2. Create the post file in a known folder.
  3. Add metadata (frontmatter).
  4. Open in a tab for editing.
  5. Open the post folder's "images" folder.
  6. Commit and publish

Here's the code for creating the post, some of which is seen in the above screenshot.

There were some things I couldn't accomplish, noted in the code comments, probably due to my ignorance of how the MM API is supposed to work.

#r Westwind.Utilities.dll
#r Microsoft.VisualBasic.dll
using Westwind.Utilities;
using System.IO;

string defaultPath = @"c:\path\to\posts";

string title = Microsoft.VisualBasic.Interaction.InputBox("File Name", "Enter file name", "", 600, 300);

if (String.IsNullOrWhiteSpace(title))
{
    return null;
}

string datetime = DateTime.Now.ToString("yyyyMMdd-HHmm");
string folder = Path.Combine(defaultPath, datetime + " " + title);
string file = Path.Combine(folder, title + ".md");
string images = Path.Combine(folder, "images");
string publishedOn = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
string slug = title.ToLower();
slug = slug.Replace(" ", "_");
slug = slug.Replace("?", "_");
slug = slug.Replace("&", "_");
slug = slug.Replace(".", "_");
string meta = String.Format(@"---
Title          : {0}
PublishedOn    : {1}
Slug           : {2}
Tags           : 
Status         : Draft
---
",title, publishedOn, slug);

Directory.CreateDirectory(images);
File.WriteAllText(file,meta);
//Model.ExecuteProcess("explorer.exe", images);
//System.Threading.Thread.Sleep(2000);
Model.Window.OpenTab(file);
//I can't figure out how to set the cursor position
//The code seems right, but the cursor doesn't move unless I run the command
//from the addin window. Even then, the Console.WriteLine is what
//forces it to work.
var editor = Model.ActiveEditor;
var doc = Model.ActiveDocument;
var len = doc.CurrentText.Split('\n').Length;
Console.WriteLine(len);
Console.WriteLine(editor.GetLineNumber());
editor.SetCursorPosition(0,len);
editor.SetEditorFocus();


//Couldn't get this working
//var editor = Model.ActiveEditor;
////HACK: For some reason this allows the document to be edited.
//Console.WriteLine(editor.GetLineNumber());
//editor.SetSelection(meta);
//editor.SaveDocument();

To open the images folder, I created this command-let:

#r Westwind.Utilities.dll

using Westwind.Utilities;
using System.IO;
using System.Windows;

string folder = Model.ActiveDocument.Filename;
folder = Directory.GetParent(folder).FullName;

string images = Path.Combine(folder, "images");
if (Directory.Exists(images)) {
    Model.ExecuteProcess("explorer.exe", images);
}
else {
    MessageBox.Show("No folder " + images);
}

And, to publish, I do this. I have a batch file in my blog folder that takes care of the actual commit/push to Git.

#r Westwind.Utilities.dll

using Westwind.Utilities;
using System.IO;
using System.Windows;
using System.Diagnostics;

string folder = Model.ActiveDocument.Filename;
folder = Directory.GetParent(Path.Combine(folder,@"..\..\..\")).FullName;

string cmd = Path.Combine(folder, "publish.cmd");
cmd = @"""" + cmd + @"""";
Console.WriteLine(cmd);
//Model.ExecuteProcess("cmd.exe", "/C " + cmd);
var si = new ProcessStartInfo();
si.FileName = "cmd.exe";
si.Arguments = "/C " +  cmd;
si.WorkingDirectory = folder;

Process.Start(si);