1 minute read

I love Brett Terpstra’s Marked2 App. It’s fantastic, it’s fast, it gives immediate feedback and while not free, it’s absolutely worth the cost.

Unfortunately, it’s Mac Only. :(

For those of us Windows bound, there’s a Powershell method for watching files and publishing the results in HTML format. But if you need that quick feedback, on Windows, there’s a solution.

Couple of caveats:

  • It uses the Github API to get the HTML
    • Good : because there’s no problems parsing
    • Bad : because there’s no theming yet.
  • It requires installing the Convert_From_Markdown module

Watch the folder

First - you have to watch the folder for changes - Stackoverflow Article

Convert the files

Second - you have to convert the changed files into HMTL. - Craig Forrester and available on his Github page.

Combined Code

Github

Import-Module Convert_From_Markdown

$folder = "<enter your source folder here>"

$results = "<enter your output publish folder here>"

# Clean out existing subscriptions
foreach($sub in Get-EventSubscriber) {
    
    unregister-event -subscriptionid $sub.SubscriptionId
    }

# Get ready to register a new one
Function Register-Watcher {
    param ($folder)
    $filter = "*.MD" #all files
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }

    $changeAction = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
		
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
        $outpath = $results + $name.TrimEnd(".md") + ".html"
        gc $path -Raw | ConvertFrom-Markdown | Out-File -FilePath "$outpath"
    ')
    Register-ObjectEvent $Watcher -EventName "Changed" -Action $changeAction
}

# Use the new function 
Register-Watcher $folder