Vivaldi Community Blog: Markdown to html fragment part 1 of 3

Of the 3 options I use for converting markdown files to html fragments, PowerShell is the one requiring no additional modules or libraries to be added while also offering the least flexibility (without doing a ton of extra work). You can either run commands directly from the terminal (shell) or use scripts to save a few steps with future runs.

Using commands

$md = ConvertFrom-Markdown -Path "markdown-file.md"
$md.Html | Add-Content -Path "output-file.html"

Replace "markdown-file.md" with the path to the markdown file you want to convert and "output-file.html" with the path of the html file you want to write the result of the conversion to and you should have the html fragment file needed for this.

An example script

Just going to hit the basics here. If you choose to go this route you should make it yours by writing your own.

With something like the following in a script file (.ps1) —

function Get-HtmlContentFromMarkdown {
    param (
        [parameter(
            Position=0
        )][string]$Path,

        [parameter(
            Position=1
        )][string]$OutputPath
    )

    process {
        $md = ConvertFrom-Markdown -Path $Path
        if ($OutputPath) {
            $md.Html | Add-Content -Path $OutputPath
            return Test-Path -Path $OutputPath
        }
        
        return $md.Html
    }
}

— you can execute with commands like —

Get-HtmlContentFromMarkdown -Path $mdFilePath -OutputPath $htmlFilePath

— to save a few lines in the future , or even —

Get-HtmlContentFromMarkdown $mdFilePath $htmlFilePath

— to add a few saved keystrokes to those saved lines. You could even add your own code to make it more pipeline friendly.

Still to come

In the next 2 posts in this series I will briefly show how to use Python then, my usual preference, Pandoc, for the same purpose.