Skip to content

Latest commit

 

History

History
84 lines (68 loc) · 2.14 KB

File metadata and controls

84 lines (68 loc) · 2.14 KB

The 'convert-md2html.ps1' Script

This PowerShell script converts Markdown file(s) into HTML.

Parameters

PS> ./convert-md2html.ps1 [[-FilePattern] <String>] [<CommonParameters>]

-FilePattern <String>
    Specifies the file pattern to the Markdown file(s)
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Aliases                      
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./convert-md2html.ps1 *.md

Notes

Author: Markus Fleschutz | License: CC0

Related Links

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Converts Markdown file(s) into HTML 
.DESCRIPTION
	This PowerShell script converts Markdown file(s) into HTML.
.PARAMETER FilePattern
	Specifies the file pattern to the Markdown file(s)
.EXAMPLE
	PS> ./convert-md2html.ps1 *.md
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$FilePattern = "")

try {
	$StopWatch = [system.diagnostics.stopwatch]::startNew()

	if ($FilePattern -eq "" ) { $FilePattern = Read-Host "Enter the file pattern to the Markdown file(s)" }

	Write-Host "⏳ Searching for pandoc..." 
	$null = (pandoc --version)
	if ($lastExitCode -ne 0) { throw "Can't execute 'pandoc' - make sure it's installed and available" }

	Write-Host "⏳ Converting..."
	gci -r -i $FilePattern | foreach {
		$TargetPath = $_.directoryname + "\" + $_.basename + ".html"
		pandoc --standalone --template "$PSScriptRoot/data/templates/template.html" -s $_.name -o $TargetPath
	}
	[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
	"✅ converted in $Elapsed sec"
	exit 0 # success
} catch {
	"⚠️ ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

(page generated by convert-ps2md.ps1 as of 03/27/2026 07:17:16)